Skip to content

Commit

Permalink
Merge pull request #118 from netreplica/interface-tags
Browse files Browse the repository at this point in the history
Capability to filter links between devices via interface tags
  • Loading branch information
bortok authored May 1, 2024
2 parents 749a295 + 84edf96 commit 30c0bff
Show file tree
Hide file tree
Showing 12 changed files with 649 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pypi.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Publish Python distribution to PyPI and TestPyPI
name: Publish Python distribution to PyPI

on:
push:
Expand Down Expand Up @@ -49,5 +49,5 @@ jobs:
with:
name: python-package-distributions
path: dist/
- name: Publish distribution to TestPyPI
- name: Publish distribution to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
57 changes: 57 additions & 0 deletions .github/workflows/testpypi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: Publish Python distribution to TestPyPI

on:
push:
branches: [ "dev" ]
pull_request:
branches: [ "dev" ]
workflow_dispatch:

jobs:
build:
name: Build distribution
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.x"
- name: Install pypa/build
run: >-
python3 -m
pip install
build
--user
- name: Build a binary wheel and a source tarball
run: make build
- name: Store the distribution packages
uses: actions/upload-artifact@v3
with:
name: python-package-distributions
path: dist/

publish-to-pypi:
name: Publish to TestPyPI
needs:
- build
runs-on: ubuntu-latest

environment:
name: devtest
url: https://test.pypi.org/p/nrx

permissions:
id-token: write # IMPORTANT: mandatory for trusted publishing

steps:
- name: Download all the dists
uses: actions/download-artifact@v3
with:
name: python-package-distributions
path: dist/
- name: Publish distribution to TestPyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
repository-url: https://test.pypi.org/legacy/
14 changes: 12 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ test-d2: test-dc1-cyjs-2-d2

test-dc1: test-dc1-nb-2-cyjs-previous test-dc1-nb-2-cyjs-current test-dc1-nb-2-cyjs-latest test-dc1-cyjs-2-clab test-dc1-cyjs-2-clab-custom-platform-map test-dc1-cyjs-2-graphite test-dc1-cyjs-2-d2
test-dc2: test-dc2-nb-2-cyjs-previous 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-previous test-colo-nb-2-cyjs-current test-colo-nb-2-cyjs-latest
test-colo: test-colo-nb-2-cyjs-previous test-colo-nb-2-cyjs-current test-colo-nb-2-cyjs-latest test-colo-nb-2-cyjs-interface-tags
test-site1: test-site1-nb-2-cyjs-previous 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-previous 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-current test-lrg-nb-2-cyjs-latest test-lrg-cyjs-2-graphite
Expand Down Expand Up @@ -235,7 +235,17 @@ test-colo-nb-2-cyjs-latest:
mkdir -p tests/colo/test && cd tests/colo/test && rm -rf * && \
source ../../.env_latest && \
../../../nrx -c ../nrx.conf -o cyjs -d && \
diff colo.cyjs ../data/colo.cyjs
diff colo.cyjs ../data/colo-latest.cyjs
@echo

test-colo-nb-2-cyjs-interface-tags:
@echo "#################################################################"
@echo "# Colo: read from NetBox latest version and export as CYJS using interface tags"
@echo "#################################################################"
mkdir -p tests/colo/test && cd tests/colo/test && rm -rf * && \
source ../../.env_latest && \
../../../nrx -c ../nrx.conf -o cyjs --interface-tags one,two,four -d && \
diff colo.cyjs ../data/colo-tags.cyjs
@echo

test-site1-nb-2-cyjs-previous:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ optional arguments:
-s, --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)
--interface-tags TAGS netbox tags to filter interfaces to export, for multiple tags use a comma-separated list: tag1,tag2,tag3 (uses OR logic)
-n, --name NAME name of the exported topology (site name or tags by default)
--noconfigs disable device configuration export (enabled by default)
-k, --insecure allow insecure server connections when using TLS
Expand Down
2 changes: 1 addition & 1 deletion src/nrx/__about__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
"""
Metadata for the nrx package
"""
__version__ = "0.5.2"
__version__ = "0.6.0rc0"
18 changes: 17 additions & 1 deletion src/nrx/nrx.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,16 @@ def _get_nb_interfaces(self, block_size = 4):
cabled=True,
connected=True)):
if "base" in interface.type.value: # only ethernet interfaces
debug(interface.device, ":", interface, ":", interface.type.value)
if len(self.config['export_interface_tags']) > 0:
tag_match = False
for tag in interface.tags:
if tag.name in self.config['export_interface_tags']: # implementing OR tag matching logic
tag_match = True
break
if len(self.config['export_interface_tags']) > 0 and not tag_match:
debug(f"{interface.device} : {interface} skipping, doesn't have any of the required tags")
continue
debug(f"{interface.device} : {interface} adding as {interface.type.value}")
i = {
"id": interface.id,
"type": "interface",
Expand Down Expand Up @@ -1014,6 +1023,9 @@ def parse_args():
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('--interface-tags', required=False, help='netbox tags to filter interfaces to export, for multiple tags use a comma-separated list: \
tag1,tag2,tag3 (uses OR logic)',
metavar='TAGS')
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)
Expand Down Expand Up @@ -1162,6 +1174,7 @@ def load_toml_config(filename):
},
'export_sites': [],
'export_tags': [],
'export_interface_tags': [],
'topology_name': '',
'export_configs': True,
'templates_path': ["./templates", f"{nrx_config_dir()}/templates"],
Expand Down Expand Up @@ -1212,6 +1225,9 @@ def config_apply_netbox_args(config, args):
if args.tags is not None and len(args.tags) > 0:
config['export_tags'] = args.tags.split(',')
debug(f"List of tags to filter devices for export: {config['export_tags']}")
if args.interface_tags is not None and len(args.interface_tags) > 0:
config['export_interface_tags'] = args.interface_tags.split(',')
debug(f"List of tags to filter interfaces for export: {config['export_interface_tags']}")
if len(config['export_sites']) == 0 and len(config['export_tags']) == 0:
error("Need a Site name or Tags to export. Use --sites/--tags arguments, or EXPORT_SITE/EXPORT_TAGS key in --config file")
if args.noconfigs is not None:
Expand Down
Loading

0 comments on commit 30c0bff

Please sign in to comment.