Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve handling of complex RPC's #591

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
from __future__ import absolute_import, division, print_function

# Ansible imports
try:
from ansible.module_utils.common.validation import check_type_dict
except ImportError:
pass

from ansible.module_utils.connection import Connection
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.basic import boolean
Expand Down Expand Up @@ -629,6 +634,12 @@ def __init__(self,
else:
self._pyez_conn = self.get_connection()

# Support Ansible 4+ as well as 2.9
if 'check_type_dict' not in globals():
self.check_type_dict = self._check_type_dict
else:
self.check_type_dict = check_type_dict

def initialize_params(self):
"""
Initalize the parameters in common module
Expand Down Expand Up @@ -925,15 +936,19 @@ def parse_arg_to_list_of_dicts(self,
if string_val is None:
return None

# Evaluate the string
kwargs = self.safe_eval(string_val)
kwargs = string_val

if isinstance(string_val, basestring):
# Evaluate the string
kwargs = self.safe_eval(string_val)

if isinstance(kwargs, basestring):
# This might be a keyword1=value1 keyword2=value2 type string.
# The _check_type_dict method will parse this into a dict for us.
# If it's still a string, this might be a keyword1=value1
# keyword2=value2 type string.
# The check_type_dict method will parse this into a dict for us.
try:
kwargs = self._check_type_dict(kwargs)
except TypeError as exc:
kwargs = self.check_type_dict(kwargs)
except (TypeError, ValueError) as exc:
self.fail_json(msg="The value of the %s option (%s) is "
"invalid. Unable to translate into "
"a list of dicts." %
Expand All @@ -953,9 +968,9 @@ def parse_arg_to_list_of_dicts(self,
# If it's now a string, see if it can be parsed into a dictionary.
if isinstance(kwarg, basestring):
# This might be a keyword1=value1 keyword2=value2 type string.
# The _check_type_dict method will parse this into a dict.
# The check_type_dict method will parse this into a dict.
try:
kwarg = self._check_type_dict(kwarg)
kwarg = self.check_type_dict(kwarg)
except TypeError as exc:
self.fail_json(msg="The value of the %s option (%s) is "
"invalid. Unable to translate into a "
Expand Down Expand Up @@ -1503,7 +1518,7 @@ def load_configuration(self,
(str(ex)))

def commit_configuration(self, ignore_warning=None, comment=None,
confirmed=None, timeout=30, full=False):
confirmed=None, full=False):
"""Commit the candidate configuration.

Commit the configuration. Assumes the configuration is already opened.
Expand All @@ -1512,14 +1527,13 @@ def commit_configuration(self, ignore_warning=None, comment=None,
ignore_warning - Which warnings to ignore.
comment - The commit comment
confirmed - Number of minutes for commit confirmed.
timeout - Timeout for commit configuration. Default timeout value is 30s.
full - apply full commit

Failures:
- An error returned from committing the configuration.
"""
if self.conn_type != "local":
self._pyez_conn.commit_configuration(ignore_warning, comment, timeout, confirmed)
self._pyez_conn.commit_configuration(ignore_warning, comment, self.params.get('timeout'), confirmed)
return

if self.dev is None or self.config is None:
Expand All @@ -1530,7 +1544,7 @@ def commit_configuration(self, ignore_warning=None, comment=None,
self.config.commit(ignore_warning=ignore_warning,
comment=comment,
confirm=confirmed,
timeout=timeout,
timeout=self.params.get('timeout'),
full=full)
self.logger.debug("Configuration committed.")
except (self.pyez_exception.RpcError,
Expand Down
23 changes: 13 additions & 10 deletions ansible_collections/juniper/device/plugins/modules/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,14 +388,14 @@ def main():
default=None),
kwargs=dict(required=False,
aliases=['kwarg', 'args', 'arg'],
type='str',
type='list',
default=None),
attrs=dict(required=False,
type='str',
type='list',
aliases=['attr'],
default=None),
filter=dict(required=False,
type='str',
type='list',
aliases=['filter_xml'],
default=None),
dest=dict(required=False,
Expand Down Expand Up @@ -532,13 +532,16 @@ def main():
'successfully.')
else:
if kwarg is not None:
# Add kwarg
for (key, value) in iteritems(kwarg):
# Replace underscores with dashes in key name.
key = key.replace('_', '-')
sub_element = junos_module.etree.SubElement(rpc, key)
if not isinstance(value, bool):
sub_element.text = value
def iterdict(d, parent):
for (key, value) in iteritems(d):
# Replace underscores with dashes in key name.
key = key.replace('_', '-')
sub_element = junos_module.etree.SubElement(parent, key)
if isinstance(value, dict):
iterdict(value, sub_element)
elif not isinstance(value, bool):
sub_element.text = value
iterdict(kwarg, rpc)
if attr is not None:
# Add attr
for (key, value) in iteritems(attr):
Expand Down