Skip to content

Commit

Permalink
add filter support for one_template module
Browse files Browse the repository at this point in the history
  • Loading branch information
skropf committed Jan 7, 2025
1 parent 3af793c commit 009cdc0
Showing 1 changed file with 28 additions and 17 deletions.
45 changes: 28 additions & 17 deletions plugins/modules/one_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@
choices: ["present", "absent"]
default: present
type: str
filter:
description:
- V(-4) - Resources belonging to the user's primary group
- V(-3) - Resources belonging to the user
- V(-2) - All resources
- V(-1) - Resources belonging to the user and any of his groups
choices: [-4, -3, -2, -1]
default: -3
type: int
extends_documentation_fragment:
- community.general.opennebula
Expand Down Expand Up @@ -156,6 +165,7 @@ def __init__(self):
name=dict(type='str', required=False),
state=dict(type='str', choices=['present', 'absent'], default='present'),
template=dict(type='str', required=False),
filter=dict(type='int', required=False, default=-3),
)

mutually_exclusive = [
Expand All @@ -181,10 +191,11 @@ def run(self, one, module, result):
name = params.get('name')
desired_state = params.get('state')
template_data = params.get('template')
filter = params.get('filter')

self.result = {}

template = self.get_template_instance(id, name)
template = self.get_template_instance(id, name, filter)
needs_creation = False
if not template and desired_state != 'absent':
if id:
Expand All @@ -196,34 +207,34 @@ def run(self, one, module, result):
self.result = self.delete_template(template)
else:
if needs_creation:
self.result = self.create_template(name, template_data)
self.result = self.create_template(name, template_data, filter)
else:
self.result = self.update_template(template, template_data)
self.result = self.update_template(template, template_data, filter)

self.exit()

def get_template(self, predicate):
# -3 means "Resources belonging to the user"
def get_template(self, predicate, filter):
# filter was included, discussion here: https://github.com/ansible-collections/community.general/issues/9278
# the other two parameters are used for pagination, -1 for both essentially means "return all"
pool = self.one.templatepool.info(-3, -1, -1)
pool = self.one.templatepool.info(filter, -1, -1)

for template in pool.VMTEMPLATE:
if predicate(template):
return template

return None

def get_template_by_id(self, template_id):
return self.get_template(lambda template: (template.ID == template_id))
def get_template_by_id(self, template_id, filter):
return self.get_template(lambda template: (template.ID == template_id), filter)

def get_template_by_name(self, name):
return self.get_template(lambda template: (template.NAME == name))
def get_template_by_name(self, name, filter):
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:
return self.get_template_by_id(requested_id)
return self.get_template_by_id(requested_id, filter)
else:
return self.get_template_by_name(requested_name)
return self.get_template_by_name(requested_name, filter)

def get_template_info(self, template):
info = {
Expand All @@ -238,21 +249,21 @@ def get_template_info(self, template):

return info

def create_template(self, name, template_data):
def create_template(self, name, template_data, filter):
if not self.module.check_mode:
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

return result

def update_template(self, template, template_data):
def update_template(self, template, template_data, filter):
if not self.module.check_mode:
# 0 = replace the whole template
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:
# Unfortunately it is not easy to detect if the template would have changed, therefore always report a change here.
result['changed'] = True
Expand Down

0 comments on commit 009cdc0

Please sign in to comment.