Skip to content

Commit

Permalink
Merge pull request #1 from netreplica/pr88-site-and-sites
Browse files Browse the repository at this point in the history
Use --site and --sites interchangeably
  • Loading branch information
scotho3 authored Dec 26, 2023
2 parents 65f8b93 + 8f1324b commit fe25900
Show file tree
Hide file tree
Showing 6 changed files with 2,451 additions and 30 deletions.
52 changes: 50 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,24 @@ lint:
pylint nrx/*.py

test-local: test-dc1 test-dc2 test-colo test-site1 test-h88 test-lrg
test: test-dc1-cyjs-2-clab test-dc2-cyjs-2-cml test-site1-cyjs-2-clab test-site1-cyjs-2-clab-rename test-dc1-cyjs-2-graphite test-dc2-cyjs-2-graphite test-h88-cyjs-2-clab test-dc1-cyjs-2-d2 test-lrg-cyjs-2-graphite
test: test-args test-dc1-cyjs-2-clab test-dc2-cyjs-2-cml test-site1-cyjs-2-clab test-site1-cyjs-2-clab-rename test-dc1-cyjs-2-graphite test-dc2-cyjs-2-graphite test-h88-cyjs-2-clab test-dc1-cyjs-2-d2 test-lrg-cyjs-2-graphite

test-args: test-args-site-and-sites
test-dc1: test-dc1-nb-2-cyjs-current test-dc1-nb-2-cyjs-latest test-dc1-nb-2-cyjs-single-site test-dc1-nb-2-cyjs-single-sites test-dc1-cyjs-2-clab test-dc1-cyjs-2-graphite test-dc1-cyjs-2-d2 test-dc1-dc2-nb-2-cyjs-sites test-dc1-dc2-cyjs-2-graphite

test-dc1: test-dc1-nb-2-cyjs-current test-dc1-nb-2-cyjs-latest test-dc1-cyjs-2-clab test-dc1-cyjs-2-graphite test-dc1-cyjs-2-d2
test-dc2: test-dc2-nb-2-cyjs-current test-dc2-nb-2-cyjs-latest test-dc2-cyjs-2-cml test-dc2-cyjs-2-graphite
test-colo: test-colo-nb-2-cyjs-current test-colo-nb-2-cyjs-latest
test-site1: test-site1-nb-2-cyjs-current test-site1-nb-2-cyjs-latest test-site1-cyjs-2-clab test-site1-cyjs-2-clab-rename
test-h88: test-h88-nb-2-cyjs-current test-h88-nb-2-cyjs-latest test-h88-nb-2-cyjs-latest-noconfigs test-h88-cyjs-2-clab
test-lrg: test-lrg-nb-2-cyjs-latest test-lrg-cyjs-2-graphite

test-args-site-and-sites:
@echo "#################################################################"
@echo "# Simulteneous use of site and sites should fail"
@echo "#################################################################"
! ./nrx.py --site dc1 --sites dc1,dc2 -d
@echo

test-dc1-nb-2-cyjs-current:
@echo "#################################################################"
@echo "# DC1: read from NetBox current version and export as CYJS"
Expand All @@ -31,6 +40,36 @@ test-dc1-nb-2-cyjs-latest:
diff dc1.cyjs ../data/dc1.cyjs
@echo

test-dc1-nb-2-cyjs-single-site:
@echo "#################################################################"
@echo "# Single site DC1: read from NetBox current version and export as CYJS"
@echo "#################################################################"
mkdir -p tests/dc1/test && cd tests/dc1/test && rm -rf * && \
source ../../.env_current && \
../../../nrx.py -c ../nrx-no-site.conf -o cyjs --site dc1 -d && \
diff dc1.cyjs ../data/dc1.cyjs
@echo

test-dc1-nb-2-cyjs-single-sites:
@echo "#################################################################"
@echo "# Single site DC1: read from NetBox current version and export as CYJS"
@echo "#################################################################"
mkdir -p tests/dc1/test && cd tests/dc1/test && rm -rf * && \
source ../../.env_current && \
../../../nrx.py -c ../nrx-no-site.conf -o cyjs --sites dc1 -d && \
diff dc1.cyjs ../data/dc1.cyjs
@echo

test-dc1-dc2-nb-2-cyjs-sites:
@echo "#################################################################"
@echo "# Two site DC1 and DC2: read from NetBox current version and export as CYJS"
@echo "#################################################################"
mkdir -p tests/dc1/test && cd tests/dc1/test && rm -rf * && \
source ../../.env_current && \
../../../nrx.py -c ../nrx-no-site.conf -o cyjs --sites dc1,dc2 -d && \
diff dc1-dc2.cyjs ../data/dc1-dc2.cyjs
@echo

test-dc1-cyjs-2-clab:
@echo "#################################################################"
@echo "# DC1: read from CYJS and export as Containerlab"
Expand All @@ -49,6 +88,15 @@ test-dc1-cyjs-2-graphite:
for f in *; do echo Comparing file $$f ...; diff $$f ../data/$$f || exit 1; done
@echo

test-dc1-dc2-cyjs-2-graphite:
@echo "#################################################################"
@echo "# DC1 and DC2: read from CYJS and export as graphite"
@echo "#################################################################"
mkdir -p tests/dc1/graphite && cd tests/dc1/graphite && rm -rf * && \
../../../nrx.py -c ../nrx-no-site.conf -i cyjs -f ../data/dc1-dc2.cyjs -o graphite -d && \
for f in *; do echo Comparing file $$f ...; diff $$f ../data/$$f || exit 1; done
@echo

test-dc1-cyjs-2-d2:
@echo "#################################################################"
@echo "# DC1: read from CYJS and export as d2"
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ optional arguments:
-i, --input INPUT input source: netbox (default) | cyjs
-o, --output OUTPUT output format: cyjs | gml | clab | cml | graphite | d2
-a, --api API netbox API URL
-s, --sites SITES netbox site(s) to export, for multiple sites use a comma-separated list: site1,site2,site3 (uses OR logic)
-s SITE, --site SITE netbox site to export, cannot be combined with --sites
--sites SITES netbox sites to export, for multiple tags use a comma-separated list: site1,site2,site3 (uses OR logic)
-t, --tags TAGS netbox tags to export, for multiple tags use a comma-separated list: tag1,tag2,tag3 (uses AND logic)
-n, --name NAME name of the exported topology (site name or tags by default)
--noconfigs disable device configuration export (enabled by default)
Expand Down
62 changes: 35 additions & 27 deletions nrx/nrx.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def __init__(self, config):
if len(config['topology_name']) > 0:
self.topology_name = config['topology_name']
elif len(config['export_sites']) > 1:
self.topology_name = "__".join(config['export_sites'])
self.topology_name = "-".join(config['export_sites'])
elif len(config['export_sites']) > 0:
self.topology_name = config['export_sites'][0]
elif len(config['export_tags']) > 0:
Expand Down Expand Up @@ -768,31 +768,37 @@ def arg_output_check(s):

def parse_args():
"""CLI arguments parser"""
parser = argparse.ArgumentParser(prog='nrx', description="nrx - network topology exporter by netreplica")
parser.add_argument('-c', '--config', required=False, help='configuration file')
parser.add_argument('-i', '--input', required=False, help='input source: netbox (default) | cyjs',
default='netbox', type=arg_input_check,)
parser.add_argument('-o', '--output', required=False, help='output format: cyjs | gml | clab | cml | graphite | d2',
type=arg_output_check, )
parser.add_argument('-a', '--api', required=False, help='netbox API URL')
parser.add_argument('-s', '--sites', required=False, help='netbox site to export')
parser.add_argument('-t', '--tags', required=False, help='netbox tags to export, for multiple tags use a comma-separated list: tag1,tag2,tag3 (uses AND logic)')
parser.add_argument('-n', '--name', required=False, help='name of the exported topology (site name or tags by default)')
parser.add_argument('--noconfigs', required=False, help='disable device configuration export (enabled by default)',
action=argparse.BooleanOptionalAction)
parser.add_argument('-k', '--insecure', required=False, help='allow insecure server connections when using TLS',
action=argparse.BooleanOptionalAction)
parser.add_argument('-d', '--debug', required=False, help='enable debug output',
action=argparse.BooleanOptionalAction)
parser.add_argument('-f', '--file', required=False, help='file with the network graph to import')
parser.add_argument('-T', '--templates', required=False, help='directory with template files, \
will be prepended to TEMPLATES_PATH list \
in the configuration file')
parser.add_argument('-D', '--dir', required=False, help='save files into specified directory. \
nested relative and absolute paths are OK \
(topology name is used by default)')

args = parser.parse_args()
args_parser = argparse.ArgumentParser(prog='nrx', description="nrx - network topology exporter by netreplica")

sites_group = args_parser.add_mutually_exclusive_group()

args_parser.add_argument('-c', '--config', required=False, help='configuration file')
args_parser.add_argument('-i', '--input', required=False, help='input source: netbox (default) | cyjs',
default='netbox', type=arg_input_check,)
args_parser.add_argument('-o', '--output', required=False, help='output format: cyjs | gml | clab | cml | graphite | d2',
type=arg_output_check, )
args_parser.add_argument('-a', '--api', required=False, help='netbox API URL')
sites_group.add_argument('-s', '--site', required=False, help='netbox site to export, cannot be combined with --sites')
sites_group.add_argument( '--sites', required=False, help='netbox sites to export, for multiple tags use a comma-separated list: \
site1,site2,site3 (uses OR logic)')
args_parser.add_argument('-t', '--tags', required=False, help='netbox tags to export, for multiple tags use a comma-separated list: \
tag1,tag2,tag3 (uses AND logic)')
args_parser.add_argument('-n', '--name', required=False, help='name of the exported topology (site name or tags by default)')
args_parser.add_argument( '--noconfigs', required=False, help='disable device configuration export (enabled by default)',
action=argparse.BooleanOptionalAction)
args_parser.add_argument('-k', '--insecure', required=False, help='allow insecure server connections when using TLS',
action=argparse.BooleanOptionalAction)
args_parser.add_argument('-d', '--debug', required=False, help='enable debug output',
action=argparse.BooleanOptionalAction)
args_parser.add_argument('-f', '--file', required=False, help='file with the network graph to import')
args_parser.add_argument('-T', '--templates', required=False, help='directory with template files, \
will be prepended to TEMPLATES_PATH list \
in the configuration file')
args_parser.add_argument('-D', '--dir', required=False, help='save files into specified directory. \
nested relative and absolute paths are OK \
(topology name is used by default)')

args = args_parser.parse_args()
global DEBUG_ON
DEBUG_ON = args.debug is True
debug(f"arguments {args}")
Expand Down Expand Up @@ -857,7 +863,9 @@ def config_apply_netbox_args(config, args):
error("Need an API URL to connect to NetBox.\nUse --api argument, NB_API_URL environment variable or key in --config file")
if len(config['nb_api_token']) == 0:
error("Need an API token to connect to NetBox.\nUse NB_API_TOKEN environment variable or key in --config file")
if args.sites is not None and len(args.sites) > 0:
if args.site is not None and len(args.site) > 0:
config['export_sites'] = args.site.split(',') # --site and --sites can be used interchangeably but not at the same time
elif args.sites is not None and len(args.sites) > 0:
config['export_sites'] = args.sites.split(',')
if args.tags is not None and len(args.tags) > 0:
config['export_tags'] = args.tags.split(',')
Expand Down
Loading

0 comments on commit fe25900

Please sign in to comment.