in src/ansible_collections/alibaba/apsarastack/plugins/modules/ali_image.py [0:0]
def main():
argument_spec = common_argument_spec()
argument_spec.update(dict(
image_id=dict(type='str'),
snapshot_id=dict(type='str', aliases=['snapshot']),
description=dict(type='str'),
image_name=dict(type='str', aliases=['name']),
image_version=dict(type='str', aliases=['version']),
disk_mapping=dict(type='list', elements='dict'),
instance_id=dict(aliases=['instance']),
state=dict(default='present', choices=['present', 'absent'], type='str'),
wait=dict(default=False, type='bool'),
wait_timeout=dict(type='int', default=300)
))
module = AnsibleModule(argument_spec=argument_spec)
if HAS_FOOTMARK is False:
module.fail_json(msg='footmark required for the module apsarastack_security_group.')
ecs = ecs_connect(module)
state = module.params['state']
image_id = module.params['image_id']
image_name = module.params['image_name']
changed = False
current_image = None
try:
if image_id:
images = describe_image(module, image_id=image_id)
if images and len(images) == 1:
current_image = images[0]
elif image_name and state == 'absent':
images = describe_image(module, image_name=image_name)
if images:
if len(images) == 1:
current_image = images[0]
else:
images_ids = []
for i in images:
images_ids.append(i.id)
module.fail_json(msg="There is too many images match name '{0}', "
"please use image_id or a new image_name to specify a unique image."
"Matched images ids are: {1}".format(image_name, images_ids))
elif state == 'absent':
images = describe_image(module)
if images and len(images) > 0:
current_image = images[0]
except ECSResponseError as e:
module.fail_json(msg='Error in get_all_images: %s' % str(e))
if state == 'absent':
if not current_image:
module.fail_json(msg="Please use valid image_id or image_name to specify one image for deleting.")
try:
changed_img = current_image.delete()
module.exit_json(changed=changed_img, image_id=current_image.id, image=get_image_detail(current_image))
except Exception as e:
module.fail_json(msg='Deleting a image is failed, error: {0}'.format(e))
if not current_image:
snapshot_id = module.params['snapshot_id']
image_version = module.params['image_version']
description = module.params['description']
disk_mapping = module.params['disk_mapping']
instance_id = module.params['instance_id']
wait = module.params['wait']
wait_timeout = module.params['wait_timeout']
try:
# Calling create_image method
client_token = "Ansible-Apsarastack-%s-%s" % (hash(str(module.params)), str(time.time()))
changed, image_id, results = create_image(module=module, ecs=ecs, snapshot_id=snapshot_id,
image_name=image_name, image_version=image_version,
description=description, instance_id=instance_id,
disk_mapping=disk_mapping, client_token=client_token,
wait=wait, wait_timeout=wait_timeout)
if results:
if len(results) == 1:
current_image = results[0]
module.exit_json(changed=changed, image_id=image_id, image=get_image_detail(current_image) )
except Exception as e:
module.fail_json(msg='Creating a new image is failed, error: {0}'.format(e))