2015-05-15 01:17:18 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2015-10-30 17:53:59 +01:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2015-05-15 01:17:18 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2015-10-30 17:53:59 +01:00
|
|
|
"strings"
|
|
|
|
"time"
|
2015-05-15 01:17:18 +02:00
|
|
|
|
2015-10-30 17:53:59 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2015-05-15 01:17:18 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2015-10-30 17:53:59 +01:00
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/service/sns"
|
2015-05-15 01:17:18 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Mutable attributes
|
|
|
|
var SNSAttributeMap = map[string]string{
|
2015-06-03 15:46:03 +02:00
|
|
|
"display_name": "DisplayName",
|
|
|
|
"policy": "Policy",
|
2015-05-15 01:17:18 +02:00
|
|
|
"delivery_policy": "DeliveryPolicy",
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsSnsTopic() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsSnsTopicCreate,
|
|
|
|
Read: resourceAwsSnsTopicRead,
|
|
|
|
Update: resourceAwsSnsTopicUpdate,
|
|
|
|
Delete: resourceAwsSnsTopicDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"display_name": &schema.Schema{
|
2015-06-03 15:46:03 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: false,
|
2015-05-15 01:17:18 +02:00
|
|
|
},
|
|
|
|
"policy": &schema.Schema{
|
2015-06-03 15:46:03 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
2015-10-30 17:53:59 +01:00
|
|
|
StateFunc: func(v interface{}) string {
|
2015-11-06 21:06:50 +01:00
|
|
|
s, ok := v.(string)
|
|
|
|
if !ok || s == "" {
|
2015-11-05 22:25:04 +01:00
|
|
|
return ""
|
2015-11-04 13:23:13 +01:00
|
|
|
}
|
2015-11-06 21:06:50 +01:00
|
|
|
jsonb := []byte(s)
|
2015-10-30 17:53:59 +01:00
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
if err := json.Compact(buffer, jsonb); err != nil {
|
|
|
|
log.Printf("[WARN] Error compacting JSON for Policy in SNS Topic")
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return buffer.String()
|
|
|
|
},
|
2015-05-15 01:17:18 +02:00
|
|
|
},
|
|
|
|
"delivery_policy": &schema.Schema{
|
2015-06-03 15:46:03 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: false,
|
2015-05-15 01:17:18 +02:00
|
|
|
},
|
2015-05-23 06:12:25 +02:00
|
|
|
"arn": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2015-05-15 01:17:18 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsSnsTopicCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
snsconn := meta.(*AWSClient).snsconn
|
|
|
|
|
|
|
|
name := d.Get("name").(string)
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] SNS create topic: %s", name)
|
|
|
|
|
|
|
|
req := &sns.CreateTopicInput{
|
|
|
|
Name: aws.String(name),
|
|
|
|
}
|
|
|
|
|
|
|
|
output, err := snsconn.CreateTopic(req)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating SNS topic: %s", err)
|
|
|
|
}
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
d.SetId(*output.TopicArn)
|
2015-05-15 01:17:18 +02:00
|
|
|
|
2015-05-23 06:12:25 +02:00
|
|
|
// Write the ARN to the 'arn' field for export
|
2015-08-17 20:27:16 +02:00
|
|
|
d.Set("arn", *output.TopicArn)
|
2015-05-23 06:12:25 +02:00
|
|
|
|
2015-05-15 01:17:18 +02:00
|
|
|
return resourceAwsSnsTopicUpdate(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsSnsTopicUpdate(d *schema.ResourceData, meta interface{}) error {
|
2015-10-30 17:53:59 +01:00
|
|
|
r := *resourceAwsSnsTopic()
|
2015-05-15 01:17:18 +02:00
|
|
|
|
2015-10-30 17:53:59 +01:00
|
|
|
for k, _ := range r.Schema {
|
2015-05-15 01:17:18 +02:00
|
|
|
if attrKey, ok := SNSAttributeMap[k]; ok {
|
|
|
|
if d.HasChange(k) {
|
|
|
|
log.Printf("[DEBUG] Updating %s", attrKey)
|
|
|
|
_, n := d.GetChange(k)
|
|
|
|
// Ignore an empty policy
|
|
|
|
if !(k == "policy" && n == "") {
|
|
|
|
// Make API call to update attributes
|
2015-10-30 17:53:59 +01:00
|
|
|
req := sns.SetTopicAttributesInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
TopicArn: aws.String(d.Id()),
|
2015-06-03 15:46:03 +02:00
|
|
|
AttributeName: aws.String(attrKey),
|
2015-05-15 01:17:18 +02:00
|
|
|
AttributeValue: aws.String(n.(string)),
|
|
|
|
}
|
2015-10-30 17:53:59 +01:00
|
|
|
|
|
|
|
// Retry the update in the event of an eventually consistent style of
|
|
|
|
// error, where say an IAM resource is successfully created but not
|
|
|
|
// actually available. See https://github.com/hashicorp/terraform/issues/3660
|
|
|
|
log.Printf("[DEBUG] Updating SNS Topic (%s) attributes request: %s", d.Id(), req)
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"retrying"},
|
2016-01-21 02:20:41 +01:00
|
|
|
Target: []string{"success"},
|
2015-10-30 17:53:59 +01:00
|
|
|
Refresh: resourceAwsSNSUpdateRefreshFunc(meta, req),
|
|
|
|
Timeout: 1 * time.Minute,
|
|
|
|
MinTimeout: 3 * time.Second,
|
|
|
|
}
|
|
|
|
_, err := stateConf.WaitForState()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-15 01:17:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceAwsSnsTopicRead(d, meta)
|
|
|
|
}
|
|
|
|
|
2015-10-30 17:53:59 +01:00
|
|
|
func resourceAwsSNSUpdateRefreshFunc(
|
|
|
|
meta interface{}, params sns.SetTopicAttributesInput) resource.StateRefreshFunc {
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
snsconn := meta.(*AWSClient).snsconn
|
|
|
|
if _, err := snsconn.SetTopicAttributes(¶ms); err != nil {
|
|
|
|
log.Printf("[WARN] Erroring updating topic attributes: %s", err)
|
|
|
|
if awsErr, ok := err.(awserr.Error); ok {
|
|
|
|
// if the error contains the PrincipalNotFound message, we can retry
|
|
|
|
if strings.Contains(awsErr.Message(), "PrincipalNotFound") {
|
|
|
|
log.Printf("[DEBUG] Retrying AWS SNS Topic Update: %s", params)
|
|
|
|
return nil, "retrying", nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, "failed", err
|
|
|
|
}
|
|
|
|
return 42, "success", nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-15 01:17:18 +02:00
|
|
|
func resourceAwsSnsTopicRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
snsconn := meta.(*AWSClient).snsconn
|
|
|
|
|
|
|
|
attributeOutput, err := snsconn.GetTopicAttributes(&sns.GetTopicAttributesInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
TopicArn: aws.String(d.Id()),
|
2015-05-15 01:17:18 +02:00
|
|
|
})
|
2015-05-23 06:12:25 +02:00
|
|
|
|
2015-05-15 01:17:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-03 15:46:03 +02:00
|
|
|
if attributeOutput.Attributes != nil && len(attributeOutput.Attributes) > 0 {
|
|
|
|
attrmap := attributeOutput.Attributes
|
2015-05-15 01:17:18 +02:00
|
|
|
resource := *resourceAwsSnsTopic()
|
|
|
|
// iKey = internal struct key, oKey = AWS Attribute Map key
|
|
|
|
for iKey, oKey := range SNSAttributeMap {
|
|
|
|
log.Printf("[DEBUG] Updating %s => %s", iKey, oKey)
|
|
|
|
|
|
|
|
if attrmap[oKey] != nil {
|
|
|
|
// Some of the fetched attributes are stateful properties such as
|
|
|
|
// the number of subscriptions, the owner, etc. skip those
|
|
|
|
if resource.Schema[iKey] != nil {
|
|
|
|
value := *attrmap[oKey]
|
|
|
|
log.Printf("[DEBUG] Updating %s => %s -> %s", iKey, oKey, value)
|
|
|
|
d.Set(iKey, *attrmap[oKey])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsSnsTopicDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
snsconn := meta.(*AWSClient).snsconn
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] SNS Delete Topic: %s", d.Id())
|
|
|
|
_, err := snsconn.DeleteTopic(&sns.DeleteTopicInput{
|
2015-08-17 20:27:16 +02:00
|
|
|
TopicArn: aws.String(d.Id()),
|
2015-05-15 01:17:18 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2015-06-03 15:46:03 +02:00
|
|
|
}
|