Merge pull request #776 from cloudskiff/ec2_eip_supplier

Split ec2_eip_supplier
main
Elie 2021-07-01 16:34:00 +02:00 committed by GitHub
commit 9612ab7785
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 142 additions and 174 deletions

View File

@ -0,0 +1,49 @@
package aws
import (
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
tf "github.com/cloudskiff/driftctl/pkg/remote/terraform"
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/resource/aws"
)
type EC2EipEnumerator struct {
repository repository.EC2Repository
factory resource.ResourceFactory
providerConfig tf.TerraformProviderConfig
}
func NewEC2EipEnumerator(repo repository.EC2Repository, factory resource.ResourceFactory, providerConfig tf.TerraformProviderConfig) *EC2EipEnumerator {
return &EC2EipEnumerator{
repository: repo,
factory: factory,
providerConfig: providerConfig,
}
}
func (e *EC2EipEnumerator) SupportedType() resource.ResourceType {
return aws.AwsEipResourceType
}
func (e *EC2EipEnumerator) Enumerate() ([]resource.Resource, error) {
addresses, err := e.repository.ListAllAddresses()
if err != nil {
return nil, remoteerror.NewResourceEnumerationError(err, string(e.SupportedType()))
}
results := make([]resource.Resource, len(addresses))
for _, address := range addresses {
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
*address.AllocationId,
map[string]interface{}{},
),
)
}
return results, err
}

View File

@ -1,66 +0,0 @@
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"
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
"github.com/cloudskiff/driftctl/pkg/terraform"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/sirupsen/logrus"
"github.com/zclconf/go-cty/cty"
)
type EC2EipSupplier struct {
reader terraform.ResourceReader
deserializer *resource.Deserializer
repository repository.EC2Repository
runner *terraform.ParallelResourceReader
}
func NewEC2EipSupplier(provider *AWSTerraformProvider, repository repository.EC2Repository, deserializer *resource.Deserializer) *EC2EipSupplier {
return &EC2EipSupplier{
provider,
deserializer,
repository,
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
}
}
func (s *EC2EipSupplier) Resources() ([]resource.Resource, error) {
addresses, err := s.repository.ListAllAddresses()
if err != nil {
return nil, remoteerror.NewResourceEnumerationError(err, resourceaws.AwsEipResourceType)
}
results := make([]cty.Value, 0)
if len(addresses) > 0 {
for _, address := range addresses {
addr := *address
s.runner.Run(func() (cty.Value, error) {
return s.readEIP(addr)
})
}
results, err = s.runner.Wait()
if err != nil {
return nil, err
}
}
return s.deserializer.Deserialize(resourceaws.AwsEipResourceType, results)
}
func (s *EC2EipSupplier) readEIP(address ec2.Address) (cty.Value, error) {
id := aws.StringValue(address.AllocationId)
resAddress, err := s.reader.ReadResource(terraform.ReadResourceArgs{
Ty: resourceaws.AwsEipResourceType,
ID: id,
})
if err != nil {
logrus.Warnf("Error reading eip %s[%s]: %+v", id, resourceaws.AwsEipResourceType, err)
return cty.NilVal, err
}
return *resAddress, 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"
testresource "github.com/cloudskiff/driftctl/test/resource"
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/stretchr/testify/assert"
"github.com/cloudskiff/driftctl/pkg/parallel"
"github.com/cloudskiff/driftctl/test/goldenfile"
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/terraform"
"github.com/cloudskiff/driftctl/test"
"github.com/cloudskiff/driftctl/test/mocks"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
)
func TestEC2EipSupplier_Resources(t *testing.T) {
tests := []struct {
test string
dirName string
mock func(mock *repository.MockEC2Repository)
err error
}{
{
test: "no eips",
dirName: "ec2_eip_empty",
mock: func(mock *repository.MockEC2Repository) {
mock.On("ListAllAddresses").Return([]*ec2.Address{}, nil)
},
err: nil,
},
{
test: "with eips",
dirName: "ec2_eip_multiple",
mock: func(mock *repository.MockEC2Repository) {
mock.On("ListAllAddresses").Return([]*ec2.Address{
{
AllocationId: aws.String("eipalloc-017d5267e4dda73f1"),
},
{
AllocationId: aws.String("eipalloc-0cf714dc097c992cc"),
},
}, nil)
},
err: nil,
},
{
test: "Cannot list eips",
dirName: "ec2_eip_empty",
mock: func(mock *repository.MockEC2Repository) {
mock.On("ListAllAddresses").Return([]*ec2.Address{}, awserr.NewRequestFailure(nil, 403, ""))
},
err: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsEipResourceType),
},
}
for _, tt := range tests {
shouldUpdate := tt.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(NewEC2EipSupplier(provider, repository.NewEC2Repository(provider.session, cache.New(0)), deserializer))
}
t.Run(tt.test, func(t *testing.T) {
provider := mocks.NewMockedGoldenTFProvider(tt.dirName, providerLibrary.Provider(terraform.AWS), shouldUpdate)
client := &repository.MockEC2Repository{}
tt.mock(client)
s := &EC2EipSupplier{
provider,
deserializer,
client,
terraform.NewParallelResourceReader(parallel.NewParallelRunner(context.TODO(), 10)),
}
got, err := s.Resources()
assert.Equal(t, tt.err, err)
test.CtyTestDiff(got, tt.dirName, provider, deserializer, shouldUpdate, t)
})
}
}

View File

@ -70,11 +70,12 @@ func Init(version string, alerter *alerter.Alerter,
remoteLibrary.AddDetailsFetcher(aws.AwsEbsVolumeResourceType, common.NewGenericDetailFetcher(aws.AwsEbsVolumeResourceType, provider, deserializer)) remoteLibrary.AddDetailsFetcher(aws.AwsEbsVolumeResourceType, common.NewGenericDetailFetcher(aws.AwsEbsVolumeResourceType, provider, deserializer))
remoteLibrary.AddEnumerator(NewEC2EbsSnapshotEnumerator(ec2repository, factory, provider.Config)) remoteLibrary.AddEnumerator(NewEC2EbsSnapshotEnumerator(ec2repository, factory, provider.Config))
remoteLibrary.AddDetailsFetcher(aws.AwsEbsSnapshotResourceType, common.NewGenericDetailFetcher(aws.AwsEbsSnapshotResourceType, provider, deserializer)) remoteLibrary.AddDetailsFetcher(aws.AwsEbsSnapshotResourceType, common.NewGenericDetailFetcher(aws.AwsEbsSnapshotResourceType, provider, deserializer))
remoteLibrary.AddEnumerator(NewEC2EipEnumerator(ec2repository, factory, provider.Config))
remoteLibrary.AddDetailsFetcher(aws.AwsEipResourceType, common.NewGenericDetailFetcher(aws.AwsEipResourceType, provider, deserializer))
supplierLibrary.AddSupplier(NewS3BucketAnalyticSupplier(provider, s3Repository, deserializer)) supplierLibrary.AddSupplier(NewS3BucketAnalyticSupplier(provider, s3Repository, deserializer))
supplierLibrary.AddSupplier(NewS3BucketMetricSupplier(provider, s3Repository, deserializer)) supplierLibrary.AddSupplier(NewS3BucketMetricSupplier(provider, s3Repository, deserializer))
supplierLibrary.AddSupplier(NewS3BucketPolicySupplier(provider, s3Repository, deserializer)) supplierLibrary.AddSupplier(NewS3BucketPolicySupplier(provider, s3Repository, deserializer))
supplierLibrary.AddSupplier(NewEC2EipSupplier(provider, ec2repository, deserializer))
supplierLibrary.AddSupplier(NewEC2EipAssociationSupplier(provider, deserializer, ec2repository)) supplierLibrary.AddSupplier(NewEC2EipAssociationSupplier(provider, deserializer, ec2repository))
supplierLibrary.AddSupplier(NewRoute53ZoneSupplier(provider, deserializer, route53repository)) supplierLibrary.AddSupplier(NewRoute53ZoneSupplier(provider, deserializer, route53repository))
supplierLibrary.AddSupplier(NewRoute53RecordSupplier(provider, deserializer, route53repository)) supplierLibrary.AddSupplier(NewRoute53RecordSupplier(provider, deserializer, route53repository))

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -205,3 +205,94 @@ func TestEC2EbsSnapshot(t *testing.T) {
}) })
} }
} }
func TestEC2Eip(t *testing.T) {
tests := []struct {
test string
dirName string
mocks func(repository *repository.MockEC2Repository)
wantErr error
}{
{
test: "no eips",
dirName: "aws_ec2_eip_empty",
mocks: func(repository *repository.MockEC2Repository) {
repository.On("ListAllAddresses").Return([]*ec2.Address{}, nil)
},
},
{
test: "multiple eips",
dirName: "aws_ec2_eip_multiple",
mocks: func(repository *repository.MockEC2Repository) {
repository.On("ListAllAddresses").Return([]*ec2.Address{
{AllocationId: awssdk.String("eipalloc-017d5267e4dda73f1")},
{AllocationId: awssdk.String("eipalloc-0cf714dc097c992cc")},
}, nil)
},
},
{
test: "cannot list eips",
dirName: "aws_ec2_eip_list",
mocks: func(repository *repository.MockEC2Repository) {
repository.On("ListAllAddresses").Return(nil, awserr.NewRequestFailure(nil, 403, ""))
},
wantErr: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsEipResourceType),
},
}
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.NewEC2EipEnumerator(repo, factory, tf.TerraformProviderConfig{
Name: "test",
DefaultAlias: "eu-west-3",
}))
remoteLibrary.AddDetailsFetcher(resourceaws.AwsEipResourceType, common.NewGenericDetailFetcher(resourceaws.AwsEipResourceType, 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.AwsEipResourceType, c.dirName, provider, deserializer, shouldUpdate, tt)
})
}
}