Skip to content

Commit

Permalink
fix: r/vsphere_vm_storage_policy delete method check for use (#1863)
Browse files Browse the repository at this point in the history
Modify the `resourceVMStoragePolicyDelete` method to check the response of `pbmClient.DeleteProfile()` API call for errors and re-throw them. As a result, if a policy is in use and cannot be deleted, the destroy operation will fail and the policy will remain in the state.
  • Loading branch information
stoyan-hristov authored Mar 28, 2023
1 parent dbf4343 commit 47333a9
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions vsphere/resource_vsphere_vm_storage_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func resourceVMStoragePolicyUpdate(d *schema.ResourceData, meta interface{}) err
}

func resourceVMStoragePolicyDelete(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Performing create of VM storage policy with ID %s", d.Id())
log.Printf("[DEBUG] Performing delete of VM storage policy with ID %s", d.Id())
client := meta.(*Client).vimClient
pbmClient, err := pbm.NewClient(context.Background(), client.Client)
if err != nil {
Expand All @@ -286,10 +286,15 @@ func resourceVMStoragePolicyDelete(d *schema.ResourceData, meta interface{}) err
UniqueId: d.Id(),
})

_, err = pbmClient.DeleteProfile(context.Background(), policyIdsToDelete)
var deleteProfileOutcome []types2.PbmProfileOperationOutcome
deleteProfileOutcome, err = pbmClient.DeleteProfile(context.Background(), policyIdsToDelete)
if err != nil {
return fmt.Errorf("error while deleting policy with ID %s %s", d.Id(), err)
}
if len(deleteProfileOutcome) > 0 && deleteProfileOutcome[0].Fault != nil {
return fmt.Errorf("error while deleting policy with ID %s %s",
d.Id(), deleteProfileOutcome[0].Fault.LocalizedMessage)
}
d.SetId("")
log.Printf("[DEBUG] %s: Delete complete", d.Id())
return nil
Expand Down

0 comments on commit 47333a9

Please sign in to comment.