diff --git a/aws/awsrouter/transit_gateway.go b/aws/awsrouter/transit_gateway.go index f345454..d62f89c 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,27 @@ 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 + } + if name, err = GetNamesFromTags(tags); err != nil { + fmt.Println("error getting the name from the tags") + continue + } + tempAttachment[att.ID] = name } + att.Name = tempAttachment[att.ID] } } return nil diff --git a/aws/awsrouter/transit_gateway_helpers.go b/aws/awsrouter/transit_gateway_helpers.go index 0e4e161..56c6e97 100644 --- a/aws/awsrouter/transit_gateway_helpers.go +++ b/aws/awsrouter/transit_gateway_helpers.go @@ -27,8 +27,24 @@ func ExportTgwRoutesExcel(tgws []*Tgw, folder fs.FileInfo) error { } 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 { + for _, attachment := range tgwRouteTable.Attachments { + if _, ok := attachMap[attachment.ID]; !ok { + if attachment.Name != "" { + attachMap[attachment.ID] = attachment.Name + } + } + } + } + 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 @@ -42,11 +58,18 @@ func ExportTgwRoutesExcel(tgws []*Tgw, folder fs.FileInfo) error { } state := fmt.Sprint(route.State) routeType := fmt.Sprint(route.Type) - var attachmentName = "-" + // for _, attachment := range tgwRouteTable.Attachments { + // fmt.Println("attachment id:", attachment.ID) + // fmt.Println("attachment name:", attachment.Name) + // } + var attachmentName string if len(route.TransitGatewayAttachments) != 0 { attachmentID := fmt.Sprint(*route.TransitGatewayAttachments[0].TransitGatewayAttachmentId) - attachmentName = tgwRouteTable.GetAttachmentName(attachmentID) - if attachmentName == "" { + fmt.Println("attachment id to find:", attachmentID) + name, ok := attachMap[attachmentID] + if ok { + attachmentName = name + } else { attachmentName = attachmentID } }