Skip to content

Commit 76090c8

Browse files
authored
Merge pull request #1078 from gro-gg/main
Moving a subgroup does not force a recreation of the group and no longer deletes the underlying projects
2 parents 24db953 + 8bd9c00 commit 76090c8

File tree

4 files changed

+67
-25
lines changed

4 files changed

+67
-25
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/hashicorp/terraform-plugin-sdk/v2 v2.15.0
99
github.com/mitchellh/hashstructure v1.1.0
1010
github.com/onsi/gomega v1.19.0
11-
github.com/xanzy/go-gitlab v0.64.0
11+
github.com/xanzy/go-gitlab v0.65.0
1212
)
1313

1414
require (

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,8 @@ github.com/vmihailenco/msgpack/v4 v4.3.12 h1:07s4sz9IReOgdikxLTKNbBdqDMLsjPKXwvC
311311
github.com/vmihailenco/msgpack/v4 v4.3.12/go.mod h1:gborTTJjAo/GWTqqRjrLCn9pgNN+NXzzngzBKDPIqw4=
312312
github.com/vmihailenco/tagparser v0.1.1 h1:quXMXlA39OCbd2wAdTsGDlK9RkOk6Wuw+x37wVyIuWY=
313313
github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
314-
github.com/xanzy/go-gitlab v0.64.0 h1:rMgQdW9S1w3qvNAH2LYpFd2xh7KNLk+JWJd7sorNuTc=
315-
github.com/xanzy/go-gitlab v0.64.0/go.mod h1:F0QEXwmqiBUxCgJm8fE9S+1veX4XC9Z4cfaAbqwk4YM=
314+
github.com/xanzy/go-gitlab v0.65.0 h1:9xSA9cRVhz3Z54JacIHdvWnNmNAoSz/BDnyMGOf3yIg=
315+
github.com/xanzy/go-gitlab v0.65.0/go.mod h1:F0QEXwmqiBUxCgJm8fE9S+1veX4XC9Z4cfaAbqwk4YM=
316316
github.com/xanzy/ssh-agent v0.3.0 h1:wUMzuKtKilRgBAD1sUb8gOwwRr2FGoBVumcjoOACClI=
317317
github.com/xanzy/ssh-agent v0.3.0/go.mod h1:3s9xbODqPuuhK9JV1R321M/FlMZSBvE5aY6eAcqrDh0=
318318
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=

internal/provider/resource_gitlab_group.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ var _ = registerResource("gitlab_group", func() *schema.Resource {
141141
Description: "Id of the parent group (creates a nested group).",
142142
Type: schema.TypeInt,
143143
Optional: true,
144-
ForceNew: true,
145144
Default: 0,
146145
},
147146
"runners_token": {
@@ -369,9 +368,39 @@ func resourceGitlabGroupUpdate(ctx context.Context, d *schema.ResourceData, meta
369368
return diag.FromErr(err)
370369
}
371370

371+
if d.HasChange("parent_id") {
372+
err = transferSubGroup(ctx, d, client)
373+
if err != nil {
374+
return diag.FromErr(err)
375+
}
376+
}
377+
372378
return resourceGitlabGroupRead(ctx, d, meta)
373379
}
374380

381+
func transferSubGroup(ctx context.Context, d *schema.ResourceData, client *gitlab.Client) error {
382+
o, n := d.GetChange("parent_id")
383+
parentId, ok := n.(int)
384+
if !ok {
385+
return fmt.Errorf("error converting parent_id %v into an int", n)
386+
}
387+
388+
opt := &gitlab.TransferSubGroupOptions{}
389+
if parentId != 0 {
390+
log.Printf("[DEBUG] transfer gitlab group %s from %v to new parent group %v", d.Id(), o, n)
391+
opt.GroupID = gitlab.Int(parentId)
392+
} else {
393+
log.Printf("[DEBUG] turn gitlab group %s from %v to a new top-level group", d.Id(), o)
394+
}
395+
396+
_, _, err := client.Groups.TransferSubGroup(d.Id(), opt, gitlab.WithContext(ctx))
397+
if err != nil {
398+
return fmt.Errorf("error transfering group %s to new parent group %v: %s", d.Id(), parentId, err)
399+
}
400+
401+
return nil
402+
}
403+
375404
func resourceGitlabGroupDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
376405
client := meta.(*gitlab.Client)
377406
log.Printf("[DEBUG] Delete gitlab group %s", d.Id())

internal/provider/resource_gitlab_group_test.go

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@ func TestAccGitlabGroup_nested(t *testing.T) {
134134
var group gitlab.Group
135135
var group2 gitlab.Group
136136
var nestedGroup gitlab.Group
137+
var lastGid int
138+
testGidNotChanged := func(s *terraform.State) error {
139+
if lastGid == 0 {
140+
lastGid = nestedGroup.ID
141+
}
142+
if lastGid != nestedGroup.ID {
143+
return fmt.Errorf("group id changed")
144+
}
145+
return nil
146+
}
137147
rInt := acctest.RandInt()
138148

139149
resource.Test(t, resource.TestCase{
@@ -158,6 +168,7 @@ func TestAccGitlabGroup_nested(t *testing.T) {
158168
DefaultBranchProtection: 2, // default value
159169
Parent: &group,
160170
}),
171+
testGidNotChanged,
161172
),
162173
},
163174
{
@@ -178,6 +189,7 @@ func TestAccGitlabGroup_nested(t *testing.T) {
178189
DefaultBranchProtection: 2, // default value
179190
Parent: &group2,
180191
}),
192+
testGidNotChanged,
181193
),
182194
},
183195
{
@@ -197,29 +209,30 @@ func TestAccGitlabGroup_nested(t *testing.T) {
197209
TwoFactorGracePeriod: 48, // default value
198210
DefaultBranchProtection: 2, // default value
199211
}),
212+
testGidNotChanged,
213+
),
214+
},
215+
{
216+
Config: testAccGitlabNestedGroupConfig(rInt),
217+
Check: resource.ComposeTestCheckFunc(
218+
testAccCheckGitlabGroupExists("gitlab_group.foo", &group),
219+
testAccCheckGitlabGroupExists("gitlab_group.foo2", &group2),
220+
testAccCheckGitlabGroupExists("gitlab_group.nested_foo", &nestedGroup),
221+
testAccCheckGitlabGroupAttributes(&nestedGroup, &testAccGitlabGroupExpectedAttributes{
222+
Name: fmt.Sprintf("nfoo-name-%d", rInt),
223+
Path: fmt.Sprintf("nfoo-path-%d", rInt),
224+
Description: "Terraform acceptance tests",
225+
LFSEnabled: true,
226+
Visibility: "public", // default value
227+
ProjectCreationLevel: "maintainer", // default value
228+
SubGroupCreationLevel: "owner", // default value
229+
TwoFactorGracePeriod: 48, // default value
230+
DefaultBranchProtection: 2, // default value
231+
Parent: &group,
232+
}),
233+
testGidNotChanged,
200234
),
201235
},
202-
// TODO In EE version, re-creating on the same path where a previous group was soft-deleted doesn't work.
203-
// {
204-
// Config: testAccGitlabNestedGroupConfig(rInt),
205-
// Check: resource.ComposeTestCheckFunc(
206-
// testAccCheckGitlabGroupExists("gitlab_group.foo", &group),
207-
// testAccCheckGitlabGroupExists("gitlab_group.foo2", &group2),
208-
// testAccCheckGitlabGroupExists("gitlab_group.nested_foo", &nestedGroup),
209-
// testAccCheckGitlabGroupAttributes(&nestedGroup, &testAccGitlabGroupExpectedAttributes{
210-
// Name: fmt.Sprintf("nfoo-name-%d", rInt),
211-
// Path: fmt.Sprintf("nfoo-path-%d", rInt),
212-
// Description: "Terraform acceptance tests",
213-
// LFSEnabled: true,
214-
// Visibility: "public", // default value
215-
// ProjectCreationLevel: "maintainer", // default value
216-
// SubGroupCreationLevel: "owner", // default value
217-
// TwoFactorGracePeriod: 48, // default value
218-
// DefaultBranchProtection: 2, // default value
219-
// Parent: &group,
220-
// }),
221-
// ),
222-
// },
223236
},
224237
})
225238
}

0 commit comments

Comments
 (0)