mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-03-26 21:33:12 +00:00
Reformat everything.
This commit is contained in:
@@ -371,54 +371,68 @@ snapshots:
|
||||
from ansible_collections.community.general.plugins.module_utils.opennebula import OpenNebulaModule
|
||||
|
||||
|
||||
IMAGE_STATES = ['INIT', 'READY', 'USED', 'DISABLED', 'LOCKED', 'ERROR', 'CLONE', 'DELETE', 'USED_PERS', 'LOCKED_USED', 'LOCKED_USED_PERS']
|
||||
IMAGE_STATES = [
|
||||
"INIT",
|
||||
"READY",
|
||||
"USED",
|
||||
"DISABLED",
|
||||
"LOCKED",
|
||||
"ERROR",
|
||||
"CLONE",
|
||||
"DELETE",
|
||||
"USED_PERS",
|
||||
"LOCKED_USED",
|
||||
"LOCKED_USED_PERS",
|
||||
]
|
||||
|
||||
|
||||
class ImageModule(OpenNebulaModule):
|
||||
def __init__(self):
|
||||
argument_spec = dict(
|
||||
id=dict(type='int'),
|
||||
name=dict(type='str'),
|
||||
state=dict(type='str', choices=['present', 'absent', 'cloned', 'renamed'], default='present'),
|
||||
enabled=dict(type='bool'),
|
||||
new_name=dict(type='str'),
|
||||
persistent=dict(type='bool'),
|
||||
create=dict(type='bool'),
|
||||
template=dict(type='str'),
|
||||
datastore_id=dict(type='int'),
|
||||
wait_timeout=dict(type='int', default=60),
|
||||
id=dict(type="int"),
|
||||
name=dict(type="str"),
|
||||
state=dict(type="str", choices=["present", "absent", "cloned", "renamed"], default="present"),
|
||||
enabled=dict(type="bool"),
|
||||
new_name=dict(type="str"),
|
||||
persistent=dict(type="bool"),
|
||||
create=dict(type="bool"),
|
||||
template=dict(type="str"),
|
||||
datastore_id=dict(type="int"),
|
||||
wait_timeout=dict(type="int", default=60),
|
||||
)
|
||||
required_if = [
|
||||
['state', 'renamed', ['id']],
|
||||
['create', True, ['template', 'datastore_id', 'name']],
|
||||
["state", "renamed", ["id"]],
|
||||
["create", True, ["template", "datastore_id", "name"]],
|
||||
]
|
||||
mutually_exclusive = [
|
||||
['id', 'name'],
|
||||
["id", "name"],
|
||||
]
|
||||
|
||||
OpenNebulaModule.__init__(self,
|
||||
argument_spec,
|
||||
supports_check_mode=True,
|
||||
mutually_exclusive=mutually_exclusive,
|
||||
required_if=required_if)
|
||||
OpenNebulaModule.__init__(
|
||||
self,
|
||||
argument_spec,
|
||||
supports_check_mode=True,
|
||||
mutually_exclusive=mutually_exclusive,
|
||||
required_if=required_if,
|
||||
)
|
||||
|
||||
def run(self, one, module, result):
|
||||
params = module.params
|
||||
id = params.get('id')
|
||||
name = params.get('name')
|
||||
desired_state = params.get('state')
|
||||
enabled = params.get('enabled')
|
||||
new_name = params.get('new_name')
|
||||
persistent = params.get('persistent')
|
||||
create = params.get('create')
|
||||
template = params.get('template')
|
||||
datastore_id = params.get('datastore_id')
|
||||
wait_timeout = params.get('wait_timeout')
|
||||
id = params.get("id")
|
||||
name = params.get("name")
|
||||
desired_state = params.get("state")
|
||||
enabled = params.get("enabled")
|
||||
new_name = params.get("new_name")
|
||||
persistent = params.get("persistent")
|
||||
create = params.get("create")
|
||||
template = params.get("template")
|
||||
datastore_id = params.get("datastore_id")
|
||||
wait_timeout = params.get("wait_timeout")
|
||||
|
||||
self.result = {}
|
||||
|
||||
image = self.get_image_instance(id, name)
|
||||
if not image and desired_state != 'absent':
|
||||
if not image and desired_state != "absent":
|
||||
if create:
|
||||
self.result = self.create_image(name, template, datastore_id, wait_timeout)
|
||||
# Using 'if id:' doesn't work properly when id=0
|
||||
@@ -427,7 +441,7 @@ class ImageModule(OpenNebulaModule):
|
||||
elif name is not None:
|
||||
module.fail_json(msg=f"There is no image with name={name}")
|
||||
|
||||
if desired_state == 'absent':
|
||||
if desired_state == "absent":
|
||||
self.result = self.delete_image(image, wait_timeout)
|
||||
else:
|
||||
if persistent is not None:
|
||||
@@ -471,21 +485,22 @@ class ImageModule(OpenNebulaModule):
|
||||
image = self.get_image_by_id(image_id)
|
||||
result = self.get_image_info(image)
|
||||
|
||||
result['changed'] = True
|
||||
result["changed"] = True
|
||||
return result
|
||||
|
||||
def wait_for_ready(self, image_id, wait_timeout=60):
|
||||
import time
|
||||
|
||||
start_time = time.time()
|
||||
|
||||
while (time.time() - start_time) < wait_timeout:
|
||||
image = self.one.image.info(image_id)
|
||||
state = image.STATE
|
||||
|
||||
if state in [IMAGE_STATES.index('ERROR')]:
|
||||
if state in [IMAGE_STATES.index("ERROR")]:
|
||||
self.module.fail_json(msg=f"Got an ERROR state: {image.TEMPLATE['ERROR']}")
|
||||
|
||||
if state in [IMAGE_STATES.index('READY')]:
|
||||
if state in [IMAGE_STATES.index("READY")]:
|
||||
return True
|
||||
|
||||
time.sleep(1)
|
||||
@@ -493,6 +508,7 @@ class ImageModule(OpenNebulaModule):
|
||||
|
||||
def wait_for_delete(self, image_id, wait_timeout=60):
|
||||
import time
|
||||
|
||||
start_time = time.time()
|
||||
|
||||
while (time.time() - start_time) < wait_timeout:
|
||||
@@ -506,7 +522,7 @@ class ImageModule(OpenNebulaModule):
|
||||
|
||||
state = image.STATE
|
||||
|
||||
if state in [IMAGE_STATES.index('DELETE')]:
|
||||
if state in [IMAGE_STATES.index("DELETE")]:
|
||||
return True
|
||||
|
||||
time.sleep(1)
|
||||
@@ -519,21 +535,22 @@ class ImageModule(OpenNebulaModule):
|
||||
|
||||
state = image.STATE
|
||||
|
||||
if state not in [IMAGE_STATES.index('READY'), IMAGE_STATES.index('DISABLED'), IMAGE_STATES.index('ERROR')]:
|
||||
if state not in [IMAGE_STATES.index("READY"), IMAGE_STATES.index("DISABLED"), IMAGE_STATES.index("ERROR")]:
|
||||
if enable:
|
||||
self.module.fail_json(msg=f"Cannot enable {IMAGE_STATES[state]} image!")
|
||||
else:
|
||||
self.module.fail_json(msg=f"Cannot disable {IMAGE_STATES[state]} image!")
|
||||
|
||||
if ((enable and state != IMAGE_STATES.index('READY')) or
|
||||
(not enable and state != IMAGE_STATES.index('DISABLED'))):
|
||||
if (enable and state != IMAGE_STATES.index("READY")) or (
|
||||
not enable and state != IMAGE_STATES.index("DISABLED")
|
||||
):
|
||||
changed = True
|
||||
|
||||
if changed and not self.module.check_mode:
|
||||
self.one.image.enable(image.ID, enable)
|
||||
|
||||
result = self.get_image_info(image)
|
||||
result['changed'] = changed
|
||||
result["changed"] = changed
|
||||
|
||||
return result
|
||||
|
||||
@@ -543,21 +560,22 @@ class ImageModule(OpenNebulaModule):
|
||||
|
||||
state = image.STATE
|
||||
|
||||
if state not in [IMAGE_STATES.index('READY'), IMAGE_STATES.index('DISABLED'), IMAGE_STATES.index('ERROR')]:
|
||||
if state not in [IMAGE_STATES.index("READY"), IMAGE_STATES.index("DISABLED"), IMAGE_STATES.index("ERROR")]:
|
||||
if enable:
|
||||
self.module.fail_json(msg=f"Cannot enable persistence for {IMAGE_STATES[state]} image!")
|
||||
else:
|
||||
self.module.fail_json(msg=f"Cannot disable persistence for {IMAGE_STATES[state]} image!")
|
||||
|
||||
if ((enable and state != IMAGE_STATES.index('READY')) or
|
||||
(not enable and state != IMAGE_STATES.index('DISABLED'))):
|
||||
if (enable and state != IMAGE_STATES.index("READY")) or (
|
||||
not enable and state != IMAGE_STATES.index("DISABLED")
|
||||
):
|
||||
changed = True
|
||||
|
||||
if changed and not self.module.check_mode:
|
||||
self.one.image.persistent(image.ID, enable)
|
||||
|
||||
result = self.get_image_info(image)
|
||||
result['changed'] = changed
|
||||
result["changed"] = changed
|
||||
|
||||
return result
|
||||
|
||||
@@ -568,10 +586,10 @@ class ImageModule(OpenNebulaModule):
|
||||
tmp_image = self.get_image_by_name(new_name)
|
||||
if tmp_image:
|
||||
result = self.get_image_info(image)
|
||||
result['changed'] = False
|
||||
result["changed"] = False
|
||||
return result
|
||||
|
||||
if image.STATE == IMAGE_STATES.index('DISABLED'):
|
||||
if image.STATE == IMAGE_STATES.index("DISABLED"):
|
||||
self.module.fail_json(msg="Cannot clone DISABLED image")
|
||||
|
||||
if not self.module.check_mode:
|
||||
@@ -580,7 +598,7 @@ class ImageModule(OpenNebulaModule):
|
||||
image = self.one.image.info(new_id)
|
||||
|
||||
result = self.get_image_info(image)
|
||||
result['changed'] = True
|
||||
result["changed"] = True
|
||||
|
||||
return result
|
||||
|
||||
@@ -590,7 +608,7 @@ class ImageModule(OpenNebulaModule):
|
||||
|
||||
if new_name == image.NAME:
|
||||
result = self.get_image_info(image)
|
||||
result['changed'] = False
|
||||
result["changed"] = False
|
||||
return result
|
||||
|
||||
tmp_image = self.get_image_by_name(new_name)
|
||||
@@ -601,12 +619,12 @@ class ImageModule(OpenNebulaModule):
|
||||
self.one.image.rename(image.ID, new_name)
|
||||
|
||||
result = self.get_image_info(image)
|
||||
result['changed'] = True
|
||||
result["changed"] = True
|
||||
return result
|
||||
|
||||
def delete_image(self, image, wait_timeout):
|
||||
if not image:
|
||||
return {'changed': False}
|
||||
return {"changed": False}
|
||||
|
||||
if image.RUNNING_VMS > 0:
|
||||
self.module.fail_json(msg=f"Cannot delete image. There are {image.RUNNING_VMS!s} VMs using it.")
|
||||
@@ -615,12 +633,12 @@ class ImageModule(OpenNebulaModule):
|
||||
self.one.image.delete(image.ID)
|
||||
self.wait_for_delete(image.ID, wait_timeout)
|
||||
|
||||
return {'changed': True}
|
||||
return {"changed": True}
|
||||
|
||||
|
||||
def main():
|
||||
ImageModule().run_module()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user