github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/google/resource_pubsub_subscription.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"google.golang.org/api/pubsub/v1"
     6  	"github.com/hashicorp/terraform/helper/schema"
     7  )
     8  
     9  func resourcePubsubSubscription() *schema.Resource {
    10  	return &schema.Resource{
    11  		Create: resourcePubsubSubscriptionCreate,
    12  		Read:   resourcePubsubSubscriptionRead,
    13  		Delete: resourcePubsubSubscriptionDelete,
    14  
    15  		Schema: map[string]*schema.Schema{
    16  			"name": &schema.Schema{
    17  				Type:     schema.TypeString,
    18  				Required: true,
    19  				ForceNew: true,
    20  			},
    21  
    22  			"ack_deadline_seconds": &schema.Schema{
    23  				Type:     schema.TypeInt,
    24  				Optional: true,
    25  				ForceNew: true,
    26  			},
    27  
    28  			"push_config": &schema.Schema{
    29  				Type:     schema.TypeList,
    30  				Optional: true,
    31  				ForceNew: true,
    32  				Elem:     &schema.Resource{
    33  					Schema: map[string]*schema.Schema{
    34  						"attributes": &schema.Schema{
    35  							Type:     schema.TypeMap,
    36  							Optional: true,
    37  							ForceNew: true,
    38  							Elem:     schema.TypeString,
    39  						},
    40  
    41  						"push_endpoint": &schema.Schema{
    42  							Type:     schema.TypeString,
    43  							Optional: true,
    44  							ForceNew: true,
    45  						},
    46  					},
    47  				},
    48  			},
    49  
    50  			"topic": &schema.Schema{
    51  				Type:     schema.TypeString,
    52  				Required: true,
    53  				ForceNew: true,
    54  			},
    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  
   123  func resourcePubsubSubscriptionDelete(d *schema.ResourceData, meta interface{}) error {
   124  	config := meta.(*Config)
   125  
   126  	name := d.Id()
   127  	call := config.clientPubsub.Projects.Subscriptions.Delete(name)
   128  	_, err := call.Do()
   129  	if err != nil {
   130  		return err 
   131  	}
   132  	
   133  	return nil
   134  }