From ede82e21307969da4acf82e05ef9f8a542f19795 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Fri, 21 Jul 2017 21:42:43 +0530 Subject: [PATCH] Implement vmware_argument_spec for required params (#25731) Without the fix hostname, username and password params used to skip required check. Fixes #25696 Signed-off-by: Abhijeet Kasurde --- .../modules/cloud/vmware/vmware_guest.py | 71 +++++++++---------- .../cloud/vmware/vmware_guest_facts.py | 35 +++------ .../modules/cloud/vmware/vmware_guest_find.py | 30 +++----- .../cloud/vmware/vmware_guest_snapshot.py | 52 +++++--------- .../targets/vmware_guest_find/tasks/main.yml | 13 ++++ 5 files changed, 83 insertions(+), 118 deletions(-) diff --git a/lib/ansible/modules/cloud/vmware/vmware_guest.py b/lib/ansible/modules/cloud/vmware/vmware_guest.py index c09c3b9c9f..474f1e000e 100644 --- a/lib/ansible/modules/cloud/vmware/vmware_guest.py +++ b/lib/ansible/modules/cloud/vmware/vmware_guest.py @@ -311,12 +311,11 @@ instance: sample: None ''' -import os -import time +import time from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.pycompat24 import get_exception -from ansible.module_utils.vmware import connect_to_api, find_obj, gather_vm_facts, get_all_objs, compile_folder_path_for_object +from ansible.module_utils.vmware import connect_to_api, find_obj, gather_vm_facts, get_all_objs, compile_folder_path_for_object, vmware_argument_spec from ansible.module_utils.vmware import serialize_spec try: @@ -1461,42 +1460,40 @@ class PyVmomiHelper(object): def main(): - module = AnsibleModule( - argument_spec=dict( - hostname=dict(type='str', default=os.environ.get('VMWARE_HOST')), - username=dict(type='str', default=os.environ.get('VMWARE_USER')), - password=dict(type='str', default=os.environ.get('VMWARE_PASSWORD'), no_log=True), - state=dict(type='str', default='present', - choices=['absent', 'poweredoff', 'poweredon', 'present', 'rebootguest', 'restarted', 'shutdownguest', 'suspended']), - validate_certs=dict(type='bool', default=True), - template=dict(type='str', aliases=['template_src']), - is_template=dict(type='bool', default=False), - annotation=dict(type='str', aliases=['notes']), - customvalues=dict(type='list', default=[]), - name=dict(type='str', required=True), - name_match=dict(type='str', default='first'), - uuid=dict(type='str'), - folder=dict(type='str', default='/vm'), - guest_id=dict(type='str'), - disk=dict(type='list', default=[]), - hardware=dict(type='dict', default={}), - force=dict(type='bool', default=False), - datacenter=dict(type='str', default='ha-datacenter'), - esxi_hostname=dict(type='str'), - cluster=dict(type='str'), - wait_for_ip_address=dict(type='bool', default=False), - snapshot_src=dict(type='str'), - linked_clone=dict(type='bool', default=False), - networks=dict(type='list', default=[]), - resource_pool=dict(type='str'), - customization=dict(type='dict', default={}, no_log=True), - ), - supports_check_mode=True, - mutually_exclusive=[ - ['cluster', 'esxi_hostname'], - ], + argument_spec = vmware_argument_spec() + argument_spec.update( + state=dict(type='str', default='present', + choices=['absent', 'poweredoff', 'poweredon', 'present', 'rebootguest', 'restarted', 'shutdownguest', 'suspended']), + template=dict(type='str', aliases=['template_src']), + is_template=dict(type='bool', default=False), + annotation=dict(type='str', aliases=['notes']), + customvalues=dict(type='list', default=[]), + name=dict(type='str', required=True), + name_match=dict(type='str', default='first'), + uuid=dict(type='str'), + folder=dict(type='str', default='/vm'), + guest_id=dict(type='str'), + disk=dict(type='list', default=[]), + hardware=dict(type='dict', default={}), + force=dict(type='bool', default=False), + datacenter=dict(type='str', default='ha-datacenter'), + esxi_hostname=dict(type='str'), + cluster=dict(type='str'), + wait_for_ip_address=dict(type='bool', default=False), + snapshot_src=dict(type='str'), + linked_clone=dict(type='bool', default=False), + networks=dict(type='list', default=[]), + resource_pool=dict(type='str'), + customization=dict(type='dict', default={}, no_log=True), ) + module = AnsibleModule(argument_spec=argument_spec, + supports_check_mode=True, + mutually_exclusive=[ + ['cluster', 'esxi_hostname'], + ], + ) + result = {'failed': False, 'changed': False} # FindByInventoryPath() does not require an absolute path diff --git a/lib/ansible/modules/cloud/vmware/vmware_guest_facts.py b/lib/ansible/modules/cloud/vmware/vmware_guest_facts.py index 3a04c33b8b..17e096b770 100644 --- a/lib/ansible/modules/cloud/vmware/vmware_guest_facts.py +++ b/lib/ansible/modules/cloud/vmware/vmware_guest_facts.py @@ -41,6 +41,7 @@ options: name: description: - Name of the VM to work with + - This is required if UUID is not supplied. name_match: description: - If multiple VMs matching the name, use the first or last found @@ -95,10 +96,9 @@ instance: sample: None """ -import os from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.vmware import connect_to_api, find_vm_by_id, gather_vm_facts from ansible.module_utils._text import to_text +from ansible.module_utils.vmware import connect_to_api, find_vm_by_id, gather_vm_facts, vmware_argument_spec try: import json @@ -152,29 +152,16 @@ class PyVmomiHelper(object): def main(): - module = AnsibleModule( - argument_spec=dict( - hostname=dict( - type='str', - default=os.environ.get('VMWARE_HOST') - ), - username=dict( - type='str', - default=os.environ.get('VMWARE_USER') - ), - password=dict( - type='str', no_log=True, - default=os.environ.get('VMWARE_PASSWORD') - ), - validate_certs=dict(required=False, type='bool', default=True), - name=dict(required=False, type='str'), - name_match=dict(required=False, type='str', default='first'), - uuid=dict(required=False, type='str'), - folder=dict(required=False, type='str', default='/vm'), - datacenter=dict(required=True, type='str'), - ), - required_one_of=[['name', 'uuid']], + argument_spec = vmware_argument_spec() + argument_spec.update( + name=dict(type='str'), + name_match=dict(type='str', default='first'), + uuid=dict(type='str'), + folder=dict(type='str', default='/vm'), + datacenter=dict(type='str', required=True), ) + module = AnsibleModule(argument_spec=argument_spec, + required_one_of=[['name', 'uuid']]) # FindByInventoryPath() does not require an absolute path # so we should leave the input folder path unmodified diff --git a/lib/ansible/modules/cloud/vmware/vmware_guest_find.py b/lib/ansible/modules/cloud/vmware/vmware_guest_find.py index 1da97d203f..4c1df6dadd 100644 --- a/lib/ansible/modules/cloud/vmware/vmware_guest_find.py +++ b/lib/ansible/modules/cloud/vmware/vmware_guest_find.py @@ -76,11 +76,9 @@ RETURN = """ """ import os - -# import module snippets from ansible.module_utils.basic import AnsibleModule from ansible.module_utils._text import to_native -from ansible.module_utils.vmware import connect_to_api, gather_vm_facts, get_all_objs, compile_folder_path_for_object +from ansible.module_utils.vmware import connect_to_api, gather_vm_facts, get_all_objs, compile_folder_path_for_object, vmware_argument_spec HAS_PYVMOMI = False @@ -230,27 +228,15 @@ def get_obj(content, vimtype, name): def main(): - module = AnsibleModule( - argument_spec=dict( - hostname=dict( - type='str', - default=os.environ.get('VMWARE_HOST') - ), - username=dict( - type='str', - default=os.environ.get('VMWARE_USER') - ), - password=dict( - type='str', no_log=True, - default=os.environ.get('VMWARE_PASSWORD') - ), - validate_certs=dict(required=False, type='bool', default=True), - name=dict(required=False, type='str'), - uuid=dict(required=False, type='str'), - datacenter=dict(required=True, type='str'), - ), + argument_spec = vmware_argument_spec() + argument_spec.update( + name=dict(type='str'), + uuid=dict(type='str'), + datacenter=dict(type='str', required=True) ) + module = AnsibleModule(argument_spec=argument_spec, + required_one_of=[['name', 'uuid']]) pyv = PyVmomiHelper(module) # Check if the VM exists before continuing folders = pyv.getvm_folder_paths( diff --git a/lib/ansible/modules/cloud/vmware/vmware_guest_snapshot.py b/lib/ansible/modules/cloud/vmware/vmware_guest_snapshot.py index bfebc54244..29b8668aa7 100644 --- a/lib/ansible/modules/cloud/vmware/vmware_guest_snapshot.py +++ b/lib/ansible/modules/cloud/vmware/vmware_guest_snapshot.py @@ -172,13 +172,11 @@ instance: sample: None """ -import os -import time -# import module snippets -from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.vmware import connect_to_api +import time from ansible.module_utils._text import to_native +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.vmware import connect_to_api, vmware_argument_spec try: import json @@ -321,37 +319,21 @@ class PyVmomiHelper(object): def main(): - module = AnsibleModule( - argument_spec=dict( - hostname=dict( - type='str', - default=os.environ.get('VMWARE_HOST') - ), - username=dict( - type='str', - default=os.environ.get('VMWARE_USER') - ), - password=dict( - type='str', no_log=True, - default=os.environ.get('VMWARE_PASSWORD') - ), - state=dict( - required=False, - choices=['present', 'absent', 'revert', 'remove_all'], - default='present'), - validate_certs=dict(required=False, type='bool', default=True), - name=dict(required=True, type='str'), - name_match=dict(required=False, type='str', default='first'), - uuid=dict(required=False, type='str'), - folder=dict(required=False, type='str', default='/vm'), - datacenter=dict(required=True, type='str'), - snapshot_name=dict(required=False, type='str'), - description=dict(required=False, type='str', default=''), - quiesce=dict(type='bool', default=False), - memory_dump=dict(type='bool', default=False), - remove_children=dict(type='bool', default=False), - ), + argument_spec = vmware_argument_spec() + argument_spec.update( + state=dict(default='present', choices=['present', 'absent', 'revert', 'remove_all']), + name=dict(required=True, type='str'), + name_match=dict(type='str', default='first'), + uuid=dict(type='str'), + folder=dict(type='str', default='/vm'), + datacenter=dict(required=True, type='str'), + snapshot_name=dict(type='str'), + description=dict(type='str', default=''), + quiesce=dict(type='bool', default=False), + memory_dump=dict(type='bool', default=False), + remove_children=dict(type='bool', default=False), ) + module = AnsibleModule(argument_spec=argument_spec) # 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('/'): diff --git a/test/integration/targets/vmware_guest_find/tasks/main.yml b/test/integration/targets/vmware_guest_find/tasks/main.yml index 43ddcd3bc3..14c9903806 100644 --- a/test/integration/targets/vmware_guest_find/tasks/main.yml +++ b/test/integration/targets/vmware_guest_find/tasks/main.yml @@ -8,14 +8,27 @@ vcsim: "{{ lookup('env', 'vcenter_host') }}" - debug: var=vcsim +- name: wait for flask server + wait_for: + host: "{{ vcsim }}" + port: 5000 + state: started + - name: kill vcsim uri: url: "{{ 'http://' + vcsim + ':5000/killall' }}" + - name: start vcsim uri: url: "{{ 'http://' + vcsim + ':5000/spawn?cluster=2' }}" register: vcsim_instance +- name: wait for vcsim server + wait_for: + host: "{{ vcsim }}" + port: 443 + state: started + - name: get a list of VMS from vcsim uri: url: "{{ 'http://' + vcsim + ':5000/govc_find?filter=VM' }}"