diff --git a/builtin/providers/aws/config.go b/builtin/providers/aws/config.go index 552a0f446..bd03236ad 100644 --- a/builtin/providers/aws/config.go +++ b/builtin/providers/aws/config.go @@ -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 { @@ -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 @@ -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) diff --git a/builtin/providers/aws/provider.go b/builtin/providers/aws/provider.go index 095e392dd..b0af4e7be 100644 --- a/builtin/providers/aws/provider.go +++ b/builtin/providers/aws/provider.go @@ -121,6 +121,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(), diff --git a/builtin/providers/aws/resource_aws_sqs_queue.go b/builtin/providers/aws/resource_aws_sqs_queue.go new file mode 100644 index 000000000..affabfed2 --- /dev/null +++ b/builtin/providers/aws/resource_aws_sqs_queue.go @@ -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 +} + + diff --git a/examples/aws-sqs/main.tf b/examples/aws-sqs/main.tf new file mode 100644 index 000000000..4e5979fd5 --- /dev/null +++ b/examples/aws-sqs/main.tf @@ -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 +} diff --git a/examples/aws-sqs/variables.tf b/examples/aws-sqs/variables.tf new file mode 100644 index 000000000..617a5719f --- /dev/null +++ b/examples/aws-sqs/variables.tf @@ -0,0 +1,5 @@ +variable "aws_region" { + description = "The AWS region to create things in." + default = "us-west-2" +} +