Skip to content

Commit

Permalink
Don't delete GitLab top-level groups with attribute 'permanently_remo…
Browse files Browse the repository at this point in the history
…ve=true'

Signed-off-by: Benjamin Bühlmann <[email protected]>
  • Loading branch information
buehlmann committed Dec 3, 2024
1 parent 5d26238 commit ea72ce8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
4 changes: 3 additions & 1 deletion pkg/controller/groups/groups/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,9 @@ func (e *external) Delete(ctx context.Context, mg resource.Managed) (managed.Ext
return managed.ExternalDelete{}, errors.Wrap(err, errDeleteFailed)
}

if cr.Spec.ForProvider.PermanentlyRemove != nil && *cr.Spec.ForProvider.PermanentlyRemove {
// permanent deletion is only available on subgroups; when executed against top-level groups the backend will return an error
isSubGroup := cr.Status.AtProvider.FullPath != nil && *cr.Status.AtProvider.FullPath != cr.Spec.ForProvider.Path
if cr.Spec.ForProvider.PermanentlyRemove != nil && *cr.Spec.ForProvider.PermanentlyRemove && isSubGroup {
_, err = e.client.DeleteGroup(meta.GetExternalName(cr), &gitlab.DeleteGroupOptions{
PermanentlyRemove: cr.Spec.ForProvider.PermanentlyRemove,
FullPath: cr.Spec.ForProvider.FullPathToRemove,
Expand Down
37 changes: 34 additions & 3 deletions pkg/controller/groups/groups/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -918,16 +918,47 @@ func TestDelete(t *testing.T) {
cr: group(
withExternalName("0"),
withPermanentlyRemove(gitlab.Ptr(true)),
withFullPathToRemove(gitlab.Ptr("path/to/remove"))),
withPath("group"),
withFullPathToRemove(gitlab.Ptr("path/to/group")),
withStatus(v1alpha1.GroupObservation{FullPath: gitlab.Ptr("path/to/group")})),
},
want: want{
cr: group(
withExternalName("0"),
withPermanentlyRemove(gitlab.Ptr(true)),
withFullPathToRemove(gitlab.Ptr("path/to/remove"))),
withPath("group"),
withFullPathToRemove(gitlab.Ptr("path/to/group")),
withStatus(v1alpha1.GroupObservation{FullPath: gitlab.Ptr("path/to/group")})),
calls: []deleteGroupCalls{
{Pid: "0", Opt: &gitlab.DeleteGroupOptions{}},
{Pid: "0", Opt: &gitlab.DeleteGroupOptions{PermanentlyRemove: gitlab.Ptr(true), FullPath: gitlab.Ptr("path/to/group")}},
},
err: nil,
},
},
"SuccessfulPermanentlyTopLevelGroupDeletion": {
args: args{
group: &fake.MockClient{
MockDeleteGroup: func(pid interface{}, opt *gitlab.DeleteGroupOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error) {
recordedCalls = append(recordedCalls, deleteGroupCalls{Pid: pid, Opt: opt})
return &gitlab.Response{}, nil
},
},
cr: group(
withExternalName("0"),
withPermanentlyRemove(gitlab.Ptr(true)),
withPath("top-level-group"),
withStatus(v1alpha1.GroupObservation{FullPath: gitlab.Ptr("top-level-group")})),
},
want: want{
cr: group(
withExternalName("0"),
withPermanentlyRemove(gitlab.Ptr(true)),
withPath("top-level-group"),
withStatus(v1alpha1.GroupObservation{FullPath: gitlab.Ptr("top-level-group")}),
),
calls: []deleteGroupCalls{
{Pid: "0", Opt: &gitlab.DeleteGroupOptions{}},
{Pid: "0", Opt: &gitlab.DeleteGroupOptions{PermanentlyRemove: gitlab.Ptr(true), FullPath: gitlab.Ptr("path/to/remove")}},
},
err: nil,
},
Expand Down

0 comments on commit ea72ce8

Please sign in to comment.