Skip to content

Commit

Permalink
Initial SQS support
Browse files Browse the repository at this point in the history
  • Loading branch information
johnewart committed May 12, 2015
1 parent a21f320 commit 439af5b
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 0 deletions.
5 changes: 5 additions & 0 deletions builtin/providers/aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/awslabs/aws-sdk-go/service/rds"
"github.com/awslabs/aws-sdk-go/service/route53"
"github.com/awslabs/aws-sdk-go/service/s3"
"github.com/awslabs/aws-sdk-go/service/sqs"
)

type Config struct {
Expand All @@ -33,6 +34,7 @@ type AWSClient struct {
elbconn *elb.ELB
autoscalingconn *autoscaling.AutoScaling
s3conn *s3.S3
sqsconn *sqs.SQS
r53conn *route53.Route53
region string
rdsconn *rds.RDS
Expand Down Expand Up @@ -72,6 +74,9 @@ func (c *Config) Client() (interface{}, error) {
log.Println("[INFO] Initializing S3 connection")
client.s3conn = s3.New(awsConfig)

log.Println("[INFO] Initializing SQS connection")
client.sqsconn = sqs.New(awsConfig)

log.Println("[INFO] Initializing RDS Connection")
client.rdsconn = rds.New(awsConfig)

Expand Down
1 change: 1 addition & 0 deletions builtin/providers/aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func Provider() terraform.ResourceProvider {
"aws_route_table_association": resourceAwsRouteTableAssociation(),
"aws_s3_bucket": resourceAwsS3Bucket(),
"aws_security_group": resourceAwsSecurityGroup(),
"aws_sqs_queue": resourceAwsSqsQueue(),
"aws_subnet": resourceAwsSubnet(),
"aws_vpc": resourceAwsVpc(),
"aws_vpc_peering_connection": resourceAwsVpcPeeringConnection(),
Expand Down
188 changes: 188 additions & 0 deletions builtin/providers/aws/resource_aws_sqs_queue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
package aws

import (
"fmt"
"log"
"strconv"

"github.com/hashicorp/terraform/helper/schema"

"github.com/awslabs/aws-sdk-go/aws"
"github.com/awslabs/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",
}


func resourceAwsSqsQueue() *schema.Resource {
return &schema.Resource{
Create: resourceAwsSqsQueueCreate,
Read: resourceAwsSqsQueueRead,
Update: resourceAwsSqsQueueUpdate,
Delete: resourceAwsSqsQueueDelete,

Schema: map[string]*schema.Schema{
"queue": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"delay_seconds": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
},
"max_message_size": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
},
"message_retention_seconds": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
},
"receive_wait_time_seconds": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
},
"visibility_timeout_seconds": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
},
"policy": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"redrive_policy": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
}
}

func resourceAwsSqsQueueCreate(d *schema.ResourceData, meta interface{}) error {
sqsconn := meta.(*AWSClient).sqsconn

queue := d.Get("queue").(string)

log.Printf("[DEBUG] SQS queue create: %s", queue)

req := &sqs.CreateQueueInput{
QueueName: aws.String(queue),
}

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()),
})
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
}


22 changes: 22 additions & 0 deletions examples/aws-sqs/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Specify the provider and access details
provider "aws" {
region = "${var.aws_region}"
}

resource "aws_sqs_queue" "terraform_queue" {
queue = "terraform-example-renamed"
}

resource "aws_sqs_queue" "terrform_queue_attr" {
queue = "terraform-example-attr"
delay_seconds = 90
max_message_size = 2048
message_retention_seconds = 86400
receive_wait_time_seconds = 10
}

resource "aws_sqs_queue" "terraform_queue_too" {
queue = "terraform-queue-too"
delay_seconds = 120
max_message_size = 4096
}
5 changes: 5 additions & 0 deletions examples/aws-sqs/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
variable "aws_region" {
description = "The AWS region to create things in."
default = "us-west-2"
}

0 comments on commit 439af5b

Please sign in to comment.