github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_sns_topic.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strings"
     7  	"time"
     8  
     9  	"github.com/aws/aws-sdk-go/aws"
    10  	"github.com/aws/aws-sdk-go/aws/awserr"
    11  	"github.com/aws/aws-sdk-go/service/sns"
    12  	"github.com/hashicorp/errwrap"
    13  	"github.com/hashicorp/terraform/helper/resource"
    14  	"github.com/hashicorp/terraform/helper/schema"
    15  )
    16  
    17  // Mutable attributes
    18  var SNSAttributeMap = map[string]string{
    19  	"arn":             "TopicArn",
    20  	"display_name":    "DisplayName",
    21  	"policy":          "Policy",
    22  	"delivery_policy": "DeliveryPolicy",
    23  }
    24  
    25  func resourceAwsSnsTopic() *schema.Resource {
    26  	return &schema.Resource{
    27  		Create: resourceAwsSnsTopicCreate,
    28  		Read:   resourceAwsSnsTopicRead,
    29  		Update: resourceAwsSnsTopicUpdate,
    30  		Delete: resourceAwsSnsTopicDelete,
    31  		Importer: &schema.ResourceImporter{
    32  			State: schema.ImportStatePassthrough,
    33  		},
    34  
    35  		Schema: map[string]*schema.Schema{
    36  			"name": &schema.Schema{
    37  				Type:     schema.TypeString,
    38  				Required: true,
    39  				ForceNew: true,
    40  			},
    41  			"display_name": &schema.Schema{
    42  				Type:     schema.TypeString,
    43  				Optional: true,
    44  				ForceNew: false,
    45  			},
    46  			"policy": &schema.Schema{
    47  				Type:             schema.TypeString,
    48  				Optional:         true,
    49  				Computed:         true,
    50  				ValidateFunc:     validateJsonString,
    51  				DiffSuppressFunc: suppressEquivalentAwsPolicyDiffs,
    52  				StateFunc: func(v interface{}) string {
    53  					json, _ := normalizeJsonString(v)
    54  					return json
    55  				},
    56  			},
    57  			"delivery_policy": &schema.Schema{
    58  				Type:     schema.TypeString,
    59  				Optional: true,
    60  				ForceNew: false,
    61  			},
    62  			"arn": &schema.Schema{
    63  				Type:     schema.TypeString,
    64  				Computed: true,
    65  			},
    66  		},
    67  	}
    68  }
    69  
    70  func resourceAwsSnsTopicCreate(d *schema.ResourceData, meta interface{}) error {
    71  	snsconn := meta.(*AWSClient).snsconn
    72  
    73  	name := d.Get("name").(string)
    74  
    75  	log.Printf("[DEBUG] SNS create topic: %s", name)
    76  
    77  	req := &sns.CreateTopicInput{
    78  		Name: aws.String(name),
    79  	}
    80  
    81  	output, err := snsconn.CreateTopic(req)
    82  	if err != nil {
    83  		return fmt.Errorf("Error creating SNS topic: %s", err)
    84  	}
    85  
    86  	d.SetId(*output.TopicArn)
    87  
    88  	// Write the ARN to the 'arn' field for export
    89  	d.Set("arn", *output.TopicArn)
    90  
    91  	return resourceAwsSnsTopicUpdate(d, meta)
    92  }
    93  
    94  func resourceAwsSnsTopicUpdate(d *schema.ResourceData, meta interface{}) error {
    95  	r := *resourceAwsSnsTopic()
    96  
    97  	for k, _ := range r.Schema {
    98  		if attrKey, ok := SNSAttributeMap[k]; ok {
    99  			if d.HasChange(k) {
   100  				log.Printf("[DEBUG] Updating %s", attrKey)
   101  				_, n := d.GetChange(k)
   102  				// Ignore an empty policy
   103  				if !(k == "policy" && n == "") {
   104  					// Make API call to update attributes
   105  					req := sns.SetTopicAttributesInput{
   106  						TopicArn:       aws.String(d.Id()),
   107  						AttributeName:  aws.String(attrKey),
   108  						AttributeValue: aws.String(n.(string)),
   109  					}
   110  
   111  					// Retry the update in the event of an eventually consistent style of
   112  					// error, where say an IAM resource is successfully created but not
   113  					// actually available. See https://github.com/hashicorp/terraform/issues/3660
   114  					log.Printf("[DEBUG] Updating SNS Topic (%s) attributes request: %s", d.Id(), req)
   115  					stateConf := &resource.StateChangeConf{
   116  						Pending:    []string{"retrying"},
   117  						Target:     []string{"success"},
   118  						Refresh:    resourceAwsSNSUpdateRefreshFunc(meta, req),
   119  						Timeout:    1 * time.Minute,
   120  						MinTimeout: 3 * time.Second,
   121  					}
   122  					_, err := stateConf.WaitForState()
   123  					if err != nil {
   124  						return err
   125  					}
   126  				}
   127  			}
   128  		}
   129  	}
   130  
   131  	return resourceAwsSnsTopicRead(d, meta)
   132  }
   133  
   134  func resourceAwsSNSUpdateRefreshFunc(
   135  	meta interface{}, params sns.SetTopicAttributesInput) resource.StateRefreshFunc {
   136  	return func() (interface{}, string, error) {
   137  		snsconn := meta.(*AWSClient).snsconn
   138  		if _, err := snsconn.SetTopicAttributes(&params); err != nil {
   139  			log.Printf("[WARN] Erroring updating topic attributes: %s", err)
   140  			if awsErr, ok := err.(awserr.Error); ok {
   141  				// if the error contains the PrincipalNotFound message, we can retry
   142  				if strings.Contains(awsErr.Message(), "PrincipalNotFound") {
   143  					log.Printf("[DEBUG] Retrying AWS SNS Topic Update: %s", params)
   144  					return nil, "retrying", nil
   145  				}
   146  			}
   147  			return nil, "failed", err
   148  		}
   149  		return 42, "success", nil
   150  	}
   151  }
   152  
   153  func resourceAwsSnsTopicRead(d *schema.ResourceData, meta interface{}) error {
   154  	snsconn := meta.(*AWSClient).snsconn
   155  
   156  	attributeOutput, err := snsconn.GetTopicAttributes(&sns.GetTopicAttributesInput{
   157  		TopicArn: aws.String(d.Id()),
   158  	})
   159  	if err != nil {
   160  		if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFound" {
   161  			log.Printf("[WARN] SNS Topic (%s) not found, error code (404)", d.Id())
   162  			d.SetId("")
   163  			return nil
   164  		}
   165  
   166  		return err
   167  	}
   168  
   169  	if attributeOutput.Attributes != nil && len(attributeOutput.Attributes) > 0 {
   170  		attrmap := attributeOutput.Attributes
   171  		resource := *resourceAwsSnsTopic()
   172  		// iKey = internal struct key, oKey = AWS Attribute Map key
   173  		for iKey, oKey := range SNSAttributeMap {
   174  			log.Printf("[DEBUG] Reading %s => %s", iKey, oKey)
   175  
   176  			if attrmap[oKey] != nil {
   177  				// Some of the fetched attributes are stateful properties such as
   178  				// the number of subscriptions, the owner, etc. skip those
   179  				if resource.Schema[iKey] != nil {
   180  					var value string
   181  					if iKey == "policy" {
   182  						value, err = normalizeJsonString(*attrmap[oKey])
   183  						if err != nil {
   184  							return errwrap.Wrapf("policy contains an invalid JSON: {{err}}", err)
   185  						}
   186  					} else {
   187  						value = *attrmap[oKey]
   188  					}
   189  					log.Printf("[DEBUG] Reading %s => %s -> %s", iKey, oKey, value)
   190  					d.Set(iKey, value)
   191  				}
   192  			}
   193  		}
   194  	}
   195  
   196  	// If we have no name set (import) then determine it from the ARN.
   197  	// This is a bit of a heuristic for now since AWS provides no other
   198  	// way to get it.
   199  	if _, ok := d.GetOk("name"); !ok {
   200  		arn := d.Get("arn").(string)
   201  		idx := strings.LastIndex(arn, ":")
   202  		if idx > -1 {
   203  			d.Set("name", arn[idx+1:])
   204  		}
   205  	}
   206  
   207  	return nil
   208  }
   209  
   210  func resourceAwsSnsTopicDelete(d *schema.ResourceData, meta interface{}) error {
   211  	snsconn := meta.(*AWSClient).snsconn
   212  
   213  	log.Printf("[DEBUG] SNS Delete Topic: %s", d.Id())
   214  	_, err := snsconn.DeleteTopic(&sns.DeleteTopicInput{
   215  		TopicArn: aws.String(d.Id()),
   216  	})
   217  	if err != nil {
   218  		return err
   219  	}
   220  	return nil
   221  }