From f6a809ba921837b46257f4eb96e4b00be8ccba28 Mon Sep 17 00:00:00 2001 From: Mike Morency Date: Thu, 23 May 2024 10:05:47 -0400 Subject: [PATCH 1/5] fix guest info bug --- plugins/modules/guest_info.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/plugins/modules/guest_info.py b/plugins/modules/guest_info.py index d335098b..1de18eb3 100644 --- a/plugins/modules/guest_info.py +++ b/plugins/modules/guest_info.py @@ -71,6 +71,7 @@ ''' from collections import defaultdict +from com.vmware.vcenter_client import VM from ansible.module_utils.basic import AnsibleModule from ansible_collections.vmware.vmware.plugins.module_utils.vmware import PyVmomi @@ -114,7 +115,12 @@ def get_guest_info(self): guests = [] if self.params.get('guest_name'): - vms = self._get_vm(self.params.get('guest_name')) + matching_vms = self._get_vm(self.params.get('guest_name')) + try: + _ = iter(matching_vms) + vms = matching_vms + except TypeError: + vms = [] if not matching_vms else [matching_vms] else: vms = self.vmware_client.api_client.vcenter.VM.list() @@ -140,7 +146,7 @@ def _vvars(self, vmware_obj, r): def _get_vm(self, vm_name): names = set([vm_name]) vms = self.vmware_client.api_client.vcenter.VM.list( - self.vmware_client.api_client.VM.FilterSpec(names=names) + VM.FilterSpec(names=names) ) if len(vms) == 0: From 98229eb84f8bae6370d37d270432fd4988130acc Mon Sep 17 00:00:00 2001 From: Mike Morency Date: Thu, 23 May 2024 10:22:02 -0400 Subject: [PATCH 2/5] add changelog --- changelogs/fragments/27-guest-info-vm-name-bug.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelogs/fragments/27-guest-info-vm-name-bug.yml diff --git a/changelogs/fragments/27-guest-info-vm-name-bug.yml b/changelogs/fragments/27-guest-info-vm-name-bug.yml new file mode 100644 index 00000000..f3e03af0 --- /dev/null +++ b/changelogs/fragments/27-guest-info-vm-name-bug.yml @@ -0,0 +1,2 @@ +bugfixes: + - guest_info - Fixed bugs that caused module failure when specifying the guest_name attribute From 42bdf19af7ffce411c425c94d7196af410163841 Mon Sep 17 00:00:00 2001 From: Mike Morency Date: Thu, 23 May 2024 10:28:43 -0400 Subject: [PATCH 3/5] fix import error during tests --- plugins/modules/guest_info.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/plugins/modules/guest_info.py b/plugins/modules/guest_info.py index 1de18eb3..1c2174e6 100644 --- a/plugins/modules/guest_info.py +++ b/plugins/modules/guest_info.py @@ -8,6 +8,7 @@ from __future__ import absolute_import, division, print_function __metaclass__ = type +import traceback DOCUMENTATION = r''' --- @@ -71,7 +72,12 @@ ''' from collections import defaultdict -from com.vmware.vcenter_client import VM + +try: + from com.vmware.vcenter_client import VM +except ImportError: + # handled during class init + pass from ansible.module_utils.basic import AnsibleModule from ansible_collections.vmware.vmware.plugins.module_utils.vmware import PyVmomi @@ -117,7 +123,7 @@ def get_guest_info(self): if self.params.get('guest_name'): matching_vms = self._get_vm(self.params.get('guest_name')) try: - _ = iter(matching_vms) + _ = iter(matching_vms) # pylint: disable=disallowed-name vms = matching_vms except TypeError: vms = [] if not matching_vms else [matching_vms] From be6b826f025ff5a6883cf6686808ae3279c7395c Mon Sep 17 00:00:00 2001 From: Mike Morency Date: Thu, 23 May 2024 13:06:03 -0400 Subject: [PATCH 4/5] more import fixes --- plugins/modules/guest_info.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/plugins/modules/guest_info.py b/plugins/modules/guest_info.py index 1c2174e6..c6905276 100644 --- a/plugins/modules/guest_info.py +++ b/plugins/modules/guest_info.py @@ -8,8 +8,6 @@ from __future__ import absolute_import, division, print_function __metaclass__ = type -import traceback - DOCUMENTATION = r''' --- module: guest_info From f3db33d9d63ef459d3e900c646616d5e8fb8592a Mon Sep 17 00:00:00 2001 From: Mike Morency Date: Tue, 28 May 2024 08:13:48 -0400 Subject: [PATCH 5/5] move method to module_utils to remove import check --- plugins/module_utils/vmware_rest_client.py | 23 +++++++++++++++++++++- plugins/modules/guest_info.py | 21 +++----------------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/plugins/module_utils/vmware_rest_client.py b/plugins/module_utils/vmware_rest_client.py index 4295d5d7..4646cecf 100644 --- a/plugins/module_utils/vmware_rest_client.py +++ b/plugins/module_utils/vmware_rest_client.py @@ -41,7 +41,8 @@ ResourcePool, Datastore, Cluster, - Host) + Host, + VM) HAS_VSPHERE = True except ImportError: VSPHERE_IMP_ERR = traceback.format_exc() @@ -340,6 +341,26 @@ def get_vm_tags(self, tag_service=None, tag_association_svc=None, vm_mid=None): return tags + def get_vm_by_name(self, name): + """ + Returns a VM object that matches the given name. + + Args: + name (str): The name of VM to look for + + Returns: + list(str): VM object matching the name provided. Returns None if no + matches are found + """ + vms = self.api_client.vcenter.VM.list( + VM.FilterSpec(names=set([name])) + ) + + if len(vms) == 0: + return None + + return vms[0] + def get_library_item_by_name(self, name): """ Returns the identifier of the library item with the given name. diff --git a/plugins/modules/guest_info.py b/plugins/modules/guest_info.py index c6905276..c937520e 100644 --- a/plugins/modules/guest_info.py +++ b/plugins/modules/guest_info.py @@ -71,12 +71,6 @@ from collections import defaultdict -try: - from com.vmware.vcenter_client import VM -except ImportError: - # handled during class init - pass - from ansible.module_utils.basic import AnsibleModule from ansible_collections.vmware.vmware.plugins.module_utils.vmware import PyVmomi from ansible_collections.vmware.vmware.plugins.module_utils.vmware_rest_client import VmwareRestClient @@ -119,7 +113,9 @@ def get_guest_info(self): guests = [] if self.params.get('guest_name'): - matching_vms = self._get_vm(self.params.get('guest_name')) + matching_vms = self.vmware_client.get_vm_by_name( + name=self.params.get('guest_name') + ) try: _ = iter(matching_vms) # pylint: disable=disallowed-name vms = matching_vms @@ -147,17 +143,6 @@ def _vvars(self, vmware_obj, r): else: r[k] = str(v) - def _get_vm(self, vm_name): - names = set([vm_name]) - vms = self.vmware_client.api_client.vcenter.VM.list( - VM.FilterSpec(names=names) - ) - - if len(vms) == 0: - return None - - return vms[0] - def main(): argument_spec = VmwareRestClient.vmware_client_argument_spec()