VMware datacenter module shouldn't hold pyvmomi context in Ansible module object (#1568)
* VMware datacenter module rewritten to don't hold pyvmomi context and objects in Ansible module object fixed exceptions handling added datacenter destroy result, moved checks changed wrong value wrong value again... need some sleep * check_mode fixes * state defaults to present, default changed to true * module check fixes
This commit is contained in:
parent
2dbfdaa88b
commit
01a15f8a0b
1 changed files with 39 additions and 58 deletions
|
@ -25,9 +25,9 @@ short_description: Manage VMware vSphere Datacenters
|
|||
description:
|
||||
- Manage VMware vSphere Datacenters
|
||||
version_added: 2.0
|
||||
author: "Joseph Callen (@jcpowermac)"
|
||||
author: "Joseph Callen (@jcpowermac), Kamil Szczygiel (@kamsz)"
|
||||
notes:
|
||||
- Tested on vSphere 5.5
|
||||
- Tested on vSphere 6.0
|
||||
requirements:
|
||||
- "python >= 2.6"
|
||||
- PyVmomi
|
||||
|
@ -54,7 +54,7 @@ options:
|
|||
description:
|
||||
- If the datacenter should be present or absent
|
||||
choices: ['present', 'absent']
|
||||
required: True
|
||||
default: present
|
||||
extends_documentation_fragment: vmware.documentation
|
||||
'''
|
||||
|
||||
|
@ -64,7 +64,7 @@ EXAMPLES = '''
|
|||
local_action: >
|
||||
vmware_datacenter
|
||||
hostname="{{ ansible_ssh_host }}" username=root password=vmware
|
||||
datacenter_name="datacenter"
|
||||
datacenter_name="datacenter" state=present
|
||||
'''
|
||||
|
||||
try:
|
||||
|
@ -74,18 +74,28 @@ except ImportError:
|
|||
HAS_PYVMOMI = False
|
||||
|
||||
|
||||
def state_create_datacenter(module):
|
||||
datacenter_name = module.params['datacenter_name']
|
||||
content = module.params['content']
|
||||
changed = True
|
||||
datacenter = None
|
||||
def get_datacenter(context, module):
|
||||
try:
|
||||
datacenter_name = module.params.get('datacenter_name')
|
||||
datacenter = find_datacenter_by_name(context, datacenter_name)
|
||||
return datacenter
|
||||
except vmodl.RuntimeFault as runtime_fault:
|
||||
module.fail_json(msg=runtime_fault.msg)
|
||||
except vmodl.MethodFault as method_fault:
|
||||
module.fail_json(msg=method_fault.msg)
|
||||
|
||||
folder = content.rootFolder
|
||||
|
||||
def create_datacenter(context, module):
|
||||
datacenter_name = module.params.get('datacenter_name')
|
||||
folder = context.rootFolder
|
||||
|
||||
try:
|
||||
if not module.check_mode:
|
||||
datacenter = folder.CreateDatacenter(name=datacenter_name)
|
||||
module.exit_json(changed=changed, result=str(datacenter))
|
||||
datacenter = get_datacenter(context, module)
|
||||
if not datacenter:
|
||||
changed = True
|
||||
if not module.check_mode:
|
||||
folder.CreateDatacenter(name=datacenter_name)
|
||||
module.exit_json(changed=changed)
|
||||
except vim.fault.DuplicateName:
|
||||
module.fail_json(msg="A datacenter with the name %s already exists" % datacenter_name)
|
||||
except vim.fault.InvalidName:
|
||||
|
@ -99,34 +109,16 @@ def state_create_datacenter(module):
|
|||
module.fail_json(msg=method_fault.msg)
|
||||
|
||||
|
||||
def check_datacenter_state(module):
|
||||
datacenter_name = module.params['datacenter_name']
|
||||
|
||||
try:
|
||||
content = connect_to_api(module)
|
||||
datacenter = find_datacenter_by_name(content, datacenter_name)
|
||||
module.params['content'] = content
|
||||
|
||||
if datacenter is None:
|
||||
return 'absent'
|
||||
else:
|
||||
module.params['datacenter'] = datacenter
|
||||
return 'present'
|
||||
except vmodl.RuntimeFault as runtime_fault:
|
||||
module.fail_json(msg=runtime_fault.msg)
|
||||
except vmodl.MethodFault as method_fault:
|
||||
module.fail_json(msg=method_fault.msg)
|
||||
|
||||
|
||||
def state_destroy_datacenter(module):
|
||||
datacenter = module.params['datacenter']
|
||||
changed = True
|
||||
def destroy_datacenter(context, module):
|
||||
result = None
|
||||
|
||||
try:
|
||||
if not module.check_mode:
|
||||
task = datacenter.Destroy_Task()
|
||||
changed, result = wait_for_task(task)
|
||||
datacenter = get_datacenter(context, module)
|
||||
if datacenter:
|
||||
changed = True
|
||||
if not module.check_mode:
|
||||
task = datacenter.Destroy_Task()
|
||||
changed, result = wait_for_task(task)
|
||||
module.exit_json(changed=changed, result=result)
|
||||
except vim.fault.VimFault as vim_fault:
|
||||
module.fail_json(msg=vim_fault.msg)
|
||||
|
@ -136,39 +128,28 @@ def state_destroy_datacenter(module):
|
|||
module.fail_json(msg=method_fault.msg)
|
||||
|
||||
|
||||
def state_exit_unchanged(module):
|
||||
module.exit_json(changed=False)
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
argument_spec = vmware_argument_spec()
|
||||
argument_spec.update(
|
||||
dict(
|
||||
datacenter_name=dict(required=True, type='str'),
|
||||
state=dict(required=True, choices=['present', 'absent'], type='str'),
|
||||
)
|
||||
datacenter_name=dict(required=True, type='str'),
|
||||
state=dict(default='present', choices=['present', 'absent'], type='str')
|
||||
)
|
||||
)
|
||||
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
|
||||
|
||||
if not HAS_PYVMOMI:
|
||||
module.fail_json(msg='pyvmomi is required for this module')
|
||||
|
||||
datacenter_states = {
|
||||
'absent': {
|
||||
'present': state_destroy_datacenter,
|
||||
'absent': state_exit_unchanged,
|
||||
},
|
||||
'present': {
|
||||
'present': state_exit_unchanged,
|
||||
'absent': state_create_datacenter,
|
||||
}
|
||||
}
|
||||
desired_state = module.params['state']
|
||||
current_state = check_datacenter_state(module)
|
||||
context = connect_to_api(module)
|
||||
state = module.params.get('state')
|
||||
|
||||
datacenter_states[desired_state][current_state](module)
|
||||
if state == 'present':
|
||||
create_datacenter(context, module)
|
||||
|
||||
if state == 'absent':
|
||||
destroy_datacenter(context, module)
|
||||
|
||||
from ansible.module_utils.basic import *
|
||||
from ansible.module_utils.vmware import *
|
||||
|
|
Loading…
Reference in a new issue