forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_aws_sqs_queue.go
200 lines (170 loc) · 4.76 KB
/
resource_aws_sqs_queue.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package aws
import (
"fmt"
"log"
"strconv"
"github.com/hashicorp/terraform/helper/schema"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sqs"
)
var AttributeMap = map[string]string{
"delay_seconds": "DelaySeconds",
"max_message_size": "MaximumMessageSize",
"message_retention_seconds": "MessageRetentionPeriod",
"receive_wait_time_seconds": "ReceiveMessageWaitTimeSeconds",
"visibility_timeout_seconds": "VisibilityTimeout",
"policy": "Policy",
"redrive_policy": "RedrivePolicy",
"arn": "QueueArn",
}
// A number of these are marked as computed because if you don't
// provide a value, SQS will provide you with defaults (which are the
// default values specified below)
func resourceAwsSqsQueue() *schema.Resource {
return &schema.Resource{
Create: resourceAwsSqsQueueCreate,
Read: resourceAwsSqsQueueRead,
Update: resourceAwsSqsQueueUpdate,
Delete: resourceAwsSqsQueueDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"delay_seconds": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"max_message_size": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"message_retention_seconds": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"receive_wait_time_seconds": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"visibility_timeout_seconds": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"policy": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"redrive_policy": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"arn": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceAwsSqsQueueCreate(d *schema.ResourceData, meta interface{}) error {
sqsconn := meta.(*AWSClient).sqsconn
name := d.Get("name").(string)
log.Printf("[DEBUG] SQS queue create: %s", name)
req := &sqs.CreateQueueInput{
QueueName: aws.String(name),
}
attributes := make(map[string]*string)
resource := *resourceAwsSqsQueue()
for k, s := range resource.Schema {
if attrKey, ok := AttributeMap[k]; ok {
if value, ok := d.GetOk(k); ok {
if s.Type == schema.TypeInt {
attributes[attrKey] = aws.String(strconv.Itoa(value.(int)))
} else {
attributes[attrKey] = aws.String(value.(string))
}
}
}
}
if len(attributes) > 0 {
req.Attributes = attributes
}
output, err := sqsconn.CreateQueue(req)
if err != nil {
return fmt.Errorf("Error creating SQS queue: %s", err)
}
d.SetId(*output.QueueUrl)
return resourceAwsSqsQueueUpdate(d, meta)
}
func resourceAwsSqsQueueUpdate(d *schema.ResourceData, meta interface{}) error {
sqsconn := meta.(*AWSClient).sqsconn
attributes := make(map[string]*string)
resource := *resourceAwsSqsQueue()
for k, s := range resource.Schema {
if attrKey, ok := AttributeMap[k]; ok {
if d.HasChange(k) {
log.Printf("[DEBUG] Updating %s", attrKey)
_, n := d.GetChange(k)
if s.Type == schema.TypeInt {
attributes[attrKey] = aws.String(strconv.Itoa(n.(int)))
} else {
attributes[attrKey] = aws.String(n.(string))
}
}
}
}
if len(attributes) > 0 {
req := &sqs.SetQueueAttributesInput{
QueueUrl: aws.String(d.Id()),
Attributes: attributes,
}
sqsconn.SetQueueAttributes(req)
}
return resourceAwsSqsQueueRead(d, meta)
}
func resourceAwsSqsQueueRead(d *schema.ResourceData, meta interface{}) error {
sqsconn := meta.(*AWSClient).sqsconn
attributeOutput, err := sqsconn.GetQueueAttributes(&sqs.GetQueueAttributesInput{
QueueUrl: aws.String(d.Id()),
AttributeNames: []*string{aws.String("All")},
})
if err != nil {
return err
}
if attributeOutput.Attributes != nil && len(attributeOutput.Attributes) > 0 {
attrmap := attributeOutput.Attributes
resource := *resourceAwsSqsQueue()
// iKey = internal struct key, oKey = AWS Attribute Map key
for iKey, oKey := range AttributeMap {
if attrmap[oKey] != nil {
if resource.Schema[iKey].Type == schema.TypeInt {
value, err := strconv.Atoi(*attrmap[oKey])
if err != nil {
return err
}
d.Set(iKey, value)
} else {
d.Set(iKey, *attrmap[oKey])
}
}
}
}
return nil
}
func resourceAwsSqsQueueDelete(d *schema.ResourceData, meta interface{}) error {
sqsconn := meta.(*AWSClient).sqsconn
log.Printf("[DEBUG] SQS Delete Queue: %s", d.Id())
_, err := sqsconn.DeleteQueue(&sqs.DeleteQueueInput{
QueueUrl: aws.String(d.Id()),
})
if err != nil {
return err
}
return nil
}