Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

r/virtual_machine: Fix template UUID lookup for older vSphere versions #377

Merged
merged 3 commits into from
Jan 31, 2018

Conversation

vancluever
Copy link
Contributor

@vancluever vancluever commented Jan 29, 2018

Historically, the FindByUuid method under SearchIndex did not support
looking up VMs marked as template. However, at some point in time, this
was changed (possibly vSphere 6.5, based on bug reports), and any
mention of the UUID search not supporting templates was removed from the
API documentation, without a mention of when support for templates was
introduced. Since this is the method that we use to look up both VMs and
templates, this means that templates have not been functional for
vSphere <= 6.0 users since the resource was re-written in 1.0.

There has been a workaround in that the source template is just
converted back to virtual machine, but this update fixes it so that on
versions of vSphere older than 6.5, we fall back to using ContainerView
with a filter on the UUID. This works for all versions of vSphere. We
keep FindByUuid for 6.5 as it may possibly be more direct path.


Fixes #271

@vancluever vancluever added the bug Type: Bug label Jan 29, 2018
Historically, the FindByUuid method under SearchIndex did not support
looking up VMs marked as template. However, at some point in time, this
was changed (possibly vSphere 6.5, based on bug reports), and any
mention of the UUID search not supporting templates was removed from the
API documentation, without a mention of when support for templates was
introduced. Since this is the method that we use to look up both VMs and
templates, this means that templates have not been functional for
vSphere <= 6.0 users since the resource was re-written in 1.0.

There has been a workaround in that the source template is just
converted back to virtual machine, but this update fixes it so that on
versions of vSphere older than 6.5, we fall back to using ContainerView
with a filter on the UUID. This works for all versions of vSphere. We
keep FindByUuid for 6.5 as it may possibly be more direct path.
@vancluever vancluever force-pushed the b-uuid-template-old-vsphere branch from 18c73ab to 0adde0b Compare January 29, 2018 23:54
@vancluever
Copy link
Contributor Author

vancluever commented Jan 30, 2018

Tests pass. We unfortunately can't test the ContainerView path automatically right now as it's a compatibility path and our lab runs vSphere 6.5 (hence why we didn't pick up on this in the first place). However, I have tested it manually against a 5.5 installation.

=== RUN   TestAccResourceVSphereVirtualMachine
=== RUN   TestAccResourceVSphereVirtualMachine/basic
=== RUN   TestAccResourceVSphereVirtualMachine/ESXi-only_test
=== RUN   TestAccResourceVSphereVirtualMachine/shutdown_OK
=== RUN   TestAccResourceVSphereVirtualMachine/re-create_on_deletion
=== RUN   TestAccResourceVSphereVirtualMachine/multi-device
=== RUN   TestAccResourceVSphereVirtualMachine/add_devices
=== RUN   TestAccResourceVSphereVirtualMachine/remove_middle_devices
=== RUN   TestAccResourceVSphereVirtualMachine/remove_middle_devices,_change_disk_unit
=== RUN   TestAccResourceVSphereVirtualMachine/high_disk_unit_numbers
=== RUN   TestAccResourceVSphereVirtualMachine/high_disk_units_to_regular_single_controller
=== RUN   TestAccResourceVSphereVirtualMachine/cdrom
=== RUN   TestAccResourceVSphereVirtualMachine/maximum_number_of_nics
=== RUN   TestAccResourceVSphereVirtualMachine/upgrade_cpu_and_ram
=== RUN   TestAccResourceVSphereVirtualMachine/modify_annotation
=== RUN   TestAccResourceVSphereVirtualMachine/grow_disk
=== RUN   TestAccResourceVSphereVirtualMachine/swap_scsi_bus
=== RUN   TestAccResourceVSphereVirtualMachine/extraconfig
=== RUN   TestAccResourceVSphereVirtualMachine/extraconfig_swap_keys
=== RUN   TestAccResourceVSphereVirtualMachine/attach_existing_vmdk
=== RUN   TestAccResourceVSphereVirtualMachine/in_folder
=== RUN   TestAccResourceVSphereVirtualMachine/move_to_folder
=== RUN   TestAccResourceVSphereVirtualMachine/static_mac
=== RUN   TestAccResourceVSphereVirtualMachine/single_tag
=== RUN   TestAccResourceVSphereVirtualMachine/multiple_tags
=== RUN   TestAccResourceVSphereVirtualMachine/switch_tags
=== RUN   TestAccResourceVSphereVirtualMachine/orphaned_(renamed)_disk_in_place_of_existing
=== RUN   TestAccResourceVSphereVirtualMachine/block_computed_disk_name
=== RUN   TestAccResourceVSphereVirtualMachine/block_vapp_settings_on_non-clones
=== RUN   TestAccResourceVSphereVirtualMachine/block_vapp_settings_on_non-clones_after_creation
=== RUN   TestAccResourceVSphereVirtualMachine/block_disk_label_starting_with_orphaned_prefix
=== RUN   TestAccResourceVSphereVirtualMachine/clone_from_template
=== RUN   TestAccResourceVSphereVirtualMachine/clone,_modify_disk_and_SCSI_type_at_same_time
=== RUN   TestAccResourceVSphereVirtualMachine/clone,_multi-nic_(template_should_have_one)
=== RUN   TestAccResourceVSphereVirtualMachine/clone_with_different_timezone
=== RUN   TestAccResourceVSphereVirtualMachine/clone_with_bad_timezone
=== RUN   TestAccResourceVSphereVirtualMachine/clone_with_bad_eagerly_scrub_with_linked_clone
=== RUN   TestAccResourceVSphereVirtualMachine/clone_with_bad_thin_provisioned_with_linked_clone
=== RUN   TestAccResourceVSphereVirtualMachine/clone_with_bad_size_with_linked_clone
=== RUN   TestAccResourceVSphereVirtualMachine/clone_with_bad_size_without_linked_clone
=== RUN   TestAccResourceVSphereVirtualMachine/clone_-_attempt_to_add_vapp_properties_to_a_VM_that_does_not_support_them_-_create
=== RUN   TestAccResourceVSphereVirtualMachine/clone_-_attempt_to_add_vapp_properties_to_a_VM_that_does_not_support_them_-_update
=== RUN   TestAccResourceVSphereVirtualMachine/clone_with_different_hostname
=== RUN   TestAccResourceVSphereVirtualMachine/clone_with_extra_disks
=== RUN   TestAccResourceVSphereVirtualMachine/clone_with_cdrom
=== RUN   TestAccResourceVSphereVirtualMachine/clone_with_vapp_properties
=== RUN   TestAccResourceVSphereVirtualMachine/bad_vapp_property_-_clone_create
=== RUN   TestAccResourceVSphereVirtualMachine/bad_vapp_property_-_clone_update
=== RUN   TestAccResourceVSphereVirtualMachine/cpu_hot_add
=== RUN   TestAccResourceVSphereVirtualMachine/memory_hot_add
=== RUN   TestAccResourceVSphereVirtualMachine/dual-stack_ipv4_and_ipv6
=== RUN   TestAccResourceVSphereVirtualMachine/ipv6_only
=== RUN   TestAccResourceVSphereVirtualMachine/windows_template,_customization_events_and_proper_IP
=== RUN   TestAccResourceVSphereVirtualMachine/host_vmotion
=== RUN   TestAccResourceVSphereVirtualMachine/resource_pool_vmotion
=== RUN   TestAccResourceVSphereVirtualMachine/storage_vmotion_-_global_setting
=== RUN   TestAccResourceVSphereVirtualMachine/storage_vmotion_-_single_disk
=== RUN   TestAccResourceVSphereVirtualMachine/storage_vmotion_-_pin_datastore
=== RUN   TestAccResourceVSphereVirtualMachine/storage_vmotion_-_renamed_virtual_machine
=== RUN   TestAccResourceVSphereVirtualMachine/storage_vmotion_-_linked_clones
=== RUN   TestAccResourceVSphereVirtualMachine/storage_vmotion_-_block_externally_attached_disks
=== RUN   TestAccResourceVSphereVirtualMachine/single_custom_attribute
=== RUN   TestAccResourceVSphereVirtualMachine/multi_custom_attribute
=== RUN   TestAccResourceVSphereVirtualMachine/switch_custom_attribute
=== RUN   TestAccResourceVSphereVirtualMachine/transition_to_label
=== RUN   TestAccResourceVSphereVirtualMachine/prevent_revert_to_name
=== RUN   TestAccResourceVSphereVirtualMachine/transition_to_label_-_attached_disk
=== RUN   TestAccResourceVSphereVirtualMachine/import
=== RUN   TestAccResourceVSphereVirtualMachine/import_with_multiple_disks_at_different_SCSI_slots
--- PASS: TestAccResourceVSphereVirtualMachine (11461.50s)
    --- PASS: TestAccResourceVSphereVirtualMachine/basic (153.88s)
    --- SKIP: TestAccResourceVSphereVirtualMachine/ESXi-only_test (0.00s)
    	helper_test.go:88: set VSPHERE_TEST_ESXI to run ESXi-specific acceptance tests
    --- PASS: TestAccResourceVSphereVirtualMachine/shutdown_OK (153.52s)
    --- PASS: TestAccResourceVSphereVirtualMachine/re-create_on_deletion (293.84s)
    --- PASS: TestAccResourceVSphereVirtualMachine/multi-device (170.53s)
    --- PASS: TestAccResourceVSphereVirtualMachine/add_devices (160.28s)
    --- PASS: TestAccResourceVSphereVirtualMachine/remove_middle_devices (173.38s)
    --- PASS: TestAccResourceVSphereVirtualMachine/remove_middle_devices,_change_disk_unit (290.55s)
    --- PASS: TestAccResourceVSphereVirtualMachine/high_disk_unit_numbers (168.37s)
    --- PASS: TestAccResourceVSphereVirtualMachine/high_disk_units_to_regular_single_controller (309.24s)
    --- PASS: TestAccResourceVSphereVirtualMachine/cdrom (15.99s)
    --- PASS: TestAccResourceVSphereVirtualMachine/maximum_number_of_nics (185.70s)
    --- PASS: TestAccResourceVSphereVirtualMachine/upgrade_cpu_and_ram (292.68s)
    --- PASS: TestAccResourceVSphereVirtualMachine/modify_annotation (170.06s)
    --- PASS: TestAccResourceVSphereVirtualMachine/grow_disk (170.28s)
    --- PASS: TestAccResourceVSphereVirtualMachine/swap_scsi_bus (287.39s)
    --- PASS: TestAccResourceVSphereVirtualMachine/extraconfig (161.54s)
    --- PASS: TestAccResourceVSphereVirtualMachine/extraconfig_swap_keys (272.38s)
    --- PASS: TestAccResourceVSphereVirtualMachine/attach_existing_vmdk (157.91s)
    --- PASS: TestAccResourceVSphereVirtualMachine/in_folder (152.97s)
    --- PASS: TestAccResourceVSphereVirtualMachine/move_to_folder (166.72s)
    --- PASS: TestAccResourceVSphereVirtualMachine/static_mac (161.60s)
    --- PASS: TestAccResourceVSphereVirtualMachine/single_tag (161.23s)
    --- PASS: TestAccResourceVSphereVirtualMachine/multiple_tags (159.06s)
    --- PASS: TestAccResourceVSphereVirtualMachine/switch_tags (170.28s)
    --- PASS: TestAccResourceVSphereVirtualMachine/orphaned_(renamed)_disk_in_place_of_existing (382.60s)
    --- PASS: TestAccResourceVSphereVirtualMachine/block_computed_disk_name (3.18s)
    --- PASS: TestAccResourceVSphereVirtualMachine/block_vapp_settings_on_non-clones (4.19s)
    --- PASS: TestAccResourceVSphereVirtualMachine/block_vapp_settings_on_non-clones_after_creation (170.67s)
    --- PASS: TestAccResourceVSphereVirtualMachine/block_disk_label_starting_with_orphaned_prefix (0.01s)
    --- PASS: TestAccResourceVSphereVirtualMachine/clone_from_template (112.22s)
    --- PASS: TestAccResourceVSphereVirtualMachine/clone,_modify_disk_and_SCSI_type_at_same_time (277.11s)
    --- PASS: TestAccResourceVSphereVirtualMachine/clone,_multi-nic_(template_should_have_one) (111.57s)
    --- PASS: TestAccResourceVSphereVirtualMachine/clone_with_different_timezone (110.10s)
    --- PASS: TestAccResourceVSphereVirtualMachine/clone_with_bad_timezone (0.01s)
    --- PASS: TestAccResourceVSphereVirtualMachine/clone_with_bad_eagerly_scrub_with_linked_clone (3.37s)
    --- PASS: TestAccResourceVSphereVirtualMachine/clone_with_bad_thin_provisioned_with_linked_clone (3.28s)
    --- PASS: TestAccResourceVSphereVirtualMachine/clone_with_bad_size_with_linked_clone (3.35s)
    --- PASS: TestAccResourceVSphereVirtualMachine/clone_with_bad_size_without_linked_clone (3.38s)
    --- PASS: TestAccResourceVSphereVirtualMachine/clone_-_attempt_to_add_vapp_properties_to_a_VM_that_does_not_support_them_-_create (13.37s)
    --- PASS: TestAccResourceVSphereVirtualMachine/clone_-_attempt_to_add_vapp_properties_to_a_VM_that_does_not_support_them_-_update (123.50s)
    --- PASS: TestAccResourceVSphereVirtualMachine/clone_with_different_hostname (118.92s)
    --- PASS: TestAccResourceVSphereVirtualMachine/clone_with_extra_disks (305.43s)
    --- PASS: TestAccResourceVSphereVirtualMachine/clone_with_cdrom (126.79s)
    --- PASS: TestAccResourceVSphereVirtualMachine/clone_with_vapp_properties (259.29s)
    --- PASS: TestAccResourceVSphereVirtualMachine/bad_vapp_property_-_clone_create (7.31s)
    --- PASS: TestAccResourceVSphereVirtualMachine/bad_vapp_property_-_clone_update (96.06s)
    --- PASS: TestAccResourceVSphereVirtualMachine/cpu_hot_add (127.64s)
    --- PASS: TestAccResourceVSphereVirtualMachine/memory_hot_add (130.43s)
    --- PASS: TestAccResourceVSphereVirtualMachine/dual-stack_ipv4_and_ipv6 (129.66s)
    --- PASS: TestAccResourceVSphereVirtualMachine/ipv6_only (423.52s)
    --- PASS: TestAccResourceVSphereVirtualMachine/windows_template,_customization_events_and_proper_IP (229.63s)
    --- PASS: TestAccResourceVSphereVirtualMachine/host_vmotion (149.13s)
    --- PASS: TestAccResourceVSphereVirtualMachine/resource_pool_vmotion (130.60s)
    --- PASS: TestAccResourceVSphereVirtualMachine/storage_vmotion_-_global_setting (429.01s)
    --- PASS: TestAccResourceVSphereVirtualMachine/storage_vmotion_-_single_disk (293.75s)
    --- PASS: TestAccResourceVSphereVirtualMachine/storage_vmotion_-_pin_datastore (428.82s)
    --- PASS: TestAccResourceVSphereVirtualMachine/storage_vmotion_-_renamed_virtual_machine (452.60s)
    --- PASS: TestAccResourceVSphereVirtualMachine/storage_vmotion_-_linked_clones (260.68s)
    --- PASS: TestAccResourceVSphereVirtualMachine/storage_vmotion_-_block_externally_attached_disks (279.23s)
    --- PASS: TestAccResourceVSphereVirtualMachine/single_custom_attribute (118.17s)
    --- PASS: TestAccResourceVSphereVirtualMachine/multi_custom_attribute (121.49s)
    --- PASS: TestAccResourceVSphereVirtualMachine/switch_custom_attribute (129.03s)
    --- PASS: TestAccResourceVSphereVirtualMachine/transition_to_label (170.27s)
    --- PASS: TestAccResourceVSphereVirtualMachine/prevent_revert_to_name (170.39s)
    --- PASS: TestAccResourceVSphereVirtualMachine/transition_to_label_-_attached_disk (164.67s)
    --- PASS: TestAccResourceVSphereVirtualMachine/import (157.54s)
    --- PASS: TestAccResourceVSphereVirtualMachine/import_with_multiple_disks_at_different_SCSI_slots (170.14s)
PASS
ok  	github.com/terraform-providers/terraform-provider-vsphere/vsphere	11461.511s

func virtualMachineFromSearchIndex(client *govmomi.Client, uuid string) (object.Reference, error) {
log.Printf("[DEBUG] Using SearchIndex to look up UUID %q", uuid)
search := object.NewSearchIndex(client.Client)
ctx, cancel := context.WithTimeout(context.Background(), provider.DefaultAPITimeout)

Choose a reason for hiding this comment

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

I think it would be preferable to pass the ctx in and declare the Background up as high in the callstack as possible, in this case at lease up in FromUUID, to make switching to StopContext down the road easier.

defer func() {
dctx, dcancel := context.WithTimeout(context.Background(), provider.DefaultAPITimeout)
defer dcancel()
v.Destroy(dctx)

Choose a reason for hiding this comment

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

If you don't care about the error, explicitly discard it, ie: _ = v.Destroy so it doesn't look like an oversight.

* Consolidated contexts (this will help streamline things when we add in
support for exposing the provider contexts).
* Added in error handling for the ContainerView destroy call. We don't
expect this to error, so we panic if it does - this ensures it passes
both errcheck and gas, which I am using now that I've adopted
gometalinter. This may or may not be the standard going forward.

defer func() {
if err = v.Destroy(ctx); err != nil {
panic(err)

Choose a reason for hiding this comment

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

if you would rather not panic here, you could just wrap the body in an inner func, and do this afterwards, but seems fine to me as is, as its probably an edge.

@vancluever vancluever merged commit 4777ae4 into master Jan 31, 2018
@johnjelinek
Copy link

wewt wewt! When does 1.3.1 ship?

@vancluever
Copy link
Contributor Author

@johnjelinek it's out now!

@vancluever vancluever deleted the b-uuid-template-old-vsphere branch February 13, 2018 15:57
@ghost ghost locked and limited conversation to collaborators Apr 18, 2020
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
bug Type: Bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

vsphere_virtual_machine "cannot locate virtual machine or template with UUID"
3 participants