Merge branch 'main' into kms_key_supplier

main
Elie 2021-07-05 11:11:54 +02:00 committed by GitHub
commit 9cb0676c94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 136 additions and 343685 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 EC2InternetGatewayEnumerator struct {
repository repository.EC2Repository
factory resource.ResourceFactory
}
func NewEC2InternetGatewayEnumerator(repo repository.EC2Repository, factory resource.ResourceFactory) *EC2InternetGatewayEnumerator {
return &EC2InternetGatewayEnumerator{
repository: repo,
factory: factory,
}
}
func (e *EC2InternetGatewayEnumerator) SupportedType() resource.ResourceType {
return aws.AwsInternetGatewayResourceType
}
func (e *EC2InternetGatewayEnumerator) Enumerate() ([]resource.Resource, error) {
internetGateways, err := e.repository.ListAllInternetGateways()
if err != nil {
return nil, remoteerror.NewResourceEnumerationError(err, string(e.SupportedType()))
}
results := make([]resource.Resource, len(internetGateways))
for _, internetGateway := range internetGateways {
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
*internetGateway.InternetGatewayId,
map[string]interface{}{},
),
)
}
return results, err
}

View File

@ -81,6 +81,8 @@ func Init(version string, alerter *alerter.Alerter,
remoteLibrary.AddDetailsFetcher(aws.AwsEipAssociationResourceType, common.NewGenericDetailsFetcher(aws.AwsEipAssociationResourceType, provider, deserializer)) remoteLibrary.AddDetailsFetcher(aws.AwsEipAssociationResourceType, common.NewGenericDetailsFetcher(aws.AwsEipAssociationResourceType, provider, deserializer))
remoteLibrary.AddEnumerator(NewEC2InstanceEnumerator(ec2repository, factory)) remoteLibrary.AddEnumerator(NewEC2InstanceEnumerator(ec2repository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsInstanceResourceType, NewEC2InstanceDetailsFetcher(provider, deserializer)) remoteLibrary.AddDetailsFetcher(aws.AwsInstanceResourceType, NewEC2InstanceDetailsFetcher(provider, deserializer))
remoteLibrary.AddEnumerator(NewEC2InternetGatewayEnumerator(ec2repository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsInternetGatewayResourceType, common.NewGenericDetailsFetcher(aws.AwsInternetGatewayResourceType, 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))
@ -108,7 +110,6 @@ func Init(version string, alerter *alerter.Alerter,
supplierLibrary.AddSupplier(NewRouteSupplier(provider, deserializer, ec2repository)) supplierLibrary.AddSupplier(NewRouteSupplier(provider, deserializer, ec2repository))
supplierLibrary.AddSupplier(NewRouteTableAssociationSupplier(provider, deserializer, ec2repository)) supplierLibrary.AddSupplier(NewRouteTableAssociationSupplier(provider, deserializer, ec2repository))
supplierLibrary.AddSupplier(NewNatGatewaySupplier(provider, deserializer, ec2repository)) supplierLibrary.AddSupplier(NewNatGatewaySupplier(provider, deserializer, ec2repository))
supplierLibrary.AddSupplier(NewInternetGatewaySupplier(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))
supplierLibrary.AddSupplier(NewSNSTopicSupplier(provider, deserializer, snsRepository)) supplierLibrary.AddSupplier(NewSNSTopicSupplier(provider, deserializer, snsRepository))

View File

@ -1,65 +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 InternetGatewaySupplier struct {
reader terraform.ResourceReader
deserializer *resource.Deserializer
repository repository.EC2Repository
runner *terraform.ParallelResourceReader
}
func NewInternetGatewaySupplier(provider *AWSTerraformProvider, deserializer *resource.Deserializer, repository repository.EC2Repository) *InternetGatewaySupplier {
return &InternetGatewaySupplier{
provider,
deserializer,
repository,
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
}
}
func (s *InternetGatewaySupplier) Resources() ([]resource.Resource, error) {
internetGateways, err := s.repository.ListAllInternetGateways()
if err != nil {
return nil, remoteerror.NewResourceEnumerationError(err, aws.AwsInternetGatewayResourceType)
}
for _, internetGateway := range internetGateways {
gtw := *internetGateway
s.runner.Run(func() (cty.Value, error) {
return s.readInternetGateway(gtw)
})
}
resources, err := s.runner.Wait()
if err != nil {
return nil, err
}
return s.deserializer.Deserialize(aws.AwsInternetGatewayResourceType, resources)
}
func (s *InternetGatewaySupplier) readInternetGateway(internetGateway ec2.InternetGateway) (cty.Value, error) {
var Ty resource.ResourceType = aws.AwsInternetGatewayResourceType
val, err := s.reader.ReadResource(terraform.ReadResourceArgs{
Ty: Ty,
ID: *internetGateway.InternetGatewayId,
})
if err != nil {
logrus.WithFields(logrus.Fields{
"type": Ty,
}).Error(err)
return cty.NilVal, err
}
return *val, nil
}

View File

@ -1,105 +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/awserr"
awssdk "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"
"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 TestInternetGatewaySupplier_Resources(t *testing.T) {
cases := []struct {
test string
dirName string
mocks func(repo *repository.MockEC2Repository)
err error
}{
{
test: "no internet gateways",
dirName: "internet_gateway_empty",
mocks: func(repo *repository.MockEC2Repository) {
repo.On("ListAllInternetGateways").Once().Return([]*ec2.InternetGateway{}, nil)
},
err: nil,
},
{
test: "multiple internet gateways",
dirName: "internet_gateway_multiple",
mocks: func(repo *repository.MockEC2Repository) {
repo.On("ListAllInternetGateways").Once().Return([]*ec2.InternetGateway{
{
InternetGatewayId: awssdk.String("igw-0184eb41aadc62d1c"),
},
{
InternetGatewayId: awssdk.String("igw-047b487f5c60fca99"),
},
}, nil)
},
err: nil,
},
{
test: "cannot list internet gateways",
dirName: "internet_gateway_empty",
mocks: func(repo *repository.MockEC2Repository) {
repo.On("ListAllInternetGateways").Once().Return(nil, awserr.NewRequestFailure(nil, 403, ""))
},
err: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsInternetGatewayResourceType),
},
}
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(NewInternetGatewaySupplier(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 := &InternetGatewaySupplier{
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

@ -647,3 +647,91 @@ func TestEC2Instance(t *testing.T) {
}) })
} }
} }
func TestEC2InternetGateway(t *testing.T) {
tests := []struct {
test string
dirName string
mocks func(repository *repository.MockEC2Repository)
wantErr error
}{
{
test: "no internet gateways",
dirName: "aws_ec2_internet_gateway_empty",
mocks: func(repository *repository.MockEC2Repository) {
repository.On("ListAllInternetGateways").Return([]*ec2.InternetGateway{}, nil)
},
},
{
test: "multiple internet gateways",
dirName: "aws_ec2_internet_gateway_multiple",
mocks: func(repository *repository.MockEC2Repository) {
repository.On("ListAllInternetGateways").Return([]*ec2.InternetGateway{
{InternetGatewayId: awssdk.String("igw-0184eb41aadc62d1c")},
{InternetGatewayId: awssdk.String("igw-047b487f5c60fca99")},
}, nil)
},
},
{
test: "cannot list internet gateways",
dirName: "aws_ec2_internet_gateway_list",
mocks: func(repository *repository.MockEC2Repository) {
repository.On("ListAllInternetGateways").Return(nil, awserr.NewRequestFailure(nil, 403, ""))
},
wantErr: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsInternetGatewayResourceType),
},
}
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.NewEC2InternetGatewayEnumerator(repo, factory))
remoteLibrary.AddDetailsFetcher(resourceaws.AwsInternetGatewayResourceType, common.NewGenericDetailsFetcher(resourceaws.AwsInternetGatewayResourceType, 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.AwsInternetGatewayResourceType, c.dirName, provider, deserializer, shouldUpdate, tt)
})
}
}