github.com/jsoriano/terraform@v0.6.7-0.20151026070445-8b70867fdd95/builtin/providers/aws/resource_aws_sns_topic.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  
     9  	"github.com/aws/aws-sdk-go/aws"
    10  	"github.com/aws/aws-sdk-go/service/sns"
    11  )
    12  
    13  // Mutable attributes
    14  var SNSAttributeMap = map[string]string{
    15  	"display_name":    "DisplayName",
    16  	"policy":          "Policy",
    17  	"delivery_policy": "DeliveryPolicy",
    18  }
    19  
    20  func resourceAwsSnsTopic() *schema.Resource {
    21  	return &schema.Resource{
    22  		Create: resourceAwsSnsTopicCreate,
    23  		Read:   resourceAwsSnsTopicRead,
    24  		Update: resourceAwsSnsTopicUpdate,
    25  		Delete: resourceAwsSnsTopicDelete,
    26  
    27  		Schema: map[string]*schema.Schema{
    28  			"name": &schema.Schema{
    29  				Type:     schema.TypeString,
    30  				Required: true,
    31  				ForceNew: true,
    32  			},
    33  			"display_name": &schema.Schema{
    34  				Type:     schema.TypeString,
    35  				Optional: true,
    36  				ForceNew: false,
    37  			},
    38  			"policy": &schema.Schema{
    39  				Type:     schema.TypeString,
    40  				Optional: true,
    41  				ForceNew: false,
    42  				Computed: true,
    43  			},
    44  			"delivery_policy": &schema.Schema{
    45  				Type:     schema.TypeString,
    46  				Optional: true,
    47  				ForceNew: false,
    48  			},
    49  			"arn": &schema.Schema{
    50  				Type:     schema.TypeString,
    51  				Computed: true,
    52  			},
    53  		},
    54  	}
    55  }
    56  
    57  func resourceAwsSnsTopicCreate(d *schema.ResourceData, meta interface{}) error {
    58  	snsconn := meta.(*AWSClient).snsconn
    59  
    60  	name := d.Get("name").(string)
    61  
    62  	log.Printf("[DEBUG] SNS create topic: %s", name)
    63  
    64  	req := &sns.CreateTopicInput{
    65  		Name: aws.String(name),
    66  	}
    67  
    68  	output, err := snsconn.CreateTopic(req)
    69  	if err != nil {
    70  		return fmt.Errorf("Error creating SNS topic: %s", err)
    71  	}
    72  
    73  	d.SetId(*output.TopicArn)
    74  
    75  	// Write the ARN to the 'arn' field for export
    76  	d.Set("arn", *output.TopicArn)
    77  
    78  	return resourceAwsSnsTopicUpdate(d, meta)
    79  }
    80  
    81  func resourceAwsSnsTopicUpdate(d *schema.ResourceData, meta interface{}) error {
    82  	snsconn := meta.(*AWSClient).snsconn
    83  
    84  	resource := *resourceAwsSnsTopic()
    85  
    86  	for k, _ := range resource.Schema {
    87  		if attrKey, ok := SNSAttributeMap[k]; ok {
    88  			if d.HasChange(k) {
    89  				log.Printf("[DEBUG] Updating %s", attrKey)
    90  				_, n := d.GetChange(k)
    91  				// Ignore an empty policy
    92  				if !(k == "policy" && n == "") {
    93  					// Make API call to update attributes
    94  					req := &sns.SetTopicAttributesInput{
    95  						TopicArn:       aws.String(d.Id()),
    96  						AttributeName:  aws.String(attrKey),
    97  						AttributeValue: aws.String(n.(string)),
    98  					}
    99  					snsconn.SetTopicAttributes(req)
   100  				}
   101  			}
   102  		}
   103  	}
   104  
   105  	return resourceAwsSnsTopicRead(d, meta)
   106  }
   107  
   108  func resourceAwsSnsTopicRead(d *schema.ResourceData, meta interface{}) error {
   109  	snsconn := meta.(*AWSClient).snsconn
   110  
   111  	attributeOutput, err := snsconn.GetTopicAttributes(&sns.GetTopicAttributesInput{
   112  		TopicArn: aws.String(d.Id()),
   113  	})
   114  
   115  	if err != nil {
   116  		return err
   117  	}
   118  
   119  	if attributeOutput.Attributes != nil && len(attributeOutput.Attributes) > 0 {
   120  		attrmap := attributeOutput.Attributes
   121  		resource := *resourceAwsSnsTopic()
   122  		// iKey = internal struct key, oKey = AWS Attribute Map key
   123  		for iKey, oKey := range SNSAttributeMap {
   124  			log.Printf("[DEBUG] Updating %s => %s", iKey, oKey)
   125  
   126  			if attrmap[oKey] != nil {
   127  				// Some of the fetched attributes are stateful properties such as
   128  				// the number of subscriptions, the owner, etc. skip those
   129  				if resource.Schema[iKey] != nil {
   130  					value := *attrmap[oKey]
   131  					log.Printf("[DEBUG] Updating %s => %s -> %s", iKey, oKey, value)
   132  					d.Set(iKey, *attrmap[oKey])
   133  				}
   134  			}
   135  		}
   136  	}
   137  
   138  	return nil
   139  }
   140  
   141  func resourceAwsSnsTopicDelete(d *schema.ResourceData, meta interface{}) error {
   142  	snsconn := meta.(*AWSClient).snsconn
   143  
   144  	log.Printf("[DEBUG] SNS Delete Topic: %s", d.Id())
   145  	_, err := snsconn.DeleteTopic(&sns.DeleteTopicInput{
   146  		TopicArn: aws.String(d.Id()),
   147  	})
   148  	if err != nil {
   149  		return err
   150  	}
   151  	return nil
   152  }