Skip to content

Commit 713df8f

Browse files
committed
changing the parent_id will move group and not recreate it
this will fix #574
1 parent 89b7cd6 commit 713df8f

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ module github.com/gitlabhq/terraform-provider-gitlab
22

33
go 1.17
44

5+
// we are waiting for a new release
6+
replace github.com/xanzy/go-gitlab => ../go-gitlab
7+
58
require (
69
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
710
github.com/hashicorp/go-retryablehttp v0.7.1

internal/provider/resource_gitlab_group.go

Lines changed: 31 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,40 @@ func resourceGitlabGroupUpdate(ctx context.Context, d *schema.ResourceData, meta
369368
return diag.FromErr(err)
370369
}
371370

371+
if d.HasChange("parent_id") {
372+
diagnostic := transferSubGroup(d, client)
373+
if diagnostic.HasError() {
374+
return diagnostic
375+
}
376+
}
377+
372378
return resourceGitlabGroupRead(ctx, d, meta)
373379
}
374380

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

internal/provider/resource_gitlab_group_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ func TestAccGitlabGroup_nested(t *testing.T) {
157157
TwoFactorGracePeriod: 48, // default value
158158
DefaultBranchProtection: 2, // default value
159159
Parent: &group,
160+
AutoDevopsEnabled: true,
160161
}),
161162
),
162163
},
@@ -177,6 +178,7 @@ func TestAccGitlabGroup_nested(t *testing.T) {
177178
TwoFactorGracePeriod: 48, // default value
178179
DefaultBranchProtection: 2, // default value
179180
Parent: &group2,
181+
AutoDevopsEnabled: true,
180182
}),
181183
),
182184
},
@@ -502,6 +504,12 @@ resource "gitlab_group" "nested_foo" {
502504
# So that acceptance tests can be run in a gitlab organization
503505
# with no billing
504506
visibility_level = "public"
507+
508+
# We set this to a non-default value to check later whether the group was recreated or not
509+
auto_devops_enabled = true
510+
lifecycle {
511+
ignore_changes = [auto_devops_enabled]
512+
}
505513
}
506514
`, rInt, rInt, rInt, rInt, rInt, rInt)
507515
}
@@ -567,6 +575,11 @@ resource "gitlab_group" "nested_foo" {
567575
# So that acceptance tests can be run in a gitlab organization
568576
# with no billing
569577
visibility_level = "public"
578+
579+
# We need to read this value to check if the group is recreated or not
580+
lifecycle {
581+
ignore_changes = [auto_devops_enabled]
582+
}
570583
}
571584
`, rInt, rInt, rInt, rInt, rInt, rInt)
572585
}

0 commit comments

Comments
 (0)