Skip to content

Commit

Permalink
changing the parent_id will move group and not recreate it
Browse files Browse the repository at this point in the history
this will fix #574
  • Loading branch information
gro-gg committed May 10, 2022
1 parent e761bae commit 5387a58
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ module github.com/gitlabhq/terraform-provider-gitlab

go 1.16

// we are waiting for a new release
replace github.com/xanzy/go-gitlab => ../go-gitlab

require (
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/go-retryablehttp v0.7.1
Expand Down
32 changes: 31 additions & 1 deletion internal/provider/resource_gitlab_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ var _ = registerResource("gitlab_group", func() *schema.Resource {
Description: "Id of the parent group (creates a nested group).",
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
Default: 0,
},
"runners_token": {
Expand Down Expand Up @@ -369,9 +368,40 @@ func resourceGitlabGroupUpdate(ctx context.Context, d *schema.ResourceData, meta
return diag.FromErr(err)
}

if d.HasChange("parent_id") {
diagnostic := transferSubGroup(d, client)
if diagnostic.HasError() {
return diagnostic
}
}

return resourceGitlabGroupRead(ctx, d, meta)
}

func transferSubGroup(d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*gitlab.Client)
o, n := d.GetChange("parent_id")
parentId, ok := n.(int)
if !ok {
return diag.Errorf("error converting parent_id %v into an int", n)
}

opt := &gitlab.TransferSubGroupOptions{}
if parentId != 0 {
log.Printf("[DEBUG] transfer gitlab group %s from %v to new parent group %v", d.Id(), o, n)
opt.GroupID = gitlab.Int(parentId)
} else {
log.Printf("[DEBUG] turn gitlab group %s from %v to a new top-level group", d.Id(), o)
}

_, _, err := client.Groups.TransferSubGroup(d.Id(), opt)
if err != nil {
return diag.Errorf("error transfering group %s to new parent group %v: %s", d.Id(), parentId, err)
}

return nil
}

func resourceGitlabGroupDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*gitlab.Client)
log.Printf("[DEBUG] Delete gitlab group %s", d.Id())
Expand Down
13 changes: 13 additions & 0 deletions internal/provider/resource_gitlab_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ func TestAccGitlabGroup_nested(t *testing.T) {
TwoFactorGracePeriod: 48, // default value
DefaultBranchProtection: 2, // default value
Parent: &group,
AutoDevopsEnabled: true,
}),
),
},
Expand All @@ -177,6 +178,7 @@ func TestAccGitlabGroup_nested(t *testing.T) {
TwoFactorGracePeriod: 48, // default value
DefaultBranchProtection: 2, // default value
Parent: &group2,
AutoDevopsEnabled: true,
}),
),
},
Expand Down Expand Up @@ -504,6 +506,12 @@ resource "gitlab_group" "nested_foo" {
# So that acceptance tests can be run in a gitlab organization
# with no billing
visibility_level = "public"
# We set this to non-default value to later check if the group was recreated or not
auto_devops_enabled = true
lifecycle {
ignore_changes = [auto_devops_enabled]
}
}
`, rInt, rInt, rInt, rInt, rInt, rInt)
}
Expand Down Expand Up @@ -569,6 +577,11 @@ resource "gitlab_group" "nested_foo" {
# So that acceptance tests can be run in a gitlab organization
# with no billing
visibility_level = "public"
# We need to read this value to check if the group was recreated or not
lifecycle {
ignore_changes = [auto_devops_enabled]
}
}
`, rInt, rInt, rInt, rInt, rInt, rInt)
}
Expand Down

0 comments on commit 5387a58

Please sign in to comment.