Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PR #8889/fea0ffa5 backport][stable-9] one_image/one_image_info: refactor #8984

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changelogs/fragments/8889-refactor-one-image-modules.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
minor_changes:
- one_image - add option ``persistent`` to manage image persistence (https://github.com/ansible-collections/community.general/issues/3578, https://github.com/ansible-collections/community.general/pull/8889).
- one_image - refactor code to make it more similar to ``one_template`` and ``one_vnet`` (https://github.com/ansible-collections/community.general/pull/8889).
- one_image_info - refactor code to make it more similar to ``one_template`` and ``one_vnet`` (https://github.com/ansible-collections/community.general/pull/8889).
- one_image - extend xsd scheme to make it return a lot more info about image (https://github.com/ansible-collections/community.general/pull/8889).
- one_image_info - extend xsd scheme to make it return a lot more info about image (https://github.com/ansible-collections/community.general/pull/8889).
88 changes: 88 additions & 0 deletions plugins/module_utils/opennebula.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from ansible.module_utils.basic import AnsibleModule


IMAGE_STATES = ['INIT', 'READY', 'USED', 'DISABLED', 'LOCKED', 'ERROR', 'CLONE', 'DELETE', 'USED_PERS', 'LOCKED_USED', 'LOCKED_USED_PERS']
HAS_PYONE = True

try:
Expand Down Expand Up @@ -347,3 +348,90 @@ def run(self, one, module, result):
result: the Ansible result
"""
raise NotImplementedError("Method requires implementation")

def get_image_list_id(self, image, element):
"""
This is a helper function for get_image_info to iterate over a simple list of objects
"""
list_of_id = []

if element == 'VMS':
image_list = image.VMS
if element == 'CLONES':
image_list = image.CLONES
if element == 'APP_CLONES':
image_list = image.APP_CLONES

for iter in image_list.ID:
list_of_id.append(
# These are optional so firstly check for presence
getattr(iter, 'ID', 'Null'),
)
return list_of_id

def get_image_snapshots_list(self, image):
"""
This is a helper function for get_image_info to iterate over a dictionary
"""
list_of_snapshots = []

for iter in image.SNAPSHOTS.SNAPSHOT:
list_of_snapshots.append({
'date': iter['DATE'],
'parent': iter['PARENT'],
'size': iter['SIZE'],
# These are optional so firstly check for presence
'allow_orhans': getattr(image.SNAPSHOTS, 'ALLOW_ORPHANS', 'Null'),
'children': getattr(iter, 'CHILDREN', 'Null'),
'active': getattr(iter, 'ACTIVE', 'Null'),
'name': getattr(iter, 'NAME', 'Null'),
})
return list_of_snapshots

def get_image_info(self, image):
"""
This method is used by one_image and one_image_info modules to retrieve
information from XSD scheme of an image
Returns: a copy of the parameters that includes the resolved parameters.
"""
info = {
'id': image.ID,
'name': image.NAME,
'state': IMAGE_STATES[image.STATE],
'running_vms': image.RUNNING_VMS,
'used': bool(image.RUNNING_VMS),
'user_name': image.UNAME,
'user_id': image.UID,
'group_name': image.GNAME,
'group_id': image.GID,
'permissions': {
'owner_u': image.PERMISSIONS.OWNER_U,
'owner_m': image.PERMISSIONS.OWNER_M,
'owner_a': image.PERMISSIONS.OWNER_A,
'group_u': image.PERMISSIONS.GROUP_U,
'group_m': image.PERMISSIONS.GROUP_M,
'group_a': image.PERMISSIONS.GROUP_A,
'other_u': image.PERMISSIONS.OTHER_U,
'other_m': image.PERMISSIONS.OTHER_M,
'other_a': image.PERMISSIONS.OTHER_A
},
'type': image.TYPE,
'disk_type': image.DISK_TYPE,
'persistent': image.PERSISTENT,
'regtime': image.REGTIME,
'source': image.SOURCE,
'path': image.PATH,
'fstype': getattr(image, 'FSTYPE', 'Null'),
'size': image.SIZE,
'cloning_ops': image.CLONING_OPS,
'cloning_id': image.CLONING_ID,
'target_snapshot': image.TARGET_SNAPSHOT,
'datastore_id': image.DATASTORE_ID,
'datastore': image.DATASTORE,
'vms': self.get_image_list_id(image, 'VMS'),
'clones': self.get_image_list_id(image, 'CLONES'),
'app_clones': self.get_image_list_id(image, 'APP_CLONES'),
'snapshots': self.get_image_snapshots_list(image),
'template': image.TEMPLATE,
}
return info
Loading