Merge pull request #762 from cloudskiff/ec2_ami_supplier

Split ec2_ami_supplier
main
William BEUIL 2021-07-02 16:02:13 +02:00 committed by GitHub
commit fb6d6b5cfb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 142 additions and 168 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 EC2AmiEnumerator struct {
repository repository.EC2Repository
factory resource.ResourceFactory
providerConfig tf.TerraformProviderConfig
}
func NewEC2AmiEnumerator(repo repository.EC2Repository, factory resource.ResourceFactory, providerConfig tf.TerraformProviderConfig) *EC2AmiEnumerator {
return &EC2AmiEnumerator{
repository: repo,
factory: factory,
providerConfig: providerConfig,
}
}
func (e *EC2AmiEnumerator) SupportedType() resource.ResourceType {
return aws.AwsAmiResourceType
}
func (e *EC2AmiEnumerator) Enumerate() ([]resource.Resource, error) {
images, err := e.repository.ListAllImages()
if err != nil {
return nil, remoteerror.NewResourceEnumerationError(err, string(e.SupportedType()))
}
results := make([]resource.Resource, len(images))
for _, image := range images {
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
*image.ImageId,
map[string]interface{}{},
),
)
}
return results, err
}

View File

@ -1,64 +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/sirupsen/logrus"
"github.com/zclconf/go-cty/cty"
)
type EC2AmiSupplier struct {
reader terraform.ResourceReader
deserializer *resource.Deserializer
repository repository.EC2Repository
runner *terraform.ParallelResourceReader
}
func NewEC2AmiSupplier(provider *AWSTerraformProvider, deserializer *resource.Deserializer, repository repository.EC2Repository) *EC2AmiSupplier {
return &EC2AmiSupplier{
provider,
deserializer,
repository,
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
}
}
func (s *EC2AmiSupplier) Resources() ([]resource.Resource, error) {
images, err := s.repository.ListAllImages()
if err != nil {
return nil, remoteerror.NewResourceEnumerationError(err, resourceaws.AwsAmiResourceType)
}
results := make([]cty.Value, 0)
if len(images) > 0 {
for _, image := range images {
id := aws.StringValue(image.ImageId)
s.runner.Run(func() (cty.Value, error) {
return s.readAMI(id)
})
}
results, err = s.runner.Wait()
if err != nil {
return nil, err
}
}
return s.deserializer.Deserialize(resourceaws.AwsAmiResourceType, results)
}
func (s *EC2AmiSupplier) readAMI(id string) (cty.Value, error) {
resImage, err := s.reader.ReadResource(terraform.ReadResourceArgs{
Ty: resourceaws.AwsAmiResourceType,
ID: id,
})
if err != nil {
logrus.Warnf("Error reading image %s[%s]: %+v", id, resourceaws.AwsAmiResourceType, err)
return cty.NilVal, err
}
return *resImage, nil
}

View File

@ -1,101 +0,0 @@
package aws
import (
"context"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/cloudskiff/driftctl/pkg/remote/cache"
testresource "github.com/cloudskiff/driftctl/test/resource"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
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/cloudskiff/driftctl/pkg/parallel"
"github.com/stretchr/testify/assert"
"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 TestEC2AmiSupplier_Resources(t *testing.T) {
tests := []struct {
test string
dirName string
mock func(mock *repository.MockEC2Repository)
err error
}{
{
test: "no amis",
dirName: "ec2_ami_empty",
mock: func(mock *repository.MockEC2Repository) {
mock.On("ListAllImages").Return([]*ec2.Image{}, nil)
},
err: nil,
},
{
test: "with amis",
dirName: "ec2_ami_multiple",
mock: func(mock *repository.MockEC2Repository) {
mock.On("ListAllImages").Return([]*ec2.Image{
{ImageId: aws.String("ami-03a578b46f4c3081b")},
{ImageId: aws.String("ami-025962fd8b456731f")},
}, nil)
},
err: nil,
},
{
test: "cannot list amis",
dirName: "ec2_ami_empty",
mock: func(mock *repository.MockEC2Repository) {
mock.On("ListAllImages").Return([]*ec2.Image{}, awserr.NewRequestFailure(nil, 403, ""))
},
err: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsAmiResourceType),
},
}
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(NewEC2AmiSupplier(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 := &EC2AmiSupplier{
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

@ -74,6 +74,8 @@ func Init(version string, alerter *alerter.Alerter,
remoteLibrary.AddDetailsFetcher(aws.AwsEbsSnapshotResourceType, common.NewGenericDetailsFetcher(aws.AwsEbsSnapshotResourceType, provider, deserializer))
remoteLibrary.AddEnumerator(NewEC2EipEnumerator(ec2repository, factory, provider.Config))
remoteLibrary.AddDetailsFetcher(aws.AwsEipResourceType, common.NewGenericDetailsFetcher(aws.AwsEipResourceType, provider, deserializer))
remoteLibrary.AddEnumerator(NewEC2AmiEnumerator(ec2repository, factory, provider.Config))
remoteLibrary.AddDetailsFetcher(aws.AwsAmiResourceType, common.NewGenericDetailsFetcher(aws.AwsAmiResourceType, provider, deserializer))
supplierLibrary.AddSupplier(NewS3BucketAnalyticSupplier(provider, s3Repository, deserializer))
supplierLibrary.AddSupplier(NewS3BucketPolicySupplier(provider, s3Repository, deserializer))
@ -81,7 +83,6 @@ func Init(version string, alerter *alerter.Alerter,
supplierLibrary.AddSupplier(NewRoute53ZoneSupplier(provider, deserializer, route53repository))
supplierLibrary.AddSupplier(NewRoute53RecordSupplier(provider, deserializer, route53repository))
supplierLibrary.AddSupplier(NewEC2InstanceSupplier(provider, deserializer, ec2repository))
supplierLibrary.AddSupplier(NewEC2AmiSupplier(provider, deserializer, ec2repository))
supplierLibrary.AddSupplier(NewEC2KeyPairSupplier(provider, deserializer, ec2repository))
supplierLibrary.AddSupplier(NewLambdaFunctionSupplier(provider, deserializer, lambdaRepository))
supplierLibrary.AddSupplier(NewDBSubnetGroupSupplier(provider, deserializer, rdsRepository))

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -296,3 +296,94 @@ func TestEC2Eip(t *testing.T) {
})
}
}
func TestEC2Ami(t *testing.T) {
tests := []struct {
test string
dirName string
mocks func(repository *repository.MockEC2Repository)
wantErr error
}{
{
test: "no amis",
dirName: "aws_ec2_ami_empty",
mocks: func(repository *repository.MockEC2Repository) {
repository.On("ListAllImages").Return([]*ec2.Image{}, nil)
},
},
{
test: "multiple amis",
dirName: "aws_ec2_ami_multiple",
mocks: func(repository *repository.MockEC2Repository) {
repository.On("ListAllImages").Return([]*ec2.Image{
{ImageId: awssdk.String("ami-03a578b46f4c3081b")},
{ImageId: awssdk.String("ami-025962fd8b456731f")},
}, nil)
},
},
{
test: "cannot list ami",
dirName: "aws_ec2_ami_list",
mocks: func(repository *repository.MockEC2Repository) {
repository.On("ListAllImages").Return(nil, awserr.NewRequestFailure(nil, 403, ""))
},
wantErr: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsAmiResourceType),
},
}
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.NewEC2AmiEnumerator(repo, factory, tf.TerraformProviderConfig{
Name: "test",
DefaultAlias: "eu-west-3",
}))
remoteLibrary.AddDetailsFetcher(resourceaws.AwsAmiResourceType, common.NewGenericDetailsFetcher(resourceaws.AwsAmiResourceType, 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.AwsAmiResourceType, c.dirName, provider, deserializer, shouldUpdate, tt)
})
}
}