|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "github.com/hashicorp/go-retryablehttp" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | + gitlab "github.com/xanzy/go-gitlab" |
| 11 | + "log" |
| 12 | +) |
| 13 | + |
| 14 | +var _ = registerResource("gitlab_group_project_file_template", func() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Description: `The ` + "`gitlab_group_project_file_template`" + ` resource allows setting a project from which |
| 17 | +custom file templates will be loaded. The project selected must be a direct child of the group identified. |
| 18 | +For more information about which file types are available as templates, view |
| 19 | +[GitLab's documentation](https://docs.gitlab.com/ee/user/admin_area/settings/instance_template_repository.html#supported-file-types-and-locations) |
| 20 | +
|
| 21 | +-> This resource requires a GitLab Enterprise instance with a Premium license. |
| 22 | +
|
| 23 | +**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/groups.html#update-group)`, |
| 24 | + |
| 25 | + // Since this resource updates an in-place resource, the update method is the same as the create method |
| 26 | + CreateContext: resourceGitLabGroupProjectFileTemplateCreateOrUpdate, |
| 27 | + UpdateContext: resourceGitLabGroupProjectFileTemplateCreateOrUpdate, |
| 28 | + ReadContext: resourceGitLabGroupProjectFileTemplateRead, |
| 29 | + DeleteContext: resourceGitLabGroupProjectFileTemplateDelete, |
| 30 | + // Since this resource updates an in-place resource, importing doesn't make much sense. Simply add the resource |
| 31 | + // to the config and terraform will overwrite what's already in place and manage it from there. |
| 32 | + Schema: map[string]*schema.Schema{ |
| 33 | + "group_id": { |
| 34 | + Description: `The ID of the group that will use the file template project. This group must be the direct |
| 35 | + parent of the project defined by project_id`, |
| 36 | + Type: schema.TypeInt, |
| 37 | + |
| 38 | + // Even though there is no traditional resource to create, leave "ForceNew" as "true" so that if someone |
| 39 | + // changes a configuration to a different group, the old group gets "deleted" (updated to have a value |
| 40 | + // of 0). |
| 41 | + ForceNew: true, |
| 42 | + Required: true, |
| 43 | + }, |
| 44 | + "file_template_project_id": { |
| 45 | + Description: `The ID of the project that will be used for file templates. This project must be the direct |
| 46 | + child of the project defined by the group_id`, |
| 47 | + Type: schema.TypeInt, |
| 48 | + Required: true, |
| 49 | + }, |
| 50 | + }, |
| 51 | + } |
| 52 | +}) |
| 53 | + |
| 54 | +func resourceGitLabGroupProjectFileTemplateRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 55 | + client := meta.(*gitlab.Client) |
| 56 | + |
| 57 | + groupID := d.Get("group_id").(int) |
| 58 | + group, _, err := client.Groups.GetGroup(groupID, nil, gitlab.WithContext(ctx)) |
| 59 | + if err != nil { |
| 60 | + if is404(err) { |
| 61 | + log.Printf("[DEBUG] gitlab group %d not found, removing from state", groupID) |
| 62 | + d.SetId("") |
| 63 | + return nil |
| 64 | + } |
| 65 | + return diag.FromErr(err) |
| 66 | + } |
| 67 | + if group.MarkedForDeletionOn != nil { |
| 68 | + log.Printf("[DEBUG] gitlab group %s is marked for deletion, removing from state", d.Id()) |
| 69 | + d.SetId("") |
| 70 | + return nil |
| 71 | + } |
| 72 | + |
| 73 | + d.SetId(fmt.Sprintf("%d", group.ID)) |
| 74 | + d.Set("file_template_project_id", group.FileTemplateProjectID) |
| 75 | + |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +func resourceGitLabGroupProjectFileTemplateCreateOrUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 80 | + client := meta.(*gitlab.Client) |
| 81 | + |
| 82 | + groupID := d.Get("group_id").(int) |
| 83 | + projectID := gitlab.Int(d.Get("file_template_project_id").(int)) |
| 84 | + |
| 85 | + // Creating the resource means updating the existing group to link the project to the group. |
| 86 | + options := &gitlab.UpdateGroupOptions{} |
| 87 | + if d.HasChanges("file_template_project_id") { |
| 88 | + options.FileTemplateProjectID = gitlab.Int(d.Get("file_template_project_id").(int)) |
| 89 | + } |
| 90 | + |
| 91 | + _, _, err := client.Groups.UpdateGroup(groupID, options) |
| 92 | + if err != nil { |
| 93 | + return diag.Errorf("unable to update group %d with `file_template_project_id` set to %d: %s", groupID, projectID, err) |
| 94 | + } |
| 95 | + return resourceGitLabGroupProjectFileTemplateRead(ctx, d, meta) |
| 96 | +} |
| 97 | + |
| 98 | +func resourceGitLabGroupProjectFileTemplateDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 99 | + client := meta.(*gitlab.Client) |
| 100 | + groupID := d.Get("group_id").(int) |
| 101 | + options := &gitlab.UpdateGroupOptions{} |
| 102 | + |
| 103 | + _, _, err := updateGroupWithOverwrittenFileTemplateOption(client, groupID, options) |
| 104 | + if err != nil { |
| 105 | + return diag.Errorf("could not update group %d to remove file template ID: %s", groupID, err) |
| 106 | + } |
| 107 | + return resourceGitLabGroupProjectFileTemplateRead(ctx, d, meta) |
| 108 | +} |
| 109 | + |
| 110 | +func updateGroupWithOverwrittenFileTemplateOption(client *gitlab.Client, groupID int, options *gitlab.UpdateGroupOptions) (*gitlab.Group, *gitlab.Response, error) { |
| 111 | + return client.Groups.UpdateGroup(groupID, options, func(request *retryablehttp.Request) error { |
| 112 | + //Overwrite the GroupUpdateOptions struct to remove the "omitempty", which forces the client to send an empty |
| 113 | + //string in just this request. |
| 114 | + removeOmitEmptyOptions := struct { |
| 115 | + FileTemplateProjectID *string `url:"file_template_project_id" json:"file_template_project_id"` |
| 116 | + }{ |
| 117 | + FileTemplateProjectID: nil, |
| 118 | + } |
| 119 | + |
| 120 | + //Create the new body request with the above struct |
| 121 | + newBody, err := json.Marshal(removeOmitEmptyOptions) |
| 122 | + if err != nil { |
| 123 | + return err |
| 124 | + } |
| 125 | + |
| 126 | + //Set the request body to have the newly updated body |
| 127 | + err = request.SetBody(newBody) |
| 128 | + if err != nil { |
| 129 | + return err |
| 130 | + } |
| 131 | + |
| 132 | + return nil |
| 133 | + }) |
| 134 | +} |
0 commit comments