2015-05-12 23:34:10 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2016-04-04 18:41:36 +02:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2015-05-12 23:34:10 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2016-07-07 18:10:45 +02:00
|
|
|
"net/url"
|
2015-05-12 23:34:10 +02:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
|
2016-07-07 18:10:45 +02:00
|
|
|
"strings"
|
|
|
|
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2016-04-27 21:07:34 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/service/sqs"
|
2015-05-12 23:34:10 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var AttributeMap = map[string]string{
|
2015-05-20 13:21:23 +02:00
|
|
|
"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",
|
2015-05-23 06:12:25 +02:00
|
|
|
"arn": "QueueArn",
|
2015-05-12 23:34:10 +02:00
|
|
|
}
|
|
|
|
|
2015-05-15 22:09:20 +02:00
|
|
|
// 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)
|
2015-05-12 23:34:10 +02:00
|
|
|
func resourceAwsSqsQueue() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsSqsQueueCreate,
|
|
|
|
Read: resourceAwsSqsQueueRead,
|
|
|
|
Update: resourceAwsSqsQueueUpdate,
|
|
|
|
Delete: resourceAwsSqsQueueDelete,
|
2016-07-07 18:10:45 +02:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
2015-05-12 23:34:10 +02:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2015-05-15 22:09:20 +02:00
|
|
|
"name": &schema.Schema{
|
2015-05-12 23:34:10 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"delay_seconds": &schema.Schema{
|
2015-05-20 13:21:23 +02:00
|
|
|
Type: schema.TypeInt,
|
2015-05-12 23:34:10 +02:00
|
|
|
Optional: true,
|
2016-07-26 21:53:45 +02:00
|
|
|
Default: 0,
|
2015-05-12 23:34:10 +02:00
|
|
|
},
|
|
|
|
"max_message_size": &schema.Schema{
|
2015-05-20 13:21:23 +02:00
|
|
|
Type: schema.TypeInt,
|
2015-05-12 23:34:10 +02:00
|
|
|
Optional: true,
|
2016-07-26 21:53:45 +02:00
|
|
|
Default: 262144,
|
2015-05-12 23:34:10 +02:00
|
|
|
},
|
|
|
|
"message_retention_seconds": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
2016-07-26 21:53:45 +02:00
|
|
|
Default: 345600,
|
2015-05-12 23:34:10 +02:00
|
|
|
},
|
|
|
|
"receive_wait_time_seconds": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
2016-07-26 21:53:45 +02:00
|
|
|
Default: 0,
|
2015-05-12 23:34:10 +02:00
|
|
|
},
|
|
|
|
"visibility_timeout_seconds": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
2016-07-26 17:30:52 +02:00
|
|
|
Default: 30,
|
2015-05-12 23:34:10 +02:00
|
|
|
},
|
|
|
|
"policy": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2016-04-04 18:41:36 +02:00
|
|
|
StateFunc: func(v interface{}) string {
|
|
|
|
s, ok := v.(string)
|
|
|
|
if !ok || s == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
jsonb := []byte(s)
|
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
if err := json.Compact(buffer, jsonb); err != nil {
|
2016-05-24 17:38:32 +02:00
|
|
|
log.Printf("[WARN] Error compacting JSON for Policy in SNS Queue, using raw string: %s", err)
|
|
|
|
return s
|
2016-04-04 18:41:36 +02:00
|
|
|
}
|
|
|
|
return buffer.String()
|
|
|
|
},
|
2015-05-12 23:34:10 +02:00
|
|
|
},
|
|
|
|
"redrive_policy": &schema.Schema{
|
2016-04-04 18:41:36 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
StateFunc: normalizeJson,
|
2015-05-12 23:34:10 +02:00
|
|
|
},
|
2015-05-23 06:12:25 +02:00
|
|
|
"arn": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2015-05-12 23:34:10 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsSqsQueueCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
sqsconn := meta.(*AWSClient).sqsconn
|
|
|
|
|
2015-05-15 22:09:20 +02:00
|
|
|
name := d.Get("name").(string)
|
2015-05-20 13:21:23 +02:00
|
|
|
|
2015-05-15 22:09:20 +02:00
|
|
|
log.Printf("[DEBUG] SQS queue create: %s", name)
|
2015-05-12 23:34:10 +02:00
|
|
|
|
|
|
|
req := &sqs.CreateQueueInput{
|
2015-05-15 22:09:20 +02:00
|
|
|
QueueName: aws.String(name),
|
2015-05-12 23:34:10 +02:00
|
|
|
}
|
|
|
|
|
2015-05-20 13:21:23 +02:00
|
|
|
attributes := make(map[string]*string)
|
2015-05-12 23:34:10 +02:00
|
|
|
|
|
|
|
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 {
|
2015-06-03 15:46:03 +02:00
|
|
|
req.Attributes = attributes
|
2015-05-12 23:34:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
output, err := sqsconn.CreateQueue(req)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating SQS queue: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
d.SetId(*output.QueueUrl)
|
2015-05-12 23:34:10 +02:00
|
|
|
|
|
|
|
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{
|
2015-08-17 20:27:16 +02:00
|
|
|
QueueUrl: aws.String(d.Id()),
|
2015-06-03 15:46:03 +02:00
|
|
|
Attributes: attributes,
|
2015-05-12 23:34:10 +02:00
|
|
|
}
|
|
|
|
sqsconn.SetQueueAttributes(req)
|
2015-05-20 13:21:23 +02:00
|
|
|
}
|
2015-05-12 23:34:10 +02:00
|
|
|
|
|
|
|
return resourceAwsSqsQueueRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsSqsQueueRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
sqsconn := meta.(*AWSClient).sqsconn
|
|
|
|
|
|
|
|
attributeOutput, err := sqsconn.GetQueueAttributes(&sqs.GetQueueAttributesInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
QueueUrl: aws.String(d.Id()),
|
2015-05-15 22:09:20 +02:00
|
|
|
AttributeNames: []*string{aws.String("All")},
|
2015-05-12 23:34:10 +02:00
|
|
|
})
|
2015-05-23 06:12:25 +02:00
|
|
|
|
2015-05-12 23:34:10 +02:00
|
|
|
if err != nil {
|
2016-04-27 21:07:34 +02:00
|
|
|
if awsErr, ok := err.(awserr.Error); ok {
|
|
|
|
log.Printf("ERROR Found %s", awsErr.Code())
|
|
|
|
if "AWS.SimpleQueueService.NonExistentQueue" == awsErr.Code() {
|
|
|
|
d.SetId("")
|
|
|
|
log.Printf("[DEBUG] SQS Queue (%s) not found", d.Get("name").(string))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2015-05-12 23:34:10 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-07-07 18:10:45 +02:00
|
|
|
name, err := extractNameFromSqsQueueUrl(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.Set("name", name)
|
|
|
|
|
2015-06-03 15:46:03 +02:00
|
|
|
if attributeOutput.Attributes != nil && len(attributeOutput.Attributes) > 0 {
|
|
|
|
attrmap := attributeOutput.Attributes
|
2015-05-12 23:34:10 +02:00
|
|
|
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])
|
2015-05-20 13:21:23 +02:00
|
|
|
if err != nil {
|
2015-05-12 23:34:10 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.Set(iKey, value)
|
2016-04-04 18:41:36 +02:00
|
|
|
log.Printf("[DEBUG] Reading %s => %s -> %d", iKey, oKey, value)
|
2015-05-12 23:34:10 +02:00
|
|
|
} else {
|
2016-04-04 18:41:36 +02:00
|
|
|
log.Printf("[DEBUG] Reading %s => %s -> %s", iKey, oKey, *attrmap[oKey])
|
2015-05-12 23:34:10 +02:00
|
|
|
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{
|
2015-08-17 20:27:16 +02:00
|
|
|
QueueUrl: aws.String(d.Id()),
|
2015-05-12 23:34:10 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-07 18:10:45 +02:00
|
|
|
|
|
|
|
func extractNameFromSqsQueueUrl(queue string) (string, error) {
|
|
|
|
//http://sqs.us-west-2.amazonaws.com/123456789012/queueName
|
|
|
|
u, err := url.Parse(queue)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
segments := strings.Split(u.Path, "/")
|
|
|
|
if len(segments) != 3 {
|
|
|
|
return "", fmt.Errorf("SQS Url not parsed correctly")
|
|
|
|
}
|
|
|
|
|
|
|
|
return segments[2], nil
|
|
|
|
|
|
|
|
}
|