From 322e3f614f06c5f9627b0ae71e1507a32def3b47 Mon Sep 17 00:00:00 2001 From: Ivan Pepelnjak Date: Sun, 4 Dec 2022 17:57:53 +0100 Subject: [PATCH] Use new- or old-style formatting strings when creating interface names --- netsim/__init__.py | 2 +- netsim/augment/links.py | 15 +++++++++++++-- netsim/topology-defaults.yml | 4 ++-- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/netsim/__init__.py b/netsim/__init__.py index 6a79234ca..16f62de1a 100755 --- a/netsim/__init__.py +++ b/netsim/__init__.py @@ -1,3 +1,3 @@ #!/usr/bin/env python3 -__version__ = "1.4.1-post2" +__version__ = "1.4.2-dev1" diff --git a/netsim/augment/links.py b/netsim/augment/links.py index d93203bb9..529220f13 100644 --- a/netsim/augment/links.py +++ b/netsim/augment/links.py @@ -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: @@ -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) diff --git a/netsim/topology-defaults.yml b/netsim/topology-defaults.yml index 05778991f..b367273f2 100644 --- a/netsim/topology-defaults.yml +++ b/netsim/topology-defaults.yml @@ -534,7 +534,7 @@ devices: create_iso: asav eos: - interface_name: Ethernet%d + interface_name: Ethernet{ifindex} description: Arista vEOS mgmt_if: Management1 virtualbox: @@ -583,7 +583,7 @@ devices: protocol: [ anycast, vrrp ] clab: interface: - name: et%d + name: et{ifindex} node: kind: ceos env: