Fix IndexError when junos_config contains multiple delete lines (#25139) (#25148)

* Prevent IndexError when deleting multiple lines

The old code will raise `IndexError: list assignment index out of range` when deleting multiple lines because the indexes of the original and the copy get out of sync. Solved by deleting from the high indexes first so the lower ones remain stable.

* Don't load configuration if nothing to load

Instead of sending an empty candidate config (for example because the candidate only consisted of `delete` lines, and all of them were filtered out by `filter_delete_statements`) just return. JunOS seems to get confused by empty changes, and if the candidate config is empty then it's a no-op anyway.

(cherry picked from commit 06f76a2741)
pull/25292/head
Ganesh Nalawade 9 years ago committed by James Cammarata
parent d7ce5e68ea
commit 3932c53328

@ -168,6 +168,9 @@ def get_diff(module):
def load_config(module, candidate, warnings, action='merge', commit=False, format='xml',
comment=None, confirm=False, confirm_timeout=None):
if not candidate:
return
with locked_config(module):
if isinstance(candidate, list):
candidate = '\n'.join(candidate)

@ -231,7 +231,7 @@ def filter_delete_statements(module, candidate):
config = str(match.text)
modified_candidate = candidate[:]
for index, line in enumerate(candidate):
for index, line in reversed(list(enumerate(candidate))):
if line.startswith('delete'):
newline = re.sub('^delete', 'set', line)
if newline not in config:

Loading…
Cancel
Save