|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 11 | + gitlab "github.com/xanzy/go-gitlab" |
| 12 | +) |
| 13 | + |
| 14 | +var _ = registerResource("gitlab_group_saml_link", func() *schema.Resource { |
| 15 | + validGroupSamlLinkAccessLevelNames := []string{ |
| 16 | + "guest", |
| 17 | + "reporter", |
| 18 | + "developer", |
| 19 | + "maintainer", |
| 20 | + "owner", |
| 21 | + } |
| 22 | + |
| 23 | + return &schema.Resource{ |
| 24 | + Description: `The ` + "`gitlab_group_saml_link`" + ` resource allows to manage the lifecycle of an SAML integration with a group. |
| 25 | +
|
| 26 | +**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/groups.html#saml-group-links)`, |
| 27 | + |
| 28 | + CreateContext: resourceGitlabGroupSamlLinkCreate, |
| 29 | + ReadContext: resourceGitlabGroupSamlLinkRead, |
| 30 | + DeleteContext: resourceGitlabGroupSamlLinkDelete, |
| 31 | + Importer: &schema.ResourceImporter{ |
| 32 | + StateContext: schema.ImportStatePassthroughContext, |
| 33 | + }, |
| 34 | + |
| 35 | + Schema: map[string]*schema.Schema{ |
| 36 | + "group": { |
| 37 | + Description: "The ID or path of the group to add the SAML Group Link to.", |
| 38 | + Type: schema.TypeString, |
| 39 | + Required: true, |
| 40 | + ForceNew: true, |
| 41 | + }, |
| 42 | + "saml_group_name": { |
| 43 | + Description: "The name of the SAML group.", |
| 44 | + Type: schema.TypeString, |
| 45 | + Required: true, |
| 46 | + ForceNew: true, |
| 47 | + }, |
| 48 | + "access_level": { |
| 49 | + Description: fmt.Sprintf("Access level for members of the SAML group. Valid values are: %s.", renderValueListForDocs(validGroupSamlLinkAccessLevelNames)), |
| 50 | + Type: schema.TypeString, |
| 51 | + ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(validGroupSamlLinkAccessLevelNames, false)), |
| 52 | + Required: true, |
| 53 | + ForceNew: true, |
| 54 | + }, |
| 55 | + }, |
| 56 | + } |
| 57 | +}) |
| 58 | + |
| 59 | +func resourceGitlabGroupSamlLinkCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 60 | + client := meta.(*gitlab.Client) |
| 61 | + |
| 62 | + group := d.Get("group").(string) |
| 63 | + samlGroupName := d.Get("saml_group_name").(string) |
| 64 | + accessLevel := accessLevelNameToValue[d.Get("access_level").(string)] |
| 65 | + |
| 66 | + options := &gitlab.AddGroupSAMLLinkOptions{ |
| 67 | + SAMLGroupName: gitlab.String(samlGroupName), |
| 68 | + AccessLevel: gitlab.AccessLevel(accessLevel), |
| 69 | + } |
| 70 | + |
| 71 | + log.Printf("[DEBUG] Create GitLab Group SAML Link for group %q with name %q", group, samlGroupName) |
| 72 | + SamlLink, _, err := client.Groups.AddGroupSAMLLink(group, options, gitlab.WithContext(ctx)) |
| 73 | + if err != nil { |
| 74 | + return diag.FromErr(err) |
| 75 | + } |
| 76 | + |
| 77 | + d.SetId(buildTwoPartID(&group, &SamlLink.Name)) |
| 78 | + return resourceGitlabGroupSamlLinkRead(ctx, d, meta) |
| 79 | +} |
| 80 | + |
| 81 | +func resourceGitlabGroupSamlLinkRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 82 | + client := meta.(*gitlab.Client) |
| 83 | + group, samlGroupName, parse_err := parseTwoPartID(d.Id()) |
| 84 | + if parse_err != nil { |
| 85 | + return diag.FromErr(parse_err) |
| 86 | + } |
| 87 | + |
| 88 | + // Try to fetch all group links from GitLab |
| 89 | + log.Printf("[DEBUG] Read GitLab Group SAML Link for group %q", group) |
| 90 | + samlLink, _, err := client.Groups.GetGroupSAMLLink(group, samlGroupName, nil, gitlab.WithContext(ctx)) |
| 91 | + if err != nil { |
| 92 | + if is404(err) { |
| 93 | + log.Printf("[DEBUG] GitLab SAML Group Link %s for group ID %s not found, removing from state", samlGroupName, group) |
| 94 | + d.SetId("") |
| 95 | + return nil |
| 96 | + } |
| 97 | + return diag.FromErr(err) |
| 98 | + } |
| 99 | + |
| 100 | + d.Set("group", group) |
| 101 | + d.Set("access_level", accessLevelValueToName[samlLink.AccessLevel]) |
| 102 | + d.Set("saml_group_name", samlLink.Name) |
| 103 | + |
| 104 | + return nil |
| 105 | +} |
| 106 | + |
| 107 | +func resourceGitlabGroupSamlLinkDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 108 | + client := meta.(*gitlab.Client) |
| 109 | + group, samlGroupName, parse_err := parseTwoPartID(d.Id()) |
| 110 | + if parse_err != nil { |
| 111 | + return diag.FromErr(parse_err) |
| 112 | + } |
| 113 | + |
| 114 | + log.Printf("[DEBUG] Delete GitLab Group SAML Link for group %q with name %q", group, samlGroupName) |
| 115 | + _, err := client.Groups.DeleteGroupSAMLLink(group, samlGroupName, gitlab.WithContext(ctx)) |
| 116 | + if err != nil { |
| 117 | + if is404(err) { |
| 118 | + log.Printf("[WARNING] %s", err) |
| 119 | + } else { |
| 120 | + return diag.FromErr(err) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return nil |
| 125 | +} |
0 commit comments