Merge pull request #816 from cloudskiff/issue_750_sqs_split

Split sqs queue supplier
main
William BEUIL 2021-07-08 10:55:13 +02:00 committed by GitHub
commit b74f565567
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 165 additions and 343676 deletions

View File

@ -118,6 +118,9 @@ func Init(version string, alerter *alerter.Alerter,
remoteLibrary.AddEnumerator(NewVPCDefaultSecurityGroupEnumerator(ec2repository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsDefaultSecurityGroupResourceType, common.NewGenericDetailsFetcher(aws.AwsDefaultSecurityGroupResourceType, provider, deserializer))
remoteLibrary.AddEnumerator(NewSqsQueueEnumerator(sqsRepository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsSqsQueueResourceType, common.NewGenericDetailsFetcher(aws.AwsSqsQueueResourceType, provider, deserializer))
supplierLibrary.AddSupplier(NewS3BucketAnalyticSupplier(provider, s3Repository, deserializer))
supplierLibrary.AddSupplier(NewLambdaFunctionSupplier(provider, deserializer, lambdaRepository))
supplierLibrary.AddSupplier(NewDBSubnetGroupSupplier(provider, deserializer, rdsRepository))
@ -133,7 +136,6 @@ func Init(version string, alerter *alerter.Alerter,
supplierLibrary.AddSupplier(NewVPCSecurityGroupRuleSupplier(provider, deserializer, ec2repository))
supplierLibrary.AddSupplier(NewRouteSupplier(provider, deserializer, ec2repository))
supplierLibrary.AddSupplier(NewNatGatewaySupplier(provider, deserializer, ec2repository))
supplierLibrary.AddSupplier(NewSqsQueueSupplier(provider, deserializer, sqsRepository))
supplierLibrary.AddSupplier(NewSqsQueuePolicySupplier(provider, deserializer, sqsRepository))
supplierLibrary.AddSupplier(NewSNSTopicSupplier(provider, deserializer, snsRepository))
supplierLibrary.AddSupplier(NewSNSTopicPolicySupplier(provider, deserializer, snsRepository))

View File

@ -0,0 +1,48 @@
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"
awssdk "github.com/aws/aws-sdk-go/aws"
)
type SqsQueueEnumerator struct {
repository repository.SQSRepository
factory resource.ResourceFactory
}
func NewSqsQueueEnumerator(repo repository.SQSRepository, factory resource.ResourceFactory) *SqsQueueEnumerator {
return &SqsQueueEnumerator{
repository: repo,
factory: factory,
}
}
func (e *SqsQueueEnumerator) SupportedType() resource.ResourceType {
return aws.AwsSqsQueueResourceType
}
func (e *SqsQueueEnumerator) Enumerate() ([]resource.Resource, error) {
queues, err := e.repository.ListAllQueues()
if err != nil {
return nil, remoteerror.NewResourceEnumerationError(err, string(e.SupportedType()))
}
results := make([]resource.Resource, len(queues))
for _, queue := range queues {
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
awssdk.StringValue(queue),
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"
"github.com/cloudskiff/driftctl/pkg/resource/aws"
"github.com/cloudskiff/driftctl/pkg/terraform"
"github.com/sirupsen/logrus"
"github.com/zclconf/go-cty/cty"
)
type SqsQueueSupplier struct {
reader terraform.ResourceReader
deserializer *resource.Deserializer
repository repository.SQSRepository
runner *terraform.ParallelResourceReader
}
func NewSqsQueueSupplier(provider *AWSTerraformProvider, deserializer *resource.Deserializer, repository repository.SQSRepository) *SqsQueueSupplier {
return &SqsQueueSupplier{
provider,
deserializer,
repository,
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
}
}
func (s *SqsQueueSupplier) Resources() ([]resource.Resource, error) {
queues, err := s.repository.ListAllQueues()
if err != nil {
return nil, remoteerror.NewResourceEnumerationError(err, aws.AwsSqsQueueResourceType)
}
for _, queue := range queues {
q := *queue
s.runner.Run(func() (cty.Value, error) {
return s.readSqsQueue(q)
})
}
resources, err := s.runner.Wait()
if err != nil {
return nil, err
}
return s.deserializer.Deserialize(aws.AwsSqsQueueResourceType, resources)
}
func (s *SqsQueueSupplier) readSqsQueue(queueURL string) (cty.Value, error) {
var Ty resource.ResourceType = aws.AwsSqsQueueResourceType
val, err := s.reader.ReadResource(terraform.ReadResourceArgs{
Ty: Ty,
ID: queueURL,
})
if err != nil {
logrus.WithFields(logrus.Fields{
"type": Ty,
}).Error(err)
return cty.NilVal, err
}
return *val, nil
}

View File

@ -1,97 +0,0 @@
package aws
import (
"context"
"testing"
awssdk "github.com/aws/aws-sdk-go/aws"
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
"github.com/cloudskiff/driftctl/pkg/remote/cache"
testresource "github.com/cloudskiff/driftctl/test/resource"
"github.com/aws/aws-sdk-go/aws/awserr"
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
"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 TestSqsQueueSupplier_Resources(t *testing.T) {
cases := []struct {
test string
dirName string
mocks func(client *repository.MockSQSRepository)
err error
}{
{
test: "no sqs queues",
dirName: "sqs_queue_empty",
mocks: func(client *repository.MockSQSRepository) {
client.On("ListAllQueues").Return([]*string{}, nil)
},
err: nil,
},
{
test: "multiple sqs queues",
dirName: "sqs_queue_multiple",
mocks: func(client *repository.MockSQSRepository) {
client.On("ListAllQueues").Return([]*string{
awssdk.String("https://sqs.eu-west-3.amazonaws.com/047081014315/bar.fifo"),
awssdk.String("https://sqs.eu-west-3.amazonaws.com/047081014315/foo"),
}, nil)
},
err: nil,
},
{
test: "cannot list sqs queues",
dirName: "sqs_queue_empty",
mocks: func(client *repository.MockSQSRepository) {
client.On("ListAllQueues").Return(nil, awserr.NewRequestFailure(nil, 403, ""))
},
err: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsSqsQueueResourceType),
},
}
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(NewSqsQueueSupplier(provider, deserializer, repository.NewSQSRepository(provider.session, cache.New(0))))
}
t.Run(c.test, func(tt *testing.T) {
fakeSQS := repository.MockSQSRepository{}
c.mocks(&fakeSQS)
provider := mocks2.NewMockedGoldenTFProvider(c.dirName, providerLibrary.Provider(terraform.AWS), shouldUpdate)
s := &SqsQueueSupplier{
provider,
deserializer,
&fakeSQS,
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

@ -0,0 +1,114 @@
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/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"
"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"
"github.com/stretchr/testify/mock"
)
func TestSqsQueue(t *testing.T) {
cases := []struct {
test string
dirName string
mocks func(client *repository.MockSQSRepository)
wantErr error
}{
{
test: "no sqs queues",
dirName: "sqs_queue_empty",
mocks: func(client *repository.MockSQSRepository) {
client.On("ListAllQueues").Return([]*string{}, nil)
},
wantErr: nil,
},
{
test: "multiple sqs queues",
dirName: "sqs_queue_multiple",
mocks: func(client *repository.MockSQSRepository) {
client.On("ListAllQueues").Return([]*string{
awssdk.String("https://sqs.eu-west-3.amazonaws.com/047081014315/bar.fifo"),
awssdk.String("https://sqs.eu-west-3.amazonaws.com/047081014315/foo"),
}, nil)
},
wantErr: nil,
},
{
test: "cannot list sqs queues",
dirName: "sqs_queue_empty",
mocks: func(client *repository.MockSQSRepository) {
client.On("ListAllQueues").Return(nil, awserr.NewRequestFailure(nil, 403, ""))
},
wantErr: nil,
},
}
schemaRepository := testresource.InitFakeSchemaRepository("aws", "3.19.0")
resourceaws.InitResourcesMetadata(schemaRepository)
factory := terraform.NewTerraformResourceFactory(schemaRepository)
deserializer := resource.NewDeserializer(factory)
for _, c := range cases {
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
alerter := &mocks.AlerterInterface{}
alerter.On("SendAlert", mock.Anything, mock.Anything).Maybe().Return()
fakeRepo := &repository.MockSQSRepository{}
c.mocks(fakeRepo)
var repo repository.SQSRepository = 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.NewSQSRepository(sess, cache.New(0))
}
remoteLibrary.AddEnumerator(aws.NewSqsQueueEnumerator(repo, factory))
remoteLibrary.AddDetailsFetcher(resourceaws.AwsSqsQueueResourceType, common.NewGenericDetailsFetcher(resourceaws.AwsSqsQueueResourceType, 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.AwsSqsQueueResourceType, c.dirName, provider, deserializer, shouldUpdate, tt)
})
}
}