Issue 226: Update aws_route_table_expander to implement Alert interface

main
Louis TOUSSAINT 2021-02-12 22:36:25 +01:00
parent 0aef6f3184
commit c426e4827a
2 changed files with 25 additions and 13 deletions

View File

@ -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{

View File

@ -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{