-
Notifications
You must be signed in to change notification settings - Fork 9.1k
/
resource_aws_sqs_queue_policy.go
144 lines (127 loc) · 4.09 KB
/
resource_aws_sqs_queue_policy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package aws
import (
"fmt"
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/jen20/awspolicyequivalence"
)
func resourceAwsSqsQueuePolicy() *schema.Resource {
return &schema.Resource{
Create: resourceAwsSqsQueuePolicyUpsert,
Read: resourceAwsSqsQueuePolicyRead,
Update: resourceAwsSqsQueuePolicyUpsert,
Delete: resourceAwsSqsQueuePolicyDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
MigrateState: resourceAwsSqsQueuePolicyMigrateState,
SchemaVersion: 1,
Schema: map[string]*schema.Schema{
"queue_url": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"policy": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateJsonString,
DiffSuppressFunc: suppressEquivalentAwsPolicyDiffs,
},
},
}
}
func resourceAwsSqsQueuePolicyUpsert(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sqsconn
policy := d.Get("policy").(string)
url := d.Get("queue_url").(string)
sqaInput := &sqs.SetQueueAttributesInput{
QueueUrl: aws.String(url),
Attributes: aws.StringMap(map[string]string{
sqs.QueueAttributeNamePolicy: policy,
}),
}
log.Printf("[DEBUG] Updating SQS attributes: %s", sqaInput)
_, err := conn.SetQueueAttributes(sqaInput)
if err != nil {
return fmt.Errorf("Error updating SQS attributes: %s", err)
}
// https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_SetQueueAttributes.html
// When you change a queue's attributes, the change can take up to 60 seconds
// for most of the attributes to propagate throughout the Amazon SQS system.
gqaInput := &sqs.GetQueueAttributesInput{
QueueUrl: aws.String(url),
AttributeNames: []*string{aws.String(sqs.QueueAttributeNamePolicy)},
}
notUpdatedError := fmt.Errorf("SQS attribute %s not updated", sqs.QueueAttributeNamePolicy)
err = resource.Retry(1*time.Minute, func() *resource.RetryError {
log.Printf("[DEBUG] Reading SQS attributes: %s", gqaInput)
out, err := conn.GetQueueAttributes(gqaInput)
if err != nil {
return resource.NonRetryableError(err)
}
queuePolicy, ok := out.Attributes[sqs.QueueAttributeNamePolicy]
if !ok {
log.Printf("[DEBUG] SQS attribute %s not found - retrying", sqs.QueueAttributeNamePolicy)
return resource.RetryableError(notUpdatedError)
}
equivalent, err := awspolicy.PoliciesAreEquivalent(*queuePolicy, policy)
if err != nil {
return resource.NonRetryableError(err)
}
if !equivalent {
log.Printf("[DEBUG] SQS attribute %s not updated - retrying", sqs.QueueAttributeNamePolicy)
return resource.RetryableError(notUpdatedError)
}
return nil
})
if err != nil {
return err
}
d.SetId(url)
return resourceAwsSqsQueuePolicyRead(d, meta)
}
func resourceAwsSqsQueuePolicyRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sqsconn
out, err := conn.GetQueueAttributes(&sqs.GetQueueAttributesInput{
QueueUrl: aws.String(d.Id()),
AttributeNames: []*string{aws.String(sqs.QueueAttributeNamePolicy)},
})
if err != nil {
if isAWSErr(err, "AWS.SimpleQueueService.NonExistentQueue", "") {
log.Printf("[WARN] SQS Queue (%s) not found", d.Id())
d.SetId("")
return nil
}
return err
}
if out == nil {
return fmt.Errorf("Received empty response for SQS queue %s", d.Id())
}
policy, ok := out.Attributes[sqs.QueueAttributeNamePolicy]
if ok {
d.Set("policy", policy)
} else {
d.Set("policy", "")
}
d.Set("queue_url", d.Id())
return nil
}
func resourceAwsSqsQueuePolicyDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).sqsconn
log.Printf("[DEBUG] Deleting SQS Queue Policy of %s", d.Id())
_, err := conn.SetQueueAttributes(&sqs.SetQueueAttributesInput{
QueueUrl: aws.String(d.Id()),
Attributes: aws.StringMap(map[string]string{
sqs.QueueAttributeNamePolicy: "",
}),
})
if err != nil {
return fmt.Errorf("Error deleting SQS Queue policy: %s", err)
}
return nil
}