github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/google/resource_pubsub_topic.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 resourcePubsubTopic() *schema.Resource {
    11  	return &schema.Resource{
    12  		Create: resourcePubsubTopicCreate,
    13  		Read:   resourcePubsubTopicRead,
    14  		Delete: resourcePubsubTopicDelete,
    15  
    16  		Schema: map[string]*schema.Schema{
    17  			"name": &schema.Schema{
    18  				Type:     schema.TypeString,
    19  				Required: true,
    20  				ForceNew: true,
    21  			},
    22  
    23  			"project": &schema.Schema{
    24  				Type:     schema.TypeString,
    25  				Optional: true,
    26  				ForceNew: true,
    27  			},
    28  		},
    29  	}
    30  }
    31  
    32  func resourcePubsubTopicCreate(d *schema.ResourceData, meta interface{}) error {
    33  	config := meta.(*Config)
    34  
    35  	project, err := getProject(d, config)
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	name := fmt.Sprintf("projects/%s/topics/%s", project, d.Get("name").(string))
    41  	topic := &pubsub.Topic{}
    42  
    43  	call := config.clientPubsub.Projects.Topics.Create(name, topic)
    44  	res, err := call.Do()
    45  	if err != nil {
    46  		return err
    47  	}
    48  
    49  	d.SetId(res.Name)
    50  
    51  	return nil
    52  }
    53  
    54  func resourcePubsubTopicRead(d *schema.ResourceData, meta interface{}) error {
    55  	config := meta.(*Config)
    56  
    57  	name := d.Id()
    58  	call := config.clientPubsub.Projects.Topics.Get(name)
    59  	_, err := call.Do()
    60  	if err != nil {
    61  		return err
    62  	}
    63  
    64  	return nil
    65  }
    66  
    67  func resourcePubsubTopicDelete(d *schema.ResourceData, meta interface{}) error {
    68  	config := meta.(*Config)
    69  
    70  	name := d.Id()
    71  	call := config.clientPubsub.Projects.Topics.Delete(name)
    72  	_, err := call.Do()
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	return nil
    78  }