Skip to content

Commit a6b9e30

Browse files
Merge pull request #159 from Cloud-Architects/diagrams_net
#97 adjusted report, added docs
2 parents ff3605f + aa4cc21 commit a6b9e30

File tree

7 files changed

+53
-17
lines changed

7 files changed

+53
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ To further increase security, you can add a block to check `aws:MultiFactorAuthP
130130
Cloudiscovery provides a CLI to easily perform desired actions.
131131
132132
### AWS VPC
133-
Example of a diagram:
133+
Example of a diagram ([diagrams.net](https://www.diagrams.net/) supported):
134134
135135
![diagrams logo](docs/assets/aws-vpc.png)
136136

cloudiscovery/provider/vpc/resource/network.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def get_resources(self) -> List[Resource]:
9999

100100
for data in response["NatGateways"]:
101101

102-
if data["VpcId"] == self.vpc_options.vpc_id:
102+
if data["VpcId"] == self.vpc_options.vpc_id and data["State"] != "deleted":
103103
nametag = get_name_tag(data)
104104

105105
name = data["NatGatewayId"] if nametag is None else nametag

cloudiscovery/shared/diagram.py

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,9 @@ def generate_diagram(
404404
with open(output_filename, "w") as diagram_file:
405405
diagram_file.write(diagram)
406406

407+
message_handler("\n\nDiagrams.net diagram generated", "HEADER")
408+
message_handler("Check your diagram: " + output_filename, "OKBLUE")
409+
407410
@staticmethod
408411
def decode_inflate(value: str):
409412
decoded = base64.b64decode(value)
@@ -442,20 +445,22 @@ def build_diagram(
442445

443446
added_resources: List[ResourceDigest] = []
444447

448+
vpc_box_height = 56565656
449+
subnet_box_height = 424242
445450
vpc_cell = (
446451
'<mxCell id="zB3y0Dp3mfEUP9Fxs3Er-{0}" value="{1}" style="points=[[0,0],[0.25,0],[0.5,0],'
447452
"[0.75,0],[1,0],[1,0.25],[1,0.5],[1,0.75],[1,1],[0.75,1],[0.5,1],[0.25,1],[0,1],[0,0.75],"
448453
"[0,0.5],[0,0.25]];outlineConnect=0;gradientColor=none;html=1;whiteSpace=wrap;fontSize=12;"
449454
"fontStyle=0;shape=mxgraph.aws4.group;grIcon=mxgraph.aws4.group_vpc;strokeColor=#248814;"
450455
'fillColor=none;verticalAlign=top;align=left;spacingLeft=30;fontColor=#AAB7B8;dashed=0;" '
451-
'parent="1" vertex="1"><mxGeometry x="0" y="0" width="1040" height="1000" as="geometry" />'
452-
"</mxCell>".format(cell_id, vpc_resource.name)
456+
'parent="1" vertex="1"><mxGeometry x="0" y="0" width="960" height="{2}" as="geometry" />'
457+
"</mxCell>".format(cell_id, vpc_resource.name, vpc_box_height)
453458
)
454459
cell_id += 1
455460
mx_graph_model += vpc_cell
456461

457-
public_subnet_x = 80
458-
public_subnet_y = 80
462+
public_subnet_x = 40
463+
public_subnet_y = 40
459464
cell_id += 1
460465
# pylint: disable=line-too-long
461466
public_subnet = (
@@ -465,13 +470,17 @@ def build_diagram(
465470
"fontStyle=0;shape=mxgraph.aws4.group;grIcon=mxgraph.aws4.group_security_group;grStroke=0;"
466471
"strokeColor=#248814;fillColor=#E9F3E6;verticalAlign=top;align=left;spacingLeft=30;"
467472
'fontColor=#248814;dashed=0;" vertex="1" parent="1"><mxGeometry x="{X}" y="{Y}" width="420" '
468-
'height="500" as="geometry" /></mxCell>'.format_map(
469-
{"X": str(public_subnet_x), "Y": str(public_subnet_y)}
473+
'height="{H}" as="geometry" /></mxCell>'.format_map(
474+
{
475+
"X": str(public_subnet_x),
476+
"Y": str(public_subnet_y),
477+
"H": subnet_box_height,
478+
}
470479
)
471480
)
472481
mx_graph_model += public_subnet
473482

474-
mx_graph_model = self.render_subnet_items(
483+
(mx_graph_model, public_rows) = self.render_subnet_items(
475484
added_resources,
476485
mx_graph_model,
477486
"{public subnet}",
@@ -481,8 +490,8 @@ def build_diagram(
481490
resources,
482491
)
483492

484-
private_subnet_x = 580
485-
private_subnet_y = 80
493+
private_subnet_x = 480
494+
private_subnet_y = 40
486495
cell_id += 1
487496
private_subnet = (
488497
'<mxCell id="private_area_id" value="Private subnet" style="points=[[0,0],[0.25,0],'
@@ -491,13 +500,17 @@ def build_diagram(
491500
"fontSize=12;fontStyle=0;shape=mxgraph.aws4.group;grIcon=mxgraph.aws4.group_security_group;"
492501
"grStroke=0;strokeColor=#147EBA;fillColor=#E6F2F8;verticalAlign=top;align=left;"
493502
'spacingLeft=30;fontColor=#147EBA;dashed=0;" vertex="1" parent="1"><mxGeometry '
494-
'x="{X}" y="{Y}" width="420" height="500" as="geometry" /></mxCell>'.format_map(
495-
{"X": str(private_subnet_x), "Y": str(private_subnet_y)}
503+
'x="{X}" y="{Y}" width="420" height="{H}" as="geometry" /></mxCell>'.format_map(
504+
{
505+
"X": str(private_subnet_x),
506+
"Y": str(private_subnet_y),
507+
"H": subnet_box_height,
508+
}
496509
)
497510
)
498511
mx_graph_model += private_subnet
499512

500-
mx_graph_model = self.render_subnet_items(
513+
(mx_graph_model, private_rows) = self.render_subnet_items(
501514
added_resources,
502515
mx_graph_model,
503516
"{private subnet}",
@@ -506,6 +519,12 @@ def build_diagram(
506519
resource_relations,
507520
resources,
508521
)
522+
subnet_rows = max(public_rows, private_rows)
523+
new_subnet_box_height = subnet_rows * 100 + 80
524+
525+
mx_graph_model = mx_graph_model.replace(
526+
str(subnet_box_height), str(new_subnet_box_height)
527+
)
509528

510529
count = 0
511530
row = 0
@@ -524,7 +543,7 @@ def build_diagram(
524543
{
525544
"CELL_IDX": resource.digest.to_string(),
526545
"X": str(count * 140 + public_subnet_x + 40),
527-
"Y": str(580 + row * 100 + 40),
546+
"Y": str(new_subnet_box_height + row * 100 + 60),
528547
"STYLE": style.replace("fontSize=12", "fontSize=8"),
529548
"TITLE": resource.name,
530549
}
@@ -535,6 +554,11 @@ def build_diagram(
535554
row += 1
536555
count = 0
537556

557+
new_vpc_box_height = new_subnet_box_height + 100 * row + 180
558+
mx_graph_model = mx_graph_model.replace(
559+
str(vpc_box_height), str(new_vpc_box_height)
560+
)
561+
538562
mx_graph_model += DIAGRAM_SUFFIX
539563
return MX_FILE.replace("<MX_GRAPH>", self.deflate_encode(mx_graph_model))
540564

@@ -548,7 +572,7 @@ def render_subnet_items(
548572
subnet_y,
549573
resource_relations,
550574
resources,
551-
):
575+
) -> (str, int):
552576
count = 0
553577
row = 0
554578
# pylint: disable=too-many-nested-blocks
@@ -581,4 +605,4 @@ def render_subnet_items(
581605
if count % 3 == 0:
582606
row += 1
583607
count = 0
584-
return mx_graph_model
608+
return mx_graph_model, row

cloudiscovery/shared/report.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ def html_report(
117117
with open(image_name, "rb") as image_file:
118118
diagram_image = base64.b64encode(image_file.read()).decode("utf-8")
119119

120+
"""generate diagrams.net link"""
121+
diagramsnet_image = None
122+
if filename is not None:
123+
image_name = PATH_DIAGRAM_OUTPUT + filename + ".drawio"
124+
if os.path.exists(image_name):
125+
diagramsnet_image = f"..{os.path.sep}..{os.path.sep}" + image_name
126+
120127
group_title = "Group"
121128
if resources:
122129
if resources[0].limits:
@@ -131,6 +138,7 @@ def html_report(
131138
resources_found=resources,
132139
resources_relations=resource_relations,
133140
diagram_image=diagram_image,
141+
diagramsnet_image=diagramsnet_image,
134142
group_title=group_title,
135143
)
136144

cloudiscovery/templates/report_html.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,9 @@
5858
{% set base64img = "data:image/png;base64," + diagram_image %}
5959
<img src={{ base64img }} widht="1024" height="768">
6060
{%endif %}
61+
{% if diagramsnet_image is not none %}
62+
<p>Diagram file for <a href="https://diagrams.net/">diagrams.net</a></p>
63+
<a href={{ diagramsnet_image }}>diagrams.net diagram</a>
64+
{%endif %}
6165

6266
<p>&nbsp;</p>

docs/.DS_Store

0 Bytes
Binary file not shown.

docs/assets/aws-vpc.png

-5.24 KB
Loading

0 commit comments

Comments
 (0)