Split ec2_ebs_volume_supplier

main
William Beuil 2021-07-01 11:53:26 +02:00
parent 024cb69a82
commit 82fd4735f4
No known key found for this signature in database
GPG Key ID: BED2072C5C2BF537
11 changed files with 168 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 EC2EbsVolumeEnumerator struct {
repository repository.EC2Repository
factory resource.ResourceFactory
providerConfig tf.TerraformProviderConfig
}
func NewEC2EbsVolumeEnumerator(repo repository.EC2Repository, factory resource.ResourceFactory, providerConfig tf.TerraformProviderConfig) *EC2EbsVolumeEnumerator {
return &EC2EbsVolumeEnumerator{
repository: repo,
factory: factory,
providerConfig: providerConfig,
}
}
func (e *EC2EbsVolumeEnumerator) SupportedType() resource.ResourceType {
return aws.AwsEbsVolumeResourceType
}
func (e *EC2EbsVolumeEnumerator) Enumerate() ([]resource.Resource, error) {
volumes, err := e.repository.ListAllVolumes()
if err != nil {
return nil, remoteerror.NewResourceEnumerationError(err, string(e.SupportedType()))
}
results := make([]resource.Resource, len(volumes))
for _, volume := range volumes {
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
*volume.VolumeId,
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 EC2EbsVolumeSupplier struct {
reader terraform.ResourceReader
deserializer *resource.Deserializer
repository repository.EC2Repository
runner *terraform.ParallelResourceReader
}
func NewEC2EbsVolumeSupplier(provider *AWSTerraformProvider, deserializer *resource.Deserializer, repository repository.EC2Repository) *EC2EbsVolumeSupplier {
return &EC2EbsVolumeSupplier{
provider,
deserializer,
repository,
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
}
}
func (s *EC2EbsVolumeSupplier) Resources() ([]resource.Resource, error) {
volumes, err := s.repository.ListAllVolumes()
if err != nil {
return nil, remoteerror.NewResourceEnumerationError(err, resourceaws.AwsEbsVolumeResourceType)
}
results := make([]cty.Value, 0)
if len(volumes) > 0 {
for _, volume := range volumes {
vol := *volume
s.runner.Run(func() (cty.Value, error) {
return s.readEbsVolume(vol)
})
}
results, err = s.runner.Wait()
if err != nil {
return nil, err
}
}
return s.deserializer.Deserialize(resourceaws.AwsEbsVolumeResourceType, results)
}
func (s *EC2EbsVolumeSupplier) readEbsVolume(volume ec2.Volume) (cty.Value, error) {
id := aws.StringValue(volume.VolumeId)
resVolume, err := s.reader.ReadResource(terraform.ReadResourceArgs{
Ty: resourceaws.AwsEbsVolumeResourceType,
ID: id,
})
if err != nil {
logrus.Warnf("Error reading volume %s[%s]: %+v", id, resourceaws.AwsEbsVolumeResourceType, err)
return cty.NilVal, err
}
return *resVolume, 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/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"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
)
func TestEC2EbsVolumeSupplier_Resources(t *testing.T) {
tests := []struct {
test string
dirName string
mock func(mock *repository.MockEC2Repository)
err error
}{
{
test: "no volumes",
dirName: "ec2_ebs_volume_empty",
mock: func(mock *repository.MockEC2Repository) {
mock.On("ListAllVolumes").Return([]*ec2.Volume{}, nil)
},
err: nil,
},
{
test: "with volumes",
dirName: "ec2_ebs_volume_multiple",
mock: func(mock *repository.MockEC2Repository) {
mock.On("ListAllVolumes").Return([]*ec2.Volume{
{
VolumeId: aws.String("vol-081c7272a57a09db1"),
},
{
VolumeId: aws.String("vol-01ddc91d3d9d1318b"),
},
}, nil)
},
err: nil,
},
{
test: "cannot list volumes",
dirName: "ec2_ebs_volume_empty",
mock: func(mock *repository.MockEC2Repository) {
mock.On("ListAllVolumes").Return([]*ec2.Volume{}, awserr.NewRequestFailure(nil, 403, ""))
},
err: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsEbsVolumeResourceType),
},
}
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(NewEC2EbsVolumeSupplier(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 := &EC2EbsVolumeSupplier{
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

@ -66,12 +66,14 @@ func Init(version string, alerter *alerter.Alerter,
remoteLibrary.AddEnumerator(NewS3BucketNotificationEnumerator(s3Repository, factory, provider.Config)) remoteLibrary.AddEnumerator(NewS3BucketNotificationEnumerator(s3Repository, factory, provider.Config))
remoteLibrary.AddDetailsFetcher(aws.AwsS3BucketNotificationResourceType, NewS3BucketNotificationDetailsFetcher(provider, deserializer)) remoteLibrary.AddDetailsFetcher(aws.AwsS3BucketNotificationResourceType, NewS3BucketNotificationDetailsFetcher(provider, deserializer))
remoteLibrary.AddEnumerator(NewEC2EbsVolumeEnumerator(ec2repository, factory, provider.Config))
remoteLibrary.AddDetailsFetcher(aws.AwsEbsVolumeResourceType, common.NewGenericDetailFetcher(aws.AwsEbsVolumeResourceType, 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(NewEC2EipSupplier(provider, ec2repository, deserializer))
supplierLibrary.AddSupplier(NewEC2EipAssociationSupplier(provider, deserializer, ec2repository)) supplierLibrary.AddSupplier(NewEC2EipAssociationSupplier(provider, deserializer, ec2repository))
supplierLibrary.AddSupplier(NewEC2EbsVolumeSupplier(provider, deserializer, ec2repository))
supplierLibrary.AddSupplier(NewEC2EbsSnapshotSupplier(provider, deserializer, ec2repository)) supplierLibrary.AddSupplier(NewEC2EbsSnapshotSupplier(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

@ -0,0 +1,116 @@
package remote
import (
"testing"
awssdk "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/cloudskiff/driftctl/mocks"
"github.com/cloudskiff/driftctl/pkg/remote/aws"
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
"github.com/cloudskiff/driftctl/pkg/remote/cache"
"github.com/cloudskiff/driftctl/pkg/remote/common"
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
tf "github.com/cloudskiff/driftctl/pkg/remote/terraform"
"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"
testresource "github.com/cloudskiff/driftctl/test/resource"
terraform2 "github.com/cloudskiff/driftctl/test/terraform"
"github.com/stretchr/testify/assert"
)
func TestEC2EbsVolume(t *testing.T) {
tests := []struct {
test string
dirName string
mocks func(repository *repository.MockEC2Repository)
wantErr error
}{
{
test: "no volumes",
dirName: "aws_ec2_ebs_volume_empty",
mocks: func(repository *repository.MockEC2Repository) {
repository.On("ListAllVolumes").Return([]*ec2.Volume{}, nil)
},
},
{
test: "multiple volumes",
dirName: "aws_ec2_ebs_volume_multiple",
mocks: func(repository *repository.MockEC2Repository) {
repository.On("ListAllVolumes").Return([]*ec2.Volume{
{VolumeId: awssdk.String("vol-081c7272a57a09db1")},
{VolumeId: awssdk.String("vol-01ddc91d3d9d1318b")},
}, nil)
},
},
{
test: "cannot list volumes",
dirName: "aws_ec2_ebs_volume_list",
mocks: func(repository *repository.MockEC2Repository) {
repository.On("ListAllVolumes").Return(nil, awserr.NewRequestFailure(nil, 403, ""))
},
wantErr: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsEbsVolumeResourceType),
},
}
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.NewEC2EbsVolumeEnumerator(repo, factory, tf.TerraformProviderConfig{
Name: "test",
DefaultAlias: "eu-west-3",
}))
remoteLibrary.AddDetailsFetcher(resourceaws.AwsEbsVolumeResourceType, common.NewGenericDetailFetcher(resourceaws.AwsEbsVolumeResourceType, 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.AwsEbsVolumeResourceType, c.dirName, provider, deserializer, shouldUpdate, tt)
})
}
}