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 resourcePubsubTopic() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourcePubsubTopicCreate,
|
|
|
|
Read: resourcePubsubTopicRead,
|
|
|
|
Delete: resourcePubsubTopicDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2016-04-10 18:59:57 +02:00
|
|
|
|
|
|
|
"project": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2015-10-28 18:55:50 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePubsubTopicCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
name := fmt.Sprintf("projects/%s/topics/%s", project, d.Get("name").(string))
|
2015-10-28 18:55:50 +01:00
|
|
|
topic := &pubsub.Topic{}
|
|
|
|
|
|
|
|
call := config.clientPubsub.Projects.Topics.Create(name, topic)
|
|
|
|
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 resourcePubsubTopicRead(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.Topics.Get(name)
|
|
|
|
_, err := call.Do()
|
|
|
|
if err != nil {
|
2017-05-10 01:00:47 +02:00
|
|
|
return handleNotFoundError(err, d, fmt.Sprintf("Pubsub Topic %q", name))
|
2015-10-28 18:55:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePubsubTopicDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
|
|
|
name := d.Id()
|
|
|
|
call := config.clientPubsub.Projects.Topics.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
|
|
|
|
}
|