1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

proxmox_template: small refactor in get_template() (#8516)

* proxmox_template: small refactor in get_template()

* add changelog frag

* Update plugins/modules/proxmox_template.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* rename function as per PR suggestion

---------

Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
Alexei Znamensky 2024-06-27 21:56:19 +12:00 committed by GitHub
parent 1053545870
commit 70c8042c99
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 10 additions and 8 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- proxmox_template - small refactor in logic for determining whether a template exists or not (https://github.com/ansible-collections/community.general/pull/8516).

View file

@ -144,12 +144,12 @@ except ImportError:
class ProxmoxTemplateAnsible(ProxmoxAnsible):
def get_template(self, node, storage, content_type, template):
def has_template(self, node, storage, content_type, template):
volid = '%s:%s/%s' % (storage, content_type, template)
try:
return [True for tmpl in self.proxmox_api.nodes(node).storage(storage).content.get()
if tmpl['volid'] == '%s:%s/%s' % (storage, content_type, template)]
return any(tmpl['volid'] == volid for tmpl in self.proxmox_api.nodes(node).storage(storage).content.get())
except Exception as e:
self.module.fail_json(msg="Failed to retrieve template '%s:%s/%s': %s" % (storage, content_type, template, e))
self.module.fail_json(msg="Failed to retrieve template '%s': %s" % (volid, e))
def task_status(self, node, taskid, timeout):
"""
@ -190,7 +190,7 @@ class ProxmoxTemplateAnsible(ProxmoxAnsible):
volid = '%s:%s/%s' % (storage, content_type, template)
self.proxmox_api.nodes(node).storage(storage).content.delete(volid)
while timeout:
if not self.get_template(node, storage, content_type, template):
if not self.has_template(node, storage, content_type, template):
return True
timeout = timeout - 1
if timeout == 0:
@ -239,14 +239,14 @@ def main():
if not template:
module.fail_json(msg='template param for downloading appliance template is mandatory')
if proxmox.get_template(node, storage, content_type, template) and not module.params['force']:
if proxmox.has_template(node, storage, content_type, template) and not module.params['force']:
module.exit_json(changed=False, msg='template with volid=%s:%s/%s already exists' % (storage, content_type, template))
if proxmox.download_template(node, storage, template, timeout):
module.exit_json(changed=True, msg='template with volid=%s:%s/%s downloaded' % (storage, content_type, template))
template = os.path.basename(src)
if proxmox.get_template(node, storage, content_type, template) and not module.params['force']:
if proxmox.has_template(node, storage, content_type, template) and not module.params['force']:
module.exit_json(changed=False, msg='template with volid=%s:%s/%s is already exists' % (storage, content_type, template))
elif not src:
module.fail_json(msg='src param to uploading template file is mandatory')
@ -261,7 +261,7 @@ def main():
content_type = module.params['content_type']
template = module.params['template']
if not proxmox.get_template(node, storage, content_type, template):
if not proxmox.has_template(node, storage, content_type, template):
module.exit_json(changed=False, msg='template with volid=%s:%s/%s is already deleted' % (storage, content_type, template))
if proxmox.delete_template(node, storage, content_type, template, timeout):