Merge pull request #778 from cloudskiff/ec2_eip_association_supplier
Split ec2_eip_association_suppliermain
commit
20df796b9c
|
@ -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 EC2EipAssociationEnumerator struct {
|
||||
repository repository.EC2Repository
|
||||
factory resource.ResourceFactory
|
||||
providerConfig tf.TerraformProviderConfig
|
||||
}
|
||||
|
||||
func NewEC2EipAssociationEnumerator(repo repository.EC2Repository, factory resource.ResourceFactory, providerConfig tf.TerraformProviderConfig) *EC2EipAssociationEnumerator {
|
||||
return &EC2EipAssociationEnumerator{
|
||||
repository: repo,
|
||||
factory: factory,
|
||||
providerConfig: providerConfig,
|
||||
}
|
||||
}
|
||||
|
||||
func (e *EC2EipAssociationEnumerator) SupportedType() resource.ResourceType {
|
||||
return aws.AwsEipAssociationResourceType
|
||||
}
|
||||
|
||||
func (e *EC2EipAssociationEnumerator) Enumerate() ([]resource.Resource, error) {
|
||||
associationIds, err := e.repository.ListAllAddressesAssociation()
|
||||
if err != nil {
|
||||
return nil, remoteerror.NewResourceEnumerationError(err, string(e.SupportedType()))
|
||||
}
|
||||
|
||||
results := make([]resource.Resource, len(associationIds))
|
||||
|
||||
for _, associationId := range associationIds {
|
||||
results = append(
|
||||
results,
|
||||
e.factory.CreateAbstractResource(
|
||||
string(e.SupportedType()),
|
||||
associationId,
|
||||
map[string]interface{}{},
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
return results, err
|
||||
}
|
|
@ -1,62 +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/sirupsen/logrus"
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
)
|
||||
|
||||
type EC2EipAssociationSupplier struct {
|
||||
reader terraform.ResourceReader
|
||||
deserializer *resource.Deserializer
|
||||
repository repository.EC2Repository
|
||||
runner *terraform.ParallelResourceReader
|
||||
}
|
||||
|
||||
func NewEC2EipAssociationSupplier(provider *AWSTerraformProvider, deserializer *resource.Deserializer, repository repository.EC2Repository) *EC2EipAssociationSupplier {
|
||||
return &EC2EipAssociationSupplier{
|
||||
provider,
|
||||
deserializer,
|
||||
repository,
|
||||
terraform.NewParallelResourceReader(provider.Runner().SubRunner())}
|
||||
}
|
||||
|
||||
func (s *EC2EipAssociationSupplier) Resources() ([]resource.Resource, error) {
|
||||
associationIds, err := s.repository.ListAllAddressesAssociation()
|
||||
if err != nil {
|
||||
return nil, remoteerror.NewResourceEnumerationError(err, resourceaws.AwsEipAssociationResourceType)
|
||||
}
|
||||
results := make([]cty.Value, 0)
|
||||
if len(associationIds) > 0 {
|
||||
for _, assocId := range associationIds {
|
||||
assocId := assocId
|
||||
s.runner.Run(func() (cty.Value, error) {
|
||||
return s.readEIPAssociation(assocId)
|
||||
})
|
||||
}
|
||||
results, err = s.runner.Wait()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return s.deserializer.Deserialize(resourceaws.AwsEipAssociationResourceType, results)
|
||||
}
|
||||
|
||||
func (s *EC2EipAssociationSupplier) readEIPAssociation(assocId string) (cty.Value, error) {
|
||||
resAssoc, err := s.reader.ReadResource(terraform.ReadResourceArgs{
|
||||
Ty: resourceaws.AwsEipAssociationResourceType,
|
||||
ID: assocId,
|
||||
})
|
||||
if err != nil {
|
||||
logrus.Warnf("Error reading eip association %s[%s]: %+v", assocId, resourceaws.AwsEipAssociationResourceType, err)
|
||||
return cty.NilVal, err
|
||||
}
|
||||
return *resAssoc, nil
|
||||
}
|
|
@ -1,97 +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"
|
||||
)
|
||||
|
||||
func TestEC2EipAssociationSupplier_Resources(t *testing.T) {
|
||||
tests := []struct {
|
||||
test string
|
||||
dirName string
|
||||
mock func(mock *repository.MockEC2Repository)
|
||||
err error
|
||||
}{
|
||||
{
|
||||
test: "no eip associations",
|
||||
dirName: "ec2_eip_association_empty",
|
||||
mock: func(mock *repository.MockEC2Repository) {
|
||||
mock.On("ListAllAddressesAssociation").Return([]string{}, nil)
|
||||
},
|
||||
err: nil,
|
||||
},
|
||||
{
|
||||
test: "with eip associations",
|
||||
dirName: "ec2_eip_association_single",
|
||||
mock: func(mock *repository.MockEC2Repository) {
|
||||
mock.On("ListAllAddressesAssociation").Return([]string{
|
||||
"eipassoc-0e9a7356e30f0c3d1",
|
||||
}, nil)
|
||||
},
|
||||
err: nil,
|
||||
},
|
||||
{
|
||||
test: "Cannot list eip associations",
|
||||
dirName: "ec2_eip_association_empty",
|
||||
mock: func(mock *repository.MockEC2Repository) {
|
||||
mock.On("ListAllAddressesAssociation").Return([]string{}, awserr.NewRequestFailure(nil, 403, ""))
|
||||
},
|
||||
err: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsEipAssociationResourceType),
|
||||
},
|
||||
}
|
||||
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(NewEC2EipAssociationSupplier(provider, deserializer, repository.NewEC2Repository(provider.session, cache.New(0))))
|
||||
}
|
||||
|
||||
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 := &EC2EipAssociationSupplier{
|
||||
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)
|
||||
})
|
||||
}
|
||||
}
|
|
@ -60,7 +60,6 @@ func Init(version string, alerter *alerter.Alerter,
|
|||
|
||||
remoteLibrary.AddEnumerator(NewS3BucketEnumerator(s3Repository, factory, provider.Config))
|
||||
remoteLibrary.AddDetailsFetcher(aws.AwsS3BucketResourceType, NewS3BucketDetailsFetcher(provider, deserializer))
|
||||
|
||||
remoteLibrary.AddEnumerator(NewS3BucketInventoryEnumerator(s3Repository, factory, provider.Config))
|
||||
remoteLibrary.AddDetailsFetcher(aws.AwsS3BucketInventoryResourceType, NewS3BucketInventoryDetailsFetcher(provider, deserializer))
|
||||
remoteLibrary.AddEnumerator(NewS3BucketNotificationEnumerator(s3Repository, factory, provider.Config))
|
||||
|
@ -78,10 +77,11 @@ func Init(version string, alerter *alerter.Alerter,
|
|||
remoteLibrary.AddDetailsFetcher(aws.AwsAmiResourceType, common.NewGenericDetailsFetcher(aws.AwsAmiResourceType, provider, deserializer))
|
||||
remoteLibrary.AddEnumerator(NewEC2KeyPairEnumerator(ec2repository, factory, provider.Config))
|
||||
remoteLibrary.AddDetailsFetcher(aws.AwsKeyPairResourceType, common.NewGenericDetailsFetcher(aws.AwsKeyPairResourceType, provider, deserializer))
|
||||
remoteLibrary.AddEnumerator(NewEC2EipAssociationEnumerator(ec2repository, factory, provider.Config))
|
||||
remoteLibrary.AddDetailsFetcher(aws.AwsEipAssociationResourceType, common.NewGenericDetailsFetcher(aws.AwsEipAssociationResourceType, provider, deserializer))
|
||||
|
||||
supplierLibrary.AddSupplier(NewS3BucketAnalyticSupplier(provider, s3Repository, deserializer))
|
||||
supplierLibrary.AddSupplier(NewS3BucketPolicySupplier(provider, s3Repository, deserializer))
|
||||
supplierLibrary.AddSupplier(NewEC2EipAssociationSupplier(provider, deserializer, ec2repository))
|
||||
supplierLibrary.AddSupplier(NewRoute53ZoneSupplier(provider, deserializer, route53repository))
|
||||
supplierLibrary.AddSupplier(NewRoute53RecordSupplier(provider, deserializer, route53repository))
|
||||
supplierLibrary.AddSupplier(NewEC2InstanceSupplier(provider, deserializer, ec2repository))
|
||||
|
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -478,3 +478,93 @@ func TestEC2KeyPair(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEC2EipAssociation(t *testing.T) {
|
||||
tests := []struct {
|
||||
test string
|
||||
dirName string
|
||||
mocks func(repository *repository.MockEC2Repository)
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
test: "no eip associations",
|
||||
dirName: "aws_ec2_eip_association_empty",
|
||||
mocks: func(repository *repository.MockEC2Repository) {
|
||||
repository.On("ListAllAddressesAssociation").Return([]string{}, nil)
|
||||
},
|
||||
},
|
||||
{
|
||||
test: "single eip association",
|
||||
dirName: "aws_ec2_eip_association_single",
|
||||
mocks: func(repository *repository.MockEC2Repository) {
|
||||
repository.On("ListAllAddressesAssociation").Return([]string{
|
||||
"eipassoc-0e9a7356e30f0c3d1",
|
||||
}, nil)
|
||||
},
|
||||
},
|
||||
{
|
||||
test: "cannot list eip associations",
|
||||
dirName: "aws_ec2_eip_association_list",
|
||||
mocks: func(repository *repository.MockEC2Repository) {
|
||||
repository.On("ListAllAddressesAssociation").Return(nil, awserr.NewRequestFailure(nil, 403, ""))
|
||||
},
|
||||
wantErr: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsEipAssociationResourceType),
|
||||
},
|
||||
}
|
||||
|
||||
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.NewEC2EipAssociationEnumerator(repo, factory, tf.TerraformProviderConfig{
|
||||
Name: "test",
|
||||
DefaultAlias: "eu-west-3",
|
||||
}))
|
||||
remoteLibrary.AddDetailsFetcher(resourceaws.AwsEipAssociationResourceType, common.NewGenericDetailsFetcher(resourceaws.AwsEipAssociationResourceType, 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.AwsEipAssociationResourceType, c.dirName, provider, deserializer, shouldUpdate, tt)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue