vmware / terraform-provider-nsxt

Terraform Provider for VMware NSX
https://registry.terraform.io/providers/vmware/nsxt/
Mozilla Public License 2.0
133 stars 85 forks source link

nsxt_policy_vm_tags - Fails to lookup VMID to remove tags after VM has been removed #709

Closed kalenarndt closed 2 years ago

kalenarndt commented 2 years ago

Describe the bug

If a VM has been removed from a Computer Manager that had tags previously assigned then it causes the run to fail because the lookup of the VMID errors. You either need to target destroy the resource or modify the state in order to continue to execute the run normally.

Here is the error that you receive:

Error: Error during Virtual Machine retrieval: Could not find Virtual Machine with ID: 50250046-fccc-9449-b149-8aed149fa7f7 with module.nsxt-cts-dfw.nsxt_policy_vm_tags.tags["web03"] on . terraform/modules/nsxt-cts-dfw/main.tf line 32, in resource "nsxt_policy. vm_tags" "tags":

Reproduction steps

1. Deploy a VM in the NSX-T environment
2. Lookup VM via name with the nsxt_policy_vm data resource

data "nsxt_policy_vm" "check" {
  display_name = "yourvmname"
}

3. Assign a tag and scope to the VM with nsxt_policy_vm_tags resource
resource "nsxt_policy_vm_tags" "tags" {
  instance_id = data.nsxt_policy_vm.check.id
  tag {
    tag   = "web"
    scope = "prod"
  }
}

4. Delete VM or remove from inventory and wait a few minutes for the object to no longer be present in inventory

5. Attempt to either change the tag values (eg. scope = "dev") or just do a terraform refresh

6. Run will fail since the object cannot be looked up.

Expected behavior

If the VM has been removed from the inventory of the Compute Managers then we shouldn't attempt to unassign the tag if we are unable to locate the object in inventory or the error shouldn't be passed causing the run to fail.

Additional context

No response

annakhm commented 2 years ago

Hi @kalenarndt, the nsxt_policy_vm_tags was fixed not to error out on update/delete if vm is no longer present. However, nsxt_policy_vm data source would still error out if VM not found, since this is the expected behavior. In order to avoid this, you can use new nsxt_policy_vms data source that provides you with all VMs in the inventory, and the flexibility to lookup/change vm id values according to your logic. Hope this helps!