From 33f14e079d7b66815544824e0f3e877e7b202075 Mon Sep 17 00:00:00 2001 From: kakkotetsu Date: Tue, 9 Mar 2021 13:10:38 +0900 Subject: [PATCH] nclu: fix 'net pending' delimiter string, and exec commit only if changed (#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 * Update changelogs/fragments/219-nclu-fix-pending.yaml Co-authored-by: Felix Fontein * switched to 're' to handle both cases of quotes Co-authored-by: Felix Fontein Co-authored-by: Deric Crago (cherry picked from commit 5e2cd8802cf3339a8c43ce5ce6387c0b9f5872aa) --- changelogs/fragments/219-nclu-fix-pending.yaml | 5 +++++ plugins/modules/network/cumulus/nclu.py | 11 +++++++---- .../unit/plugins/modules/network/cumulus/test_nclu.py | 4 +--- 3 files changed, 13 insertions(+), 7 deletions(-) create mode 100644 changelogs/fragments/219-nclu-fix-pending.yaml diff --git a/changelogs/fragments/219-nclu-fix-pending.yaml b/changelogs/fragments/219-nclu-fix-pending.yaml new file mode 100644 index 00000000..29690939 --- /dev/null +++ b/changelogs/fragments/219-nclu-fix-pending.yaml @@ -0,0 +1,5 @@ +--- +bugfixes: + - nclu - fix ``net pending`` delimiter string (https://github.com/ansible-collections/community.network/pull/219). +minor_changes: + - nclu - execute ``net commit description `` only if changed ``net pending``'s diff field (https://github.com/ansible-collections/community.network/pull/219). diff --git a/plugins/modules/network/cumulus/nclu.py b/plugins/modules/network/cumulus/nclu.py index 22537b5b..4f8a7bb1 100644 --- a/plugins/modules/network/cumulus/nclu.py +++ b/plugins/modules/network/cumulus/nclu.py @@ -150,6 +150,7 @@ sample: "interface bond0 config updated" ''' +import re from ansible.module_utils.basic import AnsibleModule @@ -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() @@ -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 diff --git a/tests/unit/plugins/modules/network/cumulus/test_nclu.py b/tests/unit/plugins/modules/network/cumulus/test_nclu.py index c5864d1b..c342feb5 100644 --- a/tests/unit/plugins/modules/network/cumulus/test_nclu.py +++ b/tests/unit/plugins/modules/network/cumulus/test_nclu.py @@ -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)