Skip to content

Commit

Permalink
Use new- or old-style formatting strings when creating interface names
Browse files Browse the repository at this point in the history
  • Loading branch information
ipspace committed Dec 5, 2022
1 parent 105111d commit 322e3f6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion netsim/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env python3

__version__ = "1.4.1-post2"
__version__ = "1.4.2-dev1"
15 changes: 13 additions & 2 deletions netsim/augment/links.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,17 @@ def get_link_propagate_attributes(defaults: Box) -> set:
return set(defaults.attributes.link).union(set(defaults.attributes.link_internal)) - \
set(defaults.attributes.link_no_propagate)

"""
Get interface name: create interface name given interface name format, ifindex and optional
interface data structures. Use str.format if the name format includes '{}'
"""
def get_interface_name(fmt: str, ifdata: Box) -> str:
if '{' in fmt:
result = str(eval(f"f'{fmt}'",dict(ifdata))) # An awful hack to use f-string specified in a string variable
return result
else:
return fmt % ifdata.ifindex # Old-style formatting

"""
Add interface data structure to a node:
Expand All @@ -167,12 +178,12 @@ def add_node_interface(node: Box, ifdata: Box, defaults: Box) -> Box:

ifdata.ifindex = ifindex
if ifname_format and not 'ifname' in ifdata:
ifdata.ifname = ifname_format % ifindex
ifdata.ifname = get_interface_name(ifname_format,ifdata)

pdata = devices.get_provider_data(node,defaults).get('interface',{})
pdata = Box(pdata,box_dots=True,default_box=True) # Create a copy of the provider interface data
if 'name' in pdata:
pdata.name = pdata.name % ifindex
pdata.name = get_interface_name(pdata.name,ifdata)

if pdata:
provider = devices.get_provider(node,defaults)
Expand Down
4 changes: 2 additions & 2 deletions netsim/topology-defaults.yml
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ devices:
create_iso: asav

eos:
interface_name: Ethernet%d
interface_name: Ethernet{ifindex}
description: Arista vEOS
mgmt_if: Management1
virtualbox:
Expand Down Expand Up @@ -583,7 +583,7 @@ devices:
protocol: [ anycast, vrrp ]
clab:
interface:
name: et%d
name: et{ifindex}
node:
kind: ceos
env:
Expand Down

0 comments on commit 322e3f6

Please sign in to comment.