2015-05-12 23:34:10 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"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,
|
|
|
|
|
|
|
|
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,
|
2015-05-15 22:09:20 +02:00
|
|
|
Computed: true,
|
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,
|
2015-05-15 22:09:20 +02:00
|
|
|
Computed: true,
|
2015-05-12 23:34:10 +02:00
|
|
|
},
|
|
|
|
"message_retention_seconds": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
2015-05-15 22:09:20 +02:00
|
|
|
Computed: true,
|
2015-05-12 23:34:10 +02:00
|
|
|
},
|
|
|
|
"receive_wait_time_seconds": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
2015-05-15 22:09:20 +02:00
|
|
|
Computed: true,
|
2015-05-12 23:34:10 +02:00
|
|
|
},
|
|
|
|
"visibility_timeout_seconds": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
2015-05-15 22:09:20 +02:00
|
|
|
Computed: true,
|
2015-05-12 23:34:10 +02:00
|
|
|
},
|
|
|
|
"policy": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"redrive_policy": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
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 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
} 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{
|
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
|
|
|
|
}
|