|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 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_group_variable", func() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + Description: `The ` + "`gitlab_group_variable`" + ` data source allows to retrieve details about a group-level CI/CD variable. |
| 15 | +
|
| 16 | +**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/group_level_variables.html)`, |
| 17 | + |
| 18 | + ReadContext: dataSourceGitlabGroupVariableRead, |
| 19 | + Schema: datasourceSchemaFromResourceSchema(gitlabGroupVariableGetSchema(), []string{"group", "key"}, []string{"environment_scope"}), |
| 20 | + } |
| 21 | +}) |
| 22 | + |
| 23 | +func dataSourceGitlabGroupVariableRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 24 | + client := meta.(*gitlab.Client) |
| 25 | + group := d.Get("group").(string) |
| 26 | + key := d.Get("key").(string) |
| 27 | + environmentScope := d.Get("environment_scope").(string) |
| 28 | + |
| 29 | + variable, _, err := client.GroupVariables.GetVariable(group, key, nil, gitlab.WithContext(ctx), withEnvironmentScopeFilter(ctx, environmentScope)) |
| 30 | + if err != nil { |
| 31 | + return diag.FromErr(err) |
| 32 | + } |
| 33 | + |
| 34 | + d.SetId(fmt.Sprintf("%s:%s:%s", group, key, environmentScope)) |
| 35 | + stateMap := gitlabGroupVariableToStateMap(group, variable) |
| 36 | + if err := setStateMapInResourceData(stateMap, d); err != nil { |
| 37 | + return diag.FromErr(err) |
| 38 | + } |
| 39 | + return nil |
| 40 | +} |
0 commit comments