17
17
18
18
PATH_DIAGRAM_OUTPUT = "./assets/diagrams/"
19
19
DIAGRAM_CLUSTER = "diagram_cluster"
20
+ DIAGRAM_ROW_HEIGHT = 100
20
21
21
22
22
23
class Mapsources :
@@ -423,7 +424,7 @@ def deflate_encode(value: str):
423
424
"utf-8"
424
425
)
425
426
426
- # pylint: disable=too-many-locals
427
+ # pylint: disable=too-many-locals,too-many-statements
427
428
def build_diagram (
428
429
self ,
429
430
resources : Dict [str , List [Resource ]],
@@ -459,68 +460,88 @@ def build_diagram(
459
460
cell_id += 1
460
461
mx_graph_model += vpc_cell
461
462
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
480
468
)
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
491
471
)
492
472
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 ,
509
542
)
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
- )
522
543
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
524
545
525
546
mx_graph_model = mx_graph_model .replace (
526
547
str (subnet_box_height ), str (new_subnet_box_height )
@@ -543,7 +564,9 @@ def build_diagram(
543
564
{
544
565
"CELL_IDX" : resource .digest .to_string (),
545
566
"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
+ ),
547
570
"STYLE" : style .replace ("fontSize=12" , "fontSize=8" ),
548
571
"TITLE" : resource .name ,
549
572
}
@@ -554,7 +577,7 @@ def build_diagram(
554
577
row += 1
555
578
count = 0
556
579
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
558
581
mx_graph_model = mx_graph_model .replace (
559
582
str (vpc_box_height ), str (new_vpc_box_height )
560
583
)
@@ -572,7 +595,11 @@ def render_subnet_items(
572
595
subnet_y ,
573
596
resource_relations ,
574
597
resources ,
598
+ has_other_subnet ,
575
599
) -> (str , int ):
600
+ items_in_row = 6
601
+ if has_other_subnet :
602
+ items_in_row = 3
576
603
count = 0
577
604
row = 0
578
605
# pylint: disable=too-many-nested-blocks
@@ -595,14 +622,21 @@ def render_subnet_items(
595
622
{
596
623
"CELL_IDX" : relation .from_node .to_string (),
597
624
"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 ),
599
626
"STYLE" : style .replace ("fontSize=12" , "fontSize=8" ),
600
627
"TITLE" : resource .name ,
601
628
}
602
629
)
603
630
count += 1
604
631
mx_graph_model += cell
605
- if count % 3 == 0 :
632
+ if count % items_in_row == 0 :
606
633
row += 1
607
634
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