Skip to content

Commit

Permalink
Small edgeos_config improvements (#199)
Browse files Browse the repository at this point in the history
* edgeos_config: Match space after set/delete commands

This makes matching the commands a bit more robust and also removes
magic strings from the code by centralizing them in variables.

* edgeos_config: Use simple string replacements instead of re

* edgeos_config: Simplify logical flow

We do not need to check for each .startswith twice, we can simply use an
else: block

* Update changelogs/fragments/199-edgeos-improvements.yaml

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

Co-authored-by: Felix Fontein <[email protected]>
  • Loading branch information
jplitza and felixfontein committed Feb 4, 2021
1 parent 27843d2 commit 4130c91
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/199-edgeos-improvements.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- edgeos_config - match the space after ``set`` and ``delete`` commands (https://github.com/ansible-collections/community.network/pull/199).
24 changes: 12 additions & 12 deletions plugins/modules/network/edgeos/edgeos_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,19 +138,19 @@
sample: /playbooks/ansible/backup/edgeos_config.2016-07-16@22:28:34
"""

import re

from ansible.module_utils._text import to_native
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.config import NetworkConfig
from ansible_collections.community.network.plugins.module_utils.network.edgeos.edgeos import load_config, get_config, run_commands


DEFAULT_COMMENT = 'configured by edgeos_config'
SET_CMD = 'set '
DELETE_CMD = 'delete '


def config_to_commands(config):
set_format = config.startswith('set') or config.startswith('delete')
set_format = config.startswith(SET_CMD) or config.startswith(DELETE_CMD)
candidate = NetworkConfig(indent=4, contents=config)
if not set_format:
candidate = [c.line for c in candidate.items]
Expand All @@ -163,7 +163,7 @@ def config_to_commands(config):
break
commands.append(item)

commands = ['set %s' % cmd.replace(' {', '') for cmd in commands]
commands = [SET_CMD + cmd.replace(' {', '') for cmd in commands]

else:
commands = to_native(candidate).split('\n')
Expand Down Expand Up @@ -204,37 +204,37 @@ def diff_config(module, commands, config):

updates = list()
visited = set()
delete_commands = [line for line in commands if line.startswith('delete')]
delete_commands = [line for line in commands if line.startswith(DELETE_CMD)]

for line in commands:
item = to_native(check_command(module, line))

if not item.startswith('set') and not item.startswith('delete'):
raise ValueError('line must start with either `set` or `delete`')

elif item.startswith('set'):
if item.startswith(SET_CMD):

if item not in config:
updates.append(line)

# If there is a corresponding delete command in the desired config, make sure to append
# the set command even though it already exists in the running config
else:
ditem = re.sub('set', 'delete', item)
ditem = item.replace(SET_CMD, DELETE_CMD, 1)
for line in delete_commands:
if ditem.startswith(line):
updates.append(item)

elif item.startswith('delete'):
elif item.startswith(DELETE_CMD):
if not config:
updates.append(line)
else:
item = re.sub(r'delete', 'set', item)
item = item.replace(DELETE_CMD, SET_CMD, 1)
for entry in config:
if entry.startswith(item) and line not in visited:
updates.append(line)
visited.add(line)

else:
raise ValueError('line must start with either `set` or `delete`')

return list(updates)


Expand Down

0 comments on commit 4130c91

Please sign in to comment.