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

added timestamps to iosxr_command module #50095

Merged
merged 1 commit into from Feb 4, 2019
Merged
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
4 changes: 2 additions & 2 deletions lib/ansible/module_utils/network/iosxr/iosxr.py
Expand Up @@ -477,10 +477,10 @@ def load_config(module, command_filter, commit=False, replace=False,
return diff


def run_commands(module, commands, check_rc=True):
def run_commands(module, commands, check_rc=True, return_timestamps=False):
connection = get_connection(module)
try:
return connection.run_commands(commands=commands, check_rc=check_rc)
return connection.run_commands(commands=commands, check_rc=check_rc, return_timestamps=return_timestamps)
except ConnectionError as exc:
module.fail_json(msg=to_text(exc))

Expand Down
3 changes: 2 additions & 1 deletion lib/ansible/modules/network/iosxr/iosxr_command.py
Expand Up @@ -178,7 +178,7 @@ def main():
match = module.params['match']

while retries > 0:
responses = run_commands(module, commands)
responses, timestamps = run_commands(module, commands, return_timestamps=True)

for item in list(conditionals):
if item(responses):
Expand All @@ -201,6 +201,7 @@ def main():
result.update({
'stdout': responses,
'stdout_lines': list(to_lines(responses)),
'timestamps': timestamps
})

module.exit_json(**result)
Expand Down
11 changes: 9 additions & 2 deletions lib/ansible/plugins/cliconf/iosxr.py
Expand Up @@ -24,6 +24,7 @@

from ansible.errors import AnsibleConnectionFailure
from ansible.module_utils._text import to_text
from ansible.module_utils.basic import get_timestamp
from ansible.module_utils.common._collections_compat import Mapping
from ansible.module_utils.connection import ConnectionError
from ansible.module_utils.network.common.config import NetworkConfig, dumps
Expand Down Expand Up @@ -172,10 +173,11 @@ def commit(self, comment=None, label=None, replace=None):

self.send_command(**cmd_obj)

def run_commands(self, commands=None, check_rc=True):
def run_commands(self, commands=None, check_rc=True, return_timestamps=False):
if commands is None:
raise ValueError("'commands' value is required")
responses = list()
timestamps = list()
for cmd in to_list(commands):
if not isinstance(cmd, Mapping):
cmd = {'command': cmd}
Expand All @@ -185,6 +187,7 @@ def run_commands(self, commands=None, check_rc=True):
raise ValueError("'output' value %s is not supported for run_commands" % output)

try:
timestamp = get_timestamp()
out = self.send_command(**cmd)
except AnsibleConnectionFailure as e:
if check_rc:
Expand All @@ -203,7 +206,11 @@ def run_commands(self, commands=None, check_rc=True):
pass

responses.append(out)
return responses
timestamps.append(timestamp)
if return_timestamps:
return responses, timestamps
else:
return responses

def discard_changes(self):
self.send_command('abort')
Expand Down
5 changes: 4 additions & 1 deletion test/units/modules/network/iosxr/test_iosxr_command.py
Expand Up @@ -20,6 +20,7 @@
__metaclass__ = type

from units.compat.mock import patch
from ansible.module_utils.basic import get_timestamp
from ansible.modules.network.iosxr import iosxr_command
from units.modules.utils import set_module_args
from .iosxr_module import TestIosxrModule, load_fixture
Expand All @@ -45,6 +46,7 @@ def load_fixtures(self, commands=None):
def load_from_file(*args, **kwargs):
module, commands = args
output = list()
timestamps = list()

for item in commands:
try:
Expand All @@ -53,7 +55,8 @@ def load_from_file(*args, **kwargs):
command = item
filename = str(command).replace(' ', '_')
output.append(load_fixture(filename))
return output
timestamps.append(get_timestamp())
return output, timestamps

self.run_commands.side_effect = load_from_file

Expand Down