Skip to content

Commit

Permalink
feat: delete related resources when an organization is deleted (#180)
Browse files Browse the repository at this point in the history
Because

- When deleting an organization, it is required to automatically remove
all associated resources.

This commit

- Deletes related resources when an organization is deleted.
  • Loading branch information
donch1989 authored Feb 14, 2024
1 parent 59981c6 commit 313d942
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
8 changes: 4 additions & 4 deletions pkg/service/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import (
pipelinePB "github.com/instill-ai/protogen-go/vdp/pipeline/v1beta"
)

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

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

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

ctx = InjectOwnerToContext(ctx, owner)
ctx = InjectOwnerToContext(ctx, *owner.Uid)

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

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

ctx = InjectOwnerToContext(ctx, owner)
ctx = InjectOwnerToContext(ctx, *owner.Uid)

// lookup connector uid
if len(filter.CheckedExpr.GetExpr().GetCallExpr().GetArgs()) > 0 {
Expand Down
52 changes: 52 additions & 0 deletions pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,58 @@ func (s *service) DeleteOrganization(ctx context.Context, ctxUserUID uuid.UUID,
return err
}

// TODO: optimize this
pageToken := ""
pipelineIDList := []string{}
for {
resp, _ := s.pipelinePublicServiceClient.ListOrganizationPipelines(InjectOwnerToContext(ctx, ctxUserUID.String()),
&pipelinePB.ListOrganizationPipelinesRequest{
Parent: fmt.Sprintf("organizations/%s", id),
PageToken: &pageToken})
for _, connector := range resp.Pipelines {
pipelineIDList = append(pipelineIDList, connector.Id)
}
pageToken = resp.NextPageToken
if pageToken == "" {
break
}
}

pageToken = ""
connectorIDList := []string{}
for {
resp, _ := s.pipelinePublicServiceClient.ListOrganizationConnectors(InjectOwnerToContext(ctx, ctxUserUID.String()),
&pipelinePB.ListOrganizationConnectorsRequest{
Parent: fmt.Sprintf("organizations/%s", id),
PageToken: &pageToken})
for _, connector := range resp.Connectors {
connectorIDList = append(connectorIDList, connector.Id)
}
pageToken = resp.NextPageToken
if pageToken == "" {
break
}
}

for _, connectorID := range connectorIDList {
_, _ = s.pipelinePublicServiceClient.DeleteOrganizationConnector(InjectOwnerToContext(ctx, ctxUserUID.String()),
&pipelinePB.DeleteOrganizationConnectorRequest{
Name: fmt.Sprintf("organizations/%s/connectors/%s", id, connectorID),
})
}
for _, pipelineID := range pipelineIDList {
_, _ = s.pipelinePublicServiceClient.DeleteOrganizationPipeline(InjectOwnerToContext(ctx, ctxUserUID.String()),
&pipelinePB.DeleteOrganizationPipelineRequest{
Name: fmt.Sprintf("organizations/%s/pipelines/%s", id, pipelineID),
})
}

memberships, _ := s.aclClient.GetOrganizationUsers(org.UID)

for _, membership := range memberships {
_ = s.aclClient.DeleteOrganizationUserMembership(org.UID, membership.UID)
}

err = s.repository.DeleteOrganization(ctx, id)
if err != nil {
return fmt.Errorf("organizations/%s: %w", id, err)
Expand Down

0 comments on commit 313d942

Please sign in to comment.