Issue 165: Add route_supplier to ec2_repository

main
Louis TOUSSAINT 2021-05-20 15:43:00 +02:00
parent 86b7f52d60
commit 3f2fe6d90c
5 changed files with 326 additions and 121 deletions

View File

@ -19,6 +19,7 @@ type EC2Repository interface {
ListAllInternetGateways() ([]*ec2.InternetGateway, error)
ListAllSubnets() ([]*ec2.Subnet, []*ec2.Subnet, error)
ListAllNatGateways() ([]*ec2.NatGateway, error)
ListAllRouteTables() ([]*ec2.RouteTable, error)
}
type EC2Client interface {
@ -214,3 +215,20 @@ func (r *ec2Repository) ListAllNatGateways() ([]*ec2.NatGateway, error) {
return result, nil
}
func (r *ec2Repository) ListAllRouteTables() ([]*ec2.RouteTable, error) {
var routeTables []*ec2.RouteTable
input := ec2.DescribeRouteTablesInput{}
err := r.client.DescribeRouteTablesPages(&input,
func(resp *ec2.DescribeRouteTablesOutput, lastPage bool) bool {
routeTables = append(routeTables, resp.RouteTables...)
return !lastPage
},
)
if err != nil {
return nil, err
}
return routeTables, nil
}

View File

@ -765,3 +765,184 @@ func Test_ec2Repository_ListAllNatGateways(t *testing.T) {
})
}
}
func Test_ec2Repository_ListAllRouteTables(t *testing.T) {
tests := []struct {
name string
mocks func(client *MockEC2Client)
want []*ec2.RouteTable
wantErr error
}{
{
name: "List only route with multiple pages",
mocks: func(client *MockEC2Client) {
client.On("DescribeRouteTablesPages",
&ec2.DescribeRouteTablesInput{},
mock.MatchedBy(func(callback func(res *ec2.DescribeRouteTablesOutput, lastPage bool) bool) bool {
callback(&ec2.DescribeRouteTablesOutput{
RouteTables: []*ec2.RouteTable{
{
RouteTableId: aws.String("rtb-096bdfb69309c54c3"), // table1
Routes: []*ec2.Route{
{
DestinationCidrBlock: aws.String("10.0.0.0/16"),
Origin: aws.String("CreateRouteTable"), // default route
},
{
DestinationCidrBlock: aws.String("1.1.1.1/32"),
GatewayId: aws.String("igw-030e74f73bd67f21b"),
},
{
DestinationIpv6CidrBlock: aws.String("::/0"),
GatewayId: aws.String("igw-030e74f73bd67f21b"),
},
},
},
{
RouteTableId: aws.String("rtb-0169b0937fd963ddc"), // table2
Routes: []*ec2.Route{
{
DestinationCidrBlock: aws.String("10.0.0.0/16"),
Origin: aws.String("CreateRouteTable"), // default route
},
{
DestinationCidrBlock: aws.String("0.0.0.0/0"),
GatewayId: aws.String("igw-030e74f73bd67f21b"),
},
{
DestinationIpv6CidrBlock: aws.String("::/0"),
GatewayId: aws.String("igw-030e74f73bd67f21b"),
},
},
},
},
}, false)
callback(&ec2.DescribeRouteTablesOutput{
RouteTables: []*ec2.RouteTable{
{
RouteTableId: aws.String("rtb-02780c485f0be93c5"), // default_table
VpcId: aws.String("vpc-09fe5abc2309ba49d"),
Associations: []*ec2.RouteTableAssociation{
{
Main: aws.Bool(true),
},
},
Routes: []*ec2.Route{
{
DestinationCidrBlock: aws.String("10.0.0.0/16"),
Origin: aws.String("CreateRouteTable"), // default route
},
{
DestinationCidrBlock: aws.String("10.1.1.0/24"),
GatewayId: aws.String("igw-030e74f73bd67f21b"),
},
{
DestinationCidrBlock: aws.String("10.1.2.0/24"),
GatewayId: aws.String("igw-030e74f73bd67f21b"),
},
},
},
{
RouteTableId: aws.String(""), // table3
Routes: []*ec2.Route{
{
DestinationCidrBlock: aws.String("10.0.0.0/16"),
Origin: aws.String("CreateRouteTable"), // default route
},
},
},
},
}, true)
return true
})).Return(nil)
},
want: []*ec2.RouteTable{
{
RouteTableId: aws.String("rtb-096bdfb69309c54c3"), // table1
Routes: []*ec2.Route{
{
DestinationCidrBlock: aws.String("10.0.0.0/16"),
Origin: aws.String("CreateRouteTable"), // default route
},
{
DestinationCidrBlock: aws.String("1.1.1.1/32"),
GatewayId: aws.String("igw-030e74f73bd67f21b"),
},
{
DestinationIpv6CidrBlock: aws.String("::/0"),
GatewayId: aws.String("igw-030e74f73bd67f21b"),
},
},
},
{
RouteTableId: aws.String("rtb-0169b0937fd963ddc"), // table2
Routes: []*ec2.Route{
{
DestinationCidrBlock: aws.String("10.0.0.0/16"),
Origin: aws.String("CreateRouteTable"), // default route
},
{
DestinationCidrBlock: aws.String("0.0.0.0/0"),
GatewayId: aws.String("igw-030e74f73bd67f21b"),
},
{
DestinationIpv6CidrBlock: aws.String("::/0"),
GatewayId: aws.String("igw-030e74f73bd67f21b"),
},
},
},
{
RouteTableId: aws.String("rtb-02780c485f0be93c5"), // default_table
VpcId: aws.String("vpc-09fe5abc2309ba49d"),
Associations: []*ec2.RouteTableAssociation{
{
Main: aws.Bool(true),
},
},
Routes: []*ec2.Route{
{
DestinationCidrBlock: aws.String("10.0.0.0/16"),
Origin: aws.String("CreateRouteTable"), // default route
},
{
DestinationCidrBlock: aws.String("10.1.1.0/24"),
GatewayId: aws.String("igw-030e74f73bd67f21b"),
},
{
DestinationCidrBlock: aws.String("10.1.2.0/24"),
GatewayId: aws.String("igw-030e74f73bd67f21b"),
},
},
},
{
RouteTableId: aws.String(""), // table3
Routes: []*ec2.Route{
{
DestinationCidrBlock: aws.String("10.0.0.0/16"),
Origin: aws.String("CreateRouteTable"), // default route
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client := &MockEC2Client{}
tt.mocks(client)
r := &ec2Repository{
client: client,
}
got, err := r.ListAllRouteTables()
assert.Equal(t, tt.wantErr, err)
changelog, err := diff.Diff(got, tt.want)
assert.Nil(t, err)
if len(changelog) > 0 {
for _, change := range changelog {
t.Errorf("%s: %s -> %s", strings.Join(change.Path, "."), change.From, change.To)
}
t.Fail()
}
})
}
}

View File

@ -58,29 +58,6 @@ func (_m *MockEC2Repository) ListAllAddressesAssociation() ([]string, error) {
return r0, r1
}
// ListAllNatGateways provides a mock function with given fields:
func (_m *MockEC2Repository) ListAllNatGateways() ([]*ec2.NatGateway, error) {
ret := _m.Called()
var r0 []*ec2.NatGateway
if rf, ok := ret.Get(0).(func() []*ec2.NatGateway); ok {
r0 = rf()
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]*ec2.NatGateway)
}
}
var r1 error
if rf, ok := ret.Get(1).(func() error); ok {
r1 = rf()
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// ListAllImages provides a mock function with given fields:
func (_m *MockEC2Repository) ListAllImages() ([]*ec2.Image, error) {
ret := _m.Called()
@ -173,6 +150,52 @@ func (_m *MockEC2Repository) ListAllKeyPairs() ([]*ec2.KeyPairInfo, error) {
return r0, r1
}
// ListAllNatGateways provides a mock function with given fields:
func (_m *MockEC2Repository) ListAllNatGateways() ([]*ec2.NatGateway, error) {
ret := _m.Called()
var r0 []*ec2.NatGateway
if rf, ok := ret.Get(0).(func() []*ec2.NatGateway); ok {
r0 = rf()
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]*ec2.NatGateway)
}
}
var r1 error
if rf, ok := ret.Get(1).(func() error); ok {
r1 = rf()
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// ListAllRouteTables provides a mock function with given fields:
func (_m *MockEC2Repository) ListAllRouteTables() ([]*ec2.RouteTable, error) {
ret := _m.Called()
var r0 []*ec2.RouteTable
if rf, ok := ret.Get(0).(func() []*ec2.RouteTable); ok {
r0 = rf()
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]*ec2.RouteTable)
}
}
var r1 error
if rf, ok := ret.Get(1).(func() error); ok {
r1 = rf()
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// ListAllSnapshots provides a mock function with given fields:
func (_m *MockEC2Repository) ListAllSnapshots() ([]*ec2.Snapshot, error) {
ret := _m.Called()

View File

@ -3,6 +3,9 @@ package aws
import (
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
"github.com/cloudskiff/driftctl/pkg/remote/deserializer"
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/resource/aws"
@ -15,7 +18,7 @@ import (
type RouteSupplier struct {
reader terraform.ResourceReader
deserializer *resource.Deserializer
client ec2iface.EC2API
client repository.EC2Repository
routeRunner *terraform.ParallelResourceReader
}
@ -23,16 +26,15 @@ func NewRouteSupplier(provider *AWSTerraformProvider, deserializer *resource.Des
return &RouteSupplier{
provider,
deserializer,
ec2.New(provider.session),
repository.NewEC2Repository(provider.session),
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
}
}
func (s *RouteSupplier) Resources() ([]resource.Resource, error) {
routeTables, err := listRouteTables(s.client, aws.AwsRouteResourceType)
routeTables, err := s.client.ListAllRouteTables()
if err != nil {
return nil, err
return nil, remoteerror.NewResourceEnumerationErrorWithType(err, aws.AwsRouteResourceType, aws.AwsRouteTableResourceType)
}
for _, routeTable := range routeTables {

View File

@ -4,6 +4,7 @@ import (
"context"
"testing"
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
awstest "github.com/cloudskiff/driftctl/test/aws"
testresource "github.com/cloudskiff/driftctl/test/resource"
@ -29,7 +30,7 @@ func TestRouteSupplier_Resources(t *testing.T) {
cases := []struct {
test string
dirName string
mocks func(client *awstest.MockFakeEC2)
mocks func(client *repository.MockEC2Repository)
err error
}{
{
@ -37,111 +38,91 @@ func TestRouteSupplier_Resources(t *testing.T) {
// as a default route will always be present in each route table
test: "no route",
dirName: "route_empty",
mocks: func(client *awstest.MockFakeEC2) {
client.On("DescribeRouteTablesPages",
&ec2.DescribeRouteTablesInput{},
mock.MatchedBy(func(callback func(res *ec2.DescribeRouteTablesOutput, lastPage bool) bool) bool {
callback(&ec2.DescribeRouteTablesOutput{}, true)
return true
})).Return(nil)
mocks: func(client *repository.MockEC2Repository) {
client.On("ListAllRouteTables").Once().Return([]*ec2.RouteTable{}, nil)
},
err: nil,
},
{
test: "mixed default_route_table and route_table",
dirName: "route",
mocks: func(client *awstest.MockFakeEC2) {
client.On("DescribeRouteTablesPages",
&ec2.DescribeRouteTablesInput{},
mock.MatchedBy(func(callback func(res *ec2.DescribeRouteTablesOutput, lastPage bool) bool) bool {
callback(&ec2.DescribeRouteTablesOutput{
RouteTables: []*ec2.RouteTable{
{
RouteTableId: awssdk.String("rtb-096bdfb69309c54c3"), // table1
Routes: []*ec2.Route{
{
DestinationCidrBlock: awssdk.String("10.0.0.0/16"),
Origin: awssdk.String("CreateRouteTable"), // default route
},
{
DestinationCidrBlock: awssdk.String("1.1.1.1/32"),
GatewayId: awssdk.String("igw-030e74f73bd67f21b"),
},
{
DestinationIpv6CidrBlock: awssdk.String("::/0"),
GatewayId: awssdk.String("igw-030e74f73bd67f21b"),
},
},
},
{
RouteTableId: awssdk.String("rtb-0169b0937fd963ddc"), // table2
Routes: []*ec2.Route{
{
DestinationCidrBlock: awssdk.String("10.0.0.0/16"),
Origin: awssdk.String("CreateRouteTable"), // default route
},
{
DestinationCidrBlock: awssdk.String("0.0.0.0/0"),
GatewayId: awssdk.String("igw-030e74f73bd67f21b"),
},
{
DestinationIpv6CidrBlock: awssdk.String("::/0"),
GatewayId: awssdk.String("igw-030e74f73bd67f21b"),
},
},
},
mocks: func(client *repository.MockEC2Repository) {
client.On("ListAllRouteTables").Once().Return([]*ec2.RouteTable{
{
RouteTableId: awssdk.String("rtb-096bdfb69309c54c3"), // table1
Routes: []*ec2.Route{
{
DestinationCidrBlock: awssdk.String("10.0.0.0/16"),
Origin: awssdk.String("CreateRouteTable"), // default route
},
}, false)
callback(&ec2.DescribeRouteTablesOutput{
RouteTables: []*ec2.RouteTable{
{
RouteTableId: awssdk.String("rtb-02780c485f0be93c5"), // default_table
VpcId: awssdk.String("vpc-09fe5abc2309ba49d"),
Associations: []*ec2.RouteTableAssociation{
{
Main: awssdk.Bool(true),
},
},
Routes: []*ec2.Route{
{
DestinationCidrBlock: awssdk.String("10.0.0.0/16"),
Origin: awssdk.String("CreateRouteTable"), // default route
},
{
DestinationCidrBlock: awssdk.String("10.1.1.0/24"),
GatewayId: awssdk.String("igw-030e74f73bd67f21b"),
},
{
DestinationCidrBlock: awssdk.String("10.1.2.0/24"),
GatewayId: awssdk.String("igw-030e74f73bd67f21b"),
},
},
},
{
RouteTableId: awssdk.String(""), // table3
Routes: []*ec2.Route{
{
DestinationCidrBlock: awssdk.String("10.0.0.0/16"),
Origin: awssdk.String("CreateRouteTable"), // default route
},
},
},
{
DestinationCidrBlock: awssdk.String("1.1.1.1/32"),
GatewayId: awssdk.String("igw-030e74f73bd67f21b"),
},
}, true)
return true
})).Return(nil)
{
DestinationIpv6CidrBlock: awssdk.String("::/0"),
GatewayId: awssdk.String("igw-030e74f73bd67f21b"),
},
},
},
{
RouteTableId: awssdk.String("rtb-0169b0937fd963ddc"), // table2
Routes: []*ec2.Route{
{
DestinationCidrBlock: awssdk.String("10.0.0.0/16"),
Origin: awssdk.String("CreateRouteTable"), // default route
},
{
DestinationCidrBlock: awssdk.String("0.0.0.0/0"),
GatewayId: awssdk.String("igw-030e74f73bd67f21b"),
},
{
DestinationIpv6CidrBlock: awssdk.String("::/0"),
GatewayId: awssdk.String("igw-030e74f73bd67f21b"),
},
},
},
{
RouteTableId: awssdk.String("rtb-02780c485f0be93c5"), // default_table
VpcId: awssdk.String("vpc-09fe5abc2309ba49d"),
Associations: []*ec2.RouteTableAssociation{
{
Main: awssdk.Bool(true),
},
},
Routes: []*ec2.Route{
{
DestinationCidrBlock: awssdk.String("10.0.0.0/16"),
Origin: awssdk.String("CreateRouteTable"), // default route
},
{
DestinationCidrBlock: awssdk.String("10.1.1.0/24"),
GatewayId: awssdk.String("igw-030e74f73bd67f21b"),
},
{
DestinationCidrBlock: awssdk.String("10.1.2.0/24"),
GatewayId: awssdk.String("igw-030e74f73bd67f21b"),
},
},
},
{
RouteTableId: awssdk.String(""), // table3
Routes: []*ec2.Route{
{
DestinationCidrBlock: awssdk.String("10.0.0.0/16"),
Origin: awssdk.String("CreateRouteTable"), // default route
},
},
},
}, nil)
},
err: nil,
},
{
test: "cannot list route table",
dirName: "route_empty",
mocks: func(client *awstest.MockFakeEC2) {
client.On("DescribeRouteTablesPages",
&ec2.DescribeRouteTablesInput{},
mock.MatchedBy(func(callback func(res *ec2.DescribeRouteTablesOutput, lastPage bool) bool) bool {
return true
})).Return(awserr.NewRequestFailure(nil, 403, ""))
mocks: func(client *repository.MockEC2Repository) {
client.On("ListAllRouteTables").Once().Return(nil, awserr.NewRequestFailure(nil, 403, ""))
},
err: remoteerror.NewResourceEnumerationErrorWithType(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsRouteResourceType, resourceaws.AwsRouteTableResourceType),
},
@ -166,7 +147,7 @@ func TestRouteSupplier_Resources(t *testing.T) {
}
t.Run(c.test, func(tt *testing.T) {
fakeEC2 := awstest.MockFakeEC2{}
fakeEC2 := repository.MockEC2Repository{}
c.mocks(&fakeEC2)
provider := mocks2.NewMockedGoldenTFProvider(c.dirName, providerLibrary.Provider(terraform.AWS), shouldUpdate)
s := &RouteSupplier{