From 47333a95f7ffc1643283fd5e3a97d57b06b57823 Mon Sep 17 00:00:00 2001 From: Stoyan Hristov <74260049+stoyan-hristov@users.noreply.github.com> Date: Tue, 28 Mar 2023 23:20:24 +0300 Subject: [PATCH] fix: `r/vsphere_vm_storage_policy` delete method check for use (#1863) 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. --- vsphere/resource_vsphere_vm_storage_policy.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/vsphere/resource_vsphere_vm_storage_policy.go b/vsphere/resource_vsphere_vm_storage_policy.go index d6a25263a..25d8f9625 100644 --- a/vsphere/resource_vsphere_vm_storage_policy.go +++ b/vsphere/resource_vsphere_vm_storage_policy.go @@ -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 { @@ -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