|
| 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/mitchellh/hashstructure" |
| 11 | + "github.com/xanzy/go-gitlab" |
| 12 | +) |
| 13 | + |
| 14 | +var _ = registerDataSource("gitlab_project_tags", func() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Description: `The ` + "`gitlab_project_tags`" + ` data source allows details of project tags to be retrieved by some search criteria. |
| 17 | +
|
| 18 | +**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/tags.html#list-project-repository-tags)`, |
| 19 | + |
| 20 | + ReadContext: dataSourceGitlabProjectTagsRead, |
| 21 | + Schema: map[string]*schema.Schema{ |
| 22 | + "project": { |
| 23 | + Description: "The ID or URL-encoded path of the project owned by the authenticated user.", |
| 24 | + Type: schema.TypeString, |
| 25 | + Required: true, |
| 26 | + }, |
| 27 | + "order_by": { |
| 28 | + Description: "Return tags ordered by `name` or `updated` fields. Default is `updated`.", |
| 29 | + Type: schema.TypeString, |
| 30 | + Optional: true, |
| 31 | + }, |
| 32 | + "sort": { |
| 33 | + Description: "Return tags sorted in `asc` or `desc` order. Default is `desc`.", |
| 34 | + Type: schema.TypeString, |
| 35 | + Optional: true, |
| 36 | + }, |
| 37 | + "search": { |
| 38 | + Description: "Return list of tags matching the search criteria. You can use `^term` and `term$` to find tags that begin and end with `term` respectively. No other regular expressions are supported.", |
| 39 | + Type: schema.TypeString, |
| 40 | + Optional: true, |
| 41 | + }, |
| 42 | + "tags": { |
| 43 | + Description: "List of repository tags from a project.", |
| 44 | + Type: schema.TypeList, |
| 45 | + Computed: true, |
| 46 | + Elem: &schema.Resource{ |
| 47 | + Schema: datasourceSchemaFromResourceSchema(gitlabProjectTagGetSchema()), |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + } |
| 52 | +}) |
| 53 | + |
| 54 | +func dataSourceGitlabProjectTagsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 55 | + client := meta.(*gitlab.Client) |
| 56 | + |
| 57 | + project := d.Get("project").(string) |
| 58 | + options := gitlab.ListTagsOptions{ |
| 59 | + ListOptions: gitlab.ListOptions{ |
| 60 | + PerPage: 20, |
| 61 | + Page: 1, |
| 62 | + }, |
| 63 | + } |
| 64 | + |
| 65 | + if v, ok := d.GetOk("order_by"); ok { |
| 66 | + options.OrderBy = gitlab.String(v.(string)) |
| 67 | + } |
| 68 | + |
| 69 | + if v, ok := d.GetOk("sort"); ok { |
| 70 | + options.Sort = gitlab.String(v.(string)) |
| 71 | + } |
| 72 | + |
| 73 | + if v, ok := d.GetOk("search"); ok { |
| 74 | + options.Search = gitlab.String(v.(string)) |
| 75 | + } |
| 76 | + |
| 77 | + optionsHash, err := hashstructure.Hash(&options, nil) |
| 78 | + if err != nil { |
| 79 | + return diag.FromErr(err) |
| 80 | + } |
| 81 | + |
| 82 | + var tags []*gitlab.Tag |
| 83 | + for options.Page != 0 { |
| 84 | + paginatedTags, resp, err := client.Tags.ListTags(project, &options, gitlab.WithContext(ctx)) |
| 85 | + if err != nil { |
| 86 | + return diag.FromErr(err) |
| 87 | + } |
| 88 | + |
| 89 | + tags = append(tags, paginatedTags...) |
| 90 | + options.Page = resp.NextPage |
| 91 | + } |
| 92 | + |
| 93 | + log.Printf("[DEBUG] get gitlab tags from project: %s", project) |
| 94 | + d.SetId(fmt.Sprintf("%s:%d", project, optionsHash)) |
| 95 | + d.Set("project", project) |
| 96 | + d.Set("order_by", options.OrderBy) |
| 97 | + d.Set("sort", options.Sort) |
| 98 | + d.Set("search", options.Search) |
| 99 | + if err = d.Set("tags", flattenDataTags(tags)); err != nil { |
| 100 | + return diag.Errorf("Failed to set tags to state: %v", err) |
| 101 | + } |
| 102 | + return nil |
| 103 | +} |
| 104 | + |
| 105 | +func flattenDataTags(tags []*gitlab.Tag) (values []map[string]interface{}) { |
| 106 | + for _, tag := range tags { |
| 107 | + values = append(values, map[string]interface{}{ |
| 108 | + "commit": flattenCommit(tag.Commit), |
| 109 | + "release": flattenReleaseNote(tag.Release), |
| 110 | + "name": tag.Name, |
| 111 | + "target": tag.Target, |
| 112 | + "message": tag.Message, |
| 113 | + "protected": tag.Protected, |
| 114 | + }) |
| 115 | + } |
| 116 | + return values |
| 117 | +} |
0 commit comments