From c426e4827a6a575d3b011d1f5d9a7398d78fc2e8 Mon Sep 17 00:00:00 2001 From: Louis TOUSSAINT Date: Fri, 12 Feb 2021 22:36:25 +0100 Subject: [PATCH] Issue 226: Update aws_route_table_expander to implement Alert interface --- pkg/middlewares/aws_route_table_expander.go | 25 ++++++++++++++----- .../aws_route_table_expander_test.go | 13 +++++----- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/pkg/middlewares/aws_route_table_expander.go b/pkg/middlewares/aws_route_table_expander.go index 8fbd6af0..47037a71 100644 --- a/pkg/middlewares/aws_route_table_expander.go +++ b/pkg/middlewares/aws_route_table_expander.go @@ -10,6 +10,23 @@ import ( "github.com/sirupsen/logrus" ) +type invalidRouteAlert struct { + message string +} + +func newInvalidRouteAlert(awsRouteTableResourceType, tableId string) *invalidRouteAlert { + message := fmt.Sprintf("Skipped invalid route found in state for %s.%s", awsRouteTableResourceType, tableId) + return &invalidRouteAlert{message} +} + +func (i *invalidRouteAlert) Message() string { + return i.message +} + +func (i *invalidRouteAlert) ShouldIgnoreResource() bool { + return false +} + // Explodes routes found in aws_default_route_table.route and aws_route_table.route to dedicated resources type AwsRouteTableExpander struct { alerter alerter.AlerterInterface @@ -60,9 +77,7 @@ func (m *AwsRouteTableExpander) handleTable(table *aws.AwsRouteTable, results *[ for _, route := range *table.Route { routeId, err := aws.CalculateRouteID(&table.Id, route.CidrBlock, route.Ipv6CidrBlock) if err != nil { - m.alerter.SendAlert(aws.AwsRouteTableResourceType, alerter.Alert{ - Message: fmt.Sprintf("Skipped invalid route found in state for %s.%s", aws.AwsRouteTableResourceType, table.Id), - }) + m.alerter.SendAlert(aws.AwsRouteTableResourceType, newInvalidRouteAlert(aws.AwsRouteTableResourceType, table.Id)) continue } newRouteFromTable := &aws.AwsRoute{ @@ -107,9 +122,7 @@ func (m *AwsRouteTableExpander) handleDefaultTable(table *aws.AwsDefaultRouteTab for _, route := range *table.Route { routeId, err := aws.CalculateRouteID(&table.Id, route.CidrBlock, route.Ipv6CidrBlock) if err != nil { - m.alerter.SendAlert(aws.AwsDefaultRouteTableResourceType, alerter.Alert{ - Message: fmt.Sprintf("Skipped invalid route found in state for %s.%s", aws.AwsDefaultRouteTableResourceType, table.Id), - }) + m.alerter.SendAlert(aws.AwsDefaultRouteTableResourceType, newInvalidRouteAlert(aws.AwsDefaultRouteTableResourceType, table.Id)) continue } newRouteFromTable := &aws.AwsRoute{ diff --git a/pkg/middlewares/aws_route_table_expander_test.go b/pkg/middlewares/aws_route_table_expander_test.go index 0a6da7b4..4b5541c7 100644 --- a/pkg/middlewares/aws_route_table_expander_test.go +++ b/pkg/middlewares/aws_route_table_expander_test.go @@ -7,7 +7,6 @@ import ( awssdk "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awsutil" "github.com/cloudskiff/driftctl/mocks" - "github.com/cloudskiff/driftctl/pkg/alerter" "github.com/cloudskiff/driftctl/pkg/resource" "github.com/cloudskiff/driftctl/pkg/resource/aws" resource2 "github.com/cloudskiff/driftctl/test/resource" @@ -215,12 +214,12 @@ func TestAwsRouteTableExpander_Execute(t *testing.T) { func TestAwsRouteTableExpander_ExecuteWithInvalidRoutes(t *testing.T) { mockedAlerter := &mocks.AlerterInterface{} - mockedAlerter.On("SendAlert", aws.AwsRouteTableResourceType, alerter.Alert{ - Message: "Skipped invalid route found in state for aws_route_table.table_from_state", - }) - mockedAlerter.On("SendAlert", aws.AwsDefaultRouteTableResourceType, alerter.Alert{ - Message: "Skipped invalid route found in state for aws_default_route_table.default_table_from_state", - }) + mockedAlerter.On("SendAlert", aws.AwsRouteTableResourceType, newInvalidRouteAlert( + "aws_route_table", "table_from_state", + )) + mockedAlerter.On("SendAlert", aws.AwsDefaultRouteTableResourceType, newInvalidRouteAlert( + "aws_default_route_table", "default_table_from_state", + )) input := []resource.Resource{ &aws.AwsRouteTable{