2015-10-28 17:55:50 +00:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-12-12 11:00:54 +00:00
|
|
|
|
2015-10-28 17:55:50 +00:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2015-12-11 17:59:13 +00:00
|
|
|
"google.golang.org/api/pubsub/v1"
|
2017-10-30 20:12:14 +00:00
|
|
|
"regexp"
|
2015-10-28 17:55:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func resourcePubsubSubscription() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourcePubsubSubscriptionCreate,
|
|
|
|
Read: resourcePubsubSubscriptionRead,
|
2017-10-03 20:14:51 +00:00
|
|
|
Update: resourcePubsubSubscriptionUpdate,
|
2015-10-28 17:55:50 +00:00
|
|
|
Delete: resourcePubsubSubscriptionDelete,
|
|
|
|
|
2017-09-26 20:44:13 +00:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: resourcePubsubSubscriptionStateImporter,
|
|
|
|
},
|
|
|
|
|
2015-10-28 17:55:50 +00:00
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-04-10 21:34:15 +00:00
|
|
|
"topic": &schema.Schema{
|
2017-09-26 20:44:13 +00:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
DiffSuppressFunc: compareSelfLinkOrResourceName,
|
2016-04-10 21:34:15 +00:00
|
|
|
},
|
|
|
|
|
2015-10-28 17:55:50 +00:00
|
|
|
"ack_deadline_seconds": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
2017-09-26 20:44:13 +00:00
|
|
|
Computed: true,
|
2015-10-28 17:55:50 +00:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-04-10 21:34:15 +00:00
|
|
|
"project": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2017-11-28 00:32:20 +00:00
|
|
|
Computed: true,
|
2016-04-10 21:34:15 +00:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2017-05-05 12:31:59 +00:00
|
|
|
"path": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2015-10-28 17:55:50 +00:00
|
|
|
"push_config": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
2017-09-26 20:44:13 +00:00
|
|
|
MaxItems: 1,
|
2015-12-11 17:59:13 +00:00
|
|
|
Elem: &schema.Resource{
|
2015-10-28 17:55:50 +00:00
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"attributes": &schema.Schema{
|
|
|
|
Type: schema.TypeMap,
|
|
|
|
Optional: true,
|
2018-01-12 18:09:06 +00:00
|
|
|
Elem: schema.TypeString,
|
2015-10-28 17:55:50 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
"push_endpoint": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
2017-09-26 20:44:13 +00:00
|
|
|
Required: true,
|
2015-10-28 17:55:50 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePubsubSubscriptionCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 16:59:57 +00:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-03-06 17:52:39 +00:00
|
|
|
name := getComputedSubscriptionName(project, d.Get("name").(string))
|
2017-10-30 20:12:14 +00:00
|
|
|
computed_topic_name := getComputedTopicName(project, d.Get("topic").(string))
|
2015-10-28 17:55:50 +00:00
|
|
|
|
|
|
|
// process optional parameters
|
|
|
|
var ackDeadlineSeconds int64
|
|
|
|
ackDeadlineSeconds = 10
|
|
|
|
if v, ok := d.GetOk("ack_deadline_seconds"); ok {
|
2016-02-12 11:41:20 +00:00
|
|
|
ackDeadlineSeconds = int64(v.(int))
|
2015-10-28 17:55:50 +00:00
|
|
|
}
|
|
|
|
|
2017-10-03 20:14:51 +00:00
|
|
|
subscription := &pubsub.Subscription{
|
|
|
|
AckDeadlineSeconds: ackDeadlineSeconds,
|
|
|
|
Topic: computed_topic_name,
|
|
|
|
PushConfig: expandPubsubSubscriptionPushConfig(d.Get("push_config").([]interface{})),
|
2015-10-28 17:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
call := config.clientPubsub.Projects.Subscriptions.Create(name, subscription)
|
|
|
|
res, err := call.Do()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-11 17:59:13 +00:00
|
|
|
|
2015-10-28 17:55:50 +00:00
|
|
|
d.SetId(res.Name)
|
|
|
|
|
2017-10-03 20:14:51 +00:00
|
|
|
return resourcePubsubSubscriptionRead(d, meta)
|
2015-10-28 17:55:50 +00:00
|
|
|
}
|
|
|
|
|
2018-03-06 17:52:39 +00:00
|
|
|
func getComputedTopicName(project, topic string) string {
|
2017-10-30 20:12:14 +00:00
|
|
|
match, _ := regexp.MatchString("projects\\/.*\\/topics\\/.*", topic)
|
|
|
|
if match {
|
2018-03-06 17:52:39 +00:00
|
|
|
return topic
|
2017-10-30 20:12:14 +00:00
|
|
|
}
|
2018-03-06 17:52:39 +00:00
|
|
|
return fmt.Sprintf("projects/%s/topics/%s", project, topic)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getComputedSubscriptionName(project, subscription string) string {
|
|
|
|
match, _ := regexp.MatchString("projects\\/.*\\/subscriptions\\/.*", subscription)
|
|
|
|
if match {
|
|
|
|
return subscription
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("projects/%s/subscriptions/%s", project, subscription)
|
2017-10-30 20:12:14 +00:00
|
|
|
}
|
|
|
|
|
2015-10-28 17:55:50 +00:00
|
|
|
func resourcePubsubSubscriptionRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
2015-12-11 17:59:13 +00:00
|
|
|
|
2017-11-28 00:32:20 +00:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-28 17:55:50 +00:00
|
|
|
name := d.Id()
|
2017-09-26 20:44:13 +00:00
|
|
|
subscription, err := config.clientPubsub.Projects.Subscriptions.Get(name).Do()
|
2015-10-28 17:55:50 +00:00
|
|
|
if err != nil {
|
2017-05-09 23:00:47 +00:00
|
|
|
return handleNotFoundError(err, d, fmt.Sprintf("Pubsub Subscription %q", name))
|
2015-10-28 17:55:50 +00:00
|
|
|
}
|
|
|
|
|
2017-09-26 20:44:13 +00:00
|
|
|
d.Set("name", GetResourceNameFromSelfLink(subscription.Name))
|
|
|
|
d.Set("topic", subscription.Topic)
|
|
|
|
d.Set("ack_deadline_seconds", subscription.AckDeadlineSeconds)
|
|
|
|
d.Set("path", subscription.Name)
|
2017-10-03 20:14:51 +00:00
|
|
|
d.Set("push_config", flattenPubsubSubscriptionPushConfig(subscription.PushConfig))
|
2017-11-28 00:32:20 +00:00
|
|
|
d.Set("project", project)
|
2017-10-03 20:14:51 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePubsubSubscriptionUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
|
|
|
d.Partial(true)
|
|
|
|
|
|
|
|
if d.HasChange("push_config") {
|
|
|
|
_, err := config.clientPubsub.Projects.Subscriptions.ModifyPushConfig(d.Id(), &pubsub.ModifyPushConfigRequest{
|
|
|
|
PushConfig: expandPubsubSubscriptionPushConfig(d.Get("push_config").([]interface{})),
|
|
|
|
}).Do()
|
|
|
|
|
|
|
|
if err != nil {
|
2018-03-06 17:52:39 +00:00
|
|
|
return fmt.Errorf("Error updating subscription %q: %s", d.Get("name"), err)
|
2017-10-03 20:14:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Partial(false)
|
2017-09-26 20:44:13 +00:00
|
|
|
|
2015-10-28 17:55:50 +00:00
|
|
|
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 17:59:13 +00:00
|
|
|
return err
|
2015-10-28 17:55:50 +00:00
|
|
|
}
|
2015-12-11 17:59:13 +00:00
|
|
|
|
2015-10-28 17:55:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-09-26 20:44:13 +00:00
|
|
|
|
|
|
|
func resourcePubsubSubscriptionStateImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
id := fmt.Sprintf("projects/%s/subscriptions/%s", project, d.Id())
|
|
|
|
|
|
|
|
d.SetId(id)
|
|
|
|
|
|
|
|
return []*schema.ResourceData{d}, nil
|
|
|
|
}
|
|
|
|
|
2017-10-03 20:14:51 +00:00
|
|
|
func flattenPubsubSubscriptionPushConfig(pushConfig *pubsub.PushConfig) []map[string]interface{} {
|
2017-09-26 20:44:13 +00:00
|
|
|
configs := make([]map[string]interface{}, 0, 1)
|
|
|
|
|
|
|
|
if pushConfig == nil || len(pushConfig.PushEndpoint) == 0 {
|
|
|
|
return configs
|
|
|
|
}
|
|
|
|
|
|
|
|
configs = append(configs, map[string]interface{}{
|
|
|
|
"push_endpoint": pushConfig.PushEndpoint,
|
|
|
|
"attributes": pushConfig.Attributes,
|
|
|
|
})
|
|
|
|
|
|
|
|
return configs
|
|
|
|
}
|
2017-10-03 20:14:51 +00:00
|
|
|
|
|
|
|
func expandPubsubSubscriptionPushConfig(configured []interface{}) *pubsub.PushConfig {
|
|
|
|
if len(configured) == 0 {
|
|
|
|
// An empty `pushConfig` indicates that the Pub/Sub system should stop pushing messages
|
|
|
|
// from the given subscription and allow messages to be pulled and acknowledged.
|
|
|
|
return &pubsub.PushConfig{}
|
|
|
|
}
|
|
|
|
|
|
|
|
pushConfig := configured[0].(map[string]interface{})
|
|
|
|
return &pubsub.PushConfig{
|
|
|
|
PushEndpoint: pushConfig["push_endpoint"].(string),
|
|
|
|
Attributes: convertStringMap(pushConfig["attributes"].(map[string]interface{})),
|
|
|
|
}
|
|
|
|
}
|