From 3f93d6bd5a69b90907322e97fca7d89183018cb5 Mon Sep 17 00:00:00 2001 From: Ivan Pepelnjak Date: Fri, 20 Oct 2023 12:38:08 +0200 Subject: [PATCH] Add 'netlab show defaults' command (closes #906) --- docs/netlab/show.md | 55 ++++++++++++++++++++++++++++ netsim/cli/show.py | 5 +++ netsim/cli/show_commands/defaults.py | 50 +++++++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 netsim/cli/show_commands/defaults.py diff --git a/docs/netlab/show.md b/docs/netlab/show.md index 51293d313..8342e09e2 100644 --- a/docs/netlab/show.md +++ b/docs/netlab/show.md @@ -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 @@ -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 diff --git a/netsim/cli/show.py b/netsim/cli/show.py index 2076db5f2..260291e95 100644 --- a/netsim/cli/show.py +++ b/netsim/cli/show.py @@ -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': { @@ -56,6 +57,10 @@ 'attributes': { 'exec': _attributes.show, 'parse': _attributes.parse + }, + 'defaults': { + 'exec': _defaults.show, + 'parse': _defaults.parse } } diff --git a/netsim/cli/show_commands/defaults.py b/netsim/cli/show_commands/defaults.py new file mode 100644 index 000000000..4f21c441e --- /dev/null +++ b/netsim/cli/show_commands/defaults.py @@ -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))