Skip to content

Commit

Permalink
nclu: fix 'net pending' delimiter string, and exec commit only if cha…
Browse files Browse the repository at this point in the history
…nged (#219)

* fix 'net pendig''s delimiter string

* test for #219

* test for #219

* add changelog fragment for #219

* Update changelogs/fragments/219-nclu-fix-pending.yaml

Co-authored-by: Felix Fontein <[email protected]>

* Update changelogs/fragments/219-nclu-fix-pending.yaml

Co-authored-by: Felix Fontein <[email protected]>

* switched to 're' to handle both cases of quotes

Co-authored-by: Felix Fontein <[email protected]>
Co-authored-by: Deric Crago <[email protected]>
(cherry picked from commit 5e2cd88)
  • Loading branch information
kakkotetsu authored and Patchback committed Mar 9, 2021
1 parent a5da897 commit 33f14e0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
5 changes: 5 additions & 0 deletions changelogs/fragments/219-nclu-fix-pending.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
bugfixes:
- nclu - fix ``net pending`` delimiter string (https:/ansible-collections/community.network/pull/219).
minor_changes:
- nclu - execute ``net commit description <description>`` only if changed ``net pending``'s diff field (https:/ansible-collections/community.network/pull/219).
11 changes: 7 additions & 4 deletions plugins/modules/network/cumulus/nclu.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
sample: "interface bond0 config updated"
'''

import re
from ansible.module_utils.basic import AnsibleModule


Expand All @@ -165,10 +166,10 @@ def check_pending(module):
"""Check the pending diff of the nclu buffer."""
pending = command_helper(module, "pending", "Error in pending config. You may want to view `net pending` on this target.")

delimeter1 = "net add/del commands since the last 'net commit'"
delimeter1 = re.compile('''net add/del commands since the last ['"]net commit['"]''')
color1 = '\x1b[94m'
if delimeter1 in pending:
pending = pending.split(delimeter1)[0]
if re.search(delimeter1, pending):
pending = re.split(delimeter1, pending)[0]
pending = pending.replace(color1, '')
return pending.strip()

Expand Down Expand Up @@ -209,13 +210,15 @@ def run_nclu(module, command_list, command_string, commit, atomic, abort, descri
_changed = True

# Do the commit.
if do_commit:
if (do_commit and _changed):
result = command_helper(module, "commit description '%s'" % description)
if "commit ignored" in result:
_changed = False
command_helper(module, "abort")
elif command_helper(module, "show commit last") == "":
_changed = False
elif do_abort:
command_helper(module, "abort")

return _changed, output

Expand Down
4 changes: 1 addition & 3 deletions tests/unit/plugins/modules/network/cumulus/test_nclu.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,7 @@ def test_commit_ignored(self):
changed, output = nclu.run_nclu(module, None, None, True, False, False, "ignore me")

self.assertEqual(module.command_history, ['/usr/bin/net pending',
'/usr/bin/net pending',
"/usr/bin/net commit description 'ignore me'",
'/usr/bin/net abort'])
'/usr/bin/net pending'])
self.assertEqual(len(module.pending), 0)
self.assertEqual(module.fail_code, {})
self.assertEqual(changed, False)

0 comments on commit 33f14e0

Please sign in to comment.