Merge pull request #807 from cloudskiff/nat_gateway_supplier

Split nat_gateway_supplier
main
William BEUIL 2021-07-09 10:58:17 +02:00 committed by GitHub
commit ce479cdcad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 137 additions and 343687 deletions

View File

@ -0,0 +1,46 @@
package aws
import (
"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 EC2NatGatewayEnumerator struct {
repository repository.EC2Repository
factory resource.ResourceFactory
}
func NewEC2NatGatewayEnumerator(repo repository.EC2Repository, factory resource.ResourceFactory) *EC2NatGatewayEnumerator {
return &EC2NatGatewayEnumerator{
repository: repo,
factory: factory,
}
}
func (e *EC2NatGatewayEnumerator) SupportedType() resource.ResourceType {
return aws.AwsNatGatewayResourceType
}
func (e *EC2NatGatewayEnumerator) Enumerate() ([]resource.Resource, error) {
natGateways, err := e.repository.ListAllNatGateways()
if err != nil {
return nil, remoteerror.NewResourceEnumerationError(err, string(e.SupportedType()))
}
results := make([]resource.Resource, len(natGateways))
for _, natGateway := range natGateways {
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
*natGateway.NatGatewayId,
map[string]interface{}{},
),
)
}
return results, err
}

View File

@ -103,6 +103,8 @@ func Init(version string, alerter *alerter.Alerter,
remoteLibrary.AddDetailsFetcher(aws.AwsSecurityGroupResourceType, common.NewGenericDetailsFetcher(aws.AwsSecurityGroupResourceType, provider, deserializer))
remoteLibrary.AddEnumerator(NewVPCDefaultSecurityGroupEnumerator(ec2repository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsDefaultSecurityGroupResourceType, common.NewGenericDetailsFetcher(aws.AwsDefaultSecurityGroupResourceType, provider, deserializer))
remoteLibrary.AddEnumerator(NewEC2NatGatewayEnumerator(ec2repository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsNatGatewayResourceType, common.NewGenericDetailsFetcher(aws.AwsNatGatewayResourceType, provider, deserializer))
remoteLibrary.AddEnumerator(NewKMSKeyEnumerator(kmsRepository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsKmsKeyResourceType, common.NewGenericDetailsFetcher(aws.AwsKmsKeyResourceType, provider, deserializer))
@ -153,7 +155,6 @@ func Init(version string, alerter *alerter.Alerter,
supplierLibrary.AddSupplier(NewIamRolePolicyAttachmentSupplier(provider, deserializer, iamRepository))
supplierLibrary.AddSupplier(NewVPCSecurityGroupRuleSupplier(provider, deserializer, ec2repository))
supplierLibrary.AddSupplier(NewRouteSupplier(provider, deserializer, ec2repository))
supplierLibrary.AddSupplier(NewNatGatewaySupplier(provider, deserializer, ec2repository))
supplierLibrary.AddSupplier(NewECRRepositorySupplier(provider, deserializer, ecrRepository))
supplierLibrary.AddSupplier(NewLambdaEventSourceMappingSupplier(provider, deserializer, lambdaRepository))

View File

@ -1,72 +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/sirupsen/logrus"
"github.com/zclconf/go-cty/cty"
)
type NatGatewaySupplier struct {
reader terraform.ResourceReader
deserializer *resource.Deserializer
repository repository.EC2Repository
runner *terraform.ParallelResourceReader
}
func NewNatGatewaySupplier(provider *AWSTerraformProvider, deserializer *resource.Deserializer, repository repository.EC2Repository) *NatGatewaySupplier {
return &NatGatewaySupplier{
provider,
deserializer,
repository,
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
}
}
func (s *NatGatewaySupplier) Resources() ([]resource.Resource, error) {
retrievedNatGateways, err := s.repository.ListAllNatGateways()
if err != nil {
return nil, remoteerror.NewResourceEnumerationError(err, aws.AwsNatGatewayResourceType)
}
for _, gateway := range retrievedNatGateways {
res := *gateway
s.runner.Run(func() (cty.Value, error) {
return s.readNatGateway(res)
})
}
// Retrieve results from terraform provider
natGatewayResources, err := s.runner.Wait()
if err != nil {
return nil, err
}
// Deserialize
resources, err := s.deserializer.Deserialize(aws.AwsNatGatewayResourceType, natGatewayResources)
if err != nil {
return nil, err
}
return resources, nil
}
func (s *NatGatewaySupplier) readNatGateway(gateway ec2.NatGateway) (cty.Value, error) {
var Ty resource.ResourceType = aws.AwsNatGatewayResourceType
val, err := s.reader.ReadResource(terraform.ReadResourceArgs{
ID: *gateway.NatGatewayId,
Ty: Ty,
})
if err != nil {
logrus.WithFields(logrus.Fields{
"type": Ty,
}).Error(err)
return cty.NilVal, err
}
return *val, nil
}

View File

@ -1,100 +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"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/cloudskiff/driftctl/pkg/parallel"
"github.com/cloudskiff/driftctl/pkg/resource"
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
"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 TestNatGatewaySupplier_Resources(t *testing.T) {
cases := []struct {
test string
dirName string
mocks func(repo *repository.MockEC2Repository)
err error
}{
{
test: "no gateway",
dirName: "nat_gateway_empty",
mocks: func(repo *repository.MockEC2Repository) {
repo.On("ListAllNatGateways").Once().Return([]*ec2.NatGateway{}, nil)
},
err: nil,
},
{
test: "single aws_nat_gateway",
dirName: "nat_gateway",
mocks: func(repo *repository.MockEC2Repository) {
repo.On("ListAllNatGateways").Once().Return([]*ec2.NatGateway{
{
NatGatewayId: aws.String("nat-0a5408508b19ef490"),
},
}, nil)
},
err: nil,
},
{
test: "cannot list gateway",
dirName: "nat_gateway_empty",
mocks: func(repo *repository.MockEC2Repository) {
repo.On("ListAllNatGateways").Once().Return(nil, awserr.NewRequestFailure(nil, 403, ""))
},
err: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsNatGatewayResourceType),
},
}
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(NewNatGatewaySupplier(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 := &NatGatewaySupplier{
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

@ -1730,3 +1730,91 @@ func TestVpcDefaultSecurityGroup(t *testing.T) {
})
}
}
func TestEC2NatGateway(t *testing.T) {
tests := []struct {
test string
dirName string
mocks func(repository *repository.MockEC2Repository)
wantErr error
}{
{
test: "no nat gateways",
dirName: "aws_ec2_nat_gateway_empty",
mocks: func(repository *repository.MockEC2Repository) {
repository.On("ListAllNatGateways").Return([]*ec2.NatGateway{}, nil)
},
},
{
test: "single nat gateway",
dirName: "aws_ec2_nat_gateway_single",
mocks: func(repository *repository.MockEC2Repository) {
repository.On("ListAllNatGateways").Return([]*ec2.NatGateway{
{NatGatewayId: awssdk.String("nat-0a5408508b19ef490")},
}, nil)
},
},
{
test: "cannot list nat gateways",
dirName: "aws_ec2_nat_gateway_list",
mocks: func(repository *repository.MockEC2Repository) {
repository.On("ListAllNatGateways").Return(nil, awserr.NewRequestFailure(nil, 403, ""))
},
wantErr: nil,
},
}
schemaRepository := testresource.InitFakeSchemaRepository("aws", "3.19.0")
resourceaws.InitResourcesMetadata(schemaRepository)
factory := terraform.NewTerraformResourceFactory(schemaRepository)
deserializer := resource.NewDeserializer(factory)
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
alerter := &mocks.AlerterInterface{}
alerter.On("SendAlert", mock.Anything, mock.Anything).Maybe().Return()
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.NewEC2NatGatewayEnumerator(repo, factory))
remoteLibrary.AddDetailsFetcher(resourceaws.AwsNatGatewayResourceType, common.NewGenericDetailsFetcher(resourceaws.AwsNatGatewayResourceType, 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.AwsNatGatewayResourceType, c.dirName, provider, deserializer, shouldUpdate, tt)
})
}
}

View File

@ -0,0 +1 @@
[]