Skip to content

Commit

Permalink
Merge pull request #1939 from johnewart/sqs
Browse files Browse the repository at this point in the history
Initial SQS support
  • Loading branch information
catsby committed May 15, 2015
2 parents 5b04c70 + cb05e51 commit 9f52192
Show file tree
Hide file tree
Showing 6 changed files with 427 additions and 3 deletions.
5 changes: 5 additions & 0 deletions builtin/providers/aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,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 @@ -35,6 +36,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 @@ -84,6 +86,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 @@ -122,6 +122,7 @@ func Provider() terraform.ResourceProvider {
"aws_s3_bucket": resourceAwsS3Bucket(),
"aws_security_group": resourceAwsSecurityGroup(),
"aws_security_group_rule": resourceAwsSecurityGroupRule(),
"aws_sqs_queue": resourceAwsSqsQueue(),
"aws_subnet": resourceAwsSubnet(),
"aws_vpc_dhcp_options_association": resourceAwsVpcDhcpOptionsAssociation(),
"aws_vpc_dhcp_options": resourceAwsVpcDhcpOptions(),
Expand Down
197 changes: 197 additions & 0 deletions builtin/providers/aws/resource_aws_sqs_queue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
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",
}


// 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,
},
},
}
}

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
}


Loading

0 comments on commit 9f52192

Please sign in to comment.