From 716f94bcc5d2f993f873376716c49e33bf4a05fe Mon Sep 17 00:00:00 2001 From: Roger Gomez Date: Wed, 28 Feb 2024 17:01:13 -0500 Subject: [PATCH 1/9] Update attachment names in TgwRouteTablesAttachments function --- aws/awsrouter/transit_gateway.go | 40 ++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/aws/awsrouter/transit_gateway.go b/aws/awsrouter/transit_gateway.go index f345454..a4d105e 100644 --- a/aws/awsrouter/transit_gateway.go +++ b/aws/awsrouter/transit_gateway.go @@ -89,6 +89,8 @@ func (t *Tgw) UpdateTgwRoutes(ctx context.Context, api ports.AWSRouter) error { // UpdateTgwRouteTablesAttachments updates the Attachments of a TgwRouteTable. func (t *Tgw) UpdateTgwRouteTablesAttachments(ctx context.Context, api ports.AWSRouter) error { + tempAttachment := make(map[string]string) + var name string for _, tgwRouteTable := range t.RouteTables { input := ports.TgwRouteTableAssociationInputFilter(tgwRouteTable.ID) result, err := ports.GetTgwRouteTableAssociations(ctx, api, input) @@ -102,24 +104,28 @@ func (t *Tgw) UpdateTgwRouteTablesAttachments(ctx context.Context, api ports.AWS // Update attachment names for _, att := range tgwRouteTable.Attachments { - attInput := ec2.DescribeTransitGatewayAttachmentsInput{} - attInput.TransitGatewayAttachmentIds = []string{att.ID} - attOutput, err := ports.GetTgwAttachments(ctx, api, &attInput) - if err != nil { - return fmt.Errorf("error retrieving Transit Gateway Attachments: %w", err) - } - if len(attOutput.TransitGatewayAttachments) != 1 { - fmt.Print("there is more than one attachment with the same ID") - } - tags := attOutput.TransitGatewayAttachments[0].Tags - if len(tags) == 0 { - continue - } - name, err := GetNamesFromTags(tags) - if err == nil { - att.Name = name - fmt.Printf("Attachment %s has name %s\n", att.ID, att.Name) + if _, ok := tempAttachment[att.ID]; !ok { + attInput := ec2.DescribeTransitGatewayAttachmentsInput{} + attInput.TransitGatewayAttachmentIds = []string{att.ID} + attOutput, err := ports.GetTgwAttachments(ctx, api, &attInput) + if err != nil { + return fmt.Errorf("error retrieving Transit Gateway Attachments: %w", err) + } + if len(attOutput.TransitGatewayAttachments) != 1 { + fmt.Print("there is more than one attachment with the same ID") + } + tags := attOutput.TransitGatewayAttachments[0].Tags + if len(tags) == 0 { + continue + } + name, err = GetNamesFromTags(tags) + if err != nil { + fmt.Println("error getting the name from the tags") + continue + } + tempAttachment[att.ID] = name } + att.Name = tempAttachment[att.ID] } } return nil From 049cb45180a2c35cb97d3805b621225a597ad6c4 Mon Sep 17 00:00:00 2001 From: Roger Gomez Date: Wed, 28 Feb 2024 17:31:33 -0500 Subject: [PATCH 2/9] Fix error in getting name from tags and add debug print statement --- aws/awsrouter/transit_gateway.go | 3 +-- aws/awsrouter/transit_gateway_helpers.go | 3 +++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/aws/awsrouter/transit_gateway.go b/aws/awsrouter/transit_gateway.go index a4d105e..d62f89c 100644 --- a/aws/awsrouter/transit_gateway.go +++ b/aws/awsrouter/transit_gateway.go @@ -118,8 +118,7 @@ func (t *Tgw) UpdateTgwRouteTablesAttachments(ctx context.Context, api ports.AWS if len(tags) == 0 { continue } - name, err = GetNamesFromTags(tags) - if err != nil { + if name, err = GetNamesFromTags(tags); err != nil { fmt.Println("error getting the name from the tags") continue } diff --git a/aws/awsrouter/transit_gateway_helpers.go b/aws/awsrouter/transit_gateway_helpers.go index 0e4e161..2c9f812 100644 --- a/aws/awsrouter/transit_gateway_helpers.go +++ b/aws/awsrouter/transit_gateway_helpers.go @@ -42,6 +42,9 @@ func ExportTgwRoutesExcel(tgws []*Tgw, folder fs.FileInfo) error { } state := fmt.Sprint(route.State) routeType := fmt.Sprint(route.Type) + for _, attachment := range tgwRouteTable.Attachments { + fmt.Println(attachment) + } var attachmentName = "-" if len(route.TransitGatewayAttachments) != 0 { attachmentID := fmt.Sprint(*route.TransitGatewayAttachments[0].TransitGatewayAttachmentId) From 31eced8eee77c82c3695653354eb5912c057df11 Mon Sep 17 00:00:00 2001 From: Roger Gomez Date: Wed, 28 Feb 2024 17:33:49 -0500 Subject: [PATCH 3/9] cleanup --- aws/awsrouter/transit_gateway_helpers.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/aws/awsrouter/transit_gateway_helpers.go b/aws/awsrouter/transit_gateway_helpers.go index 2c9f812..475a338 100644 --- a/aws/awsrouter/transit_gateway_helpers.go +++ b/aws/awsrouter/transit_gateway_helpers.go @@ -43,7 +43,8 @@ func ExportTgwRoutesExcel(tgws []*Tgw, folder fs.FileInfo) error { state := fmt.Sprint(route.State) routeType := fmt.Sprint(route.Type) for _, attachment := range tgwRouteTable.Attachments { - fmt.Println(attachment) + fmt.Println("attachment id:", attachment.ID) + fmt.Println("attachment name:", attachment.Name) } var attachmentName = "-" if len(route.TransitGatewayAttachments) != 0 { From 28594e1cdb24b33204ddc8bf018f1ee3a2c31727 Mon Sep 17 00:00:00 2001 From: Roger Gomez Date: Wed, 28 Feb 2024 19:35:23 -0500 Subject: [PATCH 4/9] cleanup --- aws/awsrouter/transit_gateway_helpers.go | 1 + 1 file changed, 1 insertion(+) diff --git a/aws/awsrouter/transit_gateway_helpers.go b/aws/awsrouter/transit_gateway_helpers.go index 475a338..a4db7f8 100644 --- a/aws/awsrouter/transit_gateway_helpers.go +++ b/aws/awsrouter/transit_gateway_helpers.go @@ -48,6 +48,7 @@ func ExportTgwRoutesExcel(tgws []*Tgw, folder fs.FileInfo) error { } var attachmentName = "-" if len(route.TransitGatewayAttachments) != 0 { + fmt.Println("att len:", len(route.TransitGatewayAttachments)) attachmentID := fmt.Sprint(*route.TransitGatewayAttachments[0].TransitGatewayAttachmentId) attachmentName = tgwRouteTable.GetAttachmentName(attachmentID) if attachmentName == "" { From 4348f0626fa8e44da2d8cf7477178fe0a0ace991 Mon Sep 17 00:00:00 2001 From: Roger Gomez Date: Wed, 28 Feb 2024 19:36:48 -0500 Subject: [PATCH 5/9] cleanup --- aws/awsrouter/transit_gateway_helpers.go | 1 + 1 file changed, 1 insertion(+) diff --git a/aws/awsrouter/transit_gateway_helpers.go b/aws/awsrouter/transit_gateway_helpers.go index a4db7f8..9438b98 100644 --- a/aws/awsrouter/transit_gateway_helpers.go +++ b/aws/awsrouter/transit_gateway_helpers.go @@ -50,6 +50,7 @@ func ExportTgwRoutesExcel(tgws []*Tgw, folder fs.FileInfo) error { if len(route.TransitGatewayAttachments) != 0 { fmt.Println("att len:", len(route.TransitGatewayAttachments)) attachmentID := fmt.Sprint(*route.TransitGatewayAttachments[0].TransitGatewayAttachmentId) + fmt.Println("searching attachment:", attachmentID) attachmentName = tgwRouteTable.GetAttachmentName(attachmentID) if attachmentName == "" { attachmentName = attachmentID From 67598042c9ec5a6a36b1350ec5cc62c1f9ff402a Mon Sep 17 00:00:00 2001 From: Roger Gomez Date: Wed, 28 Feb 2024 19:48:16 -0500 Subject: [PATCH 6/9] cleanup --- aws/awsrouter/transit_gateway_helpers.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/aws/awsrouter/transit_gateway_helpers.go b/aws/awsrouter/transit_gateway_helpers.go index 9438b98..700b6eb 100644 --- a/aws/awsrouter/transit_gateway_helpers.go +++ b/aws/awsrouter/transit_gateway_helpers.go @@ -42,10 +42,10 @@ func ExportTgwRoutesExcel(tgws []*Tgw, folder fs.FileInfo) error { } state := fmt.Sprint(route.State) routeType := fmt.Sprint(route.Type) - for _, attachment := range tgwRouteTable.Attachments { - fmt.Println("attachment id:", attachment.ID) - fmt.Println("attachment name:", attachment.Name) - } + // for _, attachment := range tgwRouteTable.Attachments { + // fmt.Println("attachment id:", attachment.ID) + // fmt.Println("attachment name:", attachment.Name) + // } var attachmentName = "-" if len(route.TransitGatewayAttachments) != 0 { fmt.Println("att len:", len(route.TransitGatewayAttachments)) From faa772f75ec92eabc7d64dbf636ec86ba54daa6e Mon Sep 17 00:00:00 2001 From: Roger Gomez Date: Wed, 28 Feb 2024 20:04:18 -0500 Subject: [PATCH 7/9] cleanup --- aws/awsrouter/transit_gateway_helpers.go | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/aws/awsrouter/transit_gateway_helpers.go b/aws/awsrouter/transit_gateway_helpers.go index 700b6eb..248ab38 100644 --- a/aws/awsrouter/transit_gateway_helpers.go +++ b/aws/awsrouter/transit_gateway_helpers.go @@ -25,11 +25,23 @@ func ExportTgwRoutesExcel(tgws []*Tgw, folder fs.FileInfo) error { if !folder.IsDir() { return fmt.Errorf("folder %s is not a directory", folder.Name()) } + attachMap := make(map[string]string) folderName := folder.Name() for _, tgw := range tgws { + fmt.Println("Transit Gateway Name:", tgw.Name) f := excelize.NewFile() for _, tgwRouteTable := range tgw.RouteTables { + fmt.Println("Route Table Name:", tgwRouteTable.Name) sheet := f.NewSheet(tgwRouteTable.Name) + for _, attachment := range tgwRouteTable.Attachments { + if _, ok := attachMap[attachment.ID]; !ok { + attachMap[attachment.ID] = attachment.Name + } + } + fmt.Println("The following attachment name where found:") + for _, attachmentName := range attachMap { + fmt.Printf("\t %v\n", attachmentName) + } for i, route := range tgwRouteTable.Routes { // Only for the header if i == 0 { @@ -48,12 +60,11 @@ func ExportTgwRoutesExcel(tgws []*Tgw, folder fs.FileInfo) error { // } var attachmentName = "-" if len(route.TransitGatewayAttachments) != 0 { - fmt.Println("att len:", len(route.TransitGatewayAttachments)) + //fmt.Println("att len:", len(route.TransitGatewayAttachments)) attachmentID := fmt.Sprint(*route.TransitGatewayAttachments[0].TransitGatewayAttachmentId) - fmt.Println("searching attachment:", attachmentID) - attachmentName = tgwRouteTable.GetAttachmentName(attachmentID) - if attachmentName == "" { - attachmentName = attachmentID + name, ok := attachMap[attachmentID] + if ok { + attachmentName = name } } var prefixListId string From 37ba5b6b3e7c2c6d8cac9139121f9225f81aa8d0 Mon Sep 17 00:00:00 2001 From: Roger Gomez Date: Wed, 28 Feb 2024 20:26:41 -0500 Subject: [PATCH 8/9] cleanup --- aws/awsrouter/transit_gateway_helpers.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/aws/awsrouter/transit_gateway_helpers.go b/aws/awsrouter/transit_gateway_helpers.go index 248ab38..c5c09b8 100644 --- a/aws/awsrouter/transit_gateway_helpers.go +++ b/aws/awsrouter/transit_gateway_helpers.go @@ -35,12 +35,14 @@ func ExportTgwRoutesExcel(tgws []*Tgw, folder fs.FileInfo) error { sheet := f.NewSheet(tgwRouteTable.Name) for _, attachment := range tgwRouteTable.Attachments { if _, ok := attachMap[attachment.ID]; !ok { - attachMap[attachment.ID] = attachment.Name + if attachment.Name != "" { + attachMap[attachment.ID] = attachment.Name + } } } fmt.Println("The following attachment name where found:") - for _, attachmentName := range attachMap { - fmt.Printf("\t %v\n", attachmentName) + for key, value := range attachMap { + fmt.Printf("\t %v->%v\n", key, value) } for i, route := range tgwRouteTable.Routes { // Only for the header @@ -60,8 +62,8 @@ func ExportTgwRoutesExcel(tgws []*Tgw, folder fs.FileInfo) error { // } var attachmentName = "-" if len(route.TransitGatewayAttachments) != 0 { - //fmt.Println("att len:", len(route.TransitGatewayAttachments)) attachmentID := fmt.Sprint(*route.TransitGatewayAttachments[0].TransitGatewayAttachmentId) + fmt.Println("attachment id to find:", attachmentID) name, ok := attachMap[attachmentID] if ok { attachmentName = name From 8c76f964bcaf516ce10ad432f43de3ac02e3a3ac Mon Sep 17 00:00:00 2001 From: Roger Gomez Date: Wed, 28 Feb 2024 20:53:33 -0500 Subject: [PATCH 9/9] creating a global map per tgw --- aws/awsrouter/transit_gateway_helpers.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/aws/awsrouter/transit_gateway_helpers.go b/aws/awsrouter/transit_gateway_helpers.go index c5c09b8..56c6e97 100644 --- a/aws/awsrouter/transit_gateway_helpers.go +++ b/aws/awsrouter/transit_gateway_helpers.go @@ -25,14 +25,12 @@ func ExportTgwRoutesExcel(tgws []*Tgw, folder fs.FileInfo) error { if !folder.IsDir() { return fmt.Errorf("folder %s is not a directory", folder.Name()) } - attachMap := make(map[string]string) folderName := folder.Name() for _, tgw := range tgws { + attachMap := make(map[string]string) fmt.Println("Transit Gateway Name:", tgw.Name) f := excelize.NewFile() for _, tgwRouteTable := range tgw.RouteTables { - fmt.Println("Route Table Name:", tgwRouteTable.Name) - sheet := f.NewSheet(tgwRouteTable.Name) for _, attachment := range tgwRouteTable.Attachments { if _, ok := attachMap[attachment.ID]; !ok { if attachment.Name != "" { @@ -40,10 +38,14 @@ func ExportTgwRoutesExcel(tgws []*Tgw, folder fs.FileInfo) error { } } } - fmt.Println("The following attachment name where found:") - for key, value := range attachMap { - fmt.Printf("\t %v->%v\n", key, value) - } + } + fmt.Println("The following attachment name where found in TGW:", tgw.Name) + for key, value := range attachMap { + fmt.Printf("\t %v->%v\n", key, value) + } + for _, tgwRouteTable := range tgw.RouteTables { + fmt.Println("Route Table Name:", tgwRouteTable.Name) + sheet := f.NewSheet(tgwRouteTable.Name) for i, route := range tgwRouteTable.Routes { // Only for the header if i == 0 { @@ -60,13 +62,15 @@ func ExportTgwRoutesExcel(tgws []*Tgw, folder fs.FileInfo) error { // fmt.Println("attachment id:", attachment.ID) // fmt.Println("attachment name:", attachment.Name) // } - var attachmentName = "-" + var attachmentName string if len(route.TransitGatewayAttachments) != 0 { attachmentID := fmt.Sprint(*route.TransitGatewayAttachments[0].TransitGatewayAttachmentId) fmt.Println("attachment id to find:", attachmentID) name, ok := attachMap[attachmentID] if ok { attachmentName = name + } else { + attachmentName = attachmentID } } var prefixListId string