Skip to content

Commit 28ab13d

Browse files
committed
Add test and docs
Signed-off-by: Sune Keller <[email protected]>
1 parent 350ef9c commit 28ab13d

File tree

2 files changed

+177
-0
lines changed

2 files changed

+177
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
layout: "gitlab"
3+
page_title: "GitLab: gitlab_instance_variable"
4+
sidebar_current: "docs-gitlab-resource-instance-variable"
5+
description: |-
6+
Creates and manages CI/CD variables for GitLab instances
7+
---
8+
9+
# gitlab\_instance\_variable
10+
11+
This resource allows you to create and manage CI/CD variables for your GitLab instance.
12+
For further information on variables, consult the [gitlab
13+
documentation](https://docs.gitlab.com/ee/api/instance_level_ci_variables.html).
14+
15+
## Example Usage
16+
17+
```hcl
18+
resource "gitlab_instance_variable" "example" {
19+
key = "instance_variable_key"
20+
value = "instance_variable_value"
21+
protected = false
22+
masked = false
23+
}
24+
```
25+
26+
## Argument Reference
27+
28+
The following arguments are supported:
29+
30+
* `key` - (Required, string) The name of the variable.
31+
32+
* `value` - (Required, string) The value of the variable.
33+
34+
* `variable_type` - (Optional, string) The type of a variable. Available types are: env_var (default) and file.
35+
36+
* `protected` - (Optional, boolean) If set to `true`, the variable will be passed only to pipelines running on protected branches and tags. Defaults to `false`.
37+
38+
* `masked` - (Optional, 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`.
39+
40+
## Import
41+
42+
GitLab instance variables can be imported using an id made up of `variablename`, e.g.
43+
44+
```console
45+
$ terraform import gitlab_instance_variable.example instance_variable_key
46+
```
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package gitlab
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-sdk/terraform"
10+
"github.com/xanzy/go-gitlab"
11+
)
12+
13+
func TestAccGitlabInstanceVariable_basic(t *testing.T) {
14+
var instanceVariable gitlab.InstanceVariable
15+
rString := acctest.RandString(5)
16+
17+
resource.Test(t, resource.TestCase{
18+
PreCheck: func() { testAccPreCheck(t) },
19+
Providers: testAccProviders,
20+
Steps: []resource.TestStep{
21+
// Create a variable with default options
22+
{
23+
Config: testAccGitlabInstanceVariableConfig(rString),
24+
Check: resource.ComposeTestCheckFunc(
25+
testAccCheckGitlabInstanceVariableExists("gitlab_instance_variable.foo", &instanceVariable),
26+
testAccCheckGitlabInstanceVariableAttributes(&instanceVariable, &testAccGitlabInstanceVariableExpectedAttributes{
27+
Key: fmt.Sprintf("key_%s", rString),
28+
Value: fmt.Sprintf("value-%s", rString),
29+
}),
30+
),
31+
},
32+
// Update the instance variable to toggle all the values to their inverse
33+
{
34+
Config: testAccGitlabInstanceVariableUpdateConfig(rString),
35+
Check: resource.ComposeTestCheckFunc(
36+
testAccCheckGitlabInstanceVariableExists("gitlab_instance_variable.foo", &instanceVariable),
37+
testAccCheckGitlabInstanceVariableAttributes(&instanceVariable, &testAccGitlabInstanceVariableExpectedAttributes{
38+
Key: fmt.Sprintf("key_%s", rString),
39+
Value: fmt.Sprintf("value-inverse-%s", rString),
40+
Protected: true,
41+
}),
42+
),
43+
},
44+
// Update the instance variable to toggle the options back
45+
{
46+
Config: testAccGitlabInstanceVariableConfig(rString),
47+
Check: resource.ComposeTestCheckFunc(
48+
testAccCheckGitlabInstanceVariableExists("gitlab_instance_variable.foo", &instanceVariable),
49+
testAccCheckGitlabInstanceVariableAttributes(&instanceVariable, &testAccGitlabInstanceVariableExpectedAttributes{
50+
Key: fmt.Sprintf("key_%s", rString),
51+
Value: fmt.Sprintf("value-%s", rString),
52+
Protected: false,
53+
}),
54+
),
55+
},
56+
},
57+
})
58+
}
59+
60+
func testAccCheckGitlabInstanceVariableExists(n string, instanceVariable *gitlab.InstanceVariable) resource.TestCheckFunc {
61+
return func(s *terraform.State) error {
62+
rs, ok := s.RootModule().Resources[n]
63+
if !ok {
64+
return fmt.Errorf("Not Found: %s", n)
65+
}
66+
67+
key := rs.Primary.Attributes["key"]
68+
if key == "" {
69+
return fmt.Errorf("No variable key is set")
70+
}
71+
conn := testAccProvider.Meta().(*gitlab.Client)
72+
73+
gotVariable, _, err := conn.InstanceVariables.GetVariable(key)
74+
if err != nil {
75+
return err
76+
}
77+
*instanceVariable = *gotVariable
78+
return nil
79+
}
80+
}
81+
82+
type testAccGitlabInstanceVariableExpectedAttributes struct {
83+
Key string
84+
Value string
85+
Protected bool
86+
Masked bool
87+
}
88+
89+
func testAccCheckGitlabInstanceVariableAttributes(variable *gitlab.InstanceVariable, want *testAccGitlabInstanceVariableExpectedAttributes) resource.TestCheckFunc {
90+
return func(s *terraform.State) error {
91+
if variable.Key != want.Key {
92+
return fmt.Errorf("got key %s; want %s", variable.Key, want.Key)
93+
}
94+
95+
if variable.Value != want.Value {
96+
return fmt.Errorf("got value %s; value %s", variable.Value, want.Value)
97+
}
98+
99+
if variable.Protected != want.Protected {
100+
return fmt.Errorf("got protected %t; want %t", variable.Protected, want.Protected)
101+
}
102+
103+
if variable.Masked != want.Masked {
104+
return fmt.Errorf("got masked %t; want %t", variable.Masked, want.Masked)
105+
}
106+
107+
return nil
108+
}
109+
}
110+
111+
func testAccGitlabInstanceVariableConfig(rString string) string {
112+
return fmt.Sprintf(`
113+
resource "gitlab_instance_variable" "foo" {
114+
key = "key_%s"
115+
value = "value-%s"
116+
variable_type = "file"
117+
masked = false
118+
}
119+
`, rString, rString)
120+
}
121+
122+
func testAccGitlabInstanceVariableUpdateConfig(rString string) string {
123+
return fmt.Sprintf(`
124+
resource "gitlab_instance_variable" "foo" {
125+
key = "key_%s"
126+
value = "value-inverse-%s"
127+
protected = true
128+
masked = false
129+
}
130+
`, rString, rString)
131+
}

0 commit comments

Comments
 (0)