[PR #9547/c823e37d backport][stable-10] add filter support for one_template module (#9575)

add filter support for one_template module (#9547)

apply suggestion from code review
add one_template filter changelog fragment
rewrote filter flag to use string instead of int
renamed flag to option in changelog
added PR link to changelog fragment

Co-authored-by: Felix Fontein <felix@fontein.de>
Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
(cherry picked from commit c823e37d00)

Co-authored-by: Simon <simon.kropf@protonmail.com>
pull/9581/head
patchback[bot] 2025-01-15 20:56:55 +01:00 committed by GitHub
parent 6260d5f873
commit 1bc052ae6b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 17 deletions

View File

@ -0,0 +1,2 @@
minor_changes:
- one_template - adds ``filter`` option for retrieving templates which are not owned by the user (https://github.com/ansible-collections/community.general/pull/9547, https://github.com/ansible-collections/community.general/issues/9278).

View File

@ -51,6 +51,16 @@ options:
choices: ["present", "absent"] choices: ["present", "absent"]
default: present default: present
type: str type: str
filter:
description:
- V(user_primary_group) - Resources belonging to the user's primary group.
- V(user) - Resources belonging to the user.
- V(all) - All resources.
- V(user_groups) - Resources belonging to the user and any of his groups.
choices: [user_primary_group, user, all, user_groups]
default: user
type: str
version_added: 10.3.0
extends_documentation_fragment: extends_documentation_fragment:
- community.general.opennebula - community.general.opennebula
@ -157,6 +167,7 @@ class TemplateModule(OpenNebulaModule):
name=dict(type='str', required=False), name=dict(type='str', required=False),
state=dict(type='str', choices=['present', 'absent'], default='present'), state=dict(type='str', choices=['present', 'absent'], default='present'),
template=dict(type='str', required=False), template=dict(type='str', required=False),
filter=dict(type='str', required=False, choices=['user_primary_group', 'user', 'all', 'user_groups'], default='user'),
) )
mutually_exclusive = [ mutually_exclusive = [
@ -182,10 +193,11 @@ class TemplateModule(OpenNebulaModule):
name = params.get('name') name = params.get('name')
desired_state = params.get('state') desired_state = params.get('state')
template_data = params.get('template') template_data = params.get('template')
filter = params.get('filter')
self.result = {} self.result = {}
template = self.get_template_instance(id, name) template = self.get_template_instance(id, name, filter)
needs_creation = False needs_creation = False
if not template and desired_state != 'absent': if not template and desired_state != 'absent':
if id: if id:
@ -197,16 +209,19 @@ class TemplateModule(OpenNebulaModule):
self.result = self.delete_template(template) self.result = self.delete_template(template)
else: else:
if needs_creation: if needs_creation:
self.result = self.create_template(name, template_data) self.result = self.create_template(name, template_data, filter)
else: else:
self.result = self.update_template(template, template_data) self.result = self.update_template(template, template_data, filter)
self.exit() self.exit()
def get_template(self, predicate): def get_template(self, predicate, filter):
# -3 means "Resources belonging to the user" # filter was included, for discussions see:
# Issue: https://github.com/ansible-collections/community.general/issues/9278
# PR: https://github.com/ansible-collections/community.general/pull/9547
# the other two parameters are used for pagination, -1 for both essentially means "return all" # the other two parameters are used for pagination, -1 for both essentially means "return all"
pool = self.one.templatepool.info(-3, -1, -1) filter_values = {'user_primary_group': -4, 'user': -3, 'all': -2, 'user_groups': -1}
pool = self.one.templatepool.info(filter_values[filter], -1, -1)
for template in pool.VMTEMPLATE: for template in pool.VMTEMPLATE:
if predicate(template): if predicate(template):
@ -214,17 +229,17 @@ class TemplateModule(OpenNebulaModule):
return None return None
def get_template_by_id(self, template_id): def get_template_by_id(self, template_id, filter):
return self.get_template(lambda template: (template.ID == template_id)) return self.get_template(lambda template: (template.ID == template_id), filter)
def get_template_by_name(self, name): def get_template_by_name(self, name, filter):
return self.get_template(lambda template: (template.NAME == name)) return self.get_template(lambda template: (template.NAME == name), filter)
def get_template_instance(self, requested_id, requested_name): def get_template_instance(self, requested_id, requested_name, filter):
if requested_id: if requested_id:
return self.get_template_by_id(requested_id) return self.get_template_by_id(requested_id, filter)
else: else:
return self.get_template_by_name(requested_name) return self.get_template_by_name(requested_name, filter)
def get_template_info(self, template): def get_template_info(self, template):
info = { info = {
@ -239,21 +254,21 @@ class TemplateModule(OpenNebulaModule):
return info return info
def create_template(self, name, template_data): def create_template(self, name, template_data, filter):
if not self.module.check_mode: if not self.module.check_mode:
self.one.template.allocate("NAME = \"" + name + "\"\n" + template_data) self.one.template.allocate("NAME = \"" + name + "\"\n" + template_data)
result = self.get_template_info(self.get_template_by_name(name)) result = self.get_template_info(self.get_template_by_name(name, filter))
result['changed'] = True result['changed'] = True
return result return result
def update_template(self, template, template_data): def update_template(self, template, template_data, filter):
if not self.module.check_mode: if not self.module.check_mode:
# 0 = replace the whole template # 0 = replace the whole template
self.one.template.update(template.ID, template_data, 0) self.one.template.update(template.ID, template_data, 0)
result = self.get_template_info(self.get_template_by_id(template.ID)) result = self.get_template_info(self.get_template_by_id(template.ID, filter))
if self.module.check_mode: if self.module.check_mode:
# Unfortunately it is not easy to detect if the template would have changed, therefore always report a change here. # Unfortunately it is not easy to detect if the template would have changed, therefore always report a change here.
result['changed'] = True result['changed'] = True