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