-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
resource_aws_sqs_queue_policy.go
104 lines (88 loc) · 2.55 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
package aws
import (
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/hashicorp/terraform/helper/schema"
)
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
url := d.Get("queue_url").(string)
_, err := conn.SetQueueAttributes(&sqs.SetQueueAttributesInput{
QueueUrl: aws.String(url),
Attributes: aws.StringMap(map[string]string{
"Policy": d.Get("policy").(string),
}),
})
if err != nil {
return fmt.Errorf("Error updating SQS attributes: %s", 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("Policy")},
})
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["Policy"]
if !ok {
return fmt.Errorf("SQS Queue policy not found for %s", d.Id())
}
d.Set("policy", 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{
"Policy": "",
}),
})
if err != nil {
return fmt.Errorf("Error deleting SQS Queue policy: %s", err)
}
return nil
}