2021-02-04 15:11:51 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
2021-05-03 17:22:36 +00:00
|
|
|
"github.com/pkg/errors"
|
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
|
|
|
}
|
|
|
|
|
2021-05-18 15:06:11 +00:00
|
|
|
func (m AwsSNSTopicPolicyExpander) Execute(remoteResources, resourcesFromState *[]resource.Resource) error {
|
|
|
|
|
|
|
|
for _, res := range *remoteResources {
|
|
|
|
if res.TerraformType() != aws.AwsSnsTopicResourceType {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
topic, _ := res.(*resource.AbstractResource)
|
|
|
|
topic.Attrs.SafeDelete([]string{"policy"})
|
|
|
|
}
|
|
|
|
|
2021-02-04 15:11:51 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-05-03 17:22:36 +00:00
|
|
|
topic, _ := res.(*resource.AbstractResource)
|
2021-02-04 15:11:51 +00:00
|
|
|
newList = append(newList, res)
|
|
|
|
|
|
|
|
if m.hasPolicyAttached(topic, resourcesFromState) {
|
2021-05-03 17:22:36 +00:00
|
|
|
topic.Attrs.SafeDelete([]string{"policy"})
|
2021-02-04 15:11:51 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err := m.splitPolicy(topic, &newList)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*resourcesFromState = newList
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-03 17:22:36 +00:00
|
|
|
func (m *AwsSNSTopicPolicyExpander) splitPolicy(topic *resource.AbstractResource, results *[]resource.Resource) error {
|
|
|
|
policy, exist := topic.Attrs.Get("policy")
|
|
|
|
if !exist || policy == "" {
|
2021-02-04 15:11:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-03 17:22:36 +00:00
|
|
|
arn, exist := topic.Attrs.Get("arn")
|
|
|
|
if !exist || arn == "" {
|
|
|
|
return errors.Errorf("No arn found for resource %s (%s)", topic.Id, topic.Type)
|
2021-03-29 16:10:50 +00:00
|
|
|
}
|
|
|
|
|
2021-05-03 17:22:36 +00:00
|
|
|
data := map[string]interface{}{
|
|
|
|
"arn": arn,
|
|
|
|
"id": topic.Id,
|
|
|
|
"policy": policy,
|
2021-02-04 15:11:51 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 15:06:11 +00:00
|
|
|
newPolicy := m.resourceFactory.CreateAbstractResource("aws_sns_topic_policy", topic.Id, data)
|
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")
|
|
|
|
|
2021-05-03 17:22:36 +00:00
|
|
|
topic.Attrs.SafeDelete([]string{"policy"})
|
2021-02-04 15:11:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-03 17:22:36 +00:00
|
|
|
func (m *AwsSNSTopicPolicyExpander) hasPolicyAttached(topic *resource.AbstractResource, resourcesFromState *[]resource.Resource) bool {
|
2021-02-04 15:11:51 +00:00
|
|
|
for _, res := range *resourcesFromState {
|
|
|
|
if res.TerraformType() == aws.AwsSnsTopicPolicyResourceType &&
|
|
|
|
res.TerraformId() == topic.Id {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|