Skip to content

Commit baa9aa1

Browse files
committed
New data source gitlab_instance_variable
Refs: #386
1 parent 25f70e8 commit baa9aa1

File tree

6 files changed

+194
-40
lines changed

6 files changed

+194
-40
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gitlab_instance_variable Data Source - terraform-provider-gitlab"
4+
subcategory: ""
5+
description: |-
6+
The gitlab_instance_variable data source allows to retrieve details about an instance-level CI/CD variable.
7+
Upstream API: GitLab REST API docs https://docs.gitlab.com/ee/api/instance_level_ci_variables.html
8+
---
9+
10+
# gitlab_instance_variable (Data Source)
11+
12+
The `gitlab_instance_variable` data source allows to retrieve details about an instance-level CI/CD variable.
13+
14+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/instance_level_ci_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+
25+
### Optional
26+
27+
- `id` (String) The ID of this resource.
28+
29+
### Read-Only
30+
31+
- `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`.
32+
- `protected` (Boolean) If set to `true`, the variable will be passed only to pipelines running on protected branches and tags. Defaults to `false`.
33+
- `value` (String) The value of the variable.
34+
- `variable_type` (String) The type of a variable. Valid values are: `env_var`, `file`. Default is `env_var`.
35+
36+

docs/resources/instance_variable.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
page_title: "gitlab_instance_variable Resource - terraform-provider-gitlab"
44
subcategory: ""
55
description: |-
6-
The gitlab_instance_variable resource allows to manage the lifecycle of a CI/CD variable for an instance.
6+
The gitlab_instance_variable resource allows to manage the lifecycle of an instance-level CI/CD variable.
77
Upstream API: GitLab REST API docs https://docs.gitlab.com/ee/api/instance_level_variables.html
88
---
99

1010
# gitlab_instance_variable (Resource)
1111

12-
The `gitlab_instance_variable` resource allows to manage the lifecycle of a CI/CD variable for an instance.
12+
The `gitlab_instance_variable` resource allows to manage the lifecycle of an instance-level CI/CD variable.
1313

1414
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/instance_level_variables.html)
1515

@@ -35,9 +35,9 @@ resource "gitlab_instance_variable" "example" {
3535
### Optional
3636

3737
- `id` (String) The ID of this resource.
38-
- `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-variable-requirements). Defaults to `false`.
38+
- `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`.
3939
- `protected` (Boolean) If set to `true`, the variable will be passed only to pipelines running on protected branches and tags. Defaults to `false`.
40-
- `variable_type` (String) The type of a variable. Available types are: env_var (default) and file.
40+
- `variable_type` (String) The type of a variable. Valid values are: `env_var`, `file`. Default is `env_var`.
4141

4242
## Import
4343

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
"github.com/xanzy/go-gitlab"
9+
)
10+
11+
var _ = registerDataSource("gitlab_instance_variable", func() *schema.Resource {
12+
return &schema.Resource{
13+
Description: `The ` + "`gitlab_instance_variable`" + ` data source allows to retrieve details about an instance-level CI/CD variable.
14+
15+
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/instance_level_ci_variables.html)`,
16+
17+
ReadContext: dataSourceGitlabInstanceVariableRead,
18+
Schema: datasourceSchemaFromResourceSchema(gitlabInstanceVariableGetSchema(), []string{"key"}, nil),
19+
}
20+
})
21+
22+
func dataSourceGitlabInstanceVariableRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
23+
client := meta.(*gitlab.Client)
24+
key := d.Get("key").(string)
25+
26+
variable, _, err := client.InstanceVariables.GetVariable(key, nil, gitlab.WithContext(ctx))
27+
if err != nil {
28+
return diag.FromErr(err)
29+
}
30+
31+
d.SetId(key)
32+
stateMap := gitlabInstanceVariableToStateMap(variable)
33+
if err := setStateMapInResourceData(stateMap, d); err != nil {
34+
return diag.FromErr(err)
35+
}
36+
return nil
37+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 TestAccDataSourceGitlabInstanceVariable_basic(t *testing.T) {
12+
resource.Test(t, resource.TestCase{
13+
PreCheck: func() { testAccPreCheck(t) },
14+
ProviderFactories: providerFactories,
15+
Steps: []resource.TestStep{
16+
{
17+
Config: `
18+
resource "gitlab_instance_variable" "this" {
19+
key = "any_key"
20+
value = "any-value"
21+
}
22+
23+
data "gitlab_instance_variable" "this" {
24+
key = gitlab_instance_variable.this.key
25+
}
26+
`,
27+
Check: resource.ComposeTestCheckFunc(
28+
testAccDataSourceGitlabInstanceVariable("gitlab_instance_variable.this", "data.gitlab_instance_variable.this"),
29+
),
30+
},
31+
},
32+
})
33+
}
34+
35+
func testAccDataSourceGitlabInstanceVariable(src, n string) resource.TestCheckFunc {
36+
return func(s *terraform.State) error {
37+
38+
resource := s.RootModule().Resources[src]
39+
resourceAttributes := resource.Primary.Attributes
40+
41+
datasource := s.RootModule().Resources[n]
42+
datasourceAttributes := datasource.Primary.Attributes
43+
44+
testAttributes := attributeNamesFromSchema(gitlabInstanceVariableGetSchema())
45+
46+
for _, attribute := range testAttributes {
47+
if datasourceAttributes[attribute] != resourceAttributes[attribute] {
48+
return fmt.Errorf("Expected variable's attribute `%s` to be: %s, but got: `%s`", attribute, resourceAttributes[attribute], datasourceAttributes[attribute])
49+
}
50+
}
51+
52+
return nil
53+
}
54+
}

internal/provider/resource_gitlab_instance_variable.go

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
var _ = registerResource("gitlab_instance_variable", func() *schema.Resource {
1414
return &schema.Resource{
15-
Description: `The ` + "`" + `gitlab_instance_variable` + "`" + ` resource allows to manage the lifecycle of a CI/CD variable for an instance.
15+
Description: `The ` + "`" + `gitlab_instance_variable` + "`" + ` resource allows to manage the lifecycle of an instance-level CI/CD variable.
1616
1717
**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/instance_level_variables.html)`,
1818

@@ -24,40 +24,7 @@ var _ = registerResource("gitlab_instance_variable", func() *schema.Resource {
2424
StateContext: schema.ImportStatePassthroughContext,
2525
},
2626

27-
Schema: map[string]*schema.Schema{
28-
"key": {
29-
Description: "The name of the variable.",
30-
Type: schema.TypeString,
31-
ForceNew: true,
32-
Required: true,
33-
ValidateFunc: StringIsGitlabVariableName,
34-
},
35-
"value": {
36-
Description: "The value of the variable.",
37-
Type: schema.TypeString,
38-
Required: true,
39-
Sensitive: true,
40-
},
41-
"variable_type": {
42-
Description: "The type of a variable. Available types are: env_var (default) and file.",
43-
Type: schema.TypeString,
44-
Optional: true,
45-
Default: "env_var",
46-
ValidateFunc: StringIsGitlabVariableType,
47-
},
48-
"protected": {
49-
Description: "If set to `true`, the variable will be passed only to pipelines running on protected branches and tags. Defaults to `false`.",
50-
Type: schema.TypeBool,
51-
Optional: true,
52-
Default: false,
53-
},
54-
"masked": {
55-
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-variable-requirements). Defaults to `false`.",
56-
Type: schema.TypeBool,
57-
Optional: true,
58-
Default: false,
59-
},
60-
},
27+
Schema: gitlabInstanceVariableGetSchema(),
6128
}
6229
})
6330

@@ -85,7 +52,6 @@ func resourceGitlabInstanceVariableCreate(ctx context.Context, d *schema.Resourc
8552
}
8653

8754
d.SetId(key)
88-
8955
return resourceGitlabInstanceVariableRead(ctx, d, meta)
9056
}
9157

@@ -111,6 +77,11 @@ func resourceGitlabInstanceVariableRead(ctx context.Context, d *schema.ResourceD
11177
d.Set("variable_type", v.VariableType)
11278
d.Set("protected", v.Protected)
11379
d.Set("masked", v.Masked)
80+
81+
stateMap := gitlabInstanceVariableToStateMap(v)
82+
if err = setStateMapInResourceData(stateMap, d); err != nil {
83+
return diag.FromErr(err)
84+
}
11485
return nil
11586
}
11687

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

0 commit comments

Comments
 (0)