mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 12:50:22 -07:00
Add testcase for vmware_guest_facts (#26873)
Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
parent
efbc65bff0
commit
e979663dfe
3 changed files with 108 additions and 18 deletions
|
@ -53,8 +53,22 @@ options:
|
|||
- This is required if name is not supplied.
|
||||
folder:
|
||||
description:
|
||||
- Destination folder, absolute path to find an existing guest.
|
||||
- Destination folder, absolute or relative path to find an existing guest.
|
||||
- This is required if name is supplied.
|
||||
- The folder should include the datacenter. ESX's datacenter is ha-datacenter
|
||||
- 'Examples:'
|
||||
- ' folder: /ha-datacenter/vm'
|
||||
- ' folder: ha-datacenter/vm'
|
||||
- ' folder: /datacenter1/vm'
|
||||
- ' folder: datacenter1/vm'
|
||||
- ' folder: /datacenter1/vm/folder1'
|
||||
- ' folder: datacenter1/vm/folder1'
|
||||
- ' folder: /folder1/datacenter1/vm'
|
||||
- ' folder: folder1/datacenter1/vm'
|
||||
- ' folder: /folder1/datacenter1/vm/folder2'
|
||||
- ' folder: vm/folder2'
|
||||
- ' fodler: folder2'
|
||||
default: /vm
|
||||
datacenter:
|
||||
description:
|
||||
- Destination datacenter for the deploy operation
|
||||
|
@ -83,27 +97,23 @@ instance:
|
|||
"""
|
||||
|
||||
import os
|
||||
import time
|
||||
|
||||
# import module snippets
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.pycompat24 import get_exception
|
||||
from ansible.module_utils.six import iteritems
|
||||
from ansible.module_utils.vmware import connect_to_api, find_vm_by_id, gather_vm_facts
|
||||
from ansible.module_utils._text import to_text
|
||||
|
||||
try:
|
||||
import json
|
||||
except ImportError:
|
||||
import simplejson as json
|
||||
|
||||
HAS_PYVMOMI = False
|
||||
|
||||
try:
|
||||
import pyVmomi
|
||||
from pyVmomi import vim
|
||||
|
||||
HAS_PYVMOMI = True
|
||||
except ImportError:
|
||||
pass
|
||||
HAS_PYVMOMI = False
|
||||
|
||||
|
||||
class PyVmomiHelper(object):
|
||||
|
@ -121,10 +131,7 @@ class PyVmomiHelper(object):
|
|||
if uuid:
|
||||
vm = find_vm_by_id(self.content, vm_id=uuid, vm_id_type="uuid")
|
||||
elif folder:
|
||||
# Build the absolute folder path to pass into the search method
|
||||
if not self.params['folder'].startswith('/'):
|
||||
self.module.fail_json(msg="Folder %(folder)s needs to be an absolute path, starting with '/'." % self.params)
|
||||
searchpath = '%(datacenter)s%(folder)s' % self.params
|
||||
searchpath = self.params['folder']
|
||||
|
||||
# get all objects for this path ...
|
||||
f_obj = self.content.searchIndex.FindByInventoryPath(searchpath)
|
||||
|
@ -167,11 +174,11 @@ def main():
|
|||
folder=dict(required=False, type='str', default='/vm'),
|
||||
datacenter=dict(required=True, type='str'),
|
||||
),
|
||||
required_together=[('name', 'folder')],
|
||||
)
|
||||
|
||||
# Prepend /vm if it was missing from the folder path, also strip trailing slashes
|
||||
if not module.params['folder'].startswith('/vm') and module.params['folder'].startswith('/'):
|
||||
module.params['folder'] = '/vm%(folder)s' % module.params
|
||||
# FindByInventoryPath() does not require an absolute path
|
||||
# so we should leave the input folder path unmodified
|
||||
module.params['folder'] = module.params['folder'].rstrip('/')
|
||||
|
||||
pyv = PyVmomiHelper(module)
|
||||
|
@ -184,9 +191,8 @@ def main():
|
|||
if vm:
|
||||
try:
|
||||
module.exit_json(instance=pyv.gather_facts(vm))
|
||||
except Exception:
|
||||
e = get_exception()
|
||||
module.fail_json(msg="Fact gather failed with exception %s" % e)
|
||||
except Exception as exc:
|
||||
module.fail_json(msg="Fact gather failed with exception %s" % to_text(exc))
|
||||
else:
|
||||
module.fail_json(msg="Unable to gather facts for non-existing VM %(name)s" % module.params)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue