Merge "Migrating image module from AnsibleModule to OpenStackModule"

This commit is contained in:
Zuul 2021-04-29 12:09:05 +00:00 committed by Gerrit Code Review
commit 464d7767bb
1 changed files with 58 additions and 58 deletions

View File

@ -147,95 +147,95 @@ EXAMPLES = '''
name: myvol_image name: myvol_image
''' '''
from ansible.module_utils.basic import AnsibleModule from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule
from ansible_collections.openstack.cloud.plugins.module_utils.openstack import (openstack_full_argument_spec,
openstack_module_kwargs,
openstack_cloud_from_module)
def main(): class ImageModule(OpenStackModule):
argument_spec = openstack_full_argument_spec( deprecated_names = ('os_image', 'openstack.cloud.os_image')
name=dict(required=True),
id=dict(default=None), argument_spec = dict(
checksum=dict(default=None), name=dict(required=True, type='str'),
disk_format=dict(default='qcow2', choices=['ami', 'ari', 'aki', 'vhd', 'vmdk', 'raw', 'qcow2', 'vdi', 'iso', 'vhdx', 'ploop']), id=dict(type='str'),
checksum=dict(type='str'),
disk_format=dict(default='qcow2',
choices=['ami', 'ari', 'aki', 'vhd', 'vmdk', 'raw', 'qcow2', 'vdi', 'iso', 'vhdx', 'ploop']),
container_format=dict(default='bare', choices=['ami', 'aki', 'ari', 'bare', 'ovf', 'ova', 'docker']), container_format=dict(default='bare', choices=['ami', 'aki', 'ari', 'bare', 'ovf', 'ova', 'docker']),
owner=dict(default=None), owner=dict(type='str'),
min_disk=dict(type='int', default=0), min_disk=dict(type='int', default=0),
min_ram=dict(type='int', default=0), min_ram=dict(type='int', default=0),
is_public=dict(type='bool', default=False), is_public=dict(type='bool', default=False),
protected=dict(type='bool', default=False), protected=dict(type='bool', default=False),
filename=dict(default=None), filename=dict(type='str'),
ramdisk=dict(default=None), ramdisk=dict(type='str'),
kernel=dict(default=None), kernel=dict(type='str'),
properties=dict(type='dict', default={}), properties=dict(type='dict', default={}),
volume=dict(default=None), volume=dict(type='str'),
state=dict(default='present', choices=['absent', 'present']), state=dict(default='present', choices=['absent', 'present']),
) )
module_kwargs = openstack_module_kwargs( module_kwargs = dict(
mutually_exclusive=[['filename', 'volume']], mutually_exclusive=[['filename', 'volume']],
) )
module = AnsibleModule(argument_spec, **module_kwargs)
sdk, cloud = openstack_cloud_from_module(module) def run(self):
try:
changed = False changed = False
if module.params['id']: if self.params['id']:
image = cloud.get_image(name_or_id=module.params['id']) image = self.conn.get_image(name_or_id=self.params['id'])
elif module.params['checksum']: elif self.params['checksum']:
image = cloud.get_image(name_or_id=module.params['name'], filters={'checksum': module.params['checksum']}) image = self.conn.get_image(name_or_id=self.params['name'], filters={'checksum': self.params['checksum']})
else: else:
image = cloud.get_image(name_or_id=module.params['name']) image = self.conn.get_image(name_or_id=self.params['name'])
if module.params['state'] == 'present': if self.params['state'] == 'present':
if not image: if not image:
kwargs = {} kwargs = {}
if module.params['id'] is not None: if self.params['id'] is not None:
kwargs['id'] = module.params['id'] kwargs['id'] = self.params['id']
image = cloud.create_image( image = self.conn.create_image(
name=module.params['name'], name=self.params['name'],
filename=module.params['filename'], filename=self.params['filename'],
disk_format=module.params['disk_format'], disk_format=self.params['disk_format'],
container_format=module.params['container_format'], container_format=self.params['container_format'],
wait=module.params['wait'], wait=self.params['wait'],
timeout=module.params['timeout'], timeout=self.params['timeout'],
is_public=module.params['is_public'], is_public=self.params['is_public'],
protected=module.params['protected'], protected=self.params['protected'],
min_disk=module.params['min_disk'], min_disk=self.params['min_disk'],
min_ram=module.params['min_ram'], min_ram=self.params['min_ram'],
volume=module.params['volume'], volume=self.params['volume'],
**kwargs **kwargs
) )
changed = True changed = True
if not module.params['wait']: if not self.params['wait']:
module.exit_json(changed=changed, image=image, id=image.id) self.exit(changed=changed, image=image, id=image.id)
cloud.update_image_properties( self.conn.update_image_properties(
image=image, image=image,
kernel=module.params['kernel'], kernel=self.params['kernel'],
ramdisk=module.params['ramdisk'], ramdisk=self.params['ramdisk'],
protected=module.params['protected'], protected=self.params['protected'],
**module.params['properties']) **self.params['properties'])
image = cloud.get_image(name_or_id=image.id) image = self.conn.get_image(name_or_id=image.id)
module.exit_json(changed=changed, image=image, id=image.id) self.exit(changed=changed, image=image, id=image.id)
elif module.params['state'] == 'absent': elif self.params['state'] == 'absent':
if not image: if not image:
changed = False changed = False
else: else:
cloud.delete_image( self.conn.delete_image(
name_or_id=module.params['name'], name_or_id=self.params['name'],
wait=module.params['wait'], wait=self.params['wait'],
timeout=module.params['timeout']) timeout=self.params['timeout'])
changed = True changed = True
module.exit_json(changed=changed) self.exit(changed=changed)
except sdk.exceptions.OpenStackCloudException as e:
module.fail_json(msg=str(e), extra_data=e.extra_data)
if __name__ == "__main__": def main():
module = ImageModule()
module()
if __name__ == '__main__':
main() main()