github.com/paulmey/terraform@v0.5.2-0.20150519145237-046e9b4c884d/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/awslabs/aws-sdk-go/aws" 11 "github.com/awslabs/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 } 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 }, 75 } 76 } 77 78 func resourceAwsSqsQueueCreate(d *schema.ResourceData, meta interface{}) error { 79 sqsconn := meta.(*AWSClient).sqsconn 80 81 name := d.Get("name").(string) 82 83 log.Printf("[DEBUG] SQS queue create: %s", name) 84 85 req := &sqs.CreateQueueInput{ 86 QueueName: aws.String(name), 87 } 88 89 attributes := make(map[string]*string) 90 91 resource := *resourceAwsSqsQueue() 92 93 for k, s := range resource.Schema { 94 if attrKey, ok := AttributeMap[k]; ok { 95 if value, ok := d.GetOk(k); ok { 96 if s.Type == schema.TypeInt { 97 attributes[attrKey] = aws.String(strconv.Itoa(value.(int))) 98 } else { 99 attributes[attrKey] = aws.String(value.(string)) 100 } 101 } 102 103 } 104 } 105 106 if len(attributes) > 0 { 107 req.Attributes = &attributes 108 } 109 110 output, err := sqsconn.CreateQueue(req) 111 if err != nil { 112 return fmt.Errorf("Error creating SQS queue: %s", err) 113 } 114 115 d.SetId(*output.QueueURL) 116 117 return resourceAwsSqsQueueUpdate(d, meta) 118 } 119 120 func resourceAwsSqsQueueUpdate(d *schema.ResourceData, meta interface{}) error { 121 sqsconn := meta.(*AWSClient).sqsconn 122 attributes := make(map[string]*string) 123 124 resource := *resourceAwsSqsQueue() 125 126 for k, s := range resource.Schema { 127 if attrKey, ok := AttributeMap[k]; ok { 128 if d.HasChange(k) { 129 log.Printf("[DEBUG] Updating %s", attrKey) 130 _, n := d.GetChange(k) 131 if s.Type == schema.TypeInt { 132 attributes[attrKey] = aws.String(strconv.Itoa(n.(int))) 133 } else { 134 attributes[attrKey] = aws.String(n.(string)) 135 } 136 } 137 } 138 } 139 140 if len(attributes) > 0 { 141 req := &sqs.SetQueueAttributesInput{ 142 QueueURL: aws.String(d.Id()), 143 Attributes: &attributes, 144 } 145 sqsconn.SetQueueAttributes(req) 146 } 147 148 return resourceAwsSqsQueueRead(d, meta) 149 } 150 151 func resourceAwsSqsQueueRead(d *schema.ResourceData, meta interface{}) error { 152 sqsconn := meta.(*AWSClient).sqsconn 153 154 attributeOutput, err := sqsconn.GetQueueAttributes(&sqs.GetQueueAttributesInput{ 155 QueueURL: aws.String(d.Id()), 156 AttributeNames: []*string{aws.String("All")}, 157 }) 158 if err != nil { 159 return err 160 } 161 162 if attributeOutput.Attributes != nil && len(*attributeOutput.Attributes) > 0 { 163 attrmap := *attributeOutput.Attributes 164 resource := *resourceAwsSqsQueue() 165 // iKey = internal struct key, oKey = AWS Attribute Map key 166 for iKey, oKey := range AttributeMap { 167 if attrmap[oKey] != nil { 168 if resource.Schema[iKey].Type == schema.TypeInt { 169 value, err := strconv.Atoi(*attrmap[oKey]) 170 if err != nil { 171 return err 172 } 173 d.Set(iKey, value) 174 } else { 175 d.Set(iKey, *attrmap[oKey]) 176 } 177 } 178 } 179 } 180 181 return nil 182 } 183 184 func resourceAwsSqsQueueDelete(d *schema.ResourceData, meta interface{}) error { 185 sqsconn := meta.(*AWSClient).sqsconn 186 187 log.Printf("[DEBUG] SQS Delete Queue: %s", d.Id()) 188 _, err := sqsconn.DeleteQueue(&sqs.DeleteQueueInput{ 189 QueueURL: aws.String(d.Id()), 190 }) 191 if err != nil { 192 return err 193 } 194 return nil 195 } 196 197