|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | + "github.com/xanzy/go-gitlab" |
| 10 | +) |
| 11 | + |
| 12 | +var _ = registerDataSource("gitlab_project_tag", func() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + Description: `The ` + "`gitlab_project_tag`" + ` data source allows details of a project tag to be retrieved by its name. |
| 15 | +
|
| 16 | +**Upstream API** : [GitLab API docs](https://docs.gitlab.com/ee/api/tags.html)`, |
| 17 | + |
| 18 | + ReadContext: dataSourceGitlabProjectTagRead, |
| 19 | + Schema: map[string]*schema.Schema{ |
| 20 | + "name": { |
| 21 | + Description: "The name of a tag.", |
| 22 | + Type: schema.TypeString, |
| 23 | + Required: true, |
| 24 | + }, |
| 25 | + "project": { |
| 26 | + Description: "The ID or URL-encoded path of the project owned by the authenticated user.", |
| 27 | + Type: schema.TypeString, |
| 28 | + Required: true, |
| 29 | + }, |
| 30 | + "message": { |
| 31 | + Description: "Creates annotated tag.", |
| 32 | + Type: schema.TypeString, |
| 33 | + Computed: true, |
| 34 | + }, |
| 35 | + "protected": { |
| 36 | + Description: "Bool, true if tag has tag protection.", |
| 37 | + Type: schema.TypeBool, |
| 38 | + Computed: true, |
| 39 | + }, |
| 40 | + "target": { |
| 41 | + Description: "The unique id assigned to the commit by Gitlab.", |
| 42 | + Type: schema.TypeString, |
| 43 | + Computed: true, |
| 44 | + }, |
| 45 | + "release": { |
| 46 | + Description: "The release associated with the tag.", |
| 47 | + Type: schema.TypeSet, |
| 48 | + Computed: true, |
| 49 | + Set: schema.HashResource(releaseNoteSchema), |
| 50 | + Elem: releaseNoteSchema, |
| 51 | + }, |
| 52 | + "commit": { |
| 53 | + Description: "The commit associated with the tag ref.", |
| 54 | + Type: schema.TypeSet, |
| 55 | + Computed: true, |
| 56 | + Set: schema.HashResource(commitSchema), |
| 57 | + Elem: commitSchema, |
| 58 | + }, |
| 59 | + }, |
| 60 | + } |
| 61 | +}) |
| 62 | + |
| 63 | +func dataSourceGitlabProjectTagRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 64 | + client := meta.(*gitlab.Client) |
| 65 | + name := d.Get("name").(string) |
| 66 | + project := d.Get("project").(string) |
| 67 | + log.Printf("[DEBUG] read gitlab tag %s/%s", project, name) |
| 68 | + tag, resp, err := client.Tags.GetTag(project, name, gitlab.WithContext(ctx)) |
| 69 | + if err != nil { |
| 70 | + log.Printf("[DEBUG] failed to read gitlab tag %s/%s response %v", project, name, resp) |
| 71 | + return diag.FromErr(err) |
| 72 | + } |
| 73 | + |
| 74 | + d.SetId(buildTwoPartID(&project, &name)) |
| 75 | + d.Set("name", tag.Name) |
| 76 | + d.Set("project", project) |
| 77 | + d.Set("message", tag.Message) |
| 78 | + d.Set("protected", tag.Protected) |
| 79 | + d.Set("target", tag.Target) |
| 80 | + releaseNote := flattenReleaseNote(tag.Release) |
| 81 | + if err := d.Set("release", releaseNote); err != nil { |
| 82 | + return diag.FromErr(err) |
| 83 | + } |
| 84 | + if err := d.Set("commit", flattenCommit(tag.Commit)); err != nil { |
| 85 | + return diag.FromErr(err) |
| 86 | + } |
| 87 | + return nil |
| 88 | +} |
0 commit comments