Skip to content

Commit

Permalink
Merge pull request #10 from rogerscuall/feature/attachment_name
Browse files Browse the repository at this point in the history
Feature/attachment name
  • Loading branch information
rogerscuall authored Mar 1, 2024
2 parents 7fb4b15 + 8c76f96 commit 9d139ec
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 deletions.
39 changes: 22 additions & 17 deletions aws/awsrouter/transit_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
29 changes: 26 additions & 3 deletions aws/awsrouter/transit_gateway_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}
Expand Down

0 comments on commit 9d139ec

Please sign in to comment.