|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 12 | + gitlab "github.com/xanzy/go-gitlab" |
| 13 | +) |
| 14 | + |
| 15 | +var _ = registerResource("gitlab_release_link", func() *schema.Resource { |
| 16 | + return &schema.Resource{ |
| 17 | + Description: `The ` + "`gitlab_release_link`" + ` resource allows to manage the lifecycle of a release links. |
| 18 | +
|
| 19 | +**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/releases/links.html)`, |
| 20 | + |
| 21 | + CreateContext: resourceGitlabReleaseLinkCreate, |
| 22 | + ReadContext: resourceGitlabReleaseLinkRead, |
| 23 | + UpdateContext: resourceGitlabReleaseLinkUpdate, |
| 24 | + DeleteContext: resourceGitlabReleaseLinkDelete, |
| 25 | + Importer: &schema.ResourceImporter{ |
| 26 | + StateContext: schema.ImportStatePassthroughContext, |
| 27 | + }, |
| 28 | + Schema: gitlabReleaseLinkGetSchema(), |
| 29 | + } |
| 30 | +}) |
| 31 | + |
| 32 | +func resourceGitlabReleaseLinkCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 33 | + client := meta.(*gitlab.Client) |
| 34 | + project := d.Get("project").(string) |
| 35 | + tagName := d.Get("tag_name").(string) |
| 36 | + name := d.Get("name").(string) |
| 37 | + url := d.Get("url").(string) |
| 38 | + |
| 39 | + options := &gitlab.CreateReleaseLinkOptions{ |
| 40 | + Name: &name, |
| 41 | + URL: &url, |
| 42 | + } |
| 43 | + if filePath, ok := d.GetOk("file_path"); ok { |
| 44 | + options.FilePath = gitlab.String(filePath.(string)) |
| 45 | + } |
| 46 | + if linkType, ok := d.GetOk("link_type"); ok { |
| 47 | + linkTypeValue := gitlab.LinkTypeValue(linkType.(string)) |
| 48 | + options.LinkType = &linkTypeValue |
| 49 | + } |
| 50 | + |
| 51 | + log.Printf("[DEBUG] create release link project/tagName/name: %s/%s/%s", project, tagName, name) |
| 52 | + releaseLink, resp, err := client.ReleaseLinks.CreateReleaseLink(project, tagName, options, gitlab.WithContext(ctx)) |
| 53 | + if err != nil { |
| 54 | + log.Printf("[WARN] failed to create release link project/tagName/name: %s/%s/%s (response %v)", project, tagName, name, resp) |
| 55 | + return diag.FromErr(err) |
| 56 | + } |
| 57 | + d.SetId(resourceGitLabReleaseLinkBuildId(project, tagName, releaseLink.ID)) |
| 58 | + |
| 59 | + return resourceGitlabReleaseLinkRead(ctx, d, meta) |
| 60 | +} |
| 61 | + |
| 62 | +func resourceGitlabReleaseLinkRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 63 | + client := meta.(*gitlab.Client) |
| 64 | + project, tagName, linkID, err := resourceGitLabReleaseLinkParseId(d.Id()) |
| 65 | + if err != nil { |
| 66 | + return diag.FromErr(err) |
| 67 | + } |
| 68 | + |
| 69 | + log.Printf("[DEBUG] read release link project/tagName/linkID: %s/%s/%d", project, tagName, linkID) |
| 70 | + releaseLink, resp, err := client.ReleaseLinks.GetReleaseLink(project, tagName, linkID, gitlab.WithContext(ctx)) |
| 71 | + if err != nil { |
| 72 | + if is404(err) { |
| 73 | + log.Printf("[WARN] recieved 404 for release link project/tagName/linkID: %s/%s/%d. Removing from state", project, tagName, linkID) |
| 74 | + d.SetId("") |
| 75 | + return nil |
| 76 | + } |
| 77 | + log.Printf("[WARN] failed to read release link project/tagName/linkID: %s/%s/%d. Response %v", project, tagName, linkID, resp) |
| 78 | + return diag.FromErr(err) |
| 79 | + } |
| 80 | + |
| 81 | + stateMap := gitlabReleaseLinkToStateMap(project, tagName, releaseLink) |
| 82 | + if err = setStateMapInResourceData(stateMap, d); err != nil { |
| 83 | + return diag.FromErr(err) |
| 84 | + } |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +func resourceGitlabReleaseLinkUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 89 | + client := meta.(*gitlab.Client) |
| 90 | + project, tagName, linkID, err := resourceGitLabReleaseLinkParseId(d.Id()) |
| 91 | + if err != nil { |
| 92 | + return diag.FromErr(err) |
| 93 | + } |
| 94 | + |
| 95 | + options := &gitlab.UpdateReleaseLinkOptions{} |
| 96 | + if d.HasChange("name") { |
| 97 | + options.Name = gitlab.String(d.Get("name").(string)) |
| 98 | + } |
| 99 | + if d.HasChange("url") { |
| 100 | + options.URL = gitlab.String(d.Get("url").(string)) |
| 101 | + } |
| 102 | + if d.HasChange("file_path") { |
| 103 | + options.FilePath = gitlab.String(d.Get("file_path").(string)) |
| 104 | + } |
| 105 | + if d.HasChange("link_type") { |
| 106 | + linkTypeValue := gitlab.LinkTypeValue(d.Get("link_type").(string)) |
| 107 | + options.LinkType = &linkTypeValue |
| 108 | + } |
| 109 | + |
| 110 | + log.Printf("[DEBUG] update release link project/tagName/linkID: %s/%s/%d", project, tagName, linkID) |
| 111 | + _, _, err = client.ReleaseLinks.UpdateReleaseLink(project, tagName, linkID, options, gitlab.WithContext(ctx)) |
| 112 | + if err != nil { |
| 113 | + log.Printf("[WARN] failed to update release link project/tagName/linkID: %s/%s/%d", project, tagName, linkID) |
| 114 | + return diag.FromErr(err) |
| 115 | + } |
| 116 | + |
| 117 | + return resourceGitlabReleaseLinkRead(ctx, d, meta) |
| 118 | +} |
| 119 | + |
| 120 | +func resourceGitlabReleaseLinkDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 121 | + client := meta.(*gitlab.Client) |
| 122 | + project, tagName, linkID, err := resourceGitLabReleaseLinkParseId(d.Id()) |
| 123 | + if err != nil { |
| 124 | + return diag.FromErr(err) |
| 125 | + } |
| 126 | + |
| 127 | + log.Printf("[DEBUG] delete release link project/tagName/linkID: %s/%s/%d", project, tagName, linkID) |
| 128 | + _, resp, err := client.ReleaseLinks.DeleteReleaseLink(project, tagName, linkID, gitlab.WithContext(ctx)) |
| 129 | + if err != nil { |
| 130 | + log.Printf("[DEBUG] failed to delete release link project/tagName/linkID: %s/%s/%d. Response %v", project, tagName, linkID, resp) |
| 131 | + return diag.FromErr(err) |
| 132 | + } |
| 133 | + return nil |
| 134 | +} |
| 135 | + |
| 136 | +func resourceGitLabReleaseLinkParseId(id string) (string, string, int, error) { |
| 137 | + parts := strings.SplitN(id, ":", 3) |
| 138 | + if len(parts) != 3 { |
| 139 | + return "", "", 0, fmt.Errorf("Unexpected ID format (%q). Expected project:tagName:linkID", id) |
| 140 | + } |
| 141 | + |
| 142 | + linkID, err := strconv.Atoi(parts[2]) |
| 143 | + if err != nil { |
| 144 | + return "", "", 0, err |
| 145 | + } |
| 146 | + |
| 147 | + return parts[0], parts[1], linkID, nil |
| 148 | +} |
| 149 | + |
| 150 | +func resourceGitLabReleaseLinkBuildId(project string, tagName string, linkID int) string { |
| 151 | + id := fmt.Sprintf("%s:%s:%d", project, tagName, linkID) |
| 152 | + return id |
| 153 | +} |
0 commit comments