Split route_table_association_supplier

main
William Beuil 2021-07-05 13:15:25 +02:00
parent 92bcdaa69d
commit 019f8c9773
No known key found for this signature in database
GPG Key ID: BED2072C5C2BF537
14 changed files with 269 additions and 343790 deletions

View File

@ -0,0 +1,37 @@
package aws
import (
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/resource/aws"
"github.com/cloudskiff/driftctl/pkg/terraform"
)
type EC2RouteTableAssociationDetailsFetcher struct {
reader terraform.ResourceReader
deserializer *resource.Deserializer
}
func NewEC2RouteTableAssociationDetailsFetcher(provider terraform.ResourceReader, deserializer *resource.Deserializer) *EC2RouteTableAssociationDetailsFetcher {
return &EC2RouteTableAssociationDetailsFetcher{
reader: provider,
deserializer: deserializer,
}
}
func (r *EC2RouteTableAssociationDetailsFetcher) ReadDetails(res resource.Resource) (resource.Resource, error) {
ctyVal, err := r.reader.ReadResource(terraform.ReadResourceArgs{
Ty: aws.AwsRouteTableAssociationResourceType,
ID: res.TerraformId(),
Attributes: map[string]string{
"route_table_id": *res.Attributes().GetString("route_table_id"),
},
})
if err != nil {
return nil, err
}
deserializedRes, err := r.deserializer.DeserializeOne(aws.AwsRouteTableAssociationResourceType, *ctyVal)
if err != nil {
return nil, err
}
return deserializedRes, nil
}

View File

@ -0,0 +1,69 @@
package aws
import (
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/resource/aws"
)
type EC2RouteTableAssociationEnumerator struct {
repository repository.EC2Repository
factory resource.ResourceFactory
}
func NewEC2RouteTableAssociationEnumerator(repo repository.EC2Repository, factory resource.ResourceFactory) *EC2RouteTableAssociationEnumerator {
return &EC2RouteTableAssociationEnumerator{
repository: repo,
factory: factory,
}
}
func (e *EC2RouteTableAssociationEnumerator) SupportedType() resource.ResourceType {
return aws.AwsRouteTableAssociationResourceType
}
func (e *EC2RouteTableAssociationEnumerator) Enumerate() ([]resource.Resource, error) {
routeTables, err := e.repository.ListAllRouteTables()
if err != nil {
return nil, remoteerror.NewResourceEnumerationErrorWithType(err, string(e.SupportedType()), aws.AwsRouteTableResourceType)
}
var results []resource.Resource
for _, routeTable := range routeTables {
for _, assoc := range routeTable.Associations {
if e.shouldBeIgnored(assoc) {
continue
}
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
*assoc.RouteTableAssociationId,
map[string]interface{}{
"route_table_id": *assoc.RouteTableId,
},
),
)
}
}
return results, err
}
func (e *EC2RouteTableAssociationEnumerator) shouldBeIgnored(assoc *ec2.RouteTableAssociation) bool {
// Ignore when nothing is associated
if assoc.GatewayId == nil && assoc.SubnetId == nil {
return true
}
// Ignore when association is not associated
if assoc.AssociationState != nil && assoc.AssociationState.State != nil &&
*assoc.AssociationState.State != "associated" {
return true
}
return false
}

View File

@ -83,18 +83,18 @@ func Init(version string, alerter *alerter.Alerter,
remoteLibrary.AddDetailsFetcher(aws.AwsInstanceResourceType, NewEC2InstanceDetailsFetcher(provider, deserializer)) remoteLibrary.AddDetailsFetcher(aws.AwsInstanceResourceType, NewEC2InstanceDetailsFetcher(provider, deserializer))
remoteLibrary.AddEnumerator(NewEC2InternetGatewayEnumerator(ec2repository, factory)) remoteLibrary.AddEnumerator(NewEC2InternetGatewayEnumerator(ec2repository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsInternetGatewayResourceType, common.NewGenericDetailsFetcher(aws.AwsInternetGatewayResourceType, provider, deserializer)) remoteLibrary.AddDetailsFetcher(aws.AwsInternetGatewayResourceType, common.NewGenericDetailsFetcher(aws.AwsInternetGatewayResourceType, provider, deserializer))
remoteLibrary.AddEnumerator(NewVPCEnumerator(ec2repository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsVpcResourceType, common.NewGenericDetailsFetcher(aws.AwsVpcResourceType, provider, deserializer))
remoteLibrary.AddEnumerator(NewDefaultVPCEnumerator(ec2repository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsDefaultVpcResourceType, common.NewGenericDetailsFetcher(aws.AwsDefaultVpcResourceType, provider, deserializer))
remoteLibrary.AddEnumerator(NewEC2RouteTableAssociationEnumerator(ec2repository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsRouteTableAssociationResourceType, NewEC2RouteTableAssociationDetailsFetcher(provider, deserializer))
remoteLibrary.AddEnumerator(NewKMSKeyEnumerator(kmsRepository, factory)) remoteLibrary.AddEnumerator(NewKMSKeyEnumerator(kmsRepository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsKmsKeyResourceType, common.NewGenericDetailsFetcher(aws.AwsKmsKeyResourceType, provider, deserializer)) remoteLibrary.AddDetailsFetcher(aws.AwsKmsKeyResourceType, common.NewGenericDetailsFetcher(aws.AwsKmsKeyResourceType, provider, deserializer))
remoteLibrary.AddEnumerator(NewKMSAliasEnumerator(kmsRepository, factory)) remoteLibrary.AddEnumerator(NewKMSAliasEnumerator(kmsRepository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsKmsAliasResourceType, common.NewGenericDetailsFetcher(aws.AwsKmsAliasResourceType, provider, deserializer)) remoteLibrary.AddDetailsFetcher(aws.AwsKmsAliasResourceType, common.NewGenericDetailsFetcher(aws.AwsKmsAliasResourceType, provider, deserializer))
remoteLibrary.AddEnumerator(NewVPCEnumerator(ec2repository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsVpcResourceType, common.NewGenericDetailsFetcher(aws.AwsVpcResourceType, provider, deserializer))
remoteLibrary.AddEnumerator(NewDefaultVPCEnumerator(ec2repository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsDefaultVpcResourceType, common.NewGenericDetailsFetcher(aws.AwsDefaultVpcResourceType, provider, deserializer))
remoteLibrary.AddEnumerator(NewRoute53HealthCheckEnumerator(route53repository, factory)) remoteLibrary.AddEnumerator(NewRoute53HealthCheckEnumerator(route53repository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsRoute53HealthCheckResourceType, common.NewGenericDetailsFetcher(aws.AwsRoute53HealthCheckResourceType, provider, deserializer)) remoteLibrary.AddDetailsFetcher(aws.AwsRoute53HealthCheckResourceType, common.NewGenericDetailsFetcher(aws.AwsRoute53HealthCheckResourceType, provider, deserializer))
@ -118,7 +118,6 @@ func Init(version string, alerter *alerter.Alerter,
supplierLibrary.AddSupplier(NewSubnetSupplier(provider, deserializer, ec2repository)) supplierLibrary.AddSupplier(NewSubnetSupplier(provider, deserializer, ec2repository))
supplierLibrary.AddSupplier(NewRouteTableSupplier(provider, deserializer, ec2repository)) supplierLibrary.AddSupplier(NewRouteTableSupplier(provider, deserializer, ec2repository))
supplierLibrary.AddSupplier(NewRouteSupplier(provider, deserializer, ec2repository)) supplierLibrary.AddSupplier(NewRouteSupplier(provider, deserializer, ec2repository))
supplierLibrary.AddSupplier(NewRouteTableAssociationSupplier(provider, deserializer, ec2repository))
supplierLibrary.AddSupplier(NewNatGatewaySupplier(provider, deserializer, ec2repository)) supplierLibrary.AddSupplier(NewNatGatewaySupplier(provider, deserializer, ec2repository))
supplierLibrary.AddSupplier(NewSqsQueueSupplier(provider, deserializer, sqsRepository)) supplierLibrary.AddSupplier(NewSqsQueueSupplier(provider, deserializer, sqsRepository))
supplierLibrary.AddSupplier(NewSqsQueuePolicySupplier(provider, deserializer, sqsRepository)) supplierLibrary.AddSupplier(NewSqsQueuePolicySupplier(provider, deserializer, sqsRepository))

View File

@ -1,99 +0,0 @@
package aws
import (
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/resource/aws"
"github.com/cloudskiff/driftctl/pkg/terraform"
"github.com/hashicorp/terraform/flatmap"
"github.com/sirupsen/logrus"
"github.com/zclconf/go-cty/cty"
)
type RouteTableAssociationSupplier struct {
reader terraform.ResourceReader
deserializer *resource.Deserializer
repository repository.EC2Repository
runner *terraform.ParallelResourceReader
}
func NewRouteTableAssociationSupplier(provider *AWSTerraformProvider, deserializer *resource.Deserializer, repository repository.EC2Repository) *RouteTableAssociationSupplier {
return &RouteTableAssociationSupplier{
provider,
deserializer,
repository,
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
}
}
func (s *RouteTableAssociationSupplier) Resources() ([]resource.Resource, error) {
tables, err := s.repository.ListAllRouteTables()
if err != nil {
return nil, remoteerror.NewResourceEnumerationErrorWithType(err, aws.AwsRouteTableAssociationResourceType, aws.AwsRouteTableResourceType)
}
for _, t := range tables {
table := *t
for _, assoc := range table.Associations {
res := *assoc
if s.shouldBeIgnored(assoc) {
continue
}
s.runner.Run(func() (cty.Value, error) {
return s.readRouteTableAssociation(res)
})
}
}
// Retrieve results from terraform provider
routeTableAssociationResources, err := s.runner.Wait()
if err != nil {
return nil, err
}
// Deserialize
deserializedRouteTableAssociations, err := s.deserializer.Deserialize(aws.AwsRouteTableAssociationResourceType, routeTableAssociationResources)
if err != nil {
return nil, err
}
return deserializedRouteTableAssociations, nil
}
func (s *RouteTableAssociationSupplier) readRouteTableAssociation(assoc ec2.RouteTableAssociation) (cty.Value, error) {
var Ty resource.ResourceType = aws.AwsRouteTableAssociationResourceType
attributes := map[string]interface{}{
"route_table_id": *assoc.RouteTableId,
}
val, err := s.reader.ReadResource(terraform.ReadResourceArgs{
ID: *assoc.RouteTableAssociationId,
Ty: Ty,
Attributes: flatmap.Flatten(attributes),
})
if err != nil {
logrus.WithFields(logrus.Fields{
"type": Ty,
}).Error(err)
return cty.NilVal, err
}
return *val, nil
}
func (s *RouteTableAssociationSupplier) shouldBeIgnored(assoc *ec2.RouteTableAssociation) bool {
// Ignore when nothing is associated
if assoc.GatewayId == nil && assoc.SubnetId == nil {
return true
}
// Ignore when association not associated
if assoc.AssociationState != nil && assoc.AssociationState.State != nil &&
*assoc.AssociationState.State != "associated" {
return true
}
return false
}

View File

@ -1,170 +0,0 @@
package aws
import (
"context"
"testing"
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
"github.com/cloudskiff/driftctl/pkg/remote/cache"
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
testresource "github.com/cloudskiff/driftctl/test/resource"
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
"github.com/aws/aws-sdk-go/aws"
awssdk "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/cloudskiff/driftctl/pkg/parallel"
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/terraform"
"github.com/cloudskiff/driftctl/test"
"github.com/cloudskiff/driftctl/test/goldenfile"
mocks2 "github.com/cloudskiff/driftctl/test/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestRouteTableAssociationSupplier_Resources(t *testing.T) {
cases := []struct {
test string
dirName string
mocks func(client *repository.MockEC2Repository)
err error
}{
{
test: "no route table associations (test for nil values)",
dirName: "route_table_assoc_empty",
mocks: func(client *repository.MockEC2Repository) {
client.On("ListAllRouteTables").Once().Return([]*ec2.RouteTable{
{
RouteTableId: awssdk.String("assoc_with_nil"),
Associations: []*ec2.RouteTableAssociation{
{
AssociationState: nil,
GatewayId: nil,
Main: nil,
RouteTableAssociationId: nil,
RouteTableId: nil,
SubnetId: nil,
},
},
},
{
RouteTableId: awssdk.String("nil_assoc"),
},
}, nil)
},
err: nil,
},
{
test: "route_table_association (mixed subnet and gateway associations)",
dirName: "route_table_assoc",
mocks: func(client *repository.MockEC2Repository) {
client.On("ListAllRouteTables").Once().Return([]*ec2.RouteTable{
{
RouteTableId: aws.String("rtb-05aa6c5673311a17b"), // route
Associations: []*ec2.RouteTableAssociation{
{ // Should be ignored
AssociationState: &ec2.RouteTableAssociationState{
State: awssdk.String("disassociated"),
},
GatewayId: awssdk.String("dummy-id"),
},
{ // Should be ignored
SubnetId: nil,
GatewayId: nil,
},
{ // assoc_route_subnet1
AssociationState: &ec2.RouteTableAssociationState{
State: awssdk.String("associated"),
},
Main: awssdk.Bool(false),
RouteTableAssociationId: awssdk.String("rtbassoc-0809598f92dbec03b"),
RouteTableId: awssdk.String("rtb-05aa6c5673311a17b"),
SubnetId: awssdk.String("subnet-05185af647b2eeda3"),
},
{ // assoc_route_subnet
AssociationState: &ec2.RouteTableAssociationState{
State: awssdk.String("associated"),
},
Main: awssdk.Bool(false),
RouteTableAssociationId: awssdk.String("rtbassoc-01957791b2cfe6ea4"),
RouteTableId: awssdk.String("rtb-05aa6c5673311a17b"),
SubnetId: awssdk.String("subnet-0e93dbfa2e5dd8282"),
},
{ // assoc_route_subnet2
AssociationState: &ec2.RouteTableAssociationState{
State: awssdk.String("associated"),
},
GatewayId: nil,
Main: awssdk.Bool(false),
RouteTableAssociationId: awssdk.String("rtbassoc-0b4f97ea57490e213"),
RouteTableId: awssdk.String("rtb-05aa6c5673311a17b"),
SubnetId: awssdk.String("subnet-0fd966efd884d0362"),
},
},
},
{
RouteTableId: aws.String("rtb-09df7cc9d16de9f8f"), // route2
Associations: []*ec2.RouteTableAssociation{
{ // assoc_route2_gateway
AssociationState: &ec2.RouteTableAssociationState{
State: awssdk.String("associated"),
},
RouteTableAssociationId: awssdk.String("rtbassoc-0a79ccacfceb4944b"),
RouteTableId: awssdk.String("rtb-09df7cc9d16de9f8f"),
GatewayId: awssdk.String("igw-0238f6e09185ac954"),
},
},
},
}, nil)
},
err: nil,
},
{
test: "Cannot list route table",
dirName: "route_table_assoc_empty",
mocks: func(client *repository.MockEC2Repository) {
client.On("ListAllRouteTables").Once().Return(nil, awserr.NewRequestFailure(nil, 403, ""))
},
err: remoteerror.NewResourceEnumerationErrorWithType(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsRouteTableAssociationResourceType, resourceaws.AwsRouteTableResourceType),
},
}
for _, c := range cases {
shouldUpdate := c.dirName == *goldenfile.Update
providerLibrary := terraform.NewProviderLibrary()
supplierLibrary := resource.NewSupplierLibrary()
repo := testresource.InitFakeSchemaRepository("aws", "3.19.0")
resourceaws.InitResourcesMetadata(repo)
factory := terraform.NewTerraformResourceFactory(repo)
deserializer := resource.NewDeserializer(factory)
if shouldUpdate {
provider, err := InitTestAwsProvider(providerLibrary)
if err != nil {
t.Fatal(err)
}
supplierLibrary.AddSupplier(NewRouteTableAssociationSupplier(provider, deserializer, repository.NewEC2Repository(provider.session, cache.New(0))))
}
t.Run(c.test, func(tt *testing.T) {
fakeEC2 := repository.MockEC2Repository{}
c.mocks(&fakeEC2)
provider := mocks2.NewMockedGoldenTFProvider(c.dirName, providerLibrary.Provider(terraform.AWS), shouldUpdate)
s := &RouteTableAssociationSupplier{
provider,
deserializer,
&fakeEC2,
terraform.NewParallelResourceReader(parallel.NewParallelRunner(context.TODO(), 10)),
}
got, err := s.Resources()
assert.Equal(tt, c.err, err)
mock.AssertExpectationsForObjects(tt)
test.CtyTestDiff(got, c.dirName, provider, deserializer, shouldUpdate, tt)
})
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -942,3 +942,160 @@ func TestDefaultVPC(t *testing.T) {
}) })
} }
} }
func TestEC2RouteTableAssociation(t *testing.T) {
tests := []struct {
test string
dirName string
mocks func(repository *repository.MockEC2Repository)
wantErr error
}{
{
test: "no route table associations (test for nil values)",
dirName: "aws_ec2_route_table_association_empty",
mocks: func(repository *repository.MockEC2Repository) {
repository.On("ListAllRouteTables").Return([]*ec2.RouteTable{
{
RouteTableId: awssdk.String("assoc_with_nil"),
Associations: []*ec2.RouteTableAssociation{
{
AssociationState: nil,
GatewayId: nil,
Main: nil,
RouteTableAssociationId: nil,
RouteTableId: nil,
SubnetId: nil,
},
},
},
{RouteTableId: awssdk.String("nil_assoc")},
}, nil)
},
},
{
test: "multiple route table associations (mixed subnet and gateway associations)",
dirName: "aws_ec2_route_table_association_multiple",
mocks: func(repository *repository.MockEC2Repository) {
repository.On("ListAllRouteTables").Return([]*ec2.RouteTable{
{
RouteTableId: awssdk.String("rtb-05aa6c5673311a17b"), // route
Associations: []*ec2.RouteTableAssociation{
{ // Should be ignored
AssociationState: &ec2.RouteTableAssociationState{
State: awssdk.String("disassociated"),
},
GatewayId: awssdk.String("dummy-id"),
},
{ // Should be ignored
SubnetId: nil,
GatewayId: nil,
},
{ // assoc_route_subnet1
AssociationState: &ec2.RouteTableAssociationState{
State: awssdk.String("associated"),
},
Main: awssdk.Bool(false),
RouteTableAssociationId: awssdk.String("rtbassoc-0809598f92dbec03b"),
RouteTableId: awssdk.String("rtb-05aa6c5673311a17b"),
SubnetId: awssdk.String("subnet-05185af647b2eeda3"),
},
{ // assoc_route_subnet
AssociationState: &ec2.RouteTableAssociationState{
State: awssdk.String("associated"),
},
Main: awssdk.Bool(false),
RouteTableAssociationId: awssdk.String("rtbassoc-01957791b2cfe6ea4"),
RouteTableId: awssdk.String("rtb-05aa6c5673311a17b"),
SubnetId: awssdk.String("subnet-0e93dbfa2e5dd8282"),
},
{ // assoc_route_subnet2
AssociationState: &ec2.RouteTableAssociationState{
State: awssdk.String("associated"),
},
GatewayId: nil,
Main: awssdk.Bool(false),
RouteTableAssociationId: awssdk.String("rtbassoc-0b4f97ea57490e213"),
RouteTableId: awssdk.String("rtb-05aa6c5673311a17b"),
SubnetId: awssdk.String("subnet-0fd966efd884d0362"),
},
},
},
{
RouteTableId: awssdk.String("rtb-09df7cc9d16de9f8f"), // route2
Associations: []*ec2.RouteTableAssociation{
{ // assoc_route2_gateway
AssociationState: &ec2.RouteTableAssociationState{
State: awssdk.String("associated"),
},
RouteTableAssociationId: awssdk.String("rtbassoc-0a79ccacfceb4944b"),
RouteTableId: awssdk.String("rtb-09df7cc9d16de9f8f"),
GatewayId: awssdk.String("igw-0238f6e09185ac954"),
},
},
},
}, nil)
},
},
{
test: "cannot list route table associations",
dirName: "aws_ec2_route_table_association_list",
mocks: func(repository *repository.MockEC2Repository) {
repository.On("ListAllRouteTables").Return(nil, awserr.NewRequestFailure(nil, 403, ""))
},
wantErr: remoteerror.NewResourceEnumerationErrorWithType(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsRouteTableAssociationResourceType, resourceaws.AwsRouteTableResourceType),
},
}
schemaRepository := testresource.InitFakeSchemaRepository("aws", "3.19.0")
resourceaws.InitResourcesMetadata(schemaRepository)
factory := terraform.NewTerraformResourceFactory(schemaRepository)
deserializer := resource.NewDeserializer(factory)
alerter := &mocks.AlerterInterface{}
for _, c := range tests {
t.Run(c.test, func(tt *testing.T) {
shouldUpdate := c.dirName == *goldenfile.Update
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
scanOptions := ScannerOptions{Deep: true}
providerLibrary := terraform.NewProviderLibrary()
remoteLibrary := common.NewRemoteLibrary()
// Initialize mocks
fakeRepo := &repository.MockEC2Repository{}
c.mocks(fakeRepo)
var repo repository.EC2Repository = fakeRepo
providerVersion := "3.19.0"
realProvider, err := terraform2.InitTestAwsProvider(providerLibrary, providerVersion)
if err != nil {
t.Fatal(err)
}
provider := terraform2.NewFakeTerraformProvider(realProvider)
provider.WithResponse(c.dirName)
// Replace mock by real resources if we are in update mode
if shouldUpdate {
err := realProvider.Init()
if err != nil {
t.Fatal(err)
}
provider.ShouldUpdate()
repo = repository.NewEC2Repository(sess, cache.New(0))
}
remoteLibrary.AddEnumerator(aws.NewEC2RouteTableAssociationEnumerator(repo, factory))
remoteLibrary.AddDetailsFetcher(resourceaws.AwsRouteTableAssociationResourceType, aws.NewEC2RouteTableAssociationDetailsFetcher(provider, deserializer))
s := NewScanner(nil, remoteLibrary, alerter, scanOptions)
got, err := s.Resources()
assert.Equal(tt, err, c.wantErr)
if err != nil {
return
}
test.TestAgainstGoldenFile(got, resourceaws.AwsRouteTableAssociationResourceType, c.dirName, provider, deserializer, shouldUpdate, tt)
})
}
}