Merge pull request #806 from cloudskiff/fea/split_sns_topic_supplier

split sns_topic_supplier
main
William BEUIL 2021-07-08 15:57:12 +02:00 committed by GitHub
commit 934731cc6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 206 additions and 343686 deletions

View File

@ -123,6 +123,9 @@ func Init(version string, alerter *alerter.Alerter,
remoteLibrary.AddEnumerator(NewSQSQueuePolicyEnumerator(sqsRepository, factory)) remoteLibrary.AddEnumerator(NewSQSQueuePolicyEnumerator(sqsRepository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsSqsQueuePolicyResourceType, common.NewGenericDetailsFetcher(aws.AwsSqsQueuePolicyResourceType, provider, deserializer)) remoteLibrary.AddDetailsFetcher(aws.AwsSqsQueuePolicyResourceType, common.NewGenericDetailsFetcher(aws.AwsSqsQueuePolicyResourceType, provider, deserializer))
remoteLibrary.AddEnumerator(NewSNSTopicEnumerator(snsRepository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsSnsTopicResourceType, NewSNSTopicDetailsFetcher(provider, deserializer))
supplierLibrary.AddSupplier(NewS3BucketAnalyticSupplier(provider, s3Repository, deserializer)) supplierLibrary.AddSupplier(NewS3BucketAnalyticSupplier(provider, s3Repository, deserializer))
supplierLibrary.AddSupplier(NewLambdaFunctionSupplier(provider, deserializer, lambdaRepository)) supplierLibrary.AddSupplier(NewLambdaFunctionSupplier(provider, deserializer, lambdaRepository))
supplierLibrary.AddSupplier(NewDBSubnetGroupSupplier(provider, deserializer, rdsRepository)) supplierLibrary.AddSupplier(NewDBSubnetGroupSupplier(provider, deserializer, rdsRepository))
@ -138,7 +141,6 @@ func Init(version string, alerter *alerter.Alerter,
supplierLibrary.AddSupplier(NewVPCSecurityGroupRuleSupplier(provider, deserializer, ec2repository)) supplierLibrary.AddSupplier(NewVPCSecurityGroupRuleSupplier(provider, deserializer, ec2repository))
supplierLibrary.AddSupplier(NewRouteSupplier(provider, deserializer, ec2repository)) supplierLibrary.AddSupplier(NewRouteSupplier(provider, deserializer, ec2repository))
supplierLibrary.AddSupplier(NewNatGatewaySupplier(provider, deserializer, ec2repository)) supplierLibrary.AddSupplier(NewNatGatewaySupplier(provider, deserializer, ec2repository))
supplierLibrary.AddSupplier(NewSNSTopicSupplier(provider, deserializer, snsRepository))
supplierLibrary.AddSupplier(NewSNSTopicPolicySupplier(provider, deserializer, snsRepository)) supplierLibrary.AddSupplier(NewSNSTopicPolicySupplier(provider, deserializer, snsRepository))
supplierLibrary.AddSupplier(NewSNSTopicSubscriptionSupplier(provider, alerter, deserializer, snsRepository)) supplierLibrary.AddSupplier(NewSNSTopicSubscriptionSupplier(provider, alerter, deserializer, snsRepository))
supplierLibrary.AddSupplier(NewDynamoDBTableSupplier(provider, deserializer, dynamoDBRepository)) supplierLibrary.AddSupplier(NewDynamoDBTableSupplier(provider, deserializer, dynamoDBRepository))

View File

@ -0,0 +1,36 @@
package aws
import (
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/resource/aws"
"github.com/sirupsen/logrus"
"github.com/cloudskiff/driftctl/pkg/terraform"
)
type SNSTopicDetailsFetcher struct {
reader terraform.ResourceReader
deserializer *resource.Deserializer
}
func NewSNSTopicDetailsFetcher(provider terraform.ResourceReader, deserializer *resource.Deserializer) *SNSTopicDetailsFetcher {
return &SNSTopicDetailsFetcher{
reader: provider,
deserializer: deserializer,
}
}
func (r *SNSTopicDetailsFetcher) ReadDetails(topic resource.Resource) (resource.Resource, error) {
val, err := r.reader.ReadResource(terraform.ReadResourceArgs{
ID: topic.TerraformId(),
Ty: aws.AwsSnsTopicResourceType,
Attributes: map[string]string{
"topic_arn": topic.TerraformId(),
},
})
if err != nil {
logrus.Error(err)
return nil, err
}
return r.deserializer.DeserializeOne(aws.AwsSnsTopicResourceType, *val)
}

View File

@ -0,0 +1,46 @@
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"
)
type SNSTopicEnumerator struct {
repository repository.SNSRepository
factory resource.ResourceFactory
}
func NewSNSTopicEnumerator(repo repository.SNSRepository, factory resource.ResourceFactory) *SNSTopicEnumerator {
return &SNSTopicEnumerator{
repository: repo,
factory: factory,
}
}
func (e *SNSTopicEnumerator) SupportedType() resource.ResourceType {
return aws.AwsSnsTopicResourceType
}
func (e *SNSTopicEnumerator) Enumerate() ([]resource.Resource, error) {
topics, err := e.repository.ListAllTopics()
if err != nil {
return nil, remoteerror.NewResourceEnumerationError(err, string(e.SupportedType()))
}
results := make([]resource.Resource, len(topics))
for _, topic := range topics {
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
*topic.TopicArn,
map[string]interface{}{},
),
)
}
return results, err
}

View File

@ -80,7 +80,7 @@ func TestSNSTopicPolicySupplier_Resources(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
supplierLibrary.AddSupplier(NewSNSTopicSupplier(provider, deserializer, repository.NewSNSRepository(provider.session, cache.New(0)))) supplierLibrary.AddSupplier(NewSNSTopicPolicySupplier(provider, deserializer, repository.NewSNSRepository(provider.session, cache.New(0))))
} }
t.Run(c.test, func(tt *testing.T) { t.Run(c.test, func(tt *testing.T) {

View File

@ -1,66 +0,0 @@
package aws
import (
"github.com/aws/aws-sdk-go/service/sns"
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
"github.com/sirupsen/logrus"
"github.com/zclconf/go-cty/cty"
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/resource/aws"
"github.com/cloudskiff/driftctl/pkg/terraform"
)
type SNSTopicSupplier struct {
reader terraform.ResourceReader
deserializer *resource.Deserializer
repository repository.SNSRepository
runner *terraform.ParallelResourceReader
}
func NewSNSTopicSupplier(provider *AWSTerraformProvider, deserializer *resource.Deserializer, repository repository.SNSRepository) *SNSTopicSupplier {
return &SNSTopicSupplier{
provider,
deserializer,
repository,
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
}
}
func (s *SNSTopicSupplier) Resources() ([]resource.Resource, error) {
topics, err := s.repository.ListAllTopics()
if err != nil {
return nil, remoteerror.NewResourceEnumerationError(err, aws.AwsSnsTopicResourceType)
}
for _, topic := range topics {
t := *topic
s.runner.Run(func() (cty.Value, error) {
return s.readTopic(t)
})
}
retrieve, err := s.runner.Wait()
if err != nil {
return nil, err
}
return s.deserializer.Deserialize(aws.AwsSnsTopicResourceType, retrieve)
}
func (s *SNSTopicSupplier) readTopic(topic sns.Topic) (cty.Value, error) {
val, err := s.reader.ReadResource(terraform.ReadResourceArgs{
ID: *topic.TopicArn,
Ty: aws.AwsSnsTopicResourceType,
Attributes: map[string]string{
"topic_arn": *topic.TopicArn,
},
})
if err != nil {
logrus.Error(err)
return cty.NilVal, err
}
return *val, nil
}

View File

@ -1,104 +0,0 @@
package aws
import (
"context"
"testing"
"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/service/sns"
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/cloudskiff/driftctl/test/goldenfile"
mocks2 "github.com/cloudskiff/driftctl/test/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/terraform"
"github.com/cloudskiff/driftctl/test"
)
func TestSNSTopicSupplier_Resources(t *testing.T) {
cases := []struct {
test string
dirName string
mocks func(client *repository.MockSNSRepository)
err error
}{
{
test: "no SNS Topic",
dirName: "sns_topic_empty",
mocks: func(client *repository.MockSNSRepository) {
client.On("ListAllTopics").Return([]*sns.Topic{}, nil)
},
err: nil,
},
{
test: "Multiple SNSTopic",
dirName: "sns_topic_multiple",
mocks: func(client *repository.MockSNSRepository) {
client.On("ListAllTopics").Return([]*sns.Topic{
{TopicArn: aws.String("arn:aws:sns:eu-west-3:526954929923:user-updates-topic")},
{TopicArn: aws.String("arn:aws:sns:eu-west-3:526954929923:user-updates-topic2")},
{TopicArn: aws.String("arn:aws:sns:eu-west-3:526954929923:user-updates-topic3")},
}, nil)
},
err: nil,
},
{
test: "cannot list SNSTopic",
dirName: "sns_topic_empty",
mocks: func(client *repository.MockSNSRepository) {
client.On("ListAllTopics").Return(nil, awserr.NewRequestFailure(nil, 403, ""))
},
err: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsSnsTopicResourceType),
},
}
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(NewSNSTopicSupplier(provider, deserializer, repository.NewSNSRepository(provider.session, cache.New(0))))
}
t.Run(c.test, func(tt *testing.T) {
fakeClient := repository.MockSNSRepository{}
c.mocks(&fakeClient)
provider := mocks2.NewMockedGoldenTFProvider(c.dirName, providerLibrary.Provider(terraform.AWS), shouldUpdate)
s := &SNSTopicSupplier{
provider,
deserializer,
&fakeClient,
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,120 @@
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/sns"
"github.com/cloudskiff/driftctl/mocks"
"github.com/cloudskiff/driftctl/pkg/remote/aws"
"github.com/cloudskiff/driftctl/pkg/remote/cache"
"github.com/cloudskiff/driftctl/pkg/remote/common"
testresource "github.com/cloudskiff/driftctl/test/resource"
terraform2 "github.com/cloudskiff/driftctl/test/terraform"
"github.com/stretchr/testify/mock"
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
"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"
"github.com/stretchr/testify/assert"
)
func TestScanSNSTopic(t *testing.T) {
cases := []struct {
test string
dirName string
mocks func(client *repository.MockSNSRepository)
err error
}{
{
test: "no SNS Topic",
dirName: "sns_topic_empty",
mocks: func(client *repository.MockSNSRepository) {
client.On("ListAllTopics").Return([]*sns.Topic{}, nil)
},
err: nil,
},
{
test: "Multiple SNSTopic",
dirName: "sns_topic_multiple",
mocks: func(client *repository.MockSNSRepository) {
client.On("ListAllTopics").Return([]*sns.Topic{
{TopicArn: awssdk.String("arn:aws:sns:eu-west-3:526954929923:user-updates-topic")},
{TopicArn: awssdk.String("arn:aws:sns:eu-west-3:526954929923:user-updates-topic2")},
{TopicArn: awssdk.String("arn:aws:sns:eu-west-3:526954929923:user-updates-topic3")},
}, nil)
},
err: nil,
},
{
test: "cannot list SNSTopic",
dirName: "sns_topic_empty",
mocks: func(client *repository.MockSNSRepository) {
client.On("ListAllTopics").Return(nil, awserr.NewRequestFailure(nil, 403, ""))
},
err: 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
session := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
alerter := &mocks.AlerterInterface{}
alerter.On("SendAlert", mock.Anything, mock.Anything).Maybe().Return()
scanOptions := ScannerOptions{Deep: true}
providerLibrary := terraform.NewProviderLibrary()
remoteLibrary := common.NewRemoteLibrary()
// Initialize mocks
fakeRepo := &repository.MockSNSRepository{}
c.mocks(fakeRepo)
var repo repository.SNSRepository = 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.NewSNSRepository(session, cache.New(0))
}
remoteLibrary.AddEnumerator(aws.NewSNSTopicEnumerator(repo, factory))
remoteLibrary.AddDetailsFetcher(resourceaws.AwsSnsTopicResourceType, aws.NewSNSTopicDetailsFetcher(provider, deserializer))
s := NewScanner(nil, remoteLibrary, alerter, scanOptions)
got, err := s.Resources()
assert.Equal(tt, c.err, err)
if err != nil {
return
}
test.TestAgainstGoldenFile(got, resourceaws.AwsSnsTopicResourceType, c.dirName, provider, deserializer, shouldUpdate, tt)
})
}
}