-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
|