Skip to content

Commit fb98ccf

Browse files
committed
Add nodes to device-specific groups
This functionality can be used to automatically add devices that netlab cannot configure to the 'unprovisioned' group.
1 parent 5cd9d13 commit fb98ccf

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

docs/dev/devices.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ Device parameters file can also include numerous *features*. The following featu
114114
* **features.initial.ipv6.lla** -- The device supports IPv6 interfaces using just link-local addresses.
115115
* **features.initial.ipv6.use_ra** -- The device (when running as a host) listens to IPv6 RA messages to generate a default route
116116
* **features.initial.roles** -- The list of roles a device can have (default: `[ router ]`)
117+
* **features.groups** -- The groups this device belongs to. You can set this feature to [`[ unprovisioned ]`](group-special-names) for devices that _netlab_ cannot configure yet. Don't forget to remove the **unprovisioned** group once you implement the Ansible [device configuration task list](dev-new-devices-configure).
117118

118119
```{tip}
119120
Please note that the MTU used by netlab is always the layer-3 MTU. If your device expects layer-2 MTU configuration, add the size of the layer-2 header to the interface **mtu** variable.
@@ -207,6 +208,7 @@ features:
207208
subif_name:
208209
vxlan: true
209210
```
211+
210212
## Vagrant Template File
211213
212214
If you'll use a Vagrant box to start the network device as a VM, you have to add a template that will generate the part of *Vagrantfile* (or *containerlab* configuration file) describing your virtual machine. See `netsim/templates/provider/...` directories for more details.
@@ -264,6 +266,7 @@ clab:
264266
ansible_become_method: enable
265267
```
266268

269+
(dev-new-devices-configure)=
267270
## Configuring the Device
268271

269272
To configure your device (including initial device configuration), you'll have to create an Ansible task list that deploys configuration snippets onto your device. *netlab* merges configuration snippets with existing device configuration (instead of building a complete configuration and replacing it).

netsim/augment/groups.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -299,24 +299,24 @@ def auto_create_members(
299299
'''
300300
Add node-level group settings to global groups
301301
'''
302+
def add_node_to_group(node: str, group: str, topology: Box) -> None:
303+
g_data = topology.groups[group]
304+
if g_data.get('type','node') != 'node':
305+
log.error(
306+
f"Cannot add node {node} to non-node group {group}",
307+
category=log.IncorrectType,
308+
module='groups')
309+
310+
data.append_to_list(g_data,'members',node)
311+
302312
def add_node_level_groups(topology: Box) -> None:
303313
for name,n in topology.nodes.items():
304314
if not 'group' in n:
305315
continue
306316

307-
must_be_list(n,'group',f'nodes.{name}')
308-
309-
for grpname in n.group:
310-
# Sanity check for node groups
311-
if topology.groups[grpname].get('type','node') != 'node':
312-
log.error(
313-
f"Cannot use the 'group' attribute in node {n.name} to add a node to a non-node group {grpname}",
314-
category=log.IncorrectType,
315-
module='groups')
316-
continue
317-
318-
# Add node name to the target group
319-
data.append_to_list(topology.groups[grpname],'members',name)
317+
if must_be_list(n,'group',f'nodes.{name}'):
318+
for grpname in n.group:
319+
add_node_to_group(name,grpname,topology)
320320

321321
'''
322322
Check recursive group definitions

netsim/augment/nodes.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from .. import data
1515
from .. import utils
1616
from .. import providers
17-
from . import devices,addressing,links
17+
from . import devices,addressing,links,groups
1818
from ..data.validate import validate_attributes,get_object_attributes
1919
from ..data.types import must_be_int,must_be_string,must_be_id,must_be_device
2020
from ..data import global_vars,is_true_int
@@ -297,6 +297,9 @@ def find_node_device(n: Box, topology: Box) -> bool:
297297
if dev_def.get('node.module') and 'module' not in n: # Have to copy default device module into node data
298298
n.module = dev_def.node.module # ... before modules are initialized
299299

300+
for group in dev_def.features.get('group',[]):
301+
groups.add_node_to_group(n.name,group,topology)
302+
300303
if 'attributes' in dev_def: # Add any device specific attributes to the data model
301304
topology.defaults.attributes = topology.defaults.attributes + dev_def.attributes
302305

0 commit comments

Comments
 (0)