Skip to content

Commit

Permalink
Merge the two increment functions into one
Browse files Browse the repository at this point in the history
  • Loading branch information
katsel committed Apr 28, 2021
1 parent d898a88 commit 58bbb24
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
1 change: 0 additions & 1 deletion src/cnaas_nms/confpush/sync_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,6 @@ def push_sync_device(task, dry_run: bool = True, generate_only: bool = False,

logger.debug("Generate config for host: {}".format(task.host.name))
cnaas_jinja_env.filters['increment_ip'] = jinja_filters.increment_ip
cnaas_jinja_env.filters['increment_if'] = jinja_filters.increment_if
r = task.run(task=template_file,
name="Generate device config",
template=template,
Expand Down
19 changes: 16 additions & 3 deletions src/cnaas_nms/tools/jinja_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,22 @@


def increment_ip(ip_string, increment=1):
"""Increment an IP address by a given value. Default increment value is 1."""
ip = ipaddress.ip_address(ip_string)
return format(ip + increment)
"""Increment an IP address by a given value. Default increment value is 1.
Args:
ip_string: IP address string. Can be plain or with numeric /prefix
increment: Optional increment step, defaults to 1
Returns:
String with the incremented IP address, with optional numeric prefix
"""
# plain IP
try:
ip = ipaddress.ip_address(ip_string)
return format(ip + increment)
# IP with prefix
except ValueError:
pass # do not chain exceptions, this is expected behaviour
# handle IP with prefix as an interface
return increment_if(ip_string, increment)


def increment_if(if_string, increment=1):
Expand Down

0 comments on commit 58bbb24

Please sign in to comment.