Skip to content

Commit

Permalink
Issue:

commit_configuration API parameters are missing in ansible Py… (
Browse files Browse the repository at this point in the history
#638)

* Issue:

commit_configuration API parameters are missing in ansible PyEZ connection API  and also formatted error was noticed.
This impact committing the configuration using ansible PyEZ connection.

Fix:
Supported valid parameter in commit_configuration API parameter.
Fixed format string issue
Fixed configuration mode support for ansible PyEZ connection.

* Issue:

commit_configuration API parameters are missing in ansible PyEZ connection API  and also formatted error was noticed.
This impact committing the configuration using ansible PyEZ connection.

Fix:
Supported valid parameter in commit_configuration API parameter.
Fixed format string issue
Fixed configuration mode support for ansible PyEZ connection.

* Fix
  • Loading branch information
dineshbaburam91 authored Jan 25, 2024
1 parent cf07c23 commit 0cef75e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
38 changes: 32 additions & 6 deletions ansible_collections/juniper/device/plugins/connection/pyez.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@
)

# Supported configuration modes
CONFIG_MODE_CHOICES = ['exclusive', 'private']
CONFIG_MODE_CHOICES = ['exclusive', 'private', 'dynamic', 'batch', 'ephemeral']


class Connection(NetworkConnectionBase):
Expand Down Expand Up @@ -590,7 +590,7 @@ def invoke_jsnapy(self, data, action):
(type(responses), str(responses)))
return results

def open_configuration(self, mode, ignore_warn=None):
def open_configuration(self, mode, ignore_warn=None, ephemeral_instance=None):
"""Open candidate configuration database in exclusive or private mode.
Failures:
Expand All @@ -601,6 +601,10 @@ def open_configuration(self, mode, ignore_warn=None):
if self.config is None:
if mode not in CONFIG_MODE_CHOICES:
raise AnsibleError("Invalid configuration mode: %s" % mode)
if mode != 'ephemeral' and ephemeral_instance is not None:
self.fail_json(msg='Ephemeral instance is specified while the mode '
'is not ephemeral. Specify the mode as ephemeral '
'or do not specify the instance.')
if self.dev is None:
self.open()
config = jnpr.junos.utils.config.Config(self.dev, mode=mode)
Expand All @@ -611,6 +615,23 @@ def open_configuration(self, mode, ignore_warn=None):
self.dev.rpc.open_configuration(
private=True,
ignore_warning=ignore_warn)
elif config.mode == 'dynamic':
self.dev.rpc.open_configuration(
dynamic=True,
ignore_warning=ignore_warn)
elif config.mode == 'batch':
self.dev.rpc.open_configuration(
batch=True,
ignore_warning=ignore_warn)
elif config.mode == 'ephemeral':
if ephemeral_instance is None:
self.dev.rpc.open_configuration(
ephemeral=True,
ignore_warning=ignore_warn)
else:
self.dev.rpc.open_configuration(
ephemeral_instance = ephemeral_instance,
ignore_warning=ignore_warn)
except (pyez_exception.ConnectError,
pyez_exception.RpcError) as ex:
raise AnsibleError('Unable to open the configuration in %s '
Expand All @@ -631,7 +652,7 @@ def close_configuration(self):
try:
if config.mode == 'exclusive':
config.unlock()
elif config.mode == 'private':
elif config.mode in ['batch', 'dynamic', 'private', 'ephemeral']:
self.dev.rpc.close_configuration()
self.queue_message("log", "Configuration closed.")
except (pyez_exception.ConnectError,
Expand Down Expand Up @@ -723,7 +744,7 @@ def load_configuration(self, config, load_args):
if config is not None:
self.config.load(config, **load_args)
else:
self.queue_message("log", "Load args %s.", str(load_args))
self.queue_message("log", "Load args %s." %str(load_args))
self.config.load(**load_args)
self.queue_message("log", "Configuration loaded.")
except (self.pyez_exception.RpcError,
Expand All @@ -732,7 +753,8 @@ def load_configuration(self, config, load_args):
(str(ex)))

def commit_configuration(self, ignore_warning=None, comment=None,
confirmed=None):
confirmed=None, timeout=30, full=False,
force_sync=False, sync=False):
"""Commit the candidate configuration.
Assumes the configuration is already opened.
Expand All @@ -747,7 +769,11 @@ def commit_configuration(self, ignore_warning=None, comment=None,
try:
self.config.commit(ignore_warning=ignore_warning,
comment=comment,
confirm=confirmed)
confirm=confirmed,
timeout=timeout,
full=full,
force_sync=force_sync,
sync=sync)
self.queue_message("log", "Configuration committed.")
except (self.pyez_exception.RpcError,
self.pyez_exception.ConnectError) as ex:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1165,7 +1165,9 @@ def open_configuration(self, mode, ignore_warning=None, ephemeral_instance=None)
if mode not in CONFIG_MODE_CHOICES:
self.fail_json(msg='Invalid configuration mode: %s' % (mode))
if mode != 'ephemeral' and ephemeral_instance is not None:
self.fail_json(msg='configuration mode ephemeral is required')
self.fail_json(msg='Ephemeral instance is specified while the mode '
'is not ephemeral.Specify the mode as ephemeral or '
'do not specify the instance.')
if self.dev is None:
self.open()
config = jnpr.junos.utils.config.Config(self.dev, mode=mode)
Expand Down Expand Up @@ -1542,7 +1544,7 @@ def commit_configuration(self, ignore_warning=None, comment=None,
- 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, timeout, confirmed, full, sync, force_sync)
return

if self.dev is None or self.config is None:
Expand Down

0 comments on commit 0cef75e

Please sign in to comment.