Skip to content

Commit a900c3c

Browse files
Support import of group-variable
1 parent 7475d81 commit a900c3c

25 files changed

+1424
-159
lines changed

gitlab/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func Provider() terraform.ResourceProvider {
5656
"gitlab_project_membership": resourceGitlabProjectMembership(),
5757
"gitlab_group_membership": resourceGitlabGroupMembership(),
5858
"gitlab_project_variable": resourceGitlabProjectVariable(),
59+
"gitlab_group_variable": resourceGitlabGroupVariable(),
5960
},
6061

6162
ConfigureFunc: providerConfigure,
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package gitlab
2+
3+
import (
4+
"log"
5+
6+
"github.com/hashicorp/terraform/helper/schema"
7+
"github.com/xanzy/go-gitlab"
8+
)
9+
10+
func resourceGitlabGroupVariable() *schema.Resource {
11+
return &schema.Resource{
12+
Create: resourceGitlabGroupVariableCreate,
13+
Read: resourceGitlabGroupVariableRead,
14+
Update: resourceGitlabGroupVariableUpdate,
15+
Delete: resourceGitlabGroupVariableDelete,
16+
Importer: &schema.ResourceImporter{
17+
State: schema.ImportStatePassthrough,
18+
},
19+
20+
Schema: map[string]*schema.Schema{
21+
"group": {
22+
Type: schema.TypeString,
23+
Required: true,
24+
},
25+
"key": {
26+
Type: schema.TypeString,
27+
Required: true,
28+
ValidateFunc: StringIsGitlabVariableName(),
29+
},
30+
"value": {
31+
Type: schema.TypeString,
32+
Required: true,
33+
Sensitive: true,
34+
},
35+
"protected": {
36+
Type: schema.TypeBool,
37+
Optional: true,
38+
Default: false,
39+
},
40+
},
41+
}
42+
}
43+
44+
func resourceGitlabGroupVariableCreate(d *schema.ResourceData, meta interface{}) error {
45+
client := meta.(*gitlab.Client)
46+
47+
group := d.Get("group").(string)
48+
key := d.Get("key").(string)
49+
value := d.Get("value").(string)
50+
protected := d.Get("protected").(bool)
51+
52+
options := gitlab.CreateVariableOptions{
53+
Key: &key,
54+
Value: &value,
55+
Protected: &protected,
56+
EnvironmentScope: nil,
57+
}
58+
log.Printf("[DEBUG] create gitlab group variable %s/%s", group, key)
59+
60+
groupVariable, _, err := client.GroupVariables.CreateVariable(group, &options)
61+
if err != nil {
62+
return err
63+
}
64+
65+
d.SetId(buildTwoPartID(&group, &groupVariable.Key))
66+
67+
return resourceGitlabGroupVariableRead(d, meta)
68+
}
69+
70+
func resourceGitlabGroupVariableRead(d *schema.ResourceData, meta interface{}) error {
71+
client := meta.(*gitlab.Client)
72+
73+
group, key, err := parseTwoPartID(d.Id())
74+
if err != nil {
75+
return err
76+
}
77+
78+
log.Printf("[DEBUG] read gitlab group variable %s/%s", group, key)
79+
80+
v, _, err := client.GroupVariables.GetVariable(group, key)
81+
if err != nil {
82+
return err
83+
}
84+
85+
d.Set("key", v.Key)
86+
d.Set("value", v.Value)
87+
d.Set("group", group)
88+
d.Set("protected", v.Protected)
89+
d.SetId(buildTwoPartID(&group, &v.Key))
90+
91+
return nil
92+
}
93+
94+
func resourceGitlabGroupVariableUpdate(d *schema.ResourceData, meta interface{}) error {
95+
client := meta.(*gitlab.Client)
96+
97+
group := d.Get("group").(string)
98+
key := d.Get("key").(string)
99+
value := d.Get("value").(string)
100+
protected := d.Get("protected").(bool)
101+
102+
options := &gitlab.UpdateVariableOptions{
103+
Key: &key,
104+
Value: &value,
105+
Protected: &protected,
106+
EnvironmentScope: nil,
107+
}
108+
log.Printf("[DEBUG] update gitlab group variable %s/%s", group, key)
109+
110+
v, _, err := client.GroupVariables.UpdateVariable(group, key, options)
111+
if err != nil {
112+
return err
113+
}
114+
115+
d.SetId(buildTwoPartID(&group, &v.Key))
116+
117+
return resourceGitlabGroupVariableRead(d, meta)
118+
}
119+
120+
func resourceGitlabGroupVariableDelete(d *schema.ResourceData, meta interface{}) error {
121+
client := meta.(*gitlab.Client)
122+
group := d.Get("group").(string)
123+
key := d.Get("key").(string)
124+
log.Printf("[DEBUG] Delete gitlab group variable %s/%s", group, key)
125+
126+
_, err := client.GroupVariables.RemoveVariable(group, key)
127+
return err
128+
}
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
package gitlab
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform/helper/acctest"
8+
"github.com/hashicorp/terraform/helper/resource"
9+
"github.com/hashicorp/terraform/terraform"
10+
"github.com/xanzy/go-gitlab"
11+
)
12+
13+
func TestAccGitlabGroupVariable_basic(t *testing.T) {
14+
var groupVariable gitlab.GroupVariable
15+
rString := acctest.RandString(5)
16+
17+
resource.Test(t, resource.TestCase{
18+
PreCheck: func() { testAccPreCheck(t) },
19+
Providers: testAccProviders,
20+
CheckDestroy: testAccCheckGitlabGroupVariableDestroy,
21+
Steps: []resource.TestStep{
22+
// Create a group and variable with default options
23+
{
24+
Config: testAccGitlabGroupVariableConfig(rString),
25+
Check: resource.ComposeTestCheckFunc(
26+
testAccCheckGitlabGroupVariableExists("gitlab_group_variable.foo", &groupVariable),
27+
testAccCheckGitlabGroupVariableAttributes(&groupVariable, &testAccGitlabGroupVariableExpectedAttributes{
28+
Key: fmt.Sprintf("key_%s", rString),
29+
Value: fmt.Sprintf("value-%s", rString),
30+
}),
31+
),
32+
},
33+
// Update the group variable to toggle all the values to their inverse
34+
{
35+
Config: testAccGitlabGroupVariableUpdateConfig(rString),
36+
Check: resource.ComposeTestCheckFunc(
37+
testAccCheckGitlabGroupVariableExists("gitlab_group_variable.foo", &groupVariable),
38+
testAccCheckGitlabGroupVariableAttributes(&groupVariable, &testAccGitlabGroupVariableExpectedAttributes{
39+
Key: fmt.Sprintf("key_%s", rString),
40+
Value: fmt.Sprintf("value-inverse-%s", rString),
41+
Protected: true,
42+
}),
43+
),
44+
},
45+
// Update the group variable to toggle the options back
46+
{
47+
Config: testAccGitlabGroupVariableConfig(rString),
48+
Check: resource.ComposeTestCheckFunc(
49+
testAccCheckGitlabGroupVariableExists("gitlab_group_variable.foo", &groupVariable),
50+
testAccCheckGitlabGroupVariableAttributes(&groupVariable, &testAccGitlabGroupVariableExpectedAttributes{
51+
Key: fmt.Sprintf("key_%s", rString),
52+
Value: fmt.Sprintf("value-%s", rString),
53+
Protected: false,
54+
}),
55+
),
56+
},
57+
},
58+
})
59+
}
60+
61+
func testAccCheckGitlabGroupVariableExists(n string, groupVariable *gitlab.GroupVariable) resource.TestCheckFunc {
62+
return func(s *terraform.State) error {
63+
rs, ok := s.RootModule().Resources[n]
64+
if !ok {
65+
return fmt.Errorf("Not Found: %s", n)
66+
}
67+
68+
repoName := rs.Primary.Attributes["group"]
69+
if repoName == "" {
70+
return fmt.Errorf("No group ID is set")
71+
}
72+
key := rs.Primary.Attributes["key"]
73+
if key == "" {
74+
return fmt.Errorf("No variable key is set")
75+
}
76+
conn := testAccProvider.Meta().(*gitlab.Client)
77+
78+
gotVariable, _, err := conn.GroupVariables.GetVariable(repoName, key)
79+
if err != nil {
80+
return err
81+
}
82+
*groupVariable = *gotVariable
83+
return nil
84+
}
85+
}
86+
87+
type testAccGitlabGroupVariableExpectedAttributes struct {
88+
Key string
89+
Value string
90+
Protected bool
91+
}
92+
93+
func testAccCheckGitlabGroupVariableAttributes(variable *gitlab.GroupVariable, want *testAccGitlabGroupVariableExpectedAttributes) resource.TestCheckFunc {
94+
return func(s *terraform.State) error {
95+
if variable.Key != want.Key {
96+
return fmt.Errorf("got key %s; want %s", variable.Key, want.Key)
97+
}
98+
99+
if variable.Value != want.Value {
100+
return fmt.Errorf("got value %s; value %s", variable.Value, want.Value)
101+
}
102+
103+
if variable.Protected != want.Protected {
104+
return fmt.Errorf("got protected %t; want %t", variable.Protected, want.Protected)
105+
}
106+
107+
return nil
108+
}
109+
}
110+
111+
func testAccCheckGitlabGroupVariableDestroy(s *terraform.State) error {
112+
conn := testAccProvider.Meta().(*gitlab.Client)
113+
114+
for _, rs := range s.RootModule().Resources {
115+
if rs.Type != "gitlab_group" {
116+
continue
117+
}
118+
119+
_, resp, err := conn.Groups.GetGroup(rs.Primary.ID)
120+
if err == nil {
121+
//if gotRepo != nil && fmt.Sprintf("%d", gotRepo.ID) == rs.Primary.ID {
122+
// return fmt.Errorf("Repository still exists")
123+
//}
124+
}
125+
if resp.StatusCode != 404 {
126+
return err
127+
}
128+
return nil
129+
}
130+
return nil
131+
}
132+
133+
func testAccGitlabGroupVariableConfig(rString string) string {
134+
return fmt.Sprintf(`
135+
resource "gitlab_group" "foo" {
136+
name = "foo%v"
137+
path = "foo%v"
138+
}
139+
140+
resource "gitlab_group_variable" "foo" {
141+
group = "${gitlab_group.foo.id}"
142+
key = "key_%s"
143+
value = "value-%s"
144+
}
145+
`, rString, rString, rString, rString)
146+
}
147+
148+
func testAccGitlabGroupVariableUpdateConfig(rString string) string {
149+
return fmt.Sprintf(`
150+
resource "gitlab_group" "foo" {
151+
name = "foo%v"
152+
path = "foo%v"
153+
}
154+
155+
resource "gitlab_group_variable" "foo" {
156+
group = "${gitlab_group.foo.id}"
157+
key = "key_%s"
158+
value = "value-inverse-%s"
159+
protected = true
160+
}
161+
`, rString, rString, rString, rString)
162+
}

vendor/github.com/xanzy/go-gitlab/README.md

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)