2015-10-28 18:55:50 +01:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-12-12 12:00:54 +01:00
|
|
|
|
2015-10-28 18:55:50 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2015-12-11 18:59:13 +01:00
|
|
|
"google.golang.org/api/pubsub/v1"
|
2015-10-28 18:55:50 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func resourcePubsubSubscription() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourcePubsubSubscriptionCreate,
|
|
|
|
Read: resourcePubsubSubscriptionRead,
|
|
|
|
Delete: resourcePubsubSubscriptionDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"ack_deadline_seconds": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"push_config": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2015-12-11 18:59:13 +01:00
|
|
|
Elem: &schema.Resource{
|
2015-10-28 18:55:50 +01:00
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"attributes": &schema.Schema{
|
|
|
|
Type: schema.TypeMap,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: schema.TypeString,
|
|
|
|
},
|
|
|
|
|
|
|
|
"push_endpoint": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"topic": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanAdditionalArgs(args map[string]interface{}) map[string]string {
|
|
|
|
cleaned_args := make(map[string]string)
|
2015-12-11 18:59:13 +01:00
|
|
|
for k, v := range args {
|
2015-10-28 18:55:50 +01:00
|
|
|
cleaned_args[k] = v.(string)
|
|
|
|
}
|
|
|
|
return cleaned_args
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePubsubSubscriptionCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
|
|
|
name := fmt.Sprintf("projects/%s/subscriptions/%s", config.Project, d.Get("name").(string))
|
|
|
|
computed_topic_name := fmt.Sprintf("projects/%s/topics/%s", config.Project, d.Get("topic").(string))
|
|
|
|
|
|
|
|
// process optional parameters
|
|
|
|
var ackDeadlineSeconds int64
|
|
|
|
ackDeadlineSeconds = 10
|
|
|
|
if v, ok := d.GetOk("ack_deadline_seconds"); ok {
|
2016-02-12 12:41:20 +01:00
|
|
|
ackDeadlineSeconds = int64(v.(int))
|
2015-10-28 18:55:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var subscription *pubsub.Subscription
|
|
|
|
if v, ok := d.GetOk("push_config"); ok {
|
|
|
|
push_configs := v.([]interface{})
|
|
|
|
|
|
|
|
if len(push_configs) > 1 {
|
|
|
|
return fmt.Errorf("At most one PushConfig is allowed per subscription!")
|
|
|
|
}
|
|
|
|
|
|
|
|
push_config := push_configs[0].(map[string]interface{})
|
|
|
|
attributes := push_config["attributes"].(map[string]interface{})
|
|
|
|
attributesClean := cleanAdditionalArgs(attributes)
|
|
|
|
pushConfig := &pubsub.PushConfig{Attributes: attributesClean, PushEndpoint: push_config["push_endpoint"].(string)}
|
|
|
|
subscription = &pubsub.Subscription{AckDeadlineSeconds: ackDeadlineSeconds, Topic: computed_topic_name, PushConfig: pushConfig}
|
2015-12-11 18:59:13 +01:00
|
|
|
} else {
|
2015-10-28 18:55:50 +01:00
|
|
|
subscription = &pubsub.Subscription{AckDeadlineSeconds: ackDeadlineSeconds, Topic: computed_topic_name}
|
|
|
|
}
|
|
|
|
|
|
|
|
call := config.clientPubsub.Projects.Subscriptions.Create(name, subscription)
|
|
|
|
res, err := call.Do()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-11 18:59:13 +01:00
|
|
|
|
2015-10-28 18:55:50 +01:00
|
|
|
d.SetId(res.Name)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePubsubSubscriptionRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
2015-12-11 18:59:13 +01:00
|
|
|
|
2015-10-28 18:55:50 +01:00
|
|
|
name := d.Id()
|
|
|
|
call := config.clientPubsub.Projects.Subscriptions.Get(name)
|
|
|
|
_, err := call.Do()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePubsubSubscriptionDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
|
|
|
name := d.Id()
|
|
|
|
call := config.clientPubsub.Projects.Subscriptions.Delete(name)
|
|
|
|
_, err := call.Do()
|
|
|
|
if err != nil {
|
2015-12-11 18:59:13 +01:00
|
|
|
return err
|
2015-10-28 18:55:50 +01:00
|
|
|
}
|
2015-12-11 18:59:13 +01:00
|
|
|
|
2015-10-28 18:55:50 +01:00
|
|
|
return nil
|
|
|
|
}
|