Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

D2 export #60

Merged
merged 9 commits into from
Jul 16, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

* Topology file for [Containerlab](https://containerlab.dev) tool for container-based networking labs
* Topology file for [Cisco Modeling Labs](https://developer.cisco.com/modeling-labs/) platform for network simulation
* Topology data for visualization using [Graphite](https://github.com/netreplica/graphite)
* Topology data for visualization using [Graphite](https://github.com/netreplica/graphite) or [D2](https://d2lang.com/)
* Graph data as a JSON file in [Cytoscape](https://cytoscape.org/) format [CYJS](http://manual.cytoscape.org/en/stable/Supported_Network_File_Formats.html#cytoscape-js-json)

It can also read the topology graph previously saved as a CYJS file to convert it into other formats.
Expand Down Expand Up @@ -57,7 +57,7 @@ Export capabilities:
* Exports the graph as a Containerlab topology definition file in YAML format
* Exports the graph as a Cisco Modeling Labs (CML) topology definition file in YAML format
* Exported device configurations will be used as `startup-config` for Containerlab and CML
* Exports the graph in JSON format for visualization with Graphite
* Exports the graph in formats for visualization with Graphite or D2
* Uses NetBox Device Platform `slug` field to identify node templates when rendering the export file
* Creates mapping between real interface names and interface names used by the supported lab tools
* Calculates `level` and `rank` values for each node based on Device Role to help visualize the topology
Expand Down Expand Up @@ -136,7 +136,7 @@ optional arguments:
-h, --help show this help message and exit
-c, --config CONFIG configuration file
-i, --input INPUT input source: netbox (default) | cyjs
-o, --output OUTPUT output format: cyjs | gml | clab | cml | graphite
-o, --output OUTPUT output format: cyjs | gml | clab | cml | graphite | d2
-a, --api API netbox API URL
-s, --site SITE netbox site to export
-t, --tags TAGS netbox tags to export, for multiple tags use a comma-separated list: tag1,tag2,tag3 (uses AND logic)
Expand Down
15 changes: 10 additions & 5 deletions nrx/nrx.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,9 @@ def _write_topology(self, topo):
if self.config['output_format'] == 'graphite':
# <topology-name>.graphite.json
topo_file = f"{self.topology['name']}.{self.config['output_format']}.json"
elif self.config['output_format'] == 'd2':
# <topology-name>.d2
topo_file = f"{self.topology['name']}.{self.config['output_format']}"
else:
# <topology-name>.clab.yaml or <topology-name>.cml.yaml
topo_file = f"{self.topology['name']}.{self.config['output_format']}.yaml"
Expand Down Expand Up @@ -650,11 +653,13 @@ def _print_motd(self, topo):
print(f"{topo_dict['motd']}")
elif self.config['output_format'] == 'clab':
print(f"To deploy this topology, run: sudo -E clab dep -t {self.files_path}/{self.topology['name']}.clab.yaml")
elif self.config['output_format'] == 'd2':
print(f"To visualize this D2 topology, open https://play.d2lang.com and paste content of the file: {self.files_path}/{self.topology['name']}.d2")

def _render_interface_map(self, node):
"""Render interface mapping file for a node"""
if self.config['output_format'] == 'graphite':
# No need to render interface maps for Graphite
if self.config['output_format'] in ['graphite', 'd2']:
# No need to render interface maps
return None
if 'name' in node and node['name'] in self.device_interfaces_map:
d = node['name']
Expand Down Expand Up @@ -713,7 +718,7 @@ def arg_input_check(s):

def arg_output_check(s):
"""Check if output format is supported"""
allowed_values = ['gml', 'cyjs', 'clab', 'cml', 'graphite']
allowed_values = ['gml', 'cyjs', 'clab', 'cml', 'graphite', 'd2']
if s in allowed_values:
return s
raise argparse.ArgumentTypeError(f"output format has to be one of {allowed_values}")
Expand All @@ -724,7 +729,7 @@ def parse_args():
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',
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', '--site', required=False, help='netbox site to export')
Expand Down Expand Up @@ -884,7 +889,7 @@ def main():
else:
topo.build_from_graph(nb_network.graph())

if config['output_format'] in ['clab', 'cml', 'graphite']:
if config['output_format'] in ['clab', 'cml', 'graphite', 'd2']:
topo.export_topology()
else:
if nb_network is None:
Expand Down
2 changes: 1 addition & 1 deletion templates