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