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