github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/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 "topic": &schema.Schema{ 24 Type: schema.TypeString, 25 Required: true, 26 ForceNew: true, 27 }, 28 29 "ack_deadline_seconds": &schema.Schema{ 30 Type: schema.TypeInt, 31 Optional: true, 32 ForceNew: true, 33 }, 34 35 "project": &schema.Schema{ 36 Type: schema.TypeString, 37 Optional: true, 38 ForceNew: true, 39 }, 40 41 "path": &schema.Schema{ 42 Type: schema.TypeString, 43 Computed: true, 44 }, 45 46 "push_config": &schema.Schema{ 47 Type: schema.TypeList, 48 Optional: true, 49 ForceNew: true, 50 Elem: &schema.Resource{ 51 Schema: map[string]*schema.Schema{ 52 "attributes": &schema.Schema{ 53 Type: schema.TypeMap, 54 Optional: true, 55 ForceNew: true, 56 Elem: schema.TypeString, 57 }, 58 59 "push_endpoint": &schema.Schema{ 60 Type: schema.TypeString, 61 Optional: true, 62 ForceNew: true, 63 }, 64 }, 65 }, 66 }, 67 }, 68 } 69 } 70 71 func cleanAdditionalArgs(args map[string]interface{}) map[string]string { 72 cleaned_args := make(map[string]string) 73 for k, v := range args { 74 cleaned_args[k] = v.(string) 75 } 76 return cleaned_args 77 } 78 79 func resourcePubsubSubscriptionCreate(d *schema.ResourceData, meta interface{}) error { 80 config := meta.(*Config) 81 82 project, err := getProject(d, config) 83 if err != nil { 84 return err 85 } 86 87 name := fmt.Sprintf("projects/%s/subscriptions/%s", project, d.Get("name").(string)) 88 computed_topic_name := fmt.Sprintf("projects/%s/topics/%s", project, d.Get("topic").(string)) 89 90 // process optional parameters 91 var ackDeadlineSeconds int64 92 ackDeadlineSeconds = 10 93 if v, ok := d.GetOk("ack_deadline_seconds"); ok { 94 ackDeadlineSeconds = int64(v.(int)) 95 } 96 97 var subscription *pubsub.Subscription 98 if v, ok := d.GetOk("push_config"); ok { 99 push_configs := v.([]interface{}) 100 101 if len(push_configs) > 1 { 102 return fmt.Errorf("At most one PushConfig is allowed per subscription!") 103 } 104 105 push_config := push_configs[0].(map[string]interface{}) 106 attributes := push_config["attributes"].(map[string]interface{}) 107 attributesClean := cleanAdditionalArgs(attributes) 108 pushConfig := &pubsub.PushConfig{Attributes: attributesClean, PushEndpoint: push_config["push_endpoint"].(string)} 109 subscription = &pubsub.Subscription{AckDeadlineSeconds: ackDeadlineSeconds, Topic: computed_topic_name, PushConfig: pushConfig} 110 } else { 111 subscription = &pubsub.Subscription{AckDeadlineSeconds: ackDeadlineSeconds, Topic: computed_topic_name} 112 } 113 114 call := config.clientPubsub.Projects.Subscriptions.Create(name, subscription) 115 res, err := call.Do() 116 if err != nil { 117 return err 118 } 119 120 d.SetId(res.Name) 121 d.Set("path", name) 122 123 return nil 124 } 125 126 func resourcePubsubSubscriptionRead(d *schema.ResourceData, meta interface{}) error { 127 config := meta.(*Config) 128 129 name := d.Id() 130 call := config.clientPubsub.Projects.Subscriptions.Get(name) 131 _, err := call.Do() 132 if err != nil { 133 return err 134 } 135 136 return nil 137 } 138 139 func resourcePubsubSubscriptionDelete(d *schema.ResourceData, meta interface{}) error { 140 config := meta.(*Config) 141 142 name := d.Id() 143 call := config.clientPubsub.Projects.Subscriptions.Delete(name) 144 _, err := call.Do() 145 if err != nil { 146 return err 147 } 148 149 return nil 150 }