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 guest info bug #27

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/27-guest-info-vm-name-bug.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- guest_info - Fixed bugs that caused module failure when specifying the guest_name attribute
23 changes: 22 additions & 1 deletion plugins/module_utils/vmware_rest_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
ResourcePool,
Datastore,
Cluster,
Host)
Host,
VM)
HAS_VSPHERE = True
except ImportError:
VSPHERE_IMP_ERR = traceback.format_exc()
Expand Down Expand Up @@ -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.
Expand Down
21 changes: 8 additions & 13 deletions plugins/modules/guest_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from __future__ import absolute_import, division, print_function
__metaclass__ = type


DOCUMENTATION = r'''
---
module: guest_info
Expand Down Expand Up @@ -114,7 +113,14 @@ def get_guest_info(self):
guests = []

if self.params.get('guest_name'):
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
except TypeError:
vms = [] if not matching_vms else [matching_vms]
else:
vms = self.vmware_client.api_client.vcenter.VM.list()

Expand All @@ -137,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(
self.vmware_client.api_client.VM.FilterSpec(names=names)
)

if len(vms) == 0:
return None

return vms[0]


def main():
argument_spec = VmwareRestClient.vmware_client_argument_spec()
Expand Down
Loading