driftctl/pkg/middlewares/aws_sqs_queue_policy_expand...

98 lines
2.9 KiB
Go
Raw Normal View History

2021-01-29 11:35:11 +00:00
package middlewares
import (
2021-03-29 16:10:50 +00:00
"github.com/sirupsen/logrus"
2021-01-29 11:35:11 +00:00
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/resource/aws"
)
// Explodes policy found in aws_sqs_queue.policy from state resources to dedicated resources
type AwsSQSQueuePolicyExpander struct {
resourceFactory resource.ResourceFactory
resourceSchemaRepository resource.SchemaRepositoryInterface
2021-03-29 16:10:50 +00:00
}
2021-01-29 11:35:11 +00:00
func NewAwsSQSQueuePolicyExpander(resourceFactory resource.ResourceFactory, resourceSchemaRepository resource.SchemaRepositoryInterface) AwsSQSQueuePolicyExpander {
return AwsSQSQueuePolicyExpander{
2021-03-29 16:10:50 +00:00
resourceFactory,
resourceSchemaRepository,
2021-03-29 16:10:50 +00:00
}
2021-01-29 11:35:11 +00:00
}
func (m AwsSQSQueuePolicyExpander) Execute(remoteResources, resourcesFromState *[]resource.Resource) error {
for _, res := range *remoteResources {
if res.TerraformType() != aws.AwsSqsQueueResourceType {
continue
}
queue, _ := res.(*resource.AbstractResource)
queue.Attrs.SafeDelete([]string{"policy"})
}
2021-01-29 11:35:11 +00:00
newList := make([]resource.Resource, 0)
for _, res := range *resourcesFromState {
// Ignore all resources other than sqs_queue
if res.TerraformType() != aws.AwsSqsQueueResourceType {
newList = append(newList, res)
continue
}
2021-04-30 17:29:04 +00:00
queue, _ := res.(*resource.AbstractResource)
2021-01-29 11:35:11 +00:00
newList = append(newList, res)
2021-04-30 17:29:04 +00:00
policy, exist := queue.Attrs.Get("policy")
if !exist || policy == nil {
2021-02-03 20:08:57 +00:00
continue
}
2021-01-29 11:35:11 +00:00
if m.hasPolicyAttached(queue, resourcesFromState) {
2021-04-30 17:29:04 +00:00
queue.Attrs.SafeDelete([]string{"policy"})
2021-01-29 11:35:11 +00:00
continue
}
err := m.handlePolicy(queue, &newList)
if err != nil {
return err
}
}
*resourcesFromState = newList
return nil
}
func (m *AwsSQSQueuePolicyExpander) handlePolicy(queue *resource.AbstractResource, results *[]resource.Resource) error {
policy, exists := queue.Attrs.Get("policy")
if !exists || policy.(string) == "" {
2021-05-19 14:45:41 +00:00
queue.Attrs.SafeDelete([]string{"policy"})
return nil
}
2021-04-30 17:29:04 +00:00
2021-03-29 16:10:50 +00:00
data := map[string]interface{}{
"queue_url": queue.Id,
"id": queue.Id,
2021-04-30 17:29:04 +00:00
"policy": policy,
2021-01-29 11:35:11 +00:00
}
2021-04-30 17:29:04 +00:00
newPolicy := m.resourceFactory.CreateAbstractResource("aws_sqs_queue_policy", queue.Id, data)
*results = append(*results, newPolicy)
2021-01-29 11:35:11 +00:00
logrus.WithFields(logrus.Fields{
"id": newPolicy.TerraformId(),
}).Debug("Created new policy from sqs queue")
2021-04-30 17:29:04 +00:00
queue.Attrs.SafeDelete([]string{"policy"})
2021-01-29 11:35:11 +00:00
return nil
}
// Return true if the sqs queue has a aws_sqs_queue_policy resource attached to itself.
// It is mandatory since it's possible to have a aws_sqs_queue with an inline policy
// AND a aws_sqs_queue_policy resource at the same time. At the end, on the AWS console,
// the aws_sqs_queue_policy will be used.
func (m *AwsSQSQueuePolicyExpander) hasPolicyAttached(queue *resource.AbstractResource, resourcesFromState *[]resource.Resource) bool {
2021-01-29 11:35:11 +00:00
for _, res := range *resourcesFromState {
if res.TerraformType() == aws.AwsSqsQueuePolicyResourceType &&
res.TerraformId() == queue.Id {
return true
}
}
return false
}