Skip to content

Commit ff569d3

Browse files
committed
New data source gitlab_group_variable
Refs: #386
1 parent eb30a44 commit ff569d3

7 files changed

+297
-57
lines changed

docs/data-sources/group_variable.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gitlab_group_variable Data Source - terraform-provider-gitlab"
4+
subcategory: ""
5+
description: |-
6+
The gitlab_group_variable data source allows to retrieve details about a group-level CI/CD variable.
7+
Upstream API: GitLab REST API docs https://docs.gitlab.com/ee/api/group_level_variables.html
8+
---
9+
10+
# gitlab_group_variable (Data Source)
11+
12+
The `gitlab_group_variable` data source allows to retrieve details about a group-level CI/CD variable.
13+
14+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/group_level_variables.html)
15+
16+
17+
18+
<!-- schema generated by tfplugindocs -->
19+
## Schema
20+
21+
### Required
22+
23+
- `group` (String) The name or id of the group.
24+
- `key` (String) The name of the variable.
25+
26+
### Optional
27+
28+
- `environment_scope` (String) The environment scope of the variable. Defaults to all environment (`*`). Note that in Community Editions of Gitlab, values other than `*` will cause inconsistent plans.
29+
- `id` (String) The ID of this resource.
30+
31+
### Read-Only
32+
33+
- `masked` (Boolean) If set to `true`, the value of the variable will be hidden in job logs. The value must meet the [masking requirements](https://docs.gitlab.com/ee/ci/variables/#masked-variables). Defaults to `false`.
34+
- `protected` (Boolean) If set to `true`, the variable will be passed only to pipelines running on protected branches and tags. Defaults to `false`.
35+
- `value` (String) The value of the variable.
36+
- `variable_type` (String) The type of a variable. Valid values are: `env_var`, `file`. Default is `env_var`.
37+
38+

docs/resources/group_variable.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ resource "gitlab_group_variable" "example" {
3737

3838
### Optional
3939

40-
- `environment_scope` (String) The environment scope of the variable. Defaults to all environment (`*`). Note that in Community Editions of Gitlab, values other than `*` will cause inconsistent plans. See https://docs.gitlab.com/ee/ci/variables/#add-a-cicd-variable-to-a-group
40+
- `environment_scope` (String) The environment scope of the variable. Defaults to all environment (`*`). Note that in Community Editions of Gitlab, values other than `*` will cause inconsistent plans.
4141
- `id` (String) The ID of this resource.
4242
- `masked` (Boolean) If set to `true`, the value of the variable will be hidden in job logs. The value must meet the [masking requirements](https://docs.gitlab.com/ee/ci/variables/#masked-variables). Defaults to `false`.
4343
- `protected` (Boolean) If set to `true`, the variable will be passed only to pipelines running on protected branches and tags. Defaults to `false`.
44-
- `variable_type` (String) The type of a variable. Available types are: env_var (default) and file.
44+
- `variable_type` (String) The type of a variable. Valid values are: `env_var`, `file`. Default is `env_var`.
4545

4646
## Import
4747

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package provider
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
9+
)
10+
11+
func TestAccDataSourceGitlabGroupVariable_basic(t *testing.T) {
12+
testAccCheck(t)
13+
14+
testGroup := testAccCreateGroups(t, 1)[0]
15+
16+
resource.Test(t, resource.TestCase{
17+
PreCheck: func() { testAccPreCheck(t) },
18+
ProviderFactories: providerFactories,
19+
Steps: []resource.TestStep{
20+
{
21+
Config: fmt.Sprintf(`
22+
resource "gitlab_group_variable" "this" {
23+
group = %d
24+
key = "any_key"
25+
value = "any-value"
26+
environment_scope = "*"
27+
}
28+
29+
data "gitlab_group_variable" "this" {
30+
group = gitlab_group_variable.this.group
31+
key = gitlab_group_variable.this.key
32+
environment_scope = gitlab_group_variable.this.environment_scope
33+
}
34+
`, testGroup.ID,
35+
),
36+
Check: resource.ComposeTestCheckFunc(
37+
testAccDataSourceGitlabGroupVariable("gitlab_group_variable.this", "data.gitlab_group_variable.this"),
38+
),
39+
},
40+
},
41+
})
42+
}
43+
44+
func testAccDataSourceGitlabGroupVariable(src, n string) resource.TestCheckFunc {
45+
return func(s *terraform.State) error {
46+
47+
resource := s.RootModule().Resources[src]
48+
resourceAttributes := resource.Primary.Attributes
49+
50+
datasource := s.RootModule().Resources[n]
51+
datasourceAttributes := datasource.Primary.Attributes
52+
53+
testAttributes := attributeNamesFromSchema(gitlabGroupVariableGetSchema())
54+
55+
for _, attribute := range testAttributes {
56+
if datasourceAttributes[attribute] != resourceAttributes[attribute] {
57+
return fmt.Errorf("Expected variable's attribute `%s` to be: %s, but got: `%s`", attribute, resourceAttributes[attribute], datasourceAttributes[attribute])
58+
}
59+
}
60+
61+
return nil
62+
}
63+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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_variables", func() *schema.Resource {
13+
return &schema.Resource{
14+
Description: `The ` + "`gitlab_group_variables`" + ` data source allows to retrieve all group-level CI/CD variables.
15+
16+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/group_level_variables.html)`,
17+
18+
ReadContext: dataSourceGitlabGroupVariablesRead,
19+
Schema: map[string]*schema.Schema{
20+
"group": {
21+
Description: "The name or id of the group.",
22+
Type: schema.TypeString,
23+
ForceNew: true,
24+
Required: true,
25+
},
26+
"environment_scope": {
27+
Description: "The environment scope of the variable. Defaults to all environment (`*`).",
28+
Type: schema.TypeString,
29+
Optional: true,
30+
Default: "*",
31+
},
32+
"variables": {
33+
Description: "The list of variables returned by the search",
34+
Type: schema.TypeList,
35+
Computed: true,
36+
Elem: &schema.Resource{
37+
Schema: datasourceSchemaFromResourceSchema(gitlabGroupVariableGetSchema(), nil, nil),
38+
},
39+
},
40+
},
41+
}
42+
})
43+
44+
func dataSourceGitlabGroupVariablesRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
45+
client := meta.(*gitlab.Client)
46+
group := d.Get("group").(string)
47+
environmentScope := d.Get("environment_scope").(string)
48+
49+
options := &gitlab.ListGroupVariablesOptions{
50+
Page: 1,
51+
PerPage: 20,
52+
}
53+
54+
var variables []*gitlab.GroupVariable
55+
for options.Page != 0 {
56+
paginatedVariables, resp, err := client.GroupVariables.ListVariables(group, options, gitlab.WithContext(ctx), withEnvironmentScopeFilter(ctx, environmentScope))
57+
if err != nil {
58+
return diag.FromErr(err)
59+
}
60+
61+
variables = append(variables, paginatedVariables...)
62+
options.Page = resp.NextPage
63+
}
64+
65+
d.SetId(fmt.Sprintf("%s:%s", group, environmentScope))
66+
if err := d.Set("variables", flattenGitlabGroupVariables(group, variables)); err != nil {
67+
return diag.Errorf("failed to set variables to state: %v", err)
68+
}
69+
return nil
70+
}
71+
72+
func flattenGitlabGroupVariables(group string, variables []*gitlab.GroupVariable) (values []map[string]interface{}) {
73+
for _, variable := range variables {
74+
values = append(values, gitlabGroupVariableToStateMap(group, variable))
75+
}
76+
return values
77+
}

internal/provider/resource_gitlab_group_variable.go

Lines changed: 5 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -25,53 +25,7 @@ var _ = registerResource("gitlab_group_variable", func() *schema.Resource {
2525
StateContext: schema.ImportStatePassthroughContext,
2626
},
2727

28-
Schema: map[string]*schema.Schema{
29-
"group": {
30-
Description: "The name or id of the group.",
31-
Type: schema.TypeString,
32-
ForceNew: true,
33-
Required: true,
34-
},
35-
"key": {
36-
Description: "The name of the variable.",
37-
Type: schema.TypeString,
38-
ForceNew: true,
39-
Required: true,
40-
ValidateFunc: StringIsGitlabVariableName,
41-
},
42-
"value": {
43-
Description: "The value of the variable.",
44-
Type: schema.TypeString,
45-
Required: true,
46-
Sensitive: true,
47-
},
48-
"variable_type": {
49-
Description: "The type of a variable. Available types are: env_var (default) and file.",
50-
Type: schema.TypeString,
51-
Optional: true,
52-
Default: "env_var",
53-
ValidateFunc: StringIsGitlabVariableType,
54-
},
55-
"protected": {
56-
Description: "If set to `true`, the variable will be passed only to pipelines running on protected branches and tags. Defaults to `false`.",
57-
Type: schema.TypeBool,
58-
Optional: true,
59-
Default: false,
60-
},
61-
"masked": {
62-
Description: "If set to `true`, the value of the variable will be hidden in job logs. The value must meet the [masking requirements](https://docs.gitlab.com/ee/ci/variables/#masked-variables). Defaults to `false`.",
63-
Type: schema.TypeBool,
64-
Optional: true,
65-
Default: false,
66-
},
67-
"environment_scope": {
68-
Description: "The environment scope of the variable. Defaults to all environment (`*`). Note that in Community Editions of Gitlab, values other than `*` will cause inconsistent plans. See https://docs.gitlab.com/ee/ci/variables/#add-a-cicd-variable-to-a-group",
69-
Type: schema.TypeString,
70-
Optional: true,
71-
ForceNew: true,
72-
Default: "*",
73-
},
74-
},
28+
Schema: gitlabGroupVariableGetSchema(),
7529
}
7630
})
7731

@@ -104,7 +58,6 @@ func resourceGitlabGroupVariableCreate(ctx context.Context, d *schema.ResourceDa
10458
keyScope := fmt.Sprintf("%s:%s", key, environmentScope)
10559

10660
d.SetId(buildTwoPartID(&group, &keyScope))
107-
10861
return resourceGitlabGroupVariableRead(ctx, d, meta)
10962
}
11063

@@ -140,13 +93,10 @@ func resourceGitlabGroupVariableRead(ctx context.Context, d *schema.ResourceData
14093
return augmentVariableClientError(d, err)
14194
}
14295

143-
d.Set("key", v.Key)
144-
d.Set("value", v.Value)
145-
d.Set("variable_type", v.VariableType)
146-
d.Set("group", group)
147-
d.Set("protected", v.Protected)
148-
d.Set("masked", v.Masked)
149-
d.Set("environment_scope", v.EnvironmentScope)
96+
stateMap := gitlabGroupVariableToStateMap(group, v)
97+
if err = setStateMapInResourceData(stateMap, d); err != nil {
98+
return diag.FromErr(err)
99+
}
150100
return nil
151101
}
152102

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package provider
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
8+
"github.com/xanzy/go-gitlab"
9+
)
10+
11+
func gitlabGroupVariableGetSchema() map[string]*schema.Schema {
12+
return map[string]*schema.Schema{
13+
"group": {
14+
Description: "The name or id of the group.",
15+
Type: schema.TypeString,
16+
ForceNew: true,
17+
Required: true,
18+
},
19+
"key": {
20+
Description: "The name of the variable.",
21+
Type: schema.TypeString,
22+
ForceNew: true,
23+
Required: true,
24+
ValidateFunc: StringIsGitlabVariableName,
25+
},
26+
"value": {
27+
Description: "The value of the variable.",
28+
Type: schema.TypeString,
29+
Required: true,
30+
Sensitive: true,
31+
},
32+
"variable_type": {
33+
Description: fmt.Sprintf("The type of a variable. Valid values are: %s. Default is `env_var`.", renderValueListForDocs(gitlabVariableTypeValues)),
34+
Type: schema.TypeString,
35+
Optional: true,
36+
Default: "env_var",
37+
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(gitlabVariableTypeValues, false)),
38+
},
39+
"protected": {
40+
Description: "If set to `true`, the variable will be passed only to pipelines running on protected branches and tags. Defaults to `false`.",
41+
Type: schema.TypeBool,
42+
Optional: true,
43+
Default: false,
44+
},
45+
"masked": {
46+
Description: "If set to `true`, the value of the variable will be hidden in job logs. The value must meet the [masking requirements](https://docs.gitlab.com/ee/ci/variables/#masked-variables). Defaults to `false`.",
47+
Type: schema.TypeBool,
48+
Optional: true,
49+
Default: false,
50+
},
51+
"environment_scope": {
52+
Description: "The environment scope of the variable. Defaults to all environment (`*`). Note that in Community Editions of Gitlab, values other than `*` will cause inconsistent plans.",
53+
Type: schema.TypeString,
54+
Optional: true,
55+
Default: "*",
56+
// Versions of GitLab prior to 13.4 cannot update environment_scope.
57+
ForceNew: true,
58+
},
59+
}
60+
}
61+
62+
func gitlabGroupVariableToStateMap(group string, variable *gitlab.GroupVariable) map[string]interface{} {
63+
stateMap := make(map[string]interface{})
64+
stateMap["group"] = group
65+
stateMap["key"] = variable.Key
66+
stateMap["value"] = variable.Value
67+
stateMap["variable_type"] = variable.VariableType
68+
stateMap["protected"] = variable.Protected
69+
stateMap["masked"] = variable.Masked
70+
stateMap["environment_scope"] = variable.EnvironmentScope
71+
return stateMap
72+
}

0 commit comments

Comments
 (0)