Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apimanagement: pulling the location field from the API rather than the local config during deletion #22752

Merged
merged 2 commits into from
Aug 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions internal/services/apimanagement/api_management_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

"github.com/Azure/azure-sdk-for-go/services/apimanagement/mgmt/2021-08-01/apimanagement" // nolint: staticcheck
"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/identity"
Expand Down Expand Up @@ -1170,34 +1169,37 @@ func resourceApiManagementServiceDelete(d *pluginsdk.ResourceData, meta interfac
return err
}

existing, err := client.Get(ctx, id.ResourceGroup, id.ServiceName)
if err != nil {
return fmt.Errorf("retrieving %s: %+v", *id, err)
}
location := location.NormalizeNilable(existing.Location)

log.Printf("[DEBUG] Deleting %s", *id)
future, err := client.Delete(ctx, id.ResourceGroup, id.ServiceName)
if err != nil {
return fmt.Errorf("deleting %s: %+v", *id, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
if !response.WasNotFound(future.Response()) {
return fmt.Errorf("waiting for deletion of %s: %+v", *id, err)
}
return fmt.Errorf("waiting for deletion of %s: %+v", *id, err)
}

// Purge the soft deleted Api Management permanently if the feature flag is enabled
if meta.(*clients.Client).Features.ApiManagement.PurgeSoftDeleteOnDestroy {
log.Printf("[DEBUG] %s marked for purge - executing purge", *id)
_, err := deletedServicesClient.GetByName(ctx, id.ServiceName, azure.NormalizeLocation(d.Get("location").(string)))
if err != nil {
return err
if _, err := deletedServicesClient.GetByName(ctx, id.ServiceName, location); err != nil {
return fmt.Errorf("retrieving the deleted %s to be able to purge it: %+v", *id, err)
}
future, err := deletedServicesClient.Purge(ctx, id.ServiceName, azure.NormalizeLocation(d.Get("location").(string)))
future, err := deletedServicesClient.Purge(ctx, id.ServiceName, location)
if err != nil {
return err
return fmt.Errorf("purging the deleted %s: %+v", *id, err)
}

log.Printf("[DEBUG] Waiting for purge of %s..", *id)
err = future.WaitForCompletionRef(ctx, deletedServicesClient.Client)
if err != nil {
return fmt.Errorf("purging %s: %+v", *id, err)
return fmt.Errorf("waiting for the purge of deleted %s: %+v", *id, err)
}
log.Printf("[DEBUG] Purged %s.", *id)
return nil
Expand Down