Skip to content

Commit

Permalink
frontend: Add DeleteAllResources method
Browse files Browse the repository at this point in the history
Called when a subscription is deleted. The method is idempotent in
case of multiple subscription PUT requests.
  • Loading branch information
Matthew Barnes committed Dec 3, 2024
1 parent 9cebd53 commit 353d9e9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
9 changes: 9 additions & 0 deletions frontend/pkg/frontend/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,15 @@ func (f *Frontend) ArmSubscriptionPut(writer http.ResponseWriter, request *http.
"state": string(subscription.State),
})

// Clean up resources if subscription is deleted.
if subscription.State == arm.SubscriptionStateDeleted {
cloudError := f.DeleteAllResources(ctx, subscriptionID)
if cloudError != nil {
arm.WriteCloudError(writer, cloudError)
return
}
}

_, err = arm.WriteJSONResponse(writer, http.StatusOK, subscription)
if err != nil {
f.logger.Error(err.Error())
Expand Down
37 changes: 37 additions & 0 deletions frontend/pkg/frontend/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,43 @@ func (f *Frontend) CheckForProvisioningStateConflict(ctx context.Context, operat
return nil
}

func (f *Frontend) DeleteAllResources(ctx context.Context, subscriptionID string) *arm.CloudError {
prefix, err := arm.ParseResourceID("/subscriptions/" + subscriptionID)
if err != nil {
f.logger.Error(err.Error())
return arm.NewInternalServerError()
}

dbIterator := f.dbClient.ListResourceDocs(ctx, prefix, -1, nil)

// Start a deletion operation for all clusters under the subscription.
// Cluster Service will delete all node pools belonging to these clusters
// so we don't need to explicitly delete node pools here.
for item := range dbIterator.Items(ctx) {
var resourceDoc *database.ResourceDocument

err = json.Unmarshal(item, &resourceDoc)
if err != nil {
f.logger.Error(err.Error())
return arm.NewInternalServerError()
}

if !strings.EqualFold(resourceDoc.Key.ResourceType.String(), api.ClusterResourceType.String()) {
continue
}

// Allow this method to be idempotent.
if resourceDoc.ProvisioningState != arm.ProvisioningStateDeleting {
_, cloudError := f.DeleteResource(ctx, resourceDoc)
if cloudError != nil {
return cloudError
}
}
}

return nil
}

func (f *Frontend) DeleteResource(ctx context.Context, resourceDoc *database.ResourceDocument) (string, *arm.CloudError) {
const operationRequest = database.OperationRequestDelete
var err error
Expand Down

0 comments on commit 353d9e9

Please sign in to comment.