github.com/armen/terraform@v0.5.2-0.20150529052519-caa8117a08f1/builtin/providers/aws/resource_aws_sns_topic_subscription.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 14 func resourceAwsSnsTopicSubscription() *schema.Resource { 15 return &schema.Resource{ 16 Create: resourceAwsSnsTopicSubscriptionCreate, 17 Read: resourceAwsSnsTopicSubscriptionRead, 18 Update: resourceAwsSnsTopicSubscriptionUpdate, 19 Delete: resourceAwsSnsTopicSubscriptionDelete, 20 21 Schema: map[string]*schema.Schema{ 22 "protocol": &schema.Schema{ 23 Type: schema.TypeString, 24 Required: true, 25 ForceNew: false, 26 }, 27 "endpoint": &schema.Schema{ 28 Type: schema.TypeString, 29 Required: true, 30 ForceNew: false, 31 }, 32 "topic_arn": &schema.Schema{ 33 Type: schema.TypeString, 34 Required: true, 35 ForceNew: false, 36 }, 37 "delivery_policy": &schema.Schema{ 38 Type: schema.TypeString, 39 Optional: true, 40 ForceNew: false, 41 }, 42 "raw_message_delivery": &schema.Schema{ 43 Type: schema.TypeBool, 44 Optional: true, 45 ForceNew: false, 46 Default: false, 47 }, 48 "arn": &schema.Schema{ 49 Type: schema.TypeString, 50 Computed: true, 51 }, 52 }, 53 } 54 } 55 56 func resourceAwsSnsTopicSubscriptionCreate(d *schema.ResourceData, meta interface{}) error { 57 snsconn := meta.(*AWSClient).snsconn 58 59 if(d.Get("protocol") == "email") { 60 return fmt.Errorf("Email endpoints are not supported!") 61 } 62 63 output, err := subscribeToSNSTopic(d, snsconn) 64 65 if err != nil { 66 return err 67 } 68 69 log.Printf("New subscription ARN: %s", *output.SubscriptionARN) 70 d.SetId(*output.SubscriptionARN) 71 72 // Write the ARN to the 'arn' field for export 73 d.Set("arn", *output.SubscriptionARN) 74 75 return resourceAwsSnsTopicSubscriptionUpdate(d, meta) 76 } 77 78 func resourceAwsSnsTopicSubscriptionUpdate(d *schema.ResourceData, meta interface{}) error { 79 snsconn := meta.(*AWSClient).snsconn 80 81 // If any changes happened, un-subscribe and re-subscribe 82 if d.HasChange("protocol") || d.HasChange("endpoint") || d.HasChange("topic_arn") { 83 log.Printf("[DEBUG] Updating subscription %s", d.Id()) 84 // Unsubscribe 85 _, err := snsconn.Unsubscribe(&sns.UnsubscribeInput{ 86 SubscriptionARN: aws.String(d.Id()), 87 }) 88 89 if err != nil { 90 return fmt.Errorf("Error unsubscribing from SNS topic: %s", err) 91 } 92 93 // Re-subscribe and set id 94 output, err := subscribeToSNSTopic(d, snsconn) 95 d.SetId(*output.SubscriptionARN) 96 97 } 98 99 if d.HasChange("raw_message_delivery") { 100 _, n := d.GetChange("raw_message_delivery") 101 102 attrValue := "false" 103 104 if n.(bool) { 105 attrValue = "true" 106 } 107 108 req := &sns.SetSubscriptionAttributesInput{ 109 SubscriptionARN: aws.String(d.Id()), 110 AttributeName: aws.String("RawMessageDelivery"), 111 AttributeValue: aws.String(attrValue), 112 } 113 _, err := snsconn.SetSubscriptionAttributes(req) 114 115 if err != nil { 116 return fmt.Errorf("Unable to set raw message delivery attribute on subscription") 117 } 118 } 119 120 return resourceAwsSnsTopicSubscriptionRead(d, meta) 121 } 122 123 func resourceAwsSnsTopicSubscriptionRead(d *schema.ResourceData, meta interface{}) error { 124 snsconn := meta.(*AWSClient).snsconn 125 126 log.Printf("[DEBUG] Loading subscription %s", d.Id()) 127 128 attributeOutput, err := snsconn.GetSubscriptionAttributes(&sns.GetSubscriptionAttributesInput{ 129 SubscriptionARN: aws.String(d.Id()), 130 }) 131 if err != nil { 132 return err 133 } 134 135 if attributeOutput.Attributes != nil && len(*attributeOutput.Attributes) > 0 { 136 attrHash := *attributeOutput.Attributes 137 log.Printf("[DEBUG] raw message delivery: %s", *attrHash["RawMessageDelivery"]) 138 if *attrHash["RawMessageDelivery"] == "true" { 139 d.Set("raw_message_delivery", true) 140 } else { 141 d.Set("raw_message_delivery", false) 142 } 143 } 144 145 return nil 146 } 147 148 func resourceAwsSnsTopicSubscriptionDelete(d *schema.ResourceData, meta interface{}) error { 149 snsconn := meta.(*AWSClient).snsconn 150 151 log.Printf("[DEBUG] SNS delete topic subscription: %s", d.Id()) 152 _, err := snsconn.Unsubscribe(&sns.UnsubscribeInput{ 153 SubscriptionARN: aws.String(d.Id()), 154 }) 155 if err != nil { 156 return err 157 } 158 return nil 159 } 160 161 func subscribeToSNSTopic(d *schema.ResourceData, snsconn *sns.SNS) (output *sns.SubscribeOutput, err error) { 162 protocol := d.Get("protocol").(string) 163 endpoint := d.Get("endpoint").(string) 164 topic_arn := d.Get("topic_arn").(string) 165 166 log.Printf("[DEBUG] SNS create topic subscription: %s (%s) @ '%s'", endpoint, protocol, topic_arn) 167 168 req := &sns.SubscribeInput{ 169 Protocol: aws.String(protocol), 170 Endpoint: aws.String(endpoint), 171 TopicARN: aws.String(topic_arn), 172 } 173 174 output, err = snsconn.Subscribe(req) 175 if err != nil { 176 return nil, fmt.Errorf("Error creating SNS topic: %s", err) 177 } 178 179 log.Printf("[DEBUG] Created new subscription!") 180 return output, nil 181 }