mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-08 06:12:51 +00:00
Reformat everything.
This commit is contained in:
@@ -176,9 +176,15 @@ instance:
|
||||
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible_collections.community.general.plugins.module_utils.xenserver import (xenserver_common_argument_spec, XenServerObject, get_object_ref,
|
||||
gather_vm_params, gather_vm_facts, set_vm_power_state,
|
||||
wait_for_vm_ip_address)
|
||||
from ansible_collections.community.general.plugins.module_utils.xenserver import (
|
||||
xenserver_common_argument_spec,
|
||||
XenServerObject,
|
||||
get_object_ref,
|
||||
gather_vm_params,
|
||||
gather_vm_facts,
|
||||
set_vm_power_state,
|
||||
wait_for_vm_ip_address,
|
||||
)
|
||||
|
||||
|
||||
class XenServerVM(XenServerObject):
|
||||
@@ -198,7 +204,14 @@ class XenServerVM(XenServerObject):
|
||||
"""
|
||||
super().__init__(module)
|
||||
|
||||
self.vm_ref = get_object_ref(self.module, self.module.params['name'], self.module.params['uuid'], obj_type="VM", fail=True, msg_prefix="VM search: ")
|
||||
self.vm_ref = get_object_ref(
|
||||
self.module,
|
||||
self.module.params["name"],
|
||||
self.module.params["uuid"],
|
||||
obj_type="VM",
|
||||
fail=True,
|
||||
msg_prefix="VM search: ",
|
||||
)
|
||||
self.gather_params()
|
||||
|
||||
def gather_params(self):
|
||||
@@ -211,56 +224,72 @@ class XenServerVM(XenServerObject):
|
||||
|
||||
def set_power_state(self, power_state):
|
||||
"""Controls VM power state."""
|
||||
state_changed, current_state = set_vm_power_state(self.module, self.vm_ref, power_state, self.module.params['state_change_timeout'])
|
||||
state_changed, current_state = set_vm_power_state(
|
||||
self.module, self.vm_ref, power_state, self.module.params["state_change_timeout"]
|
||||
)
|
||||
|
||||
# If state has changed, update vm_params.
|
||||
if state_changed:
|
||||
self.vm_params['power_state'] = current_state.capitalize()
|
||||
self.vm_params["power_state"] = current_state.capitalize()
|
||||
|
||||
return state_changed
|
||||
|
||||
def wait_for_ip_address(self):
|
||||
"""Waits for VM to acquire an IP address."""
|
||||
self.vm_params['guest_metrics'] = wait_for_vm_ip_address(self.module, self.vm_ref, self.module.params['state_change_timeout'])
|
||||
self.vm_params["guest_metrics"] = wait_for_vm_ip_address(
|
||||
self.module, self.vm_ref, self.module.params["state_change_timeout"]
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
argument_spec = xenserver_common_argument_spec()
|
||||
argument_spec.update(
|
||||
state=dict(type='str', default='present',
|
||||
choices=['powered-on', 'powered-off', 'restarted', 'shutdown-guest', 'reboot-guest', 'suspended', 'present']),
|
||||
name=dict(type='str', aliases=['name_label']),
|
||||
uuid=dict(type='str'),
|
||||
wait_for_ip_address=dict(type='bool', default=False),
|
||||
state_change_timeout=dict(type='int', default=0),
|
||||
state=dict(
|
||||
type="str",
|
||||
default="present",
|
||||
choices=[
|
||||
"powered-on",
|
||||
"powered-off",
|
||||
"restarted",
|
||||
"shutdown-guest",
|
||||
"reboot-guest",
|
||||
"suspended",
|
||||
"present",
|
||||
],
|
||||
),
|
||||
name=dict(type="str", aliases=["name_label"]),
|
||||
uuid=dict(type="str"),
|
||||
wait_for_ip_address=dict(type="bool", default=False),
|
||||
state_change_timeout=dict(type="int", default=0),
|
||||
)
|
||||
|
||||
module = AnsibleModule(argument_spec=argument_spec,
|
||||
supports_check_mode=True,
|
||||
required_one_of=[
|
||||
['name', 'uuid'],
|
||||
],
|
||||
)
|
||||
module = AnsibleModule(
|
||||
argument_spec=argument_spec,
|
||||
supports_check_mode=True,
|
||||
required_one_of=[
|
||||
["name", "uuid"],
|
||||
],
|
||||
)
|
||||
|
||||
result = {'failed': False, 'changed': False}
|
||||
result = {"failed": False, "changed": False}
|
||||
|
||||
# Module will exit with an error message if no VM is found.
|
||||
vm = XenServerVM(module)
|
||||
|
||||
# Set VM power state.
|
||||
if module.params['state'] != "present":
|
||||
result['changed'] = vm.set_power_state(module.params['state'])
|
||||
if module.params["state"] != "present":
|
||||
result["changed"] = vm.set_power_state(module.params["state"])
|
||||
|
||||
if module.params['wait_for_ip_address']:
|
||||
if module.params["wait_for_ip_address"]:
|
||||
vm.wait_for_ip_address()
|
||||
|
||||
result['instance'] = vm.gather_facts()
|
||||
result["instance"] = vm.gather_facts()
|
||||
|
||||
if result['failed']:
|
||||
if result["failed"]:
|
||||
module.fail_json(**result)
|
||||
else:
|
||||
module.exit_json(**result)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user