diff --git a/pkg/driftctl.go b/pkg/driftctl.go index 5091f1ba..ea640a71 100644 --- a/pkg/driftctl.go +++ b/pkg/driftctl.go @@ -85,7 +85,7 @@ func (d DriftCTL) Run() (*analyser.Analysis, error) { middlewares.NewAwsBucketPolicyExpander(d.resourceFactory), middlewares.NewAwsSqsQueuePolicyExpander(d.resourceFactory), middlewares.NewAwsDefaultSqsQueuePolicy(), - middlewares.NewAwsSNSTopicPolicyExpander(d.resourceFactory), + middlewares.NewAwsSNSTopicPolicyExpander(d.resourceFactory, d.resourceSchemaRepository), ) if !d.strictMode { diff --git a/pkg/driftctl_test.go b/pkg/driftctl_test.go index d65efa8f..ddc10e95 100644 --- a/pkg/driftctl_test.go +++ b/pkg/driftctl_test.go @@ -821,18 +821,14 @@ func TestDriftctlRun_Middlewares(t *testing.T) { }, }, remoteResources: []resource.Resource{ - &aws.AwsSnsTopicPolicy{ - Id: "foo", - Arn: awssdk.String("arn"), - Policy: awssdk.String("{\"policy\":\"baz\"}"), - CtyVal: func() *cty.Value { - v := cty.ObjectVal(map[string]cty.Value{ - "id": cty.StringVal("foo"), - "arn": cty.StringVal("arn"), - "policy": cty.StringVal("{\"policy\":\"baz\"}"), - }) - return &v - }(), + &resource.AbstractResource{ + Id: "foo", + Type: aws.AwsSnsTopicPolicyResourceType, + Attrs: &resource.Attributes{ + "id": "foo", + "arn": "arn", + "policy": "{\"policy\":\"baz\"}", + }, }, }, mocks: func(factory resource.ResourceFactory) { @@ -854,11 +850,12 @@ func TestDriftctlRun_Middlewares(t *testing.T) { result.AssertResourceHasDrift("foo", "aws_sns_topic_policy", analyser.Change{ Change: diff.Change{ Type: diff.UPDATE, - Path: []string{"Policy"}, + Path: []string{"policy"}, From: "{\"policy\":\"bar\"}", To: "{\"policy\":\"baz\"}", }, - Computed: false, + Computed: false, + JsonString: true, }) }, options: func(t *testing.T) *pkg.ScanOptions { diff --git a/pkg/middlewares/aws_sns_topic_policy_expander.go b/pkg/middlewares/aws_sns_topic_policy_expander.go index 80502490..9ac72c8b 100644 --- a/pkg/middlewares/aws_sns_topic_policy_expander.go +++ b/pkg/middlewares/aws_sns_topic_policy_expander.go @@ -9,12 +9,14 @@ import ( // Explodes policy found in aws_sns_topic from state resources to aws_sns_topic_policy resources type AwsSNSTopicPolicyExpander struct { - resourceFactory resource.ResourceFactory + resourceFactory resource.ResourceFactory + resourceSchemaRepository resource.SchemaRepositoryInterface } -func NewAwsSNSTopicPolicyExpander(resourceFactory resource.ResourceFactory) AwsSNSTopicPolicyExpander { +func NewAwsSNSTopicPolicyExpander(resourceFactory resource.ResourceFactory, resourceSchemaRepository resource.SchemaRepositoryInterface) AwsSNSTopicPolicyExpander { return AwsSNSTopicPolicyExpander{ resourceFactory, + resourceSchemaRepository, } } @@ -59,19 +61,20 @@ func (m *AwsSNSTopicPolicyExpander) splitPolicy(topic *aws.AwsSnsTopic, results return err } - newPolicy := &aws.AwsSnsTopicPolicy{ - Id: topic.Id, - Arn: topic.Arn, - Policy: topic.Policy, - CtyVal: ctyVal, + schema, exist := m.resourceSchemaRepository.GetSchema("aws_sns_topic_policy") + ctyAttr := resource.ToResourceAttributes(ctyVal) + ctyAttr.SanitizeDefaultsV3() + if exist && schema.NormalizeFunc != nil { + schema.NormalizeFunc(ctyAttr) } - normalized, err := newPolicy.NormalizeForState() - if err != nil { - return err + newPolicy := &resource.AbstractResource{ + Id: topic.Id, + Type: aws.AwsSnsTopicPolicyResourceType, + Attrs: ctyAttr, } - *results = append(*results, normalized) + *results = append(*results, newPolicy) logrus.WithFields(logrus.Fields{ "id": newPolicy.TerraformId(), }).Debug("Created new policy from sns_topic") diff --git a/pkg/middlewares/aws_sns_topic_policy_expander_test.go b/pkg/middlewares/aws_sns_topic_policy_expander_test.go index 250dea01..30a2e30c 100644 --- a/pkg/middlewares/aws_sns_topic_policy_expander_test.go +++ b/pkg/middlewares/aws_sns_topic_policy_expander_test.go @@ -6,9 +6,11 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/stretchr/testify/mock" + "github.com/zclconf/go-cty/cty" awsresource "github.com/cloudskiff/driftctl/pkg/resource/aws" "github.com/cloudskiff/driftctl/pkg/terraform" + testresource "github.com/cloudskiff/driftctl/test/resource" "github.com/aws/aws-sdk-go/aws/awsutil" "github.com/r3labs/diff/v2" @@ -39,14 +41,26 @@ func TestAwsSNSTopicPolicyExpander_Execute(t *testing.T) { Policy: nil, Id: "ID", }, - &awsresource.AwsSnsTopicPolicy{ - Arn: aws.String("arn"), - Policy: aws.String("{\"policy\":\"coucou\"}"), - Id: "ID", + &resource.AbstractResource{ + Id: "ID", + Type: awsresource.AwsSnsTopicPolicyResourceType, + Attrs: &resource.Attributes{ + "arn": "arn", + "id": "ID", + "policy": "{\"policy\":\"coucou\"}", + }, }, }, mock: func(factory *terraform.MockResourceFactory) { - factory.On("CreateResource", mock.Anything, "aws_sns_topic_policy").Once().Return(nil, nil) + foo := cty.ObjectVal(map[string]cty.Value{ + "arn": cty.StringVal("arn"), + "id": cty.StringVal("ID"), + "policy": cty.StringVal("{\"policy\":\"coucou\"}"), + }) + + factory.On("CreateResource", mock.MatchedBy(func(input map[string]interface{}) bool { + return input["id"] == "ID" + }), awsresource.AwsSnsTopicPolicyResourceType).Once().Return(&foo, nil) }, wantErr: false, }, @@ -58,10 +72,14 @@ func TestAwsSNSTopicPolicyExpander_Execute(t *testing.T) { Policy: nil, Id: "ID", }, - &awsresource.AwsSnsTopicPolicy{ - Arn: aws.String("arn"), - Policy: aws.String("{\"policy\": \"coucou\"}"), - Id: "ID", + &resource.AbstractResource{ + Id: "ID", + Type: awsresource.AwsSnsTopicPolicyResourceType, + Attrs: &resource.Attributes{ + "arn": "arn", + "id": "ID", + "policy": "{\"policy\":\"coucou\"}", + }, }, }, expected: &[]resource.Resource{ @@ -70,10 +88,14 @@ func TestAwsSNSTopicPolicyExpander_Execute(t *testing.T) { Policy: nil, Id: "ID", }, - &awsresource.AwsSnsTopicPolicy{ - Arn: aws.String("arn"), - Policy: aws.String("{\"policy\": \"coucou\"}"), - Id: "ID", + &resource.AbstractResource{ + Id: "ID", + Type: awsresource.AwsSnsTopicPolicyResourceType, + Attrs: &resource.Attributes{ + "arn": "arn", + "id": "ID", + "policy": "{\"policy\":\"coucou\"}", + }, }, }, wantErr: false, @@ -115,7 +137,10 @@ func TestAwsSNSTopicPolicyExpander_Execute(t *testing.T) { tt.mock(factory) } - m := NewAwsSNSTopicPolicyExpander(factory) + repo := testresource.InitFakeSchemaRepository("aws", "3.19.0") + awsresource.InitResourcesMetadata(repo) + + m := NewAwsSNSTopicPolicyExpander(factory, repo) if err := m.Execute(&[]resource.Resource{}, tt.resourcesFromState); (err != nil) != tt.wantErr { t.Errorf("Execute() error = %v, wantErr %v", err, tt.wantErr) }