Skip to content

Commit f1919d2

Browse files
author
Julien Pivotto
committed
gitlab_group: support parent_id field
Signed-off-by: Julien Pivotto <[email protected]>
1 parent e34eb3f commit f1919d2

File tree

6 files changed

+207
-10
lines changed

6 files changed

+207
-10
lines changed

gitlab/resource_gitlab_group.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ func resourceGitlabGroup() *schema.Resource {
4848
Computed: true,
4949
ValidateFunc: validation.StringInSlice([]string{"private", "internal", "public"}, true),
5050
},
51+
"parent_id": {
52+
Type: schema.TypeInt,
53+
Optional: true,
54+
ForceNew: true,
55+
Default: 0,
56+
},
5157
},
5258
}
5359
}
@@ -72,6 +78,10 @@ func resourceGitlabGroupCreate(d *schema.ResourceData, meta interface{}) error {
7278
options.Visibility = stringToVisibilityLevel(v.(string))
7379
}
7480

81+
if v, ok := d.GetOk("parent_id"); ok {
82+
options.ParentID = gitlab.Int(v.(int))
83+
}
84+
7585
log.Printf("[DEBUG] create gitlab group %q", options.Name)
7686

7787
group, _, err := client.Groups.CreateGroup(options)
@@ -105,6 +115,7 @@ func resourceGitlabGroupRead(d *schema.ResourceData, meta interface{}) error {
105115
d.Set("lfs_enabled", group.LFSEnabled)
106116
d.Set("request_access_enabled", group.RequestAccessEnabled)
107117
d.Set("visibility_level", group.Visibility)
118+
d.Set("parent_id", group.ParentID)
108119

109120
return nil
110121
}

gitlab/resource_gitlab_group_test.go

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,80 @@ func TestAccGitlabGroup_import(t *testing.T) {
8282
})
8383
}
8484

85+
func TestAccGitlabGroup_nested(t *testing.T) {
86+
var group gitlab.Group
87+
var group2 gitlab.Group
88+
var nestedGroup gitlab.Group
89+
rInt := acctest.RandInt()
90+
91+
resource.Test(t, resource.TestCase{
92+
PreCheck: func() { testAccPreCheck(t) },
93+
Providers: testAccProviders,
94+
CheckDestroy: testAccCheckGitlabGroupDestroy,
95+
Steps: []resource.TestStep{
96+
{
97+
Config: testAccGitlabNestedGroupConfig(rInt),
98+
Check: resource.ComposeTestCheckFunc(
99+
testAccCheckGitlabGroupExists("gitlab_group.foo", &group),
100+
testAccCheckGitlabGroupExists("gitlab_group.foo2", &group2),
101+
testAccCheckGitlabGroupExists("gitlab_group.nested_foo", &nestedGroup),
102+
testAccCheckGitlabGroupAttributes(&nestedGroup, &testAccGitlabGroupExpectedAttributes{
103+
Name: fmt.Sprintf("nfoo-name-%d", rInt),
104+
Path: fmt.Sprintf("nfoo-path-%d", rInt),
105+
Description: "Terraform acceptance tests",
106+
LFSEnabled: true,
107+
Parent: &group,
108+
}),
109+
),
110+
},
111+
{
112+
Config: testAccGitlabNestedGroupChangeParentConfig(rInt),
113+
Check: resource.ComposeTestCheckFunc(
114+
testAccCheckGitlabGroupExists("gitlab_group.foo", &group),
115+
testAccCheckGitlabGroupExists("gitlab_group.foo2", &group2),
116+
testAccCheckGitlabGroupExists("gitlab_group.nested_foo", &nestedGroup),
117+
testAccCheckGitlabGroupAttributes(&nestedGroup, &testAccGitlabGroupExpectedAttributes{
118+
Name: fmt.Sprintf("nfoo-name-%d", rInt),
119+
Path: fmt.Sprintf("nfoo-path-%d", rInt),
120+
Description: "Terraform acceptance tests - new parent",
121+
LFSEnabled: true,
122+
Parent: &group2,
123+
}),
124+
),
125+
},
126+
{
127+
Config: testAccGitlabNestedGroupRemoveParentConfig(rInt),
128+
Check: resource.ComposeTestCheckFunc(
129+
testAccCheckGitlabGroupExists("gitlab_group.foo", &group),
130+
testAccCheckGitlabGroupExists("gitlab_group.foo2", &group2),
131+
testAccCheckGitlabGroupExists("gitlab_group.nested_foo", &nestedGroup),
132+
testAccCheckGitlabGroupAttributes(&nestedGroup, &testAccGitlabGroupExpectedAttributes{
133+
Name: fmt.Sprintf("nfoo-name-%d", rInt),
134+
Path: fmt.Sprintf("nfoo-path-%d", rInt),
135+
Description: "Terraform acceptance tests - updated",
136+
LFSEnabled: true,
137+
}),
138+
),
139+
},
140+
{
141+
Config: testAccGitlabNestedGroupConfig(rInt),
142+
Check: resource.ComposeTestCheckFunc(
143+
testAccCheckGitlabGroupExists("gitlab_group.foo", &group),
144+
testAccCheckGitlabGroupExists("gitlab_group.foo2", &group2),
145+
testAccCheckGitlabGroupExists("gitlab_group.nested_foo", &nestedGroup),
146+
testAccCheckGitlabGroupAttributes(&nestedGroup, &testAccGitlabGroupExpectedAttributes{
147+
Name: fmt.Sprintf("nfoo-name-%d", rInt),
148+
Path: fmt.Sprintf("nfoo-path-%d", rInt),
149+
Description: "Terraform acceptance tests",
150+
LFSEnabled: true,
151+
Parent: &group,
152+
}),
153+
),
154+
},
155+
},
156+
})
157+
}
158+
85159
func testAccCheckGitlabGroupExists(n string, group *gitlab.Group) resource.TestCheckFunc {
86160
return func(s *terraform.State) error {
87161
rs, ok := s.RootModule().Resources[n]
@@ -108,6 +182,7 @@ type testAccGitlabGroupExpectedAttributes struct {
108182
Name string
109183
Path string
110184
Description string
185+
Parent *gitlab.Group
111186
LFSEnabled bool
112187
RequestAccessEnabled bool
113188
}
@@ -134,6 +209,16 @@ func testAccCheckGitlabGroupAttributes(group *gitlab.Group, want *testAccGitlabG
134209
return fmt.Errorf("got request_access_enabled %t; want %t", group.RequestAccessEnabled, want.RequestAccessEnabled)
135210
}
136211

212+
if want.Parent != nil {
213+
if group.ParentID != want.Parent.ID {
214+
return fmt.Errorf("got parent_id %d; want %d", group.ParentID, want.Parent.ID)
215+
}
216+
} else {
217+
if group.ParentID != 0 {
218+
return fmt.Errorf("got parent_id %d; want %d", group.ParentID, 0)
219+
}
220+
}
221+
137222
return nil
138223
}
139224
}
@@ -189,3 +274,101 @@ resource "gitlab_group" "foo" {
189274
}
190275
`, rInt, rInt)
191276
}
277+
278+
func testAccGitlabNestedGroupConfig(rInt int) string {
279+
return fmt.Sprintf(`
280+
resource "gitlab_group" "foo" {
281+
name = "foo-name-%d"
282+
path = "foo-path-%d"
283+
description = "Terraform acceptance tests"
284+
285+
# So that acceptance tests can be run in a gitlab organization
286+
# with no billing
287+
visibility_level = "public"
288+
}
289+
resource "gitlab_group" "foo2" {
290+
name = "foo2-name-%d"
291+
path = "foo2-path-%d"
292+
description = "Terraform acceptance tests - parent2"
293+
294+
# So that acceptance tests can be run in a gitlab organization
295+
# with no billing
296+
visibility_level = "public"
297+
}
298+
resource "gitlab_group" "nested_foo" {
299+
name = "nfoo-name-%d"
300+
path = "nfoo-path-%d"
301+
parent_id = "${gitlab_group.foo.id}"
302+
description = "Terraform acceptance tests"
303+
304+
# So that acceptance tests can be run in a gitlab organization
305+
# with no billing
306+
visibility_level = "public"
307+
}
308+
`, rInt, rInt, rInt, rInt, rInt, rInt)
309+
}
310+
311+
func testAccGitlabNestedGroupRemoveParentConfig(rInt int) string {
312+
return fmt.Sprintf(`
313+
resource "gitlab_group" "foo" {
314+
name = "foo-name-%d"
315+
path = "foo-path-%d"
316+
description = "Terraform acceptance tests"
317+
318+
# So that acceptance tests can be run in a gitlab organization
319+
# with no billing
320+
visibility_level = "public"
321+
}
322+
resource "gitlab_group" "foo2" {
323+
name = "foo2-name-%d"
324+
path = "foo2-path-%d"
325+
description = "Terraform acceptance tests - parent2"
326+
327+
# So that acceptance tests can be run in a gitlab organization
328+
# with no billing
329+
visibility_level = "public"
330+
}
331+
resource "gitlab_group" "nested_foo" {
332+
name = "nfoo-name-%d"
333+
path = "nfoo-path-%d"
334+
description = "Terraform acceptance tests - updated"
335+
336+
# So that acceptance tests can be run in a gitlab organization
337+
# with no billing
338+
visibility_level = "public"
339+
}
340+
`, rInt, rInt, rInt, rInt, rInt, rInt)
341+
}
342+
343+
func testAccGitlabNestedGroupChangeParentConfig(rInt int) string {
344+
return fmt.Sprintf(`
345+
resource "gitlab_group" "foo" {
346+
name = "foo-name-%d"
347+
path = "foo-path-%d"
348+
description = "Terraform acceptance tests"
349+
350+
# So that acceptance tests can be run in a gitlab organization
351+
# with no billing
352+
visibility_level = "public"
353+
}
354+
resource "gitlab_group" "foo2" {
355+
name = "foo2-name-%d"
356+
path = "foo2-path-%d"
357+
description = "Terraform acceptance tests - parent2"
358+
359+
# So that acceptance tests can be run in a gitlab organization
360+
# with no billing
361+
visibility_level = "public"
362+
}
363+
resource "gitlab_group" "nested_foo" {
364+
name = "nfoo-name-%d"
365+
path = "nfoo-path-%d"
366+
description = "Terraform acceptance tests - new parent"
367+
parent_id = "${gitlab_group.foo2.id}"
368+
369+
# So that acceptance tests can be run in a gitlab organization
370+
# with no billing
371+
visibility_level = "public"
372+
}
373+
`, rInt, rInt, rInt, rInt, rInt, rInt)
374+
}

vendor/github.com/xanzy/go-gitlab/groups.go

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

vendor/github.com/xanzy/go-gitlab/merge_requests.go

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

vendor/vendor.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -585,10 +585,10 @@
585585
"revisionTime": "2016-09-27T10:08:44Z"
586586
},
587587
{
588-
"checksumSHA1": "NGUUV3ypJQQXQMy+grgMxuTyC/Y=",
588+
"checksumSHA1": "rBRMciteJAFVf9xWMXbMbXbJrzM=",
589589
"path": "github.com/xanzy/go-gitlab",
590-
"revision": "b3ac6922c154579c76836ccd3ab66f377986202c",
591-
"revisionTime": "2017-09-22T13:45:23Z"
590+
"revision": "d11d546f58c67430417f975b453f44f280474a4d",
591+
"revisionTime": "2017-09-24T15:03:35Z"
592592
},
593593
{
594594
"checksumSHA1": "vE43s37+4CJ2CDU6TlOUOYE0K9c=",

website/docs/r/group.html.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ enable users to request access to the group.
4848
Valid values are `private`, `internal`, `public`.
4949
Groups are created as private by default.
5050

51+
* `parent_id` - (Optional) Integer, id of the parent group (creates a nested group).
52+
5153
## Attributes Reference
5254

5355
The resource exports the following attributes:

0 commit comments

Comments
 (0)