driftctl/pkg/middlewares/aws_sqs_queue_policy_expand...

99 lines
2.8 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
2021-03-29 16:10:50 +00:00
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 {
2021-03-29 16:10:50 +00:00
return AwsSqsQueuePolicyExpander{
resourceFactory,
resourceSchemaRepository,
2021-03-29 16:10:50 +00:00
}
2021-01-29 11:35:11 +00:00
}
func (m AwsSqsQueuePolicyExpander) Execute(_, resourcesFromState *[]resource.Resource) error {
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
}
queue, _ := res.(*aws.AwsSqsQueue)
newList = append(newList, res)
2021-02-03 20:08:57 +00:00
if queue.Policy == nil {
continue
}
2021-01-29 11:35:11 +00:00
if m.hasPolicyAttached(queue, resourcesFromState) {
queue.Policy = nil
continue
}
err := m.handlePolicy(queue, &newList)
if err != nil {
return err
}
}
*resourcesFromState = newList
return nil
}
func (m *AwsSqsQueuePolicyExpander) handlePolicy(queue *aws.AwsSqsQueue, results *[]resource.Resource) error {
2021-03-29 16:10:50 +00:00
data := map[string]interface{}{
"queue_url": queue.Id,
"id": queue.Id,
"policy": queue.Policy,
}
ctyVal, err := m.resourceFactory.CreateResource(data, "aws_sqs_queue_policy")
if err != nil {
return err
}
schema, exist := m.resourceSchemaRepository.GetSchema("aws_ebs_volume")
ctyAttr := resource.ToResourceAttributes(ctyVal)
ctyAttr.SanitizeDefaultsV3()
if exist && schema.NormalizeFunc != nil {
schema.NormalizeFunc(ctyAttr)
2021-01-29 11:35:11 +00:00
}
newPolicy := &resource.AbstractResource{
Id: queue.Id,
Type: aws.AwsSqsQueuePolicyResourceType,
Attrs: ctyAttr,
2021-01-29 11:35:11 +00:00
}
*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")
queue.Policy = nil
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 *aws.AwsSqsQueue, resourcesFromState *[]resource.Resource) bool {
for _, res := range *resourcesFromState {
if res.TerraformType() == aws.AwsSqsQueuePolicyResourceType &&
res.TerraformId() == queue.Id {
return true
}
}
return false
}