Refactored image{,_info} modules

Code of the image module has been refactored partially only.
It will have to be completed in a follow up patch.

Change-Id: Id2360163117db9639a0af502ab44e02cae5cebaa
This commit is contained in:
Jakob Meng
2023-01-13 14:29:17 +01:00
parent 1f8c9fee05
commit cc9ca5a34b
6 changed files with 737 additions and 978 deletions

View File

@@ -5,135 +5,134 @@
# Copyright (c) 2013, Benno Joy <benno@ansible.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
DOCUMENTATION = '''
---
DOCUMENTATION = r'''
module: image
short_description: Add/Delete images from OpenStack Cloud
short_description: Manage images of OpenStack image (Glance) service.
author: OpenStack Ansible SIG
description:
- Add or Remove images from the OpenStack Image Repository
- Create or delete images in OpenStack image (Glance) service.
options:
name:
description:
- The name of the image when uploading - or the name/ID of the image if deleting
- If provided with the id, it can be used to change the name of existing image
required: true
type: str
id:
description:
- The ID of the image when uploading an image
- This image attribute cannot be changed.
type: str
checksum:
description:
- The checksum of the image
type: str
disk_format:
description:
- The format of the disk that is getting uploaded
- This image attribute cannot be changed.
default: qcow2
choices: ['ami', 'ari', 'aki', 'vhd', 'vmdk', 'raw', 'qcow2', 'vdi', 'iso', 'vhdx', 'ploop']
type: str
container_format:
description:
- The format of the container
- This image attribute cannot be changed.
default: bare
choices: ['ami', 'aki', 'ari', 'bare', 'ovf', 'ova', 'docker']
type: str
owner:
description:
- The name or ID of the project owning the image
type: str
aliases: ['project']
owner_domain:
description:
- The name or id of the domain the project owning the image belongs to
- May be used to identify a unique project when providing a name to the project argument and multiple projects with such name exist
type: str
aliases: ['project_domain']
min_disk:
description:
- The minimum disk space (in GB) required to boot this image
type: int
min_ram:
description:
- The minimum ram (in MB) required to boot this image
type: int
is_public:
description:
- Whether the image can be accessed publicly.
Note that publicizing an image requires admin role by default.
- Use I(visibility) instead of I(is_public),
the latter has been deprecated.
type: bool
default: false
is_protected:
description:
- Prevent image from being deleted
aliases: ['protected']
type: bool
filename:
description:
- The path to the file which has to be uploaded
- This image attribute cannot be changed.
type: str
ramdisk:
description:
- The name of an existing ramdisk image that will be associated with this image
type: str
kernel:
description:
- The name of an existing kernel image that will be associated with this image
type: str
properties:
description:
- Additional properties to be associated with this image
default: {}
type: dict
state:
description:
- Should the resource be present or absent.
choices: [present, absent]
default: present
type: str
tags:
description:
- List of tags to be applied to the image
default: []
type: list
elements: str
visibility:
description:
- The image visibility
type: str
choices: [public, private, shared, community]
volume:
description:
- ID of a volume to create an image from.
- The volume must be in AVAILABLE state.
- Switch to module M(openstack.cloud.volume) instead of using I(volume),
the latter has been deprecated.
type: str
checksum:
description:
- The checksum of the image.
type: str
container_format:
description:
- The format of the container.
- This image attribute cannot be changed.
- Examples are C(ami), C(aki), C(ari), C(bare), C(ovf), C(ova) or
C(docker).
default: bare
type: str
disk_format:
description:
- The format of the disk that is getting uploaded.
- This image attribute cannot be changed.
- Examples are C(ami), C(ari), C(aki), C(vhd), C(vmdk), C(raw),
C(qcow2), C(vdi), c(iso), C(vhdx) or C(ploop).
default: qcow2
type: str
filename:
description:
- The path to the file which has to be uploaded.
- This image attribute cannot be changed.
type: str
id:
description:
- The ID of the image when uploading an image.
- This image attribute cannot be changed.
type: str
is_protected:
description:
- Prevent image from being deleted.
aliases: ['protected']
type: bool
is_public:
description:
- Whether the image can be accessed publicly.
- Setting I(is_public) to C(true) requires admin role by default.
- I(is_public) has been deprecated. Use I(visibility) instead of
I(is_public).
type: bool
default: false
kernel:
description:
- The name of an existing kernel image that will be associated with this
image.
type: str
min_disk:
description:
- The minimum disk space (in GB) required to boot this image.
type: int
min_ram:
description:
- The minimum ram (in MB) required to boot this image.
type: int
name:
description:
- The name of the image when uploading - or the name/ID of the image if
deleting.
- If provided with the id, it can be used to change the name of existing
image.
required: true
type: str
owner:
description:
- The name or ID of the project owning the image.
type: str
aliases: ['project']
owner_domain:
description:
- The name or id of the domain the project owning the image belongs to.
- May be used to identify a unique project when providing a name to the
project argument and multiple projects with such name exist.
type: str
aliases: ['project_domain']
properties:
description:
- Additional properties to be associated with this image.
default: {}
type: dict
ramdisk:
description:
- The name of an existing ramdisk image that will be associated with this
image.
type: str
state:
description:
- Should the resource be present or absent.
choices: [present, absent]
default: present
type: str
tags:
description:
- List of tags to be applied to the image.
default: []
type: list
elements: str
visibility:
description:
- The image visibility.
type: str
choices: [public, private, shared, community]
volume:
description:
- ID of a volume to create an image from.
- The volume must be in AVAILABLE state.
- I(volume) has been deprecated. Use module M(openstack.cloud.volume)
instead.
type: str
requirements:
- "python >= 3.6"
- "openstacksdk"
- "python >= 3.6"
- "openstacksdk"
extends_documentation_fragment:
- openstack.cloud.openstack
- openstack.cloud.openstack
'''
EXAMPLES = '''
# Upload an image from a local file named cirros-0.3.0-x86_64-disk.img
- openstack.cloud.image:
auth:
auth_url: https://identity.example.com
username: admin
password: passme
project_name: admin
openstack.cloud.identity_user_domain_name: Default
openstack.cloud.project_domain_name: Default
EXAMPLES = r'''
- name: Upload an image from a local file named cirros-0.3.0-x86_64-disk.img
openstack.cloud.image:
cloud: devstack-admin
name: cirros
container_format: bare
disk_format: qcow2
@@ -148,303 +147,226 @@ EXAMPLES = '''
distro: ubuntu
'''
RETURN = '''
id:
description: ID of the image.
returned: On success when I(state) is 'present'.
type: str
RETURN = r'''
image:
description: Dictionary describing the image.
description: Dictionary describing the Glance image.
returned: On success when I(state) is C(present).
type: dict
returned: On success when I(state) is 'present'.
contains:
id:
description: Unique UUID.
returned: success
type: str
name:
description: Name given to the image.
returned: success
type: str
status:
description: Image status.
returned: success
type: str
architecture:
description: |
The CPU architecture that must be supported by the hypervisor.
returned: success
description: The CPU architecture that must be supported by
the hypervisor.
type: str
created_at:
description: Image created at timestamp.
returned: success
type: str
container_format:
description: Container format of the image.
returned: success
type: str
direct_url:
description: URL to access the image file kept in external store.
returned: success
type: str
min_ram:
description: Min amount of RAM required for this image.
returned: success
type: int
disk_format:
description: Disk format of the image.
returned: success
type: str
file:
description: The URL for the virtual machine image file.
returned: success
type: str
has_auto_disk_config:
description: >
If root partition on disk is automatically resized before the instance
boots.
returned: success
description: If root partition on disk is automatically resized
before the instance boots.
type: bool
hash_algo:
description: |
The algorithm used to compute a secure hash of the image data.
returned: success
description: The algorithm used to compute a secure hash of the
image data.
type: str
hash_value:
description: >
The hexdigest of the secure hash of the image data computed using the
algorithm whose name is the value of the os_hash_algo property.
returned: success
description: The hexdigest of the secure hash of the image data
computed using the algorithm whose name is the value of the
os_hash_algo property.
type: str
hw_cpu_cores:
description: >
Used to pin the virtual CPUs (vCPUs) of instances to the host's physical
CPU cores (pCPUs).
returned: success
description: Used to pin the virtual CPUs (vCPUs) of instances to
the host's physical CPU cores (pCPUs).
type: str
hw_cpu_policy:
description: The hexdigest of the secure hash of the image data.
returned: success
type: str
hw_cpu_sockets:
description: Preferred number of sockets to expose to the guest.
returned: success
type: str
hw_cpu_thread_policy:
description: >
Defines how hardware CPU threads in a simultaneous multithreading-based
(SMT) architecture be used.
returned: success
description: Defines how hardware CPU threads in a simultaneous
multithreading-based (SMT) architecture be used.
type: str
hw_cpu_threads:
description: |
The preferred number of threads to expose to the guest.
returned: success
description: The preferred number of threads to expose to the guest.
type: str
hw_disk_bus:
description: |
Specifies the type of disk controller to attach disk devices to.
returned: success
description: Specifies the type of disk controller to attach disk
devices to.
type: str
hw_machine_type:
description: |
Enables booting an ARM system using the specified machine type.
returned: success
description: Enables booting an ARM system using the
specified machine type.
type: str
hw_qemu_guest_agent:
description: >
A string boolean, which if "true", QEMU guest agent will be exposed to
the instance.
returned: success
description: "A string boolean, which if 'true', QEMU guest agent
will be exposed to the instance."
type: str
hw_rng_model:
description: Adds a random-number generator device to the image's instances.
returned: success
description: "Adds a random-number generator device to the image's
instances."
type: str
hw_scsi_model:
description: >
Enables the use of VirtIO SCSI (virtio-scsi) to provide block device
access for compute instances.
returned: success
description: Enables the use of VirtIO SCSI (virtio-scsi) to
provide block device access for compute instances.
type: str
hw_video_model:
description: The video image driver used.
returned: success
type: str
hw_video_ram:
description: Maximum RAM for the video image.
returned: success
type: str
hw_vif_model:
description: Specifies the model of virtual network interface device to use.
returned: success
description: Specifies the model of virtual network interface device to
use.
type: str
hw_watchdog_action:
description: >
Enables a virtual hardware watchdog device that carries out the
specified action if the server hangs.
returned: success
description: Enables a virtual hardware watchdog device that
carries out the specified action if the server hangs.
type: str
hypervisor_type:
description: The hypervisor type.
returned: success
type: str
instance_type_rxtx_factor:
description: >
Optional property allows created servers to have a different bandwidth
cap than that defined in the network they are attached to.
returned: success
description: Optional property allows created servers to have a
different bandwidth cap than that defined in the network
they are attached to.
type: str
instance_uuid:
description: >
For snapshot images, this is the UUID of the server used to create this
image.
returned: success
description: For snapshot images, this is the UUID of the server
used to create this image.
type: str
is_hidden:
description: >-
Controls whether an image is displayed in the default image-list
response
returned: success
description: Controls whether an image is displayed in the default
image-list response
type: bool
is_hw_boot_menu_enabled:
description: Enables the BIOS bootmenu.
returned: success
type: bool
is_hw_vif_multiqueue_enabled:
description: |
Enables the virtio-net multiqueue feature.
returned: success
description: Enables the virtio-net multiqueue feature.
type: bool
kernel_id:
description: >
The ID of an image stored in the Image service that should be used as
the kernel when booting an AMI-style image.
returned: success
description: The ID of an image stored in the Image service that
should be used as the kernel when booting an AMI-style
image.
type: str
locations:
description: A list of URLs to access the image file in external store.
returned: success
type: str
metadata:
description: The location metadata.
returned: success
type: str
needs_config_drive:
description: Specifies whether the image needs a config drive.
returned: success
type: bool
needs_secure_boot:
description: Whether Secure Boot is needed.
returned: success
type: bool
os_admin_user:
description: The operating system admin username.
returned: success
type: str
os_command_line:
description: The kernel command line to be used by libvirt driver.
returned: success
type: str
os_distro:
description: |
The common name of the operating system distribution in lowercase.
returned: success
description: The common name of the operating system distribution
in lowercase.
type: str
os_require_quiesce:
description: |
If true, require quiesce on snapshot via QEMU guest agent.
returned: success
description: If true, require quiesce on snapshot via
QEMU guest agent.
type: str
os_shutdown_timeout:
description: Time for graceful shutdown.
returned: success
type: str
os_type:
description: The operating system installed on the image.
returned: success
type: str
os_version:
description: |
The operating system version as specified by the distributor.
returned: success
description: The operating system version as specified by
the distributor.
type: str
owner_id:
description: 'The ID of the owner, or project, of the image.'
returned: success
description: The ID of the owner, or project, of the image.
type: str
ramdisk_id:
description: >
The ID of image stored in the Image service that should be used as the
ramdisk when booting an AMI-style image.
returned: success
description: The ID of image stored in the Image service that should
be used as the ramdisk when booting an AMI-style image.
type: str
schema:
description: URL for the schema describing a virtual machine image.
returned: success
type: str
store:
description: >
Glance will attempt to store the disk image data in the backing store
indicated by the value of the header.
returned: success
description: Glance will attempt to store the disk image data in the
backing store indicated by the value of the header.
type: str
updated_at:
description: Image updated at timestamp.
returned: success
type: str
url:
description: URL to access the image file kept in external store.
returned: success
type: str
virtual_size:
description: The virtual size of the image.
returned: success
type: str
vm_mode:
description: The virtual machine mode.
returned: success
type: str
vmware_adaptertype:
description: |
The virtual SCSI or IDE controller used by the hypervisor.
returned: success
description: The virtual SCSI or IDE controller used by the
hypervisor.
type: str
vmware_ostype:
description: Operating system installed in the image.
returned: success
type: str
filters:
description: Additional properties associated with the image.
returned: success
type: dict
min_disk:
description: Min amount of disk space required for this image.
returned: success
type: int
is_protected:
description: Image protected flag.
returned: success
type: bool
checksum:
description: Checksum for the image.
returned: success
type: str
owner:
description: Owner for the image.
returned: success
type: str
visibility:
description: Indicates who has access to the image.
returned: success
type: str
size:
description: Size of the image.
returned: success
type: int
tags:
description: List of tags assigned to the image
returned: success
type: list
'''
@@ -454,26 +376,25 @@ from ansible_collections.openstack.cloud.plugins.module_utils.openstack import O
class ImageModule(OpenStackModule):
argument_spec = dict(
name=dict(required=True),
id=dict(),
checksum=dict(),
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']),
owner=dict(aliases=['project']),
owner_domain=dict(aliases=['project_domain']),
container_format=dict(default='bare'),
disk_format=dict(default='qcow2'),
filename=dict(),
id=dict(),
is_protected=dict(type='bool', aliases=['protected']),
is_public=dict(type='bool', default=False),
kernel=dict(),
min_disk=dict(type='int'),
min_ram=dict(type='int'),
is_public=dict(type='bool', default=False),
is_protected=dict(type='bool', aliases=['protected']),
filename=dict(),
ramdisk=dict(),
kernel=dict(),
name=dict(required=True),
owner=dict(aliases=['project']),
owner_domain=dict(aliases=['project_domain']),
properties=dict(type='dict', default={}),
volume=dict(),
tags=dict(type='list', default=[], elements='str'),
ramdisk=dict(),
state=dict(default='present', choices=['absent', 'present']),
tags=dict(type='list', default=[], elements='str'),
visibility=dict(choices=['public', 'private', 'shared', 'community']),
volume=dict(),
)
module_kwargs = dict(
@@ -574,15 +495,15 @@ class ImageModule(OpenStackModule):
if self.params['state'] == 'present':
attrs = self._build_params(owner)
if not image:
# self.conn.image.create_image cannot be used because
# self.conn.create_image provides a volume parameter
# Ref.: https://opendev.org/openstack/openstacksdk/src/commit/a41d04ea197439c2f134ce3554995693933a46ac/openstack/cloud/_image.py#L306
# self.conn.image.create_image() cannot be used because it does
# not provide self.conn.create_image()'s volume parameter [0].
# [0] https://opendev.org/openstack/openstacksdk/src/commit/
# a41d04ea197439c2f134ce3554995693933a46ac/openstack/cloud/_image.py#L306
image = self.conn.create_image(**attrs)
changed = True
if not self.params['wait']:
self.exit_json(changed=changed,
image=self._return_value(image.id),
id=image.id)
image=self._return_value(image.id))
update_payload = self._build_update(image)
@@ -590,8 +511,7 @@ class ImageModule(OpenStackModule):
self.conn.image.update_image(image.id, **update_payload)
changed = True
self.exit_json(changed=changed, image=self._return_value(image.id),
id=image.id)
self.exit_json(changed=changed, image=self._return_value(image.id))
elif self.params['state'] == 'absent' and image is not None:
# self.conn.image.delete_image() does not offer a wait parameter