Skip to content

Commit

Permalink
Merge pull request #22752 from hashicorp/b/api-management-pull-from-api
Browse files Browse the repository at this point in the history
`apimanagement`: pulling the `location` field from the API rather than the local config during deletion
  • Loading branch information
tombuildsstuff authored Aug 1, 2023
2 parents 22c6d8c + 25645a1 commit a59ea0d
Showing 1 changed file with 12 additions and 10 deletions.
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

0 comments on commit a59ea0d

Please sign in to comment.