Skip to content

Commit

Permalink
[PR #6294/cb3ca05b backport][stable-6] one_vm: fix syntax error when …
Browse files Browse the repository at this point in the history
…creating VMs with a more complex template (#6342)

one_vm: fix syntax error when creating VMs with a more complex template (#6294)

* one_vm: fix syntax error when creating VMs with a more complex template

with more complex templates that make use of quoted strings the new
"render" method fails to produce a template that is accepted by
OpenNebula.  ==> escape double quotes in strings to make OpenNebula
happy again.

I also tested whether newlines need to be escaped, looks like they are
fine as they are.

Fixes #6225

* module_utils/opennebula: skip empty values in render

(cherry picked from commit cb3ca05)

Co-authored-by: Georg Gadinger <[email protected]>
  • Loading branch information
patchback[bot] and nilsding authored Apr 16, 2023
1 parent a5824a2 commit 590ff35
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/6294-fix-one_vm-instantiation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- one_vm - fix syntax error when creating VMs with a more complex template (https://github.com/ansible-collections/community.general/issues/6225).
5 changes: 5 additions & 0 deletions plugins/module_utils/opennebula.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,18 @@ def render(to_render):
"""Converts dictionary to OpenNebula template."""
def recurse(to_render):
for key, value in sorted(to_render.items()):
if value is None:
continue
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
if isinstance(value, str):
yield '{0:}="{1:}"'.format(key, value.replace('\\', '\\\\').replace('"', '\\"'))
continue
yield '{0:}="{1:}"'.format(key, value)
return '\n'.join(recurse(to_render))

Expand Down
12 changes: 12 additions & 0 deletions tests/unit/plugins/module_utils/test_opennebula.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@
NIC=[NAME="NIC1",NETWORK_ID="1"]
''').strip()
),
(
{
'EMPTY_VALUE': None,
'SCHED_REQUIREMENTS': 'CLUSTER_ID="100"',
'BACKSLASH_ESCAPED': "this is escaped: \\n; this isn't: \"\nend",
},
textwrap.dedent('''
BACKSLASH_ESCAPED="this is escaped: \\\\n; this isn't: \\"
end"
SCHED_REQUIREMENTS="CLUSTER_ID=\\"100\\""
''').strip()
),
]


Expand Down

0 comments on commit 590ff35

Please sign in to comment.