Skip to content

Commit 350ef9c

Browse files
committed
Support instance level CI variables
Signed-off-by: Sune Keller <[email protected]>
1 parent 6d084e6 commit 350ef9c

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

gitlab/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ func Provider() terraform.ResourceProvider {
9494
"gitlab_project_mirror": resourceGitlabProjectMirror(),
9595
"gitlab_project_level_mr_approvals": resourceGitlabProjectLevelMRApprovals(),
9696
"gitlab_project_approval_rule": resourceGitlabProjectApprovalRule(),
97+
"gitlab_instance_variable": resourceGitlabInstanceVariable(),
9798
},
9899
}
99100

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+
"log"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
7+
gitlab "github.com/xanzy/go-gitlab"
8+
)
9+
10+
func resourceGitlabInstanceVariable() *schema.Resource {
11+
return &schema.Resource{
12+
Create: resourceGitlabInstanceVariableCreate,
13+
Read: resourceGitlabInstanceVariableRead,
14+
Update: resourceGitlabInstanceVariableUpdate,
15+
Delete: resourceGitlabInstanceVariableDelete,
16+
Importer: &schema.ResourceImporter{
17+
State: schema.ImportStatePassthrough,
18+
},
19+
20+
Schema: map[string]*schema.Schema{
21+
"key": {
22+
Type: schema.TypeString,
23+
ForceNew: true,
24+
Required: true,
25+
ValidateFunc: StringIsGitlabVariableName,
26+
},
27+
"value": {
28+
Type: schema.TypeString,
29+
Required: true,
30+
Sensitive: true,
31+
},
32+
"variable_type": {
33+
Type: schema.TypeString,
34+
Optional: true,
35+
Default: "env_var",
36+
ValidateFunc: StringIsGitlabVariableType,
37+
},
38+
"protected": {
39+
Type: schema.TypeBool,
40+
Optional: true,
41+
Default: false,
42+
},
43+
"masked": {
44+
Type: schema.TypeBool,
45+
Optional: true,
46+
Default: false,
47+
},
48+
},
49+
}
50+
}
51+
52+
func resourceGitlabInstanceVariableCreate(d *schema.ResourceData, meta interface{}) error {
53+
client := meta.(*gitlab.Client)
54+
55+
key := d.Get("key").(string)
56+
value := d.Get("value").(string)
57+
variableType := stringToVariableType(d.Get("variable_type").(string))
58+
protected := d.Get("protected").(bool)
59+
masked := d.Get("masked").(bool)
60+
61+
options := gitlab.CreateInstanceVariableOptions{
62+
Key: &key,
63+
Value: &value,
64+
VariableType: variableType,
65+
Protected: &protected,
66+
Masked: &masked,
67+
}
68+
log.Printf("[DEBUG] create gitlab instance level CI variable %s", key)
69+
70+
_, _, err := client.InstanceVariables.CreateVariable(&options)
71+
if err != nil {
72+
return err
73+
}
74+
75+
d.SetId(key)
76+
77+
return resourceGitlabInstanceVariableRead(d, meta)
78+
}
79+
80+
func resourceGitlabInstanceVariableRead(d *schema.ResourceData, meta interface{}) error {
81+
client := meta.(*gitlab.Client)
82+
83+
key := d.Id()
84+
85+
log.Printf("[DEBUG] read gitlab instance level CI variable %s", key)
86+
87+
v, _, err := client.InstanceVariables.GetVariable(key)
88+
if err != nil {
89+
return err
90+
}
91+
92+
d.Set("key", v.Key)
93+
d.Set("value", v.Value)
94+
d.Set("variable_type", v.VariableType)
95+
d.Set("protected", v.Protected)
96+
d.Set("masked", v.Masked)
97+
return nil
98+
}
99+
100+
func resourceGitlabInstanceVariableUpdate(d *schema.ResourceData, meta interface{}) error {
101+
client := meta.(*gitlab.Client)
102+
103+
key := d.Get("key").(string)
104+
value := d.Get("value").(string)
105+
variableType := stringToVariableType(d.Get("variable_type").(string))
106+
protected := d.Get("protected").(bool)
107+
masked := d.Get("masked").(bool)
108+
109+
options := &gitlab.UpdateInstanceVariableOptions{
110+
Value: &value,
111+
Protected: &protected,
112+
VariableType: variableType,
113+
Masked: &masked,
114+
}
115+
log.Printf("[DEBUG] update gitlab instance level CI variable %s", key)
116+
117+
_, _, err := client.InstanceVariables.UpdateVariable(key, options)
118+
if err != nil {
119+
return err
120+
}
121+
return resourceGitlabInstanceVariableRead(d, meta)
122+
}
123+
124+
func resourceGitlabInstanceVariableDelete(d *schema.ResourceData, meta interface{}) error {
125+
client := meta.(*gitlab.Client)
126+
key := d.Get("key").(string)
127+
log.Printf("[DEBUG] Delete gitlab instance level CI variable %s", key)
128+
129+
_, err := client.InstanceVariables.RemoveVariable(key)
130+
return err
131+
}

0 commit comments

Comments
 (0)