Skip to content

Commit

Permalink
Add 'netlab show defaults' command (closes #906)
Browse files Browse the repository at this point in the history
  • Loading branch information
ipspace committed Oct 20, 2023
1 parent eba0e0d commit 3f93d6b
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
55 changes: 55 additions & 0 deletions docs/netlab/show.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
The following settings can be displayed:

* **[attributes](netlab-show-attributes)** -- Supported lab topology attributes
* **[defaults](netlab-show-defaults)** -- User/system [defaults](../defaults.md)
* **[devices](netlab-show-devices)** -- Supported devices
* **[images](netlab-show-images)** -- Vagrant box names or container names for all supported devices or a single device
* **[module](netlab-show-modules)** -- Configuration modules
Expand Down Expand Up @@ -137,6 +138,60 @@ weight:
type: int
```
(netlab-show-defaults)=
## Display User/System Defaults
The **netlab show defaults** displays _netlab_ defaults collected from [user/system default files](../defaults.md):
```text
usage: netlab show defaults [-h] [--system] [--format {table,text,yaml}]
[--plugin PLUGIN [PLUGIN ...]]
[match]

Display (a subset) of system/user defaults

positional arguments:
match Display defaults within the specified subtree

options:
-h, --help show this help message and exit
--system Display system information (without user defaults)
--format {table,text,yaml}
Output format (table, text, yaml)
--plugin PLUGIN [PLUGIN ...]
Add plugin attributes to the system defaults

```

**Notes**

* The `--plugin` argument must be the last parameter on the command line -- all tokens specified after it are added to the list of plugins
* The displayed information does not include lab-specific defaults specified in lab topology or [alternate default file locations](defaults-locations).
* You can also display system defaults with `netlab inspect defaults` (requires a running lab) or `netlab create -o yaml:defaults` (requires a working topology file)

**Examples**

Display [graph output module](../outputs/graph.md) defaults (**outputs.graph**):

```text
$ netlab show defaults outputs.graph
netlab default settings within the outputs.graph subtree
=============================================================================
as_clusters: true
colors:
as: '#e8e8e8'
ebgp: '#b21a1a'
ibgp: '#613913'
node: '#ff9f01'
stub: '#d1bfab'
interface_labels: false
margins:
as: 16
node_address_label: true
```

(netlab-show-devices)=
## Display Supported Devices

Expand Down
5 changes: 5 additions & 0 deletions netsim/cli/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from .show_commands import reports as _reports
from .show_commands import providers as _providers
from .show_commands import attributes as _attributes
from .show_commands import defaults as _defaults

show_dispatch: dict = {
'images': {
Expand Down Expand Up @@ -56,6 +57,10 @@
'attributes': {
'exec': _attributes.show,
'parse': _attributes.parse
},
'defaults': {
'exec': _defaults.show,
'parse': _defaults.parse
}
}

Expand Down
50 changes: 50 additions & 0 deletions netsim/cli/show_commands/defaults.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#
# netlab show attributes -- display supported attributes
#

import argparse
import typing
import textwrap
from box import Box

from ...utils import strings,log
from ... import data
from . import show_common_parser,parser_add_module,DEVICES_TO_SKIP,get_modlist

def parse() -> argparse.ArgumentParser:
parser = show_common_parser('defaults','(a subset) of system/user defaults')
parser.add_argument(
nargs='?',
default='',
dest='match',
action='store',
help='Display defaults within the specified subtree')
parser.add_argument(
'--plugin',
nargs='+',
dest='plugin',
action='store',
help='Add plugin attributes to the system defaults')

return parser

def get_attribute_subset(settings: Box, args: argparse.Namespace) -> typing.Optional[Box]:
if not args.match:
return settings

return settings.get(args.match,'None')

def show(settings: Box, args: argparse.Namespace) -> None:
show = get_attribute_subset(settings, args)
if show is None:
log.fatal('There are no system/user defaults within the {args.match} subtree')

if args.format in ['text','table']:
print(f"""
netlab default settings {"within the "+args.match+" subtree" if args.match else ""}
=============================================================================
""")
elif args.format == 'yaml':
print('---')

print(strings.get_yaml_string(show))

0 comments on commit 3f93d6b

Please sign in to comment.