Skip to content

Commit e6d191d

Browse files
committed
New data source gitlab_project_variable
Refs: #386
1 parent 5fd4230 commit e6d191d

File tree

6 files changed

+223
-58
lines changed

6 files changed

+223
-58
lines changed

docs/data-sources/project_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_project_variable Data Source - terraform-provider-gitlab"
4+
subcategory: ""
5+
description: |-
6+
The gitlab_project_variable data source allows to retrieve details about a project-level CI/CD variable.
7+
Upstream API: GitLab REST API docs https://docs.gitlab.com/ee/api/project_level_variables.html
8+
---
9+
10+
# gitlab_project_variable (Data Source)
11+
12+
The `gitlab_project_variable` data source allows to retrieve details about a project-level CI/CD variable.
13+
14+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/project_level_variables.html)
15+
16+
17+
18+
<!-- schema generated by tfplugindocs -->
19+
## Schema
20+
21+
### Required
22+
23+
- `key` (String) The name of the variable.
24+
- `project` (String) The name or id of the project.
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/project_variable.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ resource "gitlab_project_variable" "example" {
3838

3939
### Optional
4040

41-
- `environment_scope` (String) The environment_scope of the variable. Defaults to `*`.
41+
- `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.
4242
- `id` (String) The ID of this resource.
43-
- `masked` (Boolean) If set to `true`, the variable will be masked if it would have been written to the logs. Defaults to `false`.
43+
- `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`.
4444
- `protected` (Boolean) If set to `true`, the variable will be passed only to pipelines running on protected branches and tags. Defaults to `false`.
45-
- `variable_type` (String) The type of a variable. Available types are: env_var (default) and file.
45+
- `variable_type` (String) The type of a variable. Valid values are: `env_var`, `file`. Default is `env_var`.
4646

4747
## Import
4848

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_project_variable", func() *schema.Resource {
13+
return &schema.Resource{
14+
Description: `The ` + "`gitlab_project_variable`" + ` data source allows to retrieve details about a project-level CI/CD variable.
15+
16+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/project_level_variables.html)`,
17+
18+
ReadContext: dataSourceGitlabProjectVariableRead,
19+
Schema: datasourceSchemaFromResourceSchema(gitlabProjectVariableGetSchema(), []string{"project", "key"}, []string{"environment_scope"}),
20+
}
21+
})
22+
23+
func dataSourceGitlabProjectVariableRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
24+
client := meta.(*gitlab.Client)
25+
project := d.Get("project").(string)
26+
key := d.Get("key").(string)
27+
environmentScope := d.Get("environment_scope").(string)
28+
29+
variable, _, err := client.ProjectVariables.GetVariable(project, 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", project, key, environmentScope))
35+
stateMap := gitlabProjectVariableToStateMap(project, 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 TestAccDataSourceGitlabProjectVariable_basic(t *testing.T) {
12+
testAccCheck(t)
13+
14+
testProject := testAccCreateProject(t)
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_project_variable" "this" {
23+
project = %d
24+
key = "any_key"
25+
value = "any-value"
26+
environment_scope = "*"
27+
}
28+
29+
data "gitlab_project_variable" "this" {
30+
project = gitlab_project_variable.this.project
31+
key = gitlab_project_variable.this.key
32+
environment_scope = gitlab_project_variable.this.environment_scope
33+
}
34+
`, testProject.ID,
35+
),
36+
Check: resource.ComposeTestCheckFunc(
37+
testAccDataSourceGitlabProjectVariable("gitlab_project_variable.this", "data.gitlab_project_variable.this"),
38+
),
39+
},
40+
},
41+
})
42+
}
43+
44+
func testAccDataSourceGitlabProjectVariable(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(gitlabProjectVariableGetSchema())
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+
}

internal/provider/resource_gitlab_project_variable.go

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

29-
Schema: map[string]*schema.Schema{
30-
"project": {
31-
Description: "The name or id of the project.",
32-
Type: schema.TypeString,
33-
ForceNew: true,
34-
Required: true,
35-
},
36-
"key": {
37-
Description: "The name of the variable.",
38-
Type: schema.TypeString,
39-
ForceNew: true,
40-
Required: true,
41-
ValidateFunc: StringIsGitlabVariableName,
42-
},
43-
"value": {
44-
Description: "The value of the variable.",
45-
Type: schema.TypeString,
46-
Required: true,
47-
Sensitive: true,
48-
},
49-
"variable_type": {
50-
Description: "The type of a variable. Available types are: env_var (default) and file.",
51-
Type: schema.TypeString,
52-
Optional: true,
53-
Default: "env_var",
54-
ValidateFunc: StringIsGitlabVariableType,
55-
},
56-
"protected": {
57-
Description: "If set to `true`, the variable will be passed only to pipelines running on protected branches and tags. Defaults to `false`.",
58-
Type: schema.TypeBool,
59-
Optional: true,
60-
Default: false,
61-
},
62-
"masked": {
63-
Description: "If set to `true`, the variable will be masked if it would have been written to the logs. Defaults to `false`.",
64-
Type: schema.TypeBool,
65-
Optional: true,
66-
Default: false,
67-
},
68-
"environment_scope": {
69-
Description: "The environment_scope of the variable. Defaults to `*`.",
70-
Type: schema.TypeString,
71-
Optional: true,
72-
Default: "*",
73-
// Versions of GitLab prior to 13.4 cannot update environment_scope.
74-
ForceNew: true,
75-
},
76-
},
29+
Schema: gitlabProjectVariableGetSchema(),
7730
}
7831
})
7932

@@ -148,13 +101,10 @@ func resourceGitlabProjectVariableRead(ctx context.Context, d *schema.ResourceDa
148101
return augmentVariableClientError(d, err)
149102
}
150103

151-
d.Set("key", variable.Key)
152-
d.Set("value", variable.Value)
153-
d.Set("variable_type", variable.VariableType)
154-
d.Set("project", project)
155-
d.Set("protected", variable.Protected)
156-
d.Set("masked", variable.Masked)
157-
d.Set("environment_scope", variable.EnvironmentScope)
104+
stateMap := gitlabProjectVariableToStateMap(project, variable)
105+
if err = setStateMapInResourceData(stateMap, d); err != nil {
106+
return diag.FromErr(err)
107+
}
158108
return nil
159109
}
160110

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
var gitlabVariableTypeValues = []string{"env_var", "file"}
12+
13+
func gitlabProjectVariableGetSchema() map[string]*schema.Schema {
14+
return map[string]*schema.Schema{
15+
"project": {
16+
Description: "The name or id of the project.",
17+
Type: schema.TypeString,
18+
ForceNew: true,
19+
Required: true,
20+
},
21+
"key": {
22+
Description: "The name of the variable.",
23+
Type: schema.TypeString,
24+
ForceNew: true,
25+
Required: true,
26+
ValidateFunc: StringIsGitlabVariableName,
27+
},
28+
"value": {
29+
Description: "The value of the variable.",
30+
Type: schema.TypeString,
31+
Required: true,
32+
Sensitive: true,
33+
},
34+
"variable_type": {
35+
Description: fmt.Sprintf("The type of a variable. Valid values are: %s. Default is `env_var`.", renderValueListForDocs(gitlabVariableTypeValues)),
36+
Type: schema.TypeString,
37+
Optional: true,
38+
Default: "env_var",
39+
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(gitlabVariableTypeValues, false)),
40+
},
41+
"protected": {
42+
Description: "If set to `true`, the variable will be passed only to pipelines running on protected branches and tags. Defaults to `false`.",
43+
Type: schema.TypeBool,
44+
Optional: true,
45+
Default: false,
46+
},
47+
"masked": {
48+
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`.",
49+
Type: schema.TypeBool,
50+
Optional: true,
51+
Default: false,
52+
},
53+
"environment_scope": {
54+
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.",
55+
Type: schema.TypeString,
56+
Optional: true,
57+
Default: "*",
58+
// Versions of GitLab prior to 13.4 cannot update environment_scope.
59+
ForceNew: true,
60+
},
61+
}
62+
}
63+
64+
func gitlabProjectVariableToStateMap(project string, variable *gitlab.ProjectVariable) map[string]interface{} {
65+
stateMap := make(map[string]interface{})
66+
stateMap["project"] = project
67+
stateMap["key"] = variable.Key
68+
stateMap["value"] = variable.Value
69+
stateMap["variable_type"] = variable.VariableType
70+
stateMap["protected"] = variable.Protected
71+
stateMap["masked"] = variable.Masked
72+
stateMap["environment_scope"] = variable.EnvironmentScope
73+
return stateMap
74+
}

0 commit comments

Comments
 (0)