terraform/builtin/providers/aws/resource_aws_sqs_queue.go

255 lines
6.2 KiB
Go
Raw Normal View History

2015-05-12 23:34:10 +02:00
package aws
import (
"bytes"
"encoding/json"
2015-05-12 23:34:10 +02:00
"fmt"
"log"
"net/url"
2015-05-12 23:34:10 +02:00
"strconv"
"github.com/hashicorp/terraform/helper/schema"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/sqs"
2015-05-12 23:34:10 +02:00
)
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",
2015-05-12 23:34:10 +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,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
2015-05-12 23:34:10 +02:00
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
2015-05-12 23:34:10 +02:00
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"delay_seconds": &schema.Schema{
Type: schema.TypeInt,
2015-05-12 23:34:10 +02:00
Optional: true,
Default: 0,
2015-05-12 23:34:10 +02:00
},
"max_message_size": &schema.Schema{
Type: schema.TypeInt,
2015-05-12 23:34:10 +02:00
Optional: true,
Default: 262144,
2015-05-12 23:34:10 +02:00
},
"message_retention_seconds": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 345600,
2015-05-12 23:34:10 +02:00
},
"receive_wait_time_seconds": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 0,
2015-05-12 23:34:10 +02:00
},
"visibility_timeout_seconds": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 30,
2015-05-12 23:34:10 +02:00
},
"policy": &schema.Schema{
Type: schema.TypeString,
Optional: true,
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 {
log.Printf("[WARN] Error compacting JSON for Policy in SNS Queue, using raw string: %s", err)
return s
}
return buffer.String()
},
2015-05-12 23:34:10 +02:00
},
"redrive_policy": &schema.Schema{
Type: schema.TypeString,
Optional: true,
StateFunc: normalizeJson,
2015-05-12 23:34:10 +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
name := d.Get("name").(string)
log.Printf("[DEBUG] SQS queue create: %s", name)
2015-05-12 23:34:10 +02:00
req := &sqs.CreateQueueInput{
QueueName: aws.String(name),
2015-05-12 23:34:10 +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 {
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)
}
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{
QueueUrl: aws.String(d.Id()),
Attributes: attributes,
2015-05-12 23:34:10 +02:00
}
sqsconn.SetQueueAttributes(req)
}
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{
QueueUrl: aws.String(d.Id()),
AttributeNames: []*string{aws.String("All")},
2015-05-12 23:34:10 +02:00
})
2015-05-12 23:34:10 +02:00
if err != nil {
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
}
name, err := extractNameFromSqsQueueUrl(d.Id())
if err != nil {
return err
}
d.Set("name", name)
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])
if err != nil {
2015-05-12 23:34:10 +02:00
return err
}
d.Set(iKey, value)
log.Printf("[DEBUG] Reading %s => %s -> %d", iKey, oKey, value)
2015-05-12 23:34:10 +02:00
} else {
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{
QueueUrl: aws.String(d.Id()),
2015-05-12 23:34:10 +02:00
})
if err != nil {
return err
}
return nil
}
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
}