github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_sqs_queue.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strconv"
     7  
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  
    10  	"github.com/aws/aws-sdk-go/aws"
    11  	"github.com/aws/aws-sdk-go/service/sqs"
    12  )
    13  
    14  var AttributeMap = map[string]string{
    15  	"delay_seconds":              "DelaySeconds",
    16  	"max_message_size":           "MaximumMessageSize",
    17  	"message_retention_seconds":  "MessageRetentionPeriod",
    18  	"receive_wait_time_seconds":  "ReceiveMessageWaitTimeSeconds",
    19  	"visibility_timeout_seconds": "VisibilityTimeout",
    20  	"policy":                     "Policy",
    21  	"redrive_policy":             "RedrivePolicy",
    22  	"arn":                        "QueueArn",
    23  }
    24  
    25  // A number of these are marked as computed because if you don't
    26  // provide a value, SQS will provide you with defaults (which are the
    27  // default values specified below)
    28  func resourceAwsSqsQueue() *schema.Resource {
    29  	return &schema.Resource{
    30  		Create: resourceAwsSqsQueueCreate,
    31  		Read:   resourceAwsSqsQueueRead,
    32  		Update: resourceAwsSqsQueueUpdate,
    33  		Delete: resourceAwsSqsQueueDelete,
    34  
    35  		Schema: map[string]*schema.Schema{
    36  			"name": &schema.Schema{
    37  				Type:     schema.TypeString,
    38  				Required: true,
    39  				ForceNew: true,
    40  			},
    41  			"delay_seconds": &schema.Schema{
    42  				Type:     schema.TypeInt,
    43  				Optional: true,
    44  				Computed: true,
    45  			},
    46  			"max_message_size": &schema.Schema{
    47  				Type:     schema.TypeInt,
    48  				Optional: true,
    49  				Computed: true,
    50  			},
    51  			"message_retention_seconds": &schema.Schema{
    52  				Type:     schema.TypeInt,
    53  				Optional: true,
    54  				Computed: true,
    55  			},
    56  			"receive_wait_time_seconds": &schema.Schema{
    57  				Type:     schema.TypeInt,
    58  				Optional: true,
    59  				Computed: true,
    60  			},
    61  			"visibility_timeout_seconds": &schema.Schema{
    62  				Type:     schema.TypeInt,
    63  				Optional: true,
    64  				Computed: true,
    65  			},
    66  			"policy": &schema.Schema{
    67  				Type:     schema.TypeString,
    68  				Optional: true,
    69  			},
    70  			"redrive_policy": &schema.Schema{
    71  				Type:     schema.TypeString,
    72  				Optional: true,
    73  			},
    74  			"arn": &schema.Schema{
    75  				Type:     schema.TypeString,
    76  				Computed: true,
    77  			},
    78  		},
    79  	}
    80  }
    81  
    82  func resourceAwsSqsQueueCreate(d *schema.ResourceData, meta interface{}) error {
    83  	sqsconn := meta.(*AWSClient).sqsconn
    84  
    85  	name := d.Get("name").(string)
    86  
    87  	log.Printf("[DEBUG] SQS queue create: %s", name)
    88  
    89  	req := &sqs.CreateQueueInput{
    90  		QueueName: aws.String(name),
    91  	}
    92  
    93  	attributes := make(map[string]*string)
    94  
    95  	resource := *resourceAwsSqsQueue()
    96  
    97  	for k, s := range resource.Schema {
    98  		if attrKey, ok := AttributeMap[k]; ok {
    99  			if value, ok := d.GetOk(k); ok {
   100  				if s.Type == schema.TypeInt {
   101  					attributes[attrKey] = aws.String(strconv.Itoa(value.(int)))
   102  				} else {
   103  					attributes[attrKey] = aws.String(value.(string))
   104  				}
   105  			}
   106  
   107  		}
   108  	}
   109  
   110  	if len(attributes) > 0 {
   111  		req.Attributes = attributes
   112  	}
   113  
   114  	output, err := sqsconn.CreateQueue(req)
   115  	if err != nil {
   116  		return fmt.Errorf("Error creating SQS queue: %s", err)
   117  	}
   118  
   119  	d.SetId(*output.QueueUrl)
   120  
   121  	return resourceAwsSqsQueueUpdate(d, meta)
   122  }
   123  
   124  func resourceAwsSqsQueueUpdate(d *schema.ResourceData, meta interface{}) error {
   125  	sqsconn := meta.(*AWSClient).sqsconn
   126  	attributes := make(map[string]*string)
   127  
   128  	resource := *resourceAwsSqsQueue()
   129  
   130  	for k, s := range resource.Schema {
   131  		if attrKey, ok := AttributeMap[k]; ok {
   132  			if d.HasChange(k) {
   133  				log.Printf("[DEBUG] Updating %s", attrKey)
   134  				_, n := d.GetChange(k)
   135  				if s.Type == schema.TypeInt {
   136  					attributes[attrKey] = aws.String(strconv.Itoa(n.(int)))
   137  				} else {
   138  					attributes[attrKey] = aws.String(n.(string))
   139  				}
   140  			}
   141  		}
   142  	}
   143  
   144  	if len(attributes) > 0 {
   145  		req := &sqs.SetQueueAttributesInput{
   146  			QueueUrl:   aws.String(d.Id()),
   147  			Attributes: attributes,
   148  		}
   149  		sqsconn.SetQueueAttributes(req)
   150  	}
   151  
   152  	return resourceAwsSqsQueueRead(d, meta)
   153  }
   154  
   155  func resourceAwsSqsQueueRead(d *schema.ResourceData, meta interface{}) error {
   156  	sqsconn := meta.(*AWSClient).sqsconn
   157  
   158  	attributeOutput, err := sqsconn.GetQueueAttributes(&sqs.GetQueueAttributesInput{
   159  		QueueUrl:       aws.String(d.Id()),
   160  		AttributeNames: []*string{aws.String("All")},
   161  	})
   162  
   163  	if err != nil {
   164  		return err
   165  	}
   166  
   167  	if attributeOutput.Attributes != nil && len(attributeOutput.Attributes) > 0 {
   168  		attrmap := attributeOutput.Attributes
   169  		resource := *resourceAwsSqsQueue()
   170  		// iKey = internal struct key, oKey = AWS Attribute Map key
   171  		for iKey, oKey := range AttributeMap {
   172  			if attrmap[oKey] != nil {
   173  				if resource.Schema[iKey].Type == schema.TypeInt {
   174  					value, err := strconv.Atoi(*attrmap[oKey])
   175  					if err != nil {
   176  						return err
   177  					}
   178  					d.Set(iKey, value)
   179  				} else {
   180  					d.Set(iKey, *attrmap[oKey])
   181  				}
   182  			}
   183  		}
   184  	}
   185  
   186  	return nil
   187  }
   188  
   189  func resourceAwsSqsQueueDelete(d *schema.ResourceData, meta interface{}) error {
   190  	sqsconn := meta.(*AWSClient).sqsconn
   191  
   192  	log.Printf("[DEBUG] SQS Delete Queue: %s", d.Id())
   193  	_, err := sqsconn.DeleteQueue(&sqs.DeleteQueueInput{
   194  		QueueUrl: aws.String(d.Id()),
   195  	})
   196  	if err != nil {
   197  		return err
   198  	}
   199  	return nil
   200  }