2021-02-04 15:11:51 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
2021-03-29 16:10:50 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2021-02-04 15:11:51 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource/aws"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Explodes policy found in aws_sns_topic from state resources to aws_sns_topic_policy resources
|
2021-03-29 16:10:50 +00:00
|
|
|
type AwsSNSTopicPolicyExpander struct {
|
2021-05-03 17:15:14 +00:00
|
|
|
resourceFactory resource.ResourceFactory
|
|
|
|
resourceSchemaRepository resource.SchemaRepositoryInterface
|
2021-03-29 16:10:50 +00:00
|
|
|
}
|
2021-02-04 15:11:51 +00:00
|
|
|
|
2021-05-03 17:15:14 +00:00
|
|
|
func NewAwsSNSTopicPolicyExpander(resourceFactory resource.ResourceFactory, resourceSchemaRepository resource.SchemaRepositoryInterface) AwsSNSTopicPolicyExpander {
|
2021-03-29 16:10:50 +00:00
|
|
|
return AwsSNSTopicPolicyExpander{
|
|
|
|
resourceFactory,
|
2021-05-03 17:15:14 +00:00
|
|
|
resourceSchemaRepository,
|
2021-03-29 16:10:50 +00:00
|
|
|
}
|
2021-02-04 15:11:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m AwsSNSTopicPolicyExpander) Execute(_, resourcesFromState *[]resource.Resource) error {
|
|
|
|
newList := make([]resource.Resource, 0)
|
|
|
|
for _, res := range *resourcesFromState {
|
|
|
|
// Ignore all resources other than sns_topic
|
|
|
|
if res.TerraformType() != aws.AwsSnsTopicResourceType {
|
|
|
|
newList = append(newList, res)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
topic, _ := res.(*aws.AwsSnsTopic)
|
|
|
|
newList = append(newList, res)
|
|
|
|
|
|
|
|
if m.hasPolicyAttached(topic, resourcesFromState) {
|
|
|
|
topic.Policy = nil
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err := m.splitPolicy(topic, &newList)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*resourcesFromState = newList
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *AwsSNSTopicPolicyExpander) splitPolicy(topic *aws.AwsSnsTopic, results *[]resource.Resource) error {
|
|
|
|
if topic.Policy == nil || *topic.Policy == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-29 16:10:50 +00:00
|
|
|
data := map[string]interface{}{
|
|
|
|
"arn": topic.Arn,
|
|
|
|
"id": topic.Id,
|
|
|
|
"policy": topic.Policy,
|
|
|
|
}
|
|
|
|
ctyVal, err := m.resourceFactory.CreateResource(data, "aws_sns_topic_policy")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-03 17:15:14 +00:00
|
|
|
schema, exist := m.resourceSchemaRepository.GetSchema("aws_sns_topic_policy")
|
|
|
|
ctyAttr := resource.ToResourceAttributes(ctyVal)
|
|
|
|
ctyAttr.SanitizeDefaultsV3()
|
|
|
|
if exist && schema.NormalizeFunc != nil {
|
|
|
|
schema.NormalizeFunc(ctyAttr)
|
2021-02-04 15:11:51 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 17:15:14 +00:00
|
|
|
newPolicy := &resource.AbstractResource{
|
|
|
|
Id: topic.Id,
|
|
|
|
Type: aws.AwsSnsTopicPolicyResourceType,
|
|
|
|
Attrs: ctyAttr,
|
2021-02-04 15:11:51 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 17:15:14 +00:00
|
|
|
*results = append(*results, newPolicy)
|
2021-02-04 15:11:51 +00:00
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"id": newPolicy.TerraformId(),
|
|
|
|
}).Debug("Created new policy from sns_topic")
|
|
|
|
|
|
|
|
topic.Policy = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *AwsSNSTopicPolicyExpander) hasPolicyAttached(topic *aws.AwsSnsTopic, resourcesFromState *[]resource.Resource) bool {
|
|
|
|
for _, res := range *resourcesFromState {
|
|
|
|
if res.TerraformType() == aws.AwsSnsTopicPolicyResourceType &&
|
|
|
|
res.TerraformId() == topic.Id {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|