Skip to content

Commit

Permalink
OpenNebula/one_vm implement the one.vm.updateconf API call (ansible-c…
Browse files Browse the repository at this point in the history
…ollections#5812)

* opennebula: Add template manipulation helpers

* one_vm: Use 'updateconf' API call to modify running VMs

* one_vm: Emulate 'updateconf' API call for newly created VMs

* opennebula/one_vm: Satisfy linter checks

* opennebula/one_vm: Apply suggestions from code review

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>

* opennebula/one_vm: Drop 'extend' function, use 'dict_merge' instead

* Add changelog fragment

* one_vm: Refactor 'parse_updateconf' function

* opennebula/one_vm: Apply suggestions from code review

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

* one_vm: Allow for using updateconf in all scenarios

---------

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
Co-authored-by: Felix Fontein <felix@fontein.de>
  • Loading branch information
3 people authored Jan 28, 2023
1 parent 7b8b73f commit 8818a6f
Show file tree
Hide file tree
Showing 5 changed files with 363 additions and 79 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/5812-implement-updateconf-api-call.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- one_vm - add a new ``updateconf`` option which implements the ``one.vm.updateconf`` API call (https://github.com/ansible-collections/community.general/pull/5812).
30 changes: 30 additions & 0 deletions plugins/module_utils/opennebula.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,36 @@
HAS_PYONE = False


# A helper function to mitigate https://github.com/OpenNebula/one/issues/6064.
# It allows for easily handling lists like "NIC" or "DISK" in the JSON-like template representation.
# There are either lists of dictionaries (length > 1) or just dictionaries.
def flatten(to_flatten, extract=False):
"""Flattens nested lists (with optional value extraction)."""
def recurse(to_flatten):
return sum(map(recurse, to_flatten), []) if isinstance(to_flatten, list) else [to_flatten]
value = recurse(to_flatten)
if extract and len(value) == 1:
return value[0]
return value


# A helper function to mitigate https://github.com/OpenNebula/one/issues/6064.
# It renders JSON-like template representation into OpenNebula's template syntax (string).
def render(to_render):
"""Converts dictionary to OpenNebula template."""
def recurse(to_render):
for key, value in sorted(to_render.items()):
if isinstance(value, dict):
yield '{0:}=[{1:}]'.format(key, ','.join(recurse(value)))
continue
if isinstance(value, list):
for item in value:
yield '{0:}=[{1:}]'.format(key, ','.join(recurse(item)))
continue
yield '{0:}="{1:}"'.format(key, value)
return '\n'.join(recurse(to_render))


class OpenNebulaModule:
"""
Base class for all OpenNebula Ansible Modules.
Expand Down
Loading

0 comments on commit 8818a6f

Please sign in to comment.