Skip to content

Commit

Permalink
Merge pull request #9 from rogerscuall/feature/attachment_name
Browse files Browse the repository at this point in the history
Feature/attachment name
  • Loading branch information
rogerscuall authored Feb 28, 2024
2 parents ab2c1b1 + 4537cad commit 7fb4b15
Show file tree
Hide file tree
Showing 13 changed files with 129 additions and 91 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,5 @@ csv
excel
drawings/*.png
aws/draw/testdata/*.png
*.png
*.png
.envrc
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
49 changes: 23 additions & 26 deletions aws/awsrouter/awsrouter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 23 additions & 1 deletion aws/awsrouter/transit_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
12 changes: 6 additions & 6 deletions aws/awsrouter/transit_gateway_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -101,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.
Expand Down Expand Up @@ -155,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
Expand All @@ -167,7 +167,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
}
Expand Down
8 changes: 7 additions & 1 deletion aws/awsrouter/transit_gateway_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -85,3 +89,5 @@ func ExportRouteTableRoutesCsv(w *csv.Writer, tgwrt TgwRouteTable) error {
}
return nil
}

//
11 changes: 10 additions & 1 deletion aws/awsrouter/transit_gateway_route_tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -246,3 +245,13 @@ 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 ""
}
4 changes: 2 additions & 2 deletions aws/awsrouter/transit_gateway_route_tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")},
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/excel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
2 changes: 1 addition & 1 deletion internal/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 3 additions & 3 deletions ports/awsrouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
88 changes: 44 additions & 44 deletions ports/awsrouter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7fb4b15

Please sign in to comment.