Skip to content

Commit f07961b

Browse files
Merge pull request #160 from Cloud-Architects/vpc_diagram_adjustments
adjusted boxes height calculation
2 parents a6b9e30 + 2da15cc commit f07961b

File tree

1 file changed

+97
-63
lines changed

1 file changed

+97
-63
lines changed

cloudiscovery/shared/diagram.py

Lines changed: 97 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
PATH_DIAGRAM_OUTPUT = "./assets/diagrams/"
1919
DIAGRAM_CLUSTER = "diagram_cluster"
20+
DIAGRAM_ROW_HEIGHT = 100
2021

2122

2223
class Mapsources:
@@ -423,7 +424,7 @@ def deflate_encode(value: str):
423424
"utf-8"
424425
)
425426

426-
# pylint: disable=too-many-locals
427+
# pylint: disable=too-many-locals,too-many-statements
427428
def build_diagram(
428429
self,
429430
resources: Dict[str, List[Resource]],
@@ -459,68 +460,88 @@ def build_diagram(
459460
cell_id += 1
460461
mx_graph_model += vpc_cell
461462

462-
public_subnet_x = 40
463-
public_subnet_y = 40
464-
cell_id += 1
465-
# pylint: disable=line-too-long
466-
public_subnet = (
467-
'<mxCell id="public_area_id" value="Public subnet" style="points=[[0,0],[0.25,0],[0.5,0],'
468-
"[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],"
469-
"[0,0.5],[0,0.25]];outlineConnect=0;gradientColor=none;html=1;whiteSpace=wrap;fontSize=12;"
470-
"fontStyle=0;shape=mxgraph.aws4.group;grIcon=mxgraph.aws4.group_security_group;grStroke=0;"
471-
"strokeColor=#248814;fillColor=#E9F3E6;verticalAlign=top;align=left;spacingLeft=30;"
472-
'fontColor=#248814;dashed=0;" vertex="1" parent="1"><mxGeometry x="{X}" y="{Y}" width="420" '
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-
}
479-
)
463+
public_rows = 0
464+
private_rows = 0
465+
466+
has_public_resources = self.has_subnet_type(
467+
"{public subnet}", resource_relations
480468
)
481-
mx_graph_model += public_subnet
482-
483-
(mx_graph_model, public_rows) = self.render_subnet_items(
484-
added_resources,
485-
mx_graph_model,
486-
"{public subnet}",
487-
public_subnet_x,
488-
public_subnet_y,
489-
resource_relations,
490-
resources,
469+
has_private_resources = self.has_subnet_type(
470+
"{private subnet}", resource_relations
491471
)
492472

493-
private_subnet_x = 480
494-
private_subnet_y = 40
495-
cell_id += 1
496-
private_subnet = (
497-
'<mxCell id="private_area_id" value="Private subnet" style="points=[[0,0],[0.25,0],'
498-
"[0.5,0],[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],"
499-
"[0,0.75],[0,0.5],[0,0.25]];outlineConnect=0;gradientColor=none;html=1;whiteSpace=wrap;"
500-
"fontSize=12;fontStyle=0;shape=mxgraph.aws4.group;grIcon=mxgraph.aws4.group_security_group;"
501-
"grStroke=0;strokeColor=#147EBA;fillColor=#E6F2F8;verticalAlign=top;align=left;"
502-
'spacingLeft=30;fontColor=#147EBA;dashed=0;" vertex="1" parent="1"><mxGeometry '
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-
}
473+
subnet_box_width = "420"
474+
if not has_public_resources & has_private_resources:
475+
subnet_box_width = "880"
476+
477+
if has_public_resources:
478+
public_subnet_x = 40
479+
public_subnet_y = 40
480+
cell_id += 1
481+
# pylint: disable=line-too-long
482+
public_subnet = (
483+
'<mxCell id="public_area_id" value="Public subnet" style="points=[[0,0],[0.25,0],[0.5,0],'
484+
"[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],"
485+
"[0,0.5],[0,0.25]];outlineConnect=0;gradientColor=none;html=1;whiteSpace=wrap;fontSize=12;"
486+
"fontStyle=0;shape=mxgraph.aws4.group;grIcon=mxgraph.aws4.group_security_group;grStroke=0;"
487+
"strokeColor=#248814;fillColor=#E9F3E6;verticalAlign=top;align=left;spacingLeft=30;"
488+
'fontColor=#248814;dashed=0;" vertex="1" parent="1"><mxGeometry x="{X}" y="{Y}" width="{W}" '
489+
'height="{H}" as="geometry" /></mxCell>'.format_map(
490+
{
491+
"X": str(public_subnet_x),
492+
"Y": str(public_subnet_y),
493+
"H": subnet_box_height,
494+
"W": subnet_box_width,
495+
}
496+
)
497+
)
498+
mx_graph_model += public_subnet
499+
500+
(mx_graph_model, public_rows) = self.render_subnet_items(
501+
added_resources,
502+
mx_graph_model,
503+
"{public subnet}",
504+
public_subnet_x,
505+
public_subnet_y,
506+
resource_relations,
507+
resources,
508+
has_private_resources,
509+
)
510+
511+
if has_private_resources:
512+
private_subnet_x = 480
513+
private_subnet_y = 40
514+
cell_id += 1
515+
private_subnet = (
516+
'<mxCell id="private_area_id" value="Private subnet" style="points=[[0,0],[0.25,0],'
517+
"[0.5,0],[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],"
518+
"[0,0.75],[0,0.5],[0,0.25]];outlineConnect=0;gradientColor=none;html=1;whiteSpace=wrap;"
519+
"fontSize=12;fontStyle=0;shape=mxgraph.aws4.group;grIcon=mxgraph.aws4.group_security_group;"
520+
"grStroke=0;strokeColor=#147EBA;fillColor=#E6F2F8;verticalAlign=top;align=left;"
521+
'spacingLeft=30;fontColor=#147EBA;dashed=0;" vertex="1" parent="1"><mxGeometry '
522+
'x="{X}" y="{Y}" width="{W}" height="{H}" as="geometry" /></mxCell>'.format_map(
523+
{
524+
"X": str(private_subnet_x),
525+
"Y": str(private_subnet_y),
526+
"H": subnet_box_height,
527+
"W": subnet_box_width,
528+
}
529+
)
530+
)
531+
mx_graph_model += private_subnet
532+
533+
(mx_graph_model, private_rows) = self.render_subnet_items(
534+
added_resources,
535+
mx_graph_model,
536+
"{private subnet}",
537+
private_subnet_x,
538+
private_subnet_y,
539+
resource_relations,
540+
resources,
541+
has_public_resources,
509542
)
510-
)
511-
mx_graph_model += private_subnet
512-
513-
(mx_graph_model, private_rows) = self.render_subnet_items(
514-
added_resources,
515-
mx_graph_model,
516-
"{private subnet}",
517-
private_subnet_x,
518-
private_subnet_y,
519-
resource_relations,
520-
resources,
521-
)
522543
subnet_rows = max(public_rows, private_rows)
523-
new_subnet_box_height = subnet_rows * 100 + 80
544+
new_subnet_box_height = subnet_rows * DIAGRAM_ROW_HEIGHT + 40
524545

525546
mx_graph_model = mx_graph_model.replace(
526547
str(subnet_box_height), str(new_subnet_box_height)
@@ -543,7 +564,9 @@ def build_diagram(
543564
{
544565
"CELL_IDX": resource.digest.to_string(),
545566
"X": str(count * 140 + public_subnet_x + 40),
546-
"Y": str(new_subnet_box_height + row * 100 + 60),
567+
"Y": str(
568+
new_subnet_box_height + row * DIAGRAM_ROW_HEIGHT + 60
569+
),
547570
"STYLE": style.replace("fontSize=12", "fontSize=8"),
548571
"TITLE": resource.name,
549572
}
@@ -554,7 +577,7 @@ def build_diagram(
554577
row += 1
555578
count = 0
556579

557-
new_vpc_box_height = new_subnet_box_height + 100 * row + 180
580+
new_vpc_box_height = new_subnet_box_height + DIAGRAM_ROW_HEIGHT * row + 180
558581
mx_graph_model = mx_graph_model.replace(
559582
str(vpc_box_height), str(new_vpc_box_height)
560583
)
@@ -572,7 +595,11 @@ def render_subnet_items(
572595
subnet_y,
573596
resource_relations,
574597
resources,
598+
has_other_subnet,
575599
) -> (str, int):
600+
items_in_row = 6
601+
if has_other_subnet:
602+
items_in_row = 3
576603
count = 0
577604
row = 0
578605
# pylint: disable=too-many-nested-blocks
@@ -595,14 +622,21 @@ def render_subnet_items(
595622
{
596623
"CELL_IDX": relation.from_node.to_string(),
597624
"X": str(count * 140 + subnet_x + 40),
598-
"Y": str(subnet_y + row * 100 + 40),
625+
"Y": str(subnet_y + row * DIAGRAM_ROW_HEIGHT + 40),
599626
"STYLE": style.replace("fontSize=12", "fontSize=8"),
600627
"TITLE": resource.name,
601628
}
602629
)
603630
count += 1
604631
mx_graph_model += cell
605-
if count % 3 == 0:
632+
if count % items_in_row == 0:
606633
row += 1
607634
count = 0
608-
return mx_graph_model, row
635+
return mx_graph_model, row + 1
636+
637+
@staticmethod
638+
def has_subnet_type(subnet_id, resource_relations) -> bool:
639+
for relation in resource_relations:
640+
if relation.to_node == ResourceDigest(id=subnet_id, type="aws_subnet"):
641+
return True
642+
return False

0 commit comments

Comments
 (0)