Skip to content

Commit

Permalink
feat: added parsing resources from YAML file; support 3options for sr…
Browse files Browse the repository at this point in the history
…c/dst edge declaration; YAML definitions can be augmented with other resources
  • Loading branch information
tsypuk committed May 17, 2023
1 parent 14ba276 commit e1e847d
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ venv/

dist/
/__pycache__/
tests/__pycache__/
multicloud_diagrams/__pycache__/
*.iml

Expand Down
31 changes: 31 additions & 0 deletions multicloud_diagrams/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import logging
import os.path

import yaml


class MultiCloudDiagrams:
def __init__(self):
Expand Down Expand Up @@ -297,6 +299,35 @@ def add_link_list(self, links):
self.add_link(src_node_id=link['sourceNodeID'], dst_node_id=link['destinationNodeID'])
return

def augment_from_yaml(self, yaml_name: str):
with open(yaml_name, 'r') as file:
data = yaml.safe_load(file)
for vertex in data['vertices']:
self.add_vertex(
id=vertex['arn'],
node_name=vertex['name'],
arn=vertex['arn'],
node_type=vertex['type'],
# optional attributes
metadata={},
# icon
)
for edge in data['edges']:
self.add_link(
src_node_id=self._build_vertex_id(data['vertices'], edge, 'src'),
dst_node_id=self._build_vertex_id(data['vertices'], edge, 'dst'),
action=[edge['label']]
)

def _build_vertex_id(self, vertex_details, edge, src_dst_marker: str):
resource = {}
if f'{src_dst_marker}_arn' in edge:
resource["type"] = edge[f"{src_dst_marker}_type"]
resource["arn"] = edge[f'{src_dst_marker}_arn']
elif src_dst_marker in edge:
resource = [vertex for vertex in vertex_details if vertex['name'] == edge[src_dst_marker]][0]
return f'{resource["type"]}:{resource["arn"]}'

def read_coords_from_file(self, file_name: str):
if os.path.isfile(file_name):
tree = et.parse(file_name)
Expand Down
52 changes: 51 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ python = "^3.10"

[tool.poetry.group.dev.dependencies]
git-changelog = "^1.0.1"
pyyaml = "^6.0"

[build-system]
requires = ["poetry-core"]
Expand Down

0 comments on commit e1e847d

Please sign in to comment.