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