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

fix: r/vsphere_vm_storage_policy delete method check for use #1863

Merged
merged 1 commit into from
Mar 28, 2023

Conversation

stoyan-hristov
Copy link
Contributor

@stoyan-hristov stoyan-hristov commented Mar 22, 2023

Description

Modify the resourceVMStoragePolicyDelete method to check the response of pbmClient.DeleteProfile() API call for errors and re-throw them. As a result of this if a policy is in use and cannot be deleted, the destroy operation will fail and the policy will remain in the state.

Testing done:

  1. Created a policy using terraform
  2. Assigned the policy to a VM using the vSphere UI
  3. Tried to delete the policy using terraform and verified that the destroy operation failed with resource in use error:

Plan: 0 to add, 0 to change, 1 to destroy.

Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.

Enter a value: yes

vsphere_vm_storage_policy.test-policy-delete2: Destroying... [id=9a88ca5d-de6e-4c55-9ac4-03503c03e9ca]

│ Error: error while deleting policy with ID 9a88ca5d-de6e-4c55-9ac4-03503c03e9ca Profile (pbm.profile.ProfileId) {
│ dynamicType = null,
│ dynamicProperty = null,
│ uniqueId = 9a88ca5d-de6e-4c55-9ac4-03503c03e9ca
│ } is still associated with 2 entities.

Acceptance tests

  • Have you added an acceptance test for the functionality being added?
  • Have you run the acceptance tests on this branch?

Output from acceptance testing:

make testacc TESTARGS="-run='TestAccResourceVMStoragePolicy'"
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test $(go list ./... |grep -v 'vendor') -v -run='TestAccResourceVMStoragePolicy' -timeout 240m
? github.com/hashicorp/terraform-provider-vsphere [no test files]
=== RUN TestAccResourceVMStoragePolicy_basic
--- PASS: TestAccResourceVMStoragePolicy_basic (84.52s)
PASS

Release Note

Release note for CHANGELOG:
Bug fix:

r/vsphere_vm_storage_policy not deleted after terraform destroy #1428

References

Closes #1428

…ashicorp#1428

Modify the resourceVMStoragePolicyDelete method to check the response of pbmClient.DeleteProfile() API call for errors and re-throw them. As a result of this if a policy is in use and cannot be deleted, the destroy operation will fail and the policy will remain in the state.
@stoyan-hristov stoyan-hristov requested a review from a team as a code owner March 22, 2023 12:19
@github-actions github-actions bot added provider Type: Provider size/xs Relative Sizing: Extra-Small labels Mar 22, 2023
@tenthirtyam tenthirtyam changed the title fix: r/vsphere_vm_storage_policy not deleted after terraform destroy #1428 fix: r/vsphere_vm_storage_policy not deleted Mar 24, 2023
@tenthirtyam tenthirtyam added bug Type: Bug area/vm Area: Virtual Machines labels Mar 24, 2023
@tenthirtyam tenthirtyam added this to the v2.4.0 milestone Mar 24, 2023
Copy link
Collaborator

@tenthirtyam tenthirtyam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, @stoyan-hristov.

Reviewed, successfully tested, and approved. 🚀

Summary of Test Results

terraform destroy --auto-approve
data.vsphere_tag_category.policy_category: Reading...
data.vsphere_tag_category.policy_category: Read complete after 0s [id=urn:vmomi:InventoryServiceCategory:1fc04fac-74ad-46bf-972b-3aad24db8a66:GLOBAL]
data.vsphere_tag.policy_tag_include: Reading...
data.vsphere_tag.policy_tag_include: Read complete after 0s [id=urn:vmomi:InventoryServiceTag:72f80d4c-d33c-4993-a831-cc89290400d7:GLOBAL]
vsphere_vm_storage_policy.policy_tag_based_placement: Refreshing state... [id=e6bdc52e-ecaa-4a31-b813-a75d108e3d6f]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  - destroy

Terraform will perform the following actions:

  # vsphere_vm_storage_policy.policy_tag_based_placement will be destroyed
  - resource "vsphere_vm_storage_policy" "policy_tag_based_placement" {
      - description = "This storage policy is managed by Terraform." -> null
      - id          = "e6bdc52e-ecaa-4a31-b813-a75d108e3d6f" -> null
      - name        = "foo-bar" -> null

      - tag_rules {
          - include_datastores_with_tags = true -> null
          - tag_category                 = "foo" -> null
          - tags                         = [
              - "bar",
            ] -> null
        }
    }

Plan: 0 to add, 0 to change, 1 to destroy.
vsphere_vm_storage_policy.policy_tag_based_placement: Destroying... [id=e6bdc52e-ecaa-4a31-b813-a75d108e3d6f]

│ Error: error while deleting policy with ID e6bdc52e-ecaa-4a31-b813-a75d108e3d6f Profile (pbm.profile.ProfileId) {
│    dynamicType = null,
│    dynamicProperty = null,
│    uniqueId = e6bdc52e-ecaa-4a31-b813-a75d108e3d6f
│ } is still associated with 2 entities.

@tenthirtyam tenthirtyam added the area/storage Area: Storage label Mar 28, 2023
@tenthirtyam tenthirtyam changed the title fix: r/vsphere_vm_storage_policy not deleted fix: r/vsphere_vm_storage_policy delete method check for use Mar 28, 2023
@tenthirtyam tenthirtyam merged commit 47333a9 into hashicorp:main Mar 28, 2023
tenthirtyam pushed a commit that referenced this pull request Mar 28, 2023
Update `CHANGELOG.md` to include the bug fix in #1863 for #1428.

Signed-off-by: Ryan Johnson <johnsonryan@vmware.com>
@github-actions
Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area/storage Area: Storage area/vm Area: Virtual Machines bug Type: Bug provider Type: Provider size/xs Relative Sizing: Extra-Small
Projects
None yet
Development

Successfully merging this pull request may close these issues.

vsphere_vm_storage_policy not deleted after terraform destroy
2 participants