Skip to content

Commit

Permalink
Merge pull request #241 from attila123/junos_confirm_commit_fix
Browse files Browse the repository at this point in the history
Fix junos confirm-commit issue (improved version)
  • Loading branch information
einarnn committed Jun 16, 2018
2 parents 76d274b + 476639e commit 46259c2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
17 changes: 14 additions & 3 deletions ncclient/operations/third_party/juniper/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from ncclient.operations.rpc import RPCReply
from ncclient.operations.rpc import RPCError
from ncclient import NCClientError
import math


class GetConfiguration(RPC):
def request(self, format='xml', filter=None):
Expand Down Expand Up @@ -71,7 +73,7 @@ def request(self, confirmed=False, timeout=None, comment=None, synchronize=False
*confirmed* whether this is a confirmed commit. Mutually exclusive with at_time.
*timeout* specifies the confirm timeout in minutes
*timeout* specifies the confirm timeout in seconds
*comment* a string to comment the commit with. Review on the device using 'show system commit'
Expand All @@ -82,14 +84,23 @@ def request(self, confirmed=False, timeout=None, comment=None, synchronize=False
A date and time value of the form yyyy-mm-dd hh:mm[:ss] (year, month, date, hours, minutes, and, optionally, seconds)
*check* Verify the syntactic correctness of the candidate configuration"""
node = new_ele("commit-configuration")
# NOTE: non netconf standard, Junos specific commit-configuration element, see
# https://www.juniper.net/documentation/en_US/junos/topics/reference/tag-summary/junos-xml-protocol-commit-configuration.html
node = new_ele_ns("commit-configuration", "")
if confirmed and at_time is not None:
raise NCClientError("'Commit confirmed' and 'commit at' are mutually exclusive.")
if confirmed:
self._assert(":confirmed-commit")
sub_ele(node, "confirmed")
if timeout is not None:
sub_ele(node, "confirm-timeout").text = timeout
# NOTE: For Junos, confirm-timeout has to be given in minutes:
# https://www.juniper.net/documentation/en_US/junos/topics/reference/tag-summary/junos-xml-protocol-commit-configuration.html
# but the netconf standard and ncclient library uses seconds:
# https://tools.ietf.org/html/rfc6241#section-8.4.5
# http://ncclient.readthedocs.io/en/latest/manager.html#ncclient.manager.Manager.commit
# so need to convert the value in seconds to minutes.
timeout_int = int(timeout) if isinstance(timeout, str) else timeout
sub_ele(node, "confirm-timeout").text = str(int(math.ceil(timeout_int/60.0)))
elif at_time is not None:
sub_ele(node, "at-time").text = at_time
if comment is not None:
Expand Down
9 changes: 5 additions & 4 deletions test/unit/operations/third_party/juniper/test_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,10 @@ def test_commit_confirmed(self, mock_assert, mock_request, mock_session):
session = ncclient.transport.SSHSession(device_handler)
obj = Commit(session, device_handler, raise_mode=RaiseMode.ALL)
obj.request(confirmed=True, comment="message", timeout="50")
node = new_ele("commit-configuration")
node = new_ele_ns("commit-configuration", "")
sub_ele(node, "confirmed")
sub_ele(node, "confirm-timeout").text = "50"
# confirm-timeout need to be provided in minutes for this Junos specific rpc
sub_ele(node, "confirm-timeout").text = "1"
sub_ele(node, "log").text = "message"
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
Expand All @@ -221,7 +222,7 @@ def test_commit(self, mock_assert, mock_request, mock_session):
session = ncclient.transport.SSHSession(device_handler)
obj = Commit(session, device_handler, raise_mode=RaiseMode.ALL)
obj.request()
node = new_ele("commit-configuration")
node = new_ele_ns("commit-configuration", "")
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call)
Expand All @@ -236,7 +237,7 @@ def test_commit_at_time(self, mock_assert, mock_request, mock_session):
session = ncclient.transport.SSHSession(device_handler)
obj = Commit(session, device_handler, raise_mode=RaiseMode.ALL)
obj.request(at_time="1111-11-11 00:00:00", synchronize=True)
node = new_ele("commit-configuration")
node = new_ele_ns("commit-configuration", "")
sub_ele(node, "at-time").text = "1111-11-11 00:00:00"
sub_ele(node, "synchronize")
xml = ElementTree.tostring(node)
Expand Down

0 comments on commit 46259c2

Please sign in to comment.