mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
vmadm: minor refactoring (#4648)
* vmadm: minor refactor - simplifications - adjusting comments - minor changes * added changelog fragment * Update plugins/modules/cloud/smartos/vmadm.py Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
parent
358b579803
commit
a4b4756777
2 changed files with 17 additions and 26 deletions
2
changelogs/fragments/4648-vmadm-improvements-2.yaml
Normal file
2
changelogs/fragments/4648-vmadm-improvements-2.yaml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
minor_changes:
|
||||||
|
- vmadm - minor refactoring and improvement on the module (https://github.com/ansible-collections/community.general/pull/4648).
|
|
@ -430,10 +430,8 @@ def get_vm_prop(module, uuid, prop):
|
||||||
msg='Invalid JSON returned by vmadm for uuid lookup of {0}'.format(prop),
|
msg='Invalid JSON returned by vmadm for uuid lookup of {0}'.format(prop),
|
||||||
details=to_native(e), exception=traceback.format_exc())
|
details=to_native(e), exception=traceback.format_exc())
|
||||||
|
|
||||||
if len(stdout_json) > 0 and prop in stdout_json[0]:
|
if stdout_json:
|
||||||
return stdout_json[0][prop]
|
return stdout_json[0].get(prop)
|
||||||
else:
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def get_vm_uuid(module, alias):
|
def get_vm_uuid(module, alias):
|
||||||
|
@ -450,18 +448,15 @@ def get_vm_uuid(module, alias):
|
||||||
# If no VM was found matching the given alias, we get back an empty array.
|
# If no VM was found matching the given alias, we get back an empty array.
|
||||||
# That is not an error condition as we might be explicitly checking it's
|
# That is not an error condition as we might be explicitly checking it's
|
||||||
# absence.
|
# absence.
|
||||||
if stdout.strip() == '[]':
|
try:
|
||||||
return None
|
stdout_json = json.loads(stdout)
|
||||||
else:
|
except Exception as e:
|
||||||
try:
|
module.fail_json(
|
||||||
stdout_json = json.loads(stdout)
|
msg='Invalid JSON returned by vmadm for uuid lookup of {0}'.format(alias),
|
||||||
except Exception as e:
|
details=to_native(e), exception=traceback.format_exc())
|
||||||
module.fail_json(
|
|
||||||
msg='Invalid JSON returned by vmadm for uuid lookup of {0}'.format(alias),
|
|
||||||
details=to_native(e), exception=traceback.format_exc())
|
|
||||||
|
|
||||||
if len(stdout_json) > 0 and 'uuid' in stdout_json[0]:
|
if stdout_json:
|
||||||
return stdout_json[0]['uuid']
|
return stdout_json[0].get('uuid')
|
||||||
|
|
||||||
|
|
||||||
def get_all_vm_uuids(module):
|
def get_all_vm_uuids(module):
|
||||||
|
@ -484,7 +479,7 @@ def get_all_vm_uuids(module):
|
||||||
def new_vm(module, uuid, vm_state):
|
def new_vm(module, uuid, vm_state):
|
||||||
payload_file = create_payload(module, uuid)
|
payload_file = create_payload(module, uuid)
|
||||||
|
|
||||||
(rc, stdout, stderr) = vmadm_create_vm(module, payload_file)
|
(rc, dummy, stderr) = vmadm_create_vm(module, payload_file)
|
||||||
|
|
||||||
if rc != 0:
|
if rc != 0:
|
||||||
changed = False
|
changed = False
|
||||||
|
@ -519,7 +514,6 @@ def new_vm(module, uuid, vm_state):
|
||||||
|
|
||||||
def vmadm_create_vm(module, payload_file):
|
def vmadm_create_vm(module, payload_file):
|
||||||
# Create a new VM using the provided payload.
|
# Create a new VM using the provided payload.
|
||||||
cmd = '{0} create -f {1}'.format(module.vmadm, payload_file)
|
|
||||||
cmd = [module.vmadm, 'create', '-f', payload_file]
|
cmd = [module.vmadm, 'create', '-f', payload_file]
|
||||||
|
|
||||||
return module.run_command(cmd)
|
return module.run_command(cmd)
|
||||||
|
@ -547,7 +541,7 @@ def set_vm_state(module, vm_uuid, vm_state):
|
||||||
|
|
||||||
cmd = [module.vmadm, command] + force + [vm_uuid]
|
cmd = [module.vmadm, command] + force + [vm_uuid]
|
||||||
|
|
||||||
(rc, stdout, stderr) = module.run_command(cmd)
|
(dummy, dummy, stderr) = module.run_command(cmd)
|
||||||
|
|
||||||
match = re.match('^Successfully.*', stderr)
|
match = re.match('^Successfully.*', stderr)
|
||||||
return match is not None
|
return match is not None
|
||||||
|
@ -632,7 +626,7 @@ def manage_all_vms(module, vm_state):
|
||||||
if (not current_vm_state) or (get_vm_prop(module, uuid, 'state') != state):
|
if (not current_vm_state) or (get_vm_prop(module, uuid, 'state') != state):
|
||||||
any_changed = True
|
any_changed = True
|
||||||
else:
|
else:
|
||||||
any_changed = (vm_state_transition(module, uuid, vm_state) | any_changed)
|
any_changed = vm_state_transition(module, uuid, vm_state) or any_changed
|
||||||
|
|
||||||
return any_changed
|
return any_changed
|
||||||
|
|
||||||
|
@ -774,14 +768,9 @@ def main():
|
||||||
elif module.check_mode:
|
elif module.check_mode:
|
||||||
# Shortcut for check mode, if there is no VM yet, it will need to be created.
|
# Shortcut for check mode, if there is no VM yet, it will need to be created.
|
||||||
# Or, if the VM is not in the desired state yet, it needs to transition.
|
# Or, if the VM is not in the desired state yet, it needs to transition.
|
||||||
if (not current_vm_state) or (get_vm_prop(module, uuid, 'state') != state):
|
result['changed'] = (not current_vm_state) or (get_vm_prop(module, uuid, 'state') != state)
|
||||||
result['changed'] = True
|
|
||||||
else:
|
|
||||||
result['changed'] = False
|
|
||||||
|
|
||||||
module.exit_json(**result)
|
|
||||||
# No VM was found that matched the given ID (alias or uuid), so we create it.
|
|
||||||
elif not current_vm_state:
|
elif not current_vm_state:
|
||||||
|
# No VM was found that matched the given ID (alias or uuid), so we create it.
|
||||||
result['changed'], result['uuid'] = new_vm(module, uuid, vm_state)
|
result['changed'], result['uuid'] = new_vm(module, uuid, vm_state)
|
||||||
else:
|
else:
|
||||||
# VM was found, operate on its state directly.
|
# VM was found, operate on its state directly.
|
||||||
|
|
Loading…
Reference in a new issue