Skip to content

Commit a0e9ec1

Browse files
committed
feat: support deletion of related resources when an organization is deleted
1 parent 59981c6 commit a0e9ec1

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

pkg/service/metric.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import (
1717
pipelinePB "github.com/instill-ai/protogen-go/vdp/pipeline/v1beta"
1818
)
1919

20-
func InjectOwnerToContext(ctx context.Context, owner *mgmtPB.User) context.Context {
20+
func InjectOwnerToContext(ctx context.Context, uid string) context.Context {
2121
ctx = metadata.AppendToOutgoingContext(ctx, "instill-auth-type", "user")
22-
ctx = metadata.AppendToOutgoingContext(ctx, "instill-user-uid", owner.GetUid())
22+
ctx = metadata.AppendToOutgoingContext(ctx, "instill-user-uid", uid)
2323
return ctx
2424
}
2525

@@ -121,7 +121,7 @@ func (s *service) checkConnectorOwnership(ctx context.Context, filter filtering.
121121

122122
func (s *service) pipelineUIDLookup(ctx context.Context, ownerID string, ownerType string, filter filtering.Filter, owner *mgmtPB.User) (filtering.Filter, error) {
123123

124-
ctx = InjectOwnerToContext(ctx, owner)
124+
ctx = InjectOwnerToContext(ctx, *owner.Uid)
125125

126126
// lookup pipeline uid
127127
if len(filter.CheckedExpr.GetExpr().GetCallExpr().GetArgs()) > 0 {
@@ -173,7 +173,7 @@ func (s *service) pipelineUIDLookup(ctx context.Context, ownerID string, ownerTy
173173

174174
func (s *service) connectorUIDLookup(ctx context.Context, filter filtering.Filter, owner *mgmtPB.User) (filtering.Filter, error) {
175175

176-
ctx = InjectOwnerToContext(ctx, owner)
176+
ctx = InjectOwnerToContext(ctx, *owner.Uid)
177177

178178
// lookup connector uid
179179
if len(filter.CheckedExpr.GetExpr().GetCallExpr().GetArgs()) > 0 {

pkg/service/service.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,58 @@ func (s *service) DeleteOrganization(ctx context.Context, ctxUserUID uuid.UUID,
410410
return err
411411
}
412412

413+
// TODO: optimize this
414+
pageToken := ""
415+
pipelineIDList := []string{}
416+
for {
417+
resp, _ := s.pipelinePublicServiceClient.ListOrganizationPipelines(InjectOwnerToContext(ctx, ctxUserUID.String()),
418+
&pipelinePB.ListOrganizationPipelinesRequest{
419+
Parent: fmt.Sprintf("organizations/%s", id),
420+
PageToken: &pageToken})
421+
for _, connector := range resp.Pipelines {
422+
pipelineIDList = append(pipelineIDList, connector.Id)
423+
}
424+
pageToken = resp.NextPageToken
425+
if pageToken == "" {
426+
break
427+
}
428+
}
429+
430+
pageToken = ""
431+
connectorIDList := []string{}
432+
for {
433+
resp, _ := s.pipelinePublicServiceClient.ListOrganizationConnectors(InjectOwnerToContext(ctx, ctxUserUID.String()),
434+
&pipelinePB.ListOrganizationConnectorsRequest{
435+
Parent: fmt.Sprintf("organizations/%s", id),
436+
PageToken: &pageToken})
437+
for _, connector := range resp.Connectors {
438+
connectorIDList = append(connectorIDList, connector.Id)
439+
}
440+
pageToken = resp.NextPageToken
441+
if pageToken == "" {
442+
break
443+
}
444+
}
445+
446+
for _, connectorID := range connectorIDList {
447+
_, _ = s.pipelinePublicServiceClient.DeleteOrganizationConnector(InjectOwnerToContext(ctx, ctxUserUID.String()),
448+
&pipelinePB.DeleteOrganizationConnectorRequest{
449+
Name: fmt.Sprintf("organizations/%s/connectors/%s", id, connectorID),
450+
})
451+
}
452+
for _, pipelineID := range pipelineIDList {
453+
_, _ = s.pipelinePublicServiceClient.DeleteOrganizationPipeline(InjectOwnerToContext(ctx, ctxUserUID.String()),
454+
&pipelinePB.DeleteOrganizationPipelineRequest{
455+
Name: fmt.Sprintf("organizations/%s/pipelines/%s", id, pipelineID),
456+
})
457+
}
458+
459+
memberships, _ := s.aclClient.GetOrganizationUsers(org.UID)
460+
461+
for _, membership := range memberships {
462+
_ = s.aclClient.DeleteOrganizationUserMembership(org.UID, membership.UID)
463+
}
464+
413465
err = s.repository.DeleteOrganization(ctx, id)
414466
if err != nil {
415467
return fmt.Errorf("organizations/%s: %w", id, err)

0 commit comments

Comments
 (0)