github.com/leeprovoost/terraform@v0.6.10-0.20160119085442-96f3f76118e7/builtin/providers/google/resource_pubsub_subscription.go (about) 1 package google 2 3 import ( 4 "fmt" 5 6 "github.com/hashicorp/terraform/helper/schema" 7 "google.golang.org/api/pubsub/v1" 8 ) 9 10 func resourcePubsubSubscription() *schema.Resource { 11 return &schema.Resource{ 12 Create: resourcePubsubSubscriptionCreate, 13 Read: resourcePubsubSubscriptionRead, 14 Delete: resourcePubsubSubscriptionDelete, 15 16 Schema: map[string]*schema.Schema{ 17 "name": &schema.Schema{ 18 Type: schema.TypeString, 19 Required: true, 20 ForceNew: true, 21 }, 22 23 "ack_deadline_seconds": &schema.Schema{ 24 Type: schema.TypeInt, 25 Optional: true, 26 ForceNew: true, 27 }, 28 29 "push_config": &schema.Schema{ 30 Type: schema.TypeList, 31 Optional: true, 32 ForceNew: true, 33 Elem: &schema.Resource{ 34 Schema: map[string]*schema.Schema{ 35 "attributes": &schema.Schema{ 36 Type: schema.TypeMap, 37 Optional: true, 38 ForceNew: true, 39 Elem: schema.TypeString, 40 }, 41 42 "push_endpoint": &schema.Schema{ 43 Type: schema.TypeString, 44 Optional: true, 45 ForceNew: true, 46 }, 47 }, 48 }, 49 }, 50 51 "topic": &schema.Schema{ 52 Type: schema.TypeString, 53 Required: true, 54 ForceNew: true, 55 }, 56 }, 57 } 58 } 59 60 func cleanAdditionalArgs(args map[string]interface{}) map[string]string { 61 cleaned_args := make(map[string]string) 62 for k, v := range args { 63 cleaned_args[k] = v.(string) 64 } 65 return cleaned_args 66 } 67 68 func resourcePubsubSubscriptionCreate(d *schema.ResourceData, meta interface{}) error { 69 config := meta.(*Config) 70 71 name := fmt.Sprintf("projects/%s/subscriptions/%s", config.Project, d.Get("name").(string)) 72 computed_topic_name := fmt.Sprintf("projects/%s/topics/%s", config.Project, d.Get("topic").(string)) 73 74 // process optional parameters 75 var ackDeadlineSeconds int64 76 ackDeadlineSeconds = 10 77 if v, ok := d.GetOk("ack_deadline_seconds"); ok { 78 ackDeadlineSeconds = v.(int64) 79 } 80 81 var subscription *pubsub.Subscription 82 if v, ok := d.GetOk("push_config"); ok { 83 push_configs := v.([]interface{}) 84 85 if len(push_configs) > 1 { 86 return fmt.Errorf("At most one PushConfig is allowed per subscription!") 87 } 88 89 push_config := push_configs[0].(map[string]interface{}) 90 attributes := push_config["attributes"].(map[string]interface{}) 91 attributesClean := cleanAdditionalArgs(attributes) 92 pushConfig := &pubsub.PushConfig{Attributes: attributesClean, PushEndpoint: push_config["push_endpoint"].(string)} 93 subscription = &pubsub.Subscription{AckDeadlineSeconds: ackDeadlineSeconds, Topic: computed_topic_name, PushConfig: pushConfig} 94 } else { 95 subscription = &pubsub.Subscription{AckDeadlineSeconds: ackDeadlineSeconds, Topic: computed_topic_name} 96 } 97 98 call := config.clientPubsub.Projects.Subscriptions.Create(name, subscription) 99 res, err := call.Do() 100 if err != nil { 101 return err 102 } 103 104 d.SetId(res.Name) 105 106 return nil 107 } 108 109 func resourcePubsubSubscriptionRead(d *schema.ResourceData, meta interface{}) error { 110 config := meta.(*Config) 111 112 name := d.Id() 113 call := config.clientPubsub.Projects.Subscriptions.Get(name) 114 _, err := call.Do() 115 if err != nil { 116 return err 117 } 118 119 return nil 120 } 121 122 func resourcePubsubSubscriptionDelete(d *schema.ResourceData, meta interface{}) error { 123 config := meta.(*Config) 124 125 name := d.Id() 126 call := config.clientPubsub.Projects.Subscriptions.Delete(name) 127 _, err := call.Do() 128 if err != nil { 129 return err 130 } 131 132 return nil 133 }