From ea4e1cf02b7d068ce2bedfcb90f966ff9526a895 Mon Sep 17 00:00:00 2001 From: Roger Gomez Date: Wed, 28 Feb 2024 15:57:30 -0500 Subject: [PATCH 1/8] Adding the name to the attachment. --- aws/awsrouter/transit_gateway_attachment.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/aws/awsrouter/transit_gateway_attachment.go b/aws/awsrouter/transit_gateway_attachment.go index da3b33b..e1bd6df 100644 --- a/aws/awsrouter/transit_gateway_attachment.go +++ b/aws/awsrouter/transit_gateway_attachment.go @@ -21,6 +21,9 @@ type TgwAttachment struct { // The type of the resource where this attachment terminates. // Common values are: vpc, vpn, direct-connect ... Type string + + // The name of the TGW Attachment. + Name string } // newTgwAttach builds a TgwAttachment from a aws TransitGatewayRouteAttachment type. From dd5e46c827ad7b261d4b0b62c44e98fb4848fcb3 Mon Sep 17 00:00:00 2001 From: Roger Gomez Date: Wed, 28 Feb 2024 15:57:59 -0500 Subject: [PATCH 2/8] Update function name to GetTgwAttachments --- aws/awsrouter/transit_gateway_attachment.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/awsrouter/transit_gateway_attachment.go b/aws/awsrouter/transit_gateway_attachment.go index e1bd6df..f1f86b3 100644 --- a/aws/awsrouter/transit_gateway_attachment.go +++ b/aws/awsrouter/transit_gateway_attachment.go @@ -170,7 +170,7 @@ func (attPath *AttPath) Walk(ctx context.Context, api ports.AWSRouter, src, dst // Create a filter of type TgwAttachmentInputFilter input := ports.TgwAttachmentInputFilter(filter) // Get the list of TgwRouteTable that match the filter - output, err := ports.TgwGetAttachments(ctx, api, input) + output, err := ports.GetTgwAttachments(ctx, api, input) if err != nil { return err } From 2475ac81bfccd10ccfb10b9751d75869d9e10a13 Mon Sep 17 00:00:00 2001 From: Roger Gomez Date: Wed, 28 Feb 2024 15:58:44 -0500 Subject: [PATCH 3/8] renaming function per standard --- aws/awsrouter/transit_gateway_helpers.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/aws/awsrouter/transit_gateway_helpers.go b/aws/awsrouter/transit_gateway_helpers.go index 6e38169..e42e419 100644 --- a/aws/awsrouter/transit_gateway_helpers.go +++ b/aws/awsrouter/transit_gateway_helpers.go @@ -44,7 +44,11 @@ func ExportTgwRoutesExcel(tgws []*Tgw, folder fs.FileInfo) error { routeType := fmt.Sprint(route.Type) var attachmentName = "-" if len(route.TransitGatewayAttachments) != 0 { - attachmentName = fmt.Sprint(*route.TransitGatewayAttachments[0].TransitGatewayAttachmentId) + attachmentID := fmt.Sprint(*route.TransitGatewayAttachments[0].TransitGatewayAttachmentId) + attachmentName = tgwRouteTable.GetAttachmentName(attachmentID) + if attachmentName == "" { + attachmentName = attachmentID + } } var prefixListId string if route.PrefixListId == nil { @@ -85,3 +89,6 @@ func ExportRouteTableRoutesCsv(w *csv.Writer, tgwrt TgwRouteTable) error { } return nil } + + +// \ No newline at end of file From c9687dc032c3e6fd4d383a2d4b64aa2d26563a46 Mon Sep 17 00:00:00 2001 From: Roger Gomez Date: Wed, 28 Feb 2024 15:58:54 -0500 Subject: [PATCH 4/8] Add GetAttachmentName method to TgwRouteTable --- aws/awsrouter/transit_gateway_route_tables.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/aws/awsrouter/transit_gateway_route_tables.go b/aws/awsrouter/transit_gateway_route_tables.go index d80fba6..6fde51e 100644 --- a/aws/awsrouter/transit_gateway_route_tables.go +++ b/aws/awsrouter/transit_gateway_route_tables.go @@ -246,3 +246,14 @@ func (t *TgwRouteTable) PrintRoutesInTable() { } fmt.Println(table.String()) } + + +// GetAttachmentName returns the name of the attachment that has the given ID. +func (t *TgwRouteTable) GetAttachmentName(attachmentID string) string { + for _, a := range t.Attachments { + if a.ID == attachmentID { + return a.Name + } + } + return "" +} \ No newline at end of file From 3ffc5905bc1182d440211022f3d42da410cb9dce Mon Sep 17 00:00:00 2001 From: Roger Gomez Date: Wed, 28 Feb 2024 16:00:06 -0500 Subject: [PATCH 5/8] Refactor function names in awsrouter.go --- ports/awsrouter.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ports/awsrouter.go b/ports/awsrouter.go index 14102a2..fb6442f 100644 --- a/ports/awsrouter.go +++ b/ports/awsrouter.go @@ -92,7 +92,7 @@ func TgwSearchRoutesInputFilter(tgwRtID string, routeFilters ...types.Filter) *e return input } -//GetTgwRoutes returns a list of the Transit Gateway Routes that match the input filter for specific Route Table. +// GetTgwRoutes returns a list of the Transit Gateway Routes that match the input filter for specific Route Table. func GetTgwRoutes(ctx context.Context, api AWSRouter, input *ec2.SearchTransitGatewayRoutesInput) (*ec2.SearchTransitGatewayRoutesOutput, error) { return api.SearchTransitGatewayRoutes(ctx, input) } @@ -131,7 +131,7 @@ func TgwAttachmentInputFilter(attachmentFilters ...types.Filter) *ec2.DescribeTr return input } -// TgwGetAttachments describe the configuration of the TGW Attachments. -func TgwGetAttachments(ctx context.Context, api AWSRouter, input *ec2.DescribeTransitGatewayAttachmentsInput) (*ec2.DescribeTransitGatewayAttachmentsOutput, error) { +// GetTgwAttachments describe the configuration of the TGW Attachments. +func GetTgwAttachments(ctx context.Context, api AWSRouter, input *ec2.DescribeTransitGatewayAttachmentsInput) (*ec2.DescribeTransitGatewayAttachmentsOutput, error) { return api.DescribeTransitGatewayAttachments(ctx, input) } From 45e546733b56fcb527a399fa539fb89334a705f3 Mon Sep 17 00:00:00 2001 From: Roger Gomez Date: Wed, 28 Feb 2024 16:00:31 -0500 Subject: [PATCH 6/8] Add method to update TGW route tables attachments --- internal/application/application.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/application/application.go b/internal/application/application.go index 33e118e..bf38f9d 100644 --- a/internal/application/application.go +++ b/internal/application/application.go @@ -50,7 +50,7 @@ func (app *Application) UpdateRouting(ctx context.Context) (tgws []*awsrouter.Tg // Get all routes from all route tables for _, tgw := range tgws { tgw.UpdateTgwRoutes(ctx, app.RouterClient) + tgw.UpdateTgwRouteTablesAttachments(ctx, app.RouterClient) } - return tgws, nil } From 4b49914737830eb2460ffb94526932ea81b800b8 Mon Sep 17 00:00:00 2001 From: Roger Gomez Date: Wed, 28 Feb 2024 16:03:26 -0500 Subject: [PATCH 7/8] Update TgwRouteTablesAttachments to update attachment names --- aws/awsrouter/transit_gateway.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/aws/awsrouter/transit_gateway.go b/aws/awsrouter/transit_gateway.go index 3c0a967..f345454 100644 --- a/aws/awsrouter/transit_gateway.go +++ b/aws/awsrouter/transit_gateway.go @@ -12,7 +12,7 @@ import ( "github.com/rogerscuall/aws-router/ports" ) -//Tgw is the main data-structure, holds ID, Name, a list of TgwRouteTable and other TGW info. +// Tgw is the main data-structure, holds ID, Name, a list of TgwRouteTable and other TGW info. // Represents a Transit Gateway in AWS. type Tgw struct { ID string @@ -99,6 +99,28 @@ func (t *Tgw) UpdateTgwRouteTablesAttachments(ctx context.Context, api ports.AWS if err != nil { return fmt.Errorf("error updating the route table %s %w", tgwRouteTable.ID, err) } + + // 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) + } + } } return nil } From 4537cada5470d69d11c8c85421da02bf41bb704a Mon Sep 17 00:00:00 2001 From: Roger Gomez Date: Wed, 28 Feb 2024 16:35:42 -0500 Subject: [PATCH 8/8] Update .gitignore and fix linting issues --- .gitignore | 3 +- Makefile | 8 +- aws/awsrouter/awsrouter_test.go | 49 +++++------ aws/awsrouter/transit_gateway_attachment.go | 7 +- aws/awsrouter/transit_gateway_helpers.go | 3 +- aws/awsrouter/transit_gateway_route_tables.go | 4 +- .../transit_gateway_route_tables_test.go | 4 +- cmd/excel.go | 2 +- cmd/sync.go | 3 + ports/awsrouter_test.go | 88 +++++++++---------- 10 files changed, 83 insertions(+), 88 deletions(-) diff --git a/.gitignore b/.gitignore index cd9f87e..9253255 100644 --- a/.gitignore +++ b/.gitignore @@ -98,4 +98,5 @@ csv excel drawings/*.png aws/draw/testdata/*.png -*.png \ No newline at end of file +*.png +.envrc \ No newline at end of file diff --git a/Makefile b/Makefile index ebdaa3b..24eae45 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,7 @@ run/awsrouters/excel: ## run/awsrouters/cleanup: run the cmd/api application .PHONY: run/awsrouters/cleanup run/awsrouters/cleanup: - @echo 'Cleaning up excel and csv files...' + @echo 'Cleaning up excel and csv files...' rm -f csv/*.csv rm -f excel/*.xlsx @@ -43,13 +43,13 @@ audit: go mod verify @echo 'Formatting code...' go fmt ./... - @echo 'Vetting code...' - go vet ./... - staticcheck ./... @echo 'Running tests...' go test -race -vet=off ./... @echo 'Testing code coverage...' go test -cover ./... + @echo 'Vetting code...' + go vet ./... + staticcheck ./... ## vendor: tidy and vendor dependencies .PHONY: vendor diff --git a/aws/awsrouter/awsrouter_test.go b/aws/awsrouter/awsrouter_test.go index ffeccc9..5297757 100644 --- a/aws/awsrouter/awsrouter_test.go +++ b/aws/awsrouter/awsrouter_test.go @@ -170,28 +170,28 @@ var listTgwAttachments []types.TransitGatewayRouteAttachment = []types.TransitGa }, } -var listTgwAttachmentAssociations []types.TransitGatewayRouteTableAssociation = []types.TransitGatewayRouteTableAssociation{ - { - ResourceId: aws.String("vpc-0af25be733475a425"), - ResourceType: "vpc", - TransitGatewayAttachmentId: aws.String("tgw-attach-080f3014bd52ec95f"), - }, - { - ResourceId: aws.String("tgw-04408890ef44df3e3"), - ResourceType: "peering", - TransitGatewayAttachmentId: aws.String("tgw-attach-080f3014bd52ec96f"), - }, - { - ResourceId: aws.String("tgw-attach-09db78f3e74abf792"), - ResourceType: "connect", - TransitGatewayAttachmentId: aws.String("tgw-attach-080f3014bd52ec97f"), - }, - { - ResourceId: aws.String("3c1a5494-3491-481d-b82d-7e2c61204f3f"), - ResourceType: "direct-connect-gateway", - TransitGatewayAttachmentId: aws.String("tgw-attach-080f3014bd52ec99f"), - }, -} +// var listTgwAttachmentAssociations []types.TransitGatewayRouteTableAssociation = []types.TransitGatewayRouteTableAssociation{ +// { +// ResourceId: aws.String("vpc-0af25be733475a425"), +// ResourceType: "vpc", +// TransitGatewayAttachmentId: aws.String("tgw-attach-080f3014bd52ec95f"), +// }, +// { +// ResourceId: aws.String("tgw-04408890ef44df3e3"), +// ResourceType: "peering", +// TransitGatewayAttachmentId: aws.String("tgw-attach-080f3014bd52ec96f"), +// }, +// { +// ResourceId: aws.String("tgw-attach-09db78f3e74abf792"), +// ResourceType: "connect", +// TransitGatewayAttachmentId: aws.String("tgw-attach-080f3014bd52ec97f"), +// }, +// { +// ResourceId: aws.String("3c1a5494-3491-481d-b82d-7e2c61204f3f"), +// ResourceType: "direct-connect-gateway", +// TransitGatewayAttachmentId: aws.String("tgw-attach-080f3014bd52ec99f"), +// }, +// } // DescribeTransitGateways is a mock of DescribeTransitGateways // it uses listDescribeTransitGatewaysOutput to return a list of TransitGateways @@ -293,10 +293,7 @@ func TestTgwInputFilter(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { comparer := cmp.Comparer(func(x, y *ec2.DescribeTransitGatewaysInput) bool { - if cmp.Equal(x.TransitGatewayIds, y.TransitGatewayIds) { - return true - } - return false + return cmp.Equal(x.TransitGatewayIds, y.TransitGatewayIds) }) if dif := cmp.Diff(ports.TgwInputFilter(tt.args.tgwIDs), tt.want, comparer); dif != "" { t.Errorf("TgwInputFilter() = %v, want %v", ports.TgwInputFilter(tt.args.tgwIDs), tt.want) diff --git a/aws/awsrouter/transit_gateway_attachment.go b/aws/awsrouter/transit_gateway_attachment.go index f1f86b3..6341378 100644 --- a/aws/awsrouter/transit_gateway_attachment.go +++ b/aws/awsrouter/transit_gateway_attachment.go @@ -104,10 +104,7 @@ func NewAttPath() *AttPath { // isAttachmentInPath returns true if the attachment is in the path. func (attPath AttPath) isAttachmentInPath(ID string) bool { _, ok := attPath.mapPath[ID] - if ok { - return true - } - return false + return ok } // addAttachmentToPath adds an attachment to the path. @@ -158,7 +155,7 @@ func (attPath *AttPath) Walk(ctx context.Context, api ports.AWSRouter, src, dst // Add the next hop to the path err = attPath.addAttachmentToPath(nextHopAtt) if err != nil { - return fmt.Errorf("Attachment %s is already in the path", nextHopAtt.ID) + return fmt.Errorf("attachment %s is already in the path", nextHopAtt.ID) } // Find the route table associated to the attachment diff --git a/aws/awsrouter/transit_gateway_helpers.go b/aws/awsrouter/transit_gateway_helpers.go index e42e419..0e4e161 100644 --- a/aws/awsrouter/transit_gateway_helpers.go +++ b/aws/awsrouter/transit_gateway_helpers.go @@ -90,5 +90,4 @@ func ExportRouteTableRoutesCsv(w *csv.Writer, tgwrt TgwRouteTable) error { return nil } - -// \ No newline at end of file +// diff --git a/aws/awsrouter/transit_gateway_route_tables.go b/aws/awsrouter/transit_gateway_route_tables.go index 6fde51e..d3777f5 100644 --- a/aws/awsrouter/transit_gateway_route_tables.go +++ b/aws/awsrouter/transit_gateway_route_tables.go @@ -104,7 +104,6 @@ func (t *TgwRouteTable) UpdateAttachments(ctx context.Context, attachments *ec2. } // TgwRouteTableSelectionPriority select the best route table from a list of TgwRouteTables to the specific destination. -// func TgwRouteTableSelectionPriority(rts []*TgwRouteTable, src net.IP) (*TgwRouteTable, error) { var srcAttachment *TgwAttachment // cfg, err := config.LoadDefaultConfig(context.TODO()) @@ -247,7 +246,6 @@ func (t *TgwRouteTable) PrintRoutesInTable() { fmt.Println(table.String()) } - // GetAttachmentName returns the name of the attachment that has the given ID. func (t *TgwRouteTable) GetAttachmentName(attachmentID string) string { for _, a := range t.Attachments { @@ -256,4 +254,4 @@ func (t *TgwRouteTable) GetAttachmentName(attachmentID string) string { } } return "" -} \ No newline at end of file +} diff --git a/aws/awsrouter/transit_gateway_route_tables_test.go b/aws/awsrouter/transit_gateway_route_tables_test.go index be10fe3..c4ca3c9 100644 --- a/aws/awsrouter/transit_gateway_route_tables_test.go +++ b/aws/awsrouter/transit_gateway_route_tables_test.go @@ -283,7 +283,7 @@ func TestTgwRouteTableSelectionPriority(t *testing.T) { } } -func TestfindBestRoutePrefix(t *testing.T) { +func TestFindBestRoutePrefix(t *testing.T) { net10 := net.ParseIP("10.0.1.10") net192 := net.ParseIP("192.8.1.1") _, sub10, _ := net.ParseCIDR("10.0.1.0/24") @@ -382,7 +382,7 @@ func TestTgwRouteTable_UpdateAttachments(t *testing.T) { &ec2.GetTransitGatewayRouteTableAssociationsOutput{ Associations: []types.TransitGatewayRouteTableAssociation{ { - ResourceId: aws.String("tgw-0d7f9b0x"), + ResourceId: aws.String("tgw-0d7f9b0x"), ResourceType: "vpc", TransitGatewayAttachmentId: aws.String("tgw-attach-0d7f9b0x")}, }, diff --git a/cmd/excel.go b/cmd/excel.go index 0c4c688..10e3b4e 100644 --- a/cmd/excel.go +++ b/cmd/excel.go @@ -34,7 +34,7 @@ import ( var excelCmd = &cobra.Command{ Use: "excel", Short: "Export all route tables to excel", - Long: `Each Transit Gateway will have a separate Excel and each route table will have a separate sheet. + Long: `Each Transit Gateway will have a separate Excel and each route table will have a separate sheet. By default all excel are stored on the folder excel. The folder has to exist.`, Run: func(cmd *cobra.Command, args []string) { var err error diff --git a/cmd/sync.go b/cmd/sync.go index 02602b8..107014d 100644 --- a/cmd/sync.go +++ b/cmd/sync.go @@ -60,6 +60,9 @@ var syncCmd = &cobra.Command{ fmt.Println("Saving routing information to DB") for _, tgw := range tgws { err = dbAdapterTgw.SetVal(tgw.ID, tgw.Bytes()) + if err != nil { + app.ErrorLog.Println(err) + } for _, rt := range tgw.RouteTables { err = dbAdapterTgwRouteTable.SetVal(rt.ID, rt.Bytes()) } diff --git a/ports/awsrouter_test.go b/ports/awsrouter_test.go index d907991..49e1aa5 100644 --- a/ports/awsrouter_test.go +++ b/ports/awsrouter_test.go @@ -145,51 +145,51 @@ var listGetTransitGatewayRouteTableAssociationsOutput *ec2.GetTransitGatewayRout }, } -var listTgwAttachments []types.TransitGatewayRouteAttachment = []types.TransitGatewayRouteAttachment{ - { - ResourceId: aws.String("vpc-0af25be733475a425"), - ResourceType: "vpc", - TransitGatewayAttachmentId: aws.String("tgw-attach-080f3014bd52ec95f"), - }, - { - ResourceId: aws.String("tgw-04408890ef44df3e3"), - ResourceType: "peering", - TransitGatewayAttachmentId: aws.String("tgw-attach-080f3014bd52ec96f"), - }, - { - ResourceId: aws.String("tgw-attach-09db78f3e74abf792"), - ResourceType: "connect", - TransitGatewayAttachmentId: aws.String("tgw-attach-080f3014bd52ec97f"), - }, - { - ResourceId: aws.String("3c1a5494-3491-481d-b82d-7e2c61204f3f"), - ResourceType: "direct-connect-gateway", - TransitGatewayAttachmentId: aws.String("tgw-attach-080f3014bd52ec99f"), - }, -} +// var listTgwAttachments []types.TransitGatewayRouteAttachment = []types.TransitGatewayRouteAttachment{ +// { +// ResourceId: aws.String("vpc-0af25be733475a425"), +// ResourceType: "vpc", +// TransitGatewayAttachmentId: aws.String("tgw-attach-080f3014bd52ec95f"), +// }, +// { +// ResourceId: aws.String("tgw-04408890ef44df3e3"), +// ResourceType: "peering", +// TransitGatewayAttachmentId: aws.String("tgw-attach-080f3014bd52ec96f"), +// }, +// { +// ResourceId: aws.String("tgw-attach-09db78f3e74abf792"), +// ResourceType: "connect", +// TransitGatewayAttachmentId: aws.String("tgw-attach-080f3014bd52ec97f"), +// }, +// { +// ResourceId: aws.String("3c1a5494-3491-481d-b82d-7e2c61204f3f"), +// ResourceType: "direct-connect-gateway", +// TransitGatewayAttachmentId: aws.String("tgw-attach-080f3014bd52ec99f"), +// }, +// } -var listTgwAttachmentAssociations []types.TransitGatewayRouteTableAssociation = []types.TransitGatewayRouteTableAssociation{ - { - ResourceId: aws.String("vpc-0af25be733475a425"), - ResourceType: "vpc", - TransitGatewayAttachmentId: aws.String("tgw-attach-080f3014bd52ec95f"), - }, - { - ResourceId: aws.String("tgw-04408890ef44df3e3"), - ResourceType: "peering", - TransitGatewayAttachmentId: aws.String("tgw-attach-080f3014bd52ec96f"), - }, - { - ResourceId: aws.String("tgw-attach-09db78f3e74abf792"), - ResourceType: "connect", - TransitGatewayAttachmentId: aws.String("tgw-attach-080f3014bd52ec97f"), - }, - { - ResourceId: aws.String("3c1a5494-3491-481d-b82d-7e2c61204f3f"), - ResourceType: "direct-connect-gateway", - TransitGatewayAttachmentId: aws.String("tgw-attach-080f3014bd52ec99f"), - }, -} +// var listTgwAttachmentAssociations []types.TransitGatewayRouteTableAssociation = []types.TransitGatewayRouteTableAssociation{ +// { +// ResourceId: aws.String("vpc-0af25be733475a425"), +// ResourceType: "vpc", +// TransitGatewayAttachmentId: aws.String("tgw-attach-080f3014bd52ec95f"), +// }, +// { +// ResourceId: aws.String("tgw-04408890ef44df3e3"), +// ResourceType: "peering", +// TransitGatewayAttachmentId: aws.String("tgw-attach-080f3014bd52ec96f"), +// }, +// { +// ResourceId: aws.String("tgw-attach-09db78f3e74abf792"), +// ResourceType: "connect", +// TransitGatewayAttachmentId: aws.String("tgw-attach-080f3014bd52ec97f"), +// }, +// { +// ResourceId: aws.String("3c1a5494-3491-481d-b82d-7e2c61204f3f"), +// ResourceType: "direct-connect-gateway", +// TransitGatewayAttachmentId: aws.String("tgw-attach-080f3014bd52ec99f"), +// }, +// } // DescribeTransitGateways is a mock of DescribeTransitGateways // it uses listDescribeTransitGatewaysOutput to return a list of TransitGateways