Skip to content

Commit d6ae527

Browse files
committed
Merge pull request hashicorp#3671 from 22Acacia/google-pubsub-clean
Google Cloud: PubSub Topic and Subscription resources
2 parents 4343e60 + 6f7ef2f commit d6ae527

9 files changed

Lines changed: 442 additions & 0 deletions

File tree

builtin/providers/google/config.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"google.golang.org/api/dns/v1"
1919
"google.golang.org/api/sqladmin/v1beta4"
2020
"google.golang.org/api/storage/v1"
21+
"google.golang.org/api/pubsub/v1"
2122
)
2223

2324
// Config is the configuration structure used to instantiate the Google
@@ -32,6 +33,7 @@ type Config struct {
3233
clientDns *dns.Service
3334
clientStorage *storage.Service
3435
clientSqlAdmin *sqladmin.Service
36+
clientPubsub *pubsub.Service
3537
}
3638

3739
func (c *Config) loadAndValidate() error {
@@ -128,6 +130,13 @@ func (c *Config) loadAndValidate() error {
128130
}
129131
c.clientSqlAdmin.UserAgent = userAgent
130132

133+
log.Printf("[INFO] Instatiating Google Pubsub Client...")
134+
c.clientPubsub, err = pubsub.New(client)
135+
if err != nil {
136+
return err
137+
}
138+
c.clientPubsub.UserAgent = userAgent
139+
131140
return nil
132141
}
133142

builtin/providers/google/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ func Provider() terraform.ResourceProvider {
7070
"google_dns_record_set": resourceDnsRecordSet(),
7171
"google_sql_database": resourceSqlDatabase(),
7272
"google_sql_database_instance": resourceSqlDatabaseInstance(),
73+
"google_pubsub_topic": resourcePubsubTopic(),
74+
"google_pubsub_subscription": resourcePubsubSubscription(),
7375
"google_storage_bucket": resourceStorageBucket(),
7476
"google_storage_bucket_acl": resourceStorageBucketAcl(),
7577
"google_storage_bucket_object": resourceStorageBucketObject(),
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package google
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform/helper/resource"
8+
"github.com/hashicorp/terraform/terraform"
9+
)
10+
11+
func TestAccPubsubSubscriptionCreate(t *testing.T) {
12+
13+
resource.Test(t, resource.TestCase{
14+
PreCheck: func() { testAccPreCheck(t) },
15+
Providers: testAccProviders,
16+
CheckDestroy: testAccCheckPubsubSubscriptionDestroy,
17+
Steps: []resource.TestStep{
18+
resource.TestStep{
19+
Config: testAccPubsubSubscription,
20+
Check: resource.ComposeTestCheckFunc(
21+
testAccPubsubSubscriptionExists(
22+
"google_pubsub_subscription.foobar_sub"),
23+
),
24+
},
25+
},
26+
})
27+
}
28+
29+
func testAccCheckPubsubSubscriptionDestroy(s *terraform.State) error {
30+
for _, rs := range s.RootModule().Resources {
31+
if rs.Type != "google_pubsub_subscription" {
32+
continue
33+
}
34+
35+
config := testAccProvider.Meta().(*Config)
36+
_, err := config.clientPubsub.Projects.Subscriptions.Get(rs.Primary.ID).Do()
37+
if err != nil {
38+
fmt.Errorf("Subscription still present")
39+
}
40+
}
41+
42+
return nil
43+
}
44+
45+
func testAccPubsubSubscriptionExists(n string) resource.TestCheckFunc {
46+
return func(s *terraform.State) error {
47+
rs, ok := s.RootModule().Resources[n]
48+
if !ok {
49+
return fmt.Errorf("Not found: %s", n)
50+
}
51+
52+
if rs.Primary.ID == "" {
53+
return fmt.Errorf("No ID is set")
54+
}
55+
config := testAccProvider.Meta().(*Config)
56+
_, err := config.clientPubsub.Projects.Subscriptions.Get(rs.Primary.ID).Do()
57+
if err != nil {
58+
fmt.Errorf("Subscription still present")
59+
}
60+
61+
return nil
62+
}
63+
}
64+
65+
const testAccPubsubSubscription = `
66+
resource "google_pubsub_topic" "foobar_sub" {
67+
name = "foobar_sub"
68+
}
69+
70+
resource "google_pubsub_subscription" "foobar_sub" {
71+
name = "foobar_sub"
72+
topic = "${google_pubsub_topic.foobar_sub.name}"
73+
}`
74+
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 resourcePubsubTopic() *schema.Resource {
10+
return &schema.Resource{
11+
Create: resourcePubsubTopicCreate,
12+
Read: resourcePubsubTopicRead,
13+
Delete: resourcePubsubTopicDelete,
14+
15+
Schema: map[string]*schema.Schema{
16+
"name": &schema.Schema{
17+
Type: schema.TypeString,
18+
Required: true,
19+
ForceNew: true,
20+
},
21+
22+
},
23+
}
24+
}
25+
26+
func resourcePubsubTopicCreate(d *schema.ResourceData, meta interface{}) error {
27+
config := meta.(*Config)
28+
29+
name := fmt.Sprintf("projects/%s/topics/%s", config.Project, d.Get("name").(string))
30+
topic := &pubsub.Topic{}
31+
32+
call := config.clientPubsub.Projects.Topics.Create(name, topic)
33+
res, err := call.Do()
34+
if err != nil {
35+
return err
36+
}
37+
38+
d.SetId(res.Name)
39+
40+
return nil
41+
}
42+
43+
func resourcePubsubTopicRead(d *schema.ResourceData, meta interface{}) error {
44+
config := meta.(*Config)
45+
46+
name := d.Id()
47+
call := config.clientPubsub.Projects.Topics.Get(name)
48+
_, err := call.Do()
49+
if err != nil {
50+
return err
51+
}
52+
53+
return nil
54+
}
55+
56+
57+
func resourcePubsubTopicDelete(d *schema.ResourceData, meta interface{}) error {
58+
config := meta.(*Config)
59+
60+
name := d.Id()
61+
call := config.clientPubsub.Projects.Topics.Delete(name)
62+
_, err := call.Do()
63+
if err != nil {
64+
return err
65+
}
66+
67+
return nil
68+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package google
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform/helper/resource"
8+
"github.com/hashicorp/terraform/terraform"
9+
)
10+
11+
func TestAccPubsubTopicCreate(t *testing.T) {
12+
13+
resource.Test(t, resource.TestCase{
14+
PreCheck: func() { testAccPreCheck(t) },
15+
Providers: testAccProviders,
16+
CheckDestroy: testAccCheckPubsubTopicDestroy,
17+
Steps: []resource.TestStep{
18+
resource.TestStep{
19+
Config: testAccPubsubTopic,
20+
Check: resource.ComposeTestCheckFunc(
21+
testAccPubsubTopicExists(
22+
"google_pubsub_topic.foobar"),
23+
),
24+
},
25+
},
26+
})
27+
}
28+
29+
func testAccCheckPubsubTopicDestroy(s *terraform.State) error {
30+
for _, rs := range s.RootModule().Resources {
31+
if rs.Type != "google_pubsub_topic" {
32+
continue
33+
}
34+
35+
config := testAccProvider.Meta().(*Config)
36+
_, err := config.clientPubsub.Projects.Topics.Get(rs.Primary.ID).Do()
37+
if err != nil {
38+
fmt.Errorf("Topic still present")
39+
}
40+
}
41+
42+
return nil
43+
}
44+
45+
func testAccPubsubTopicExists(n string) resource.TestCheckFunc {
46+
return func(s *terraform.State) error {
47+
rs, ok := s.RootModule().Resources[n]
48+
if !ok {
49+
return fmt.Errorf("Not found: %s", n)
50+
}
51+
52+
if rs.Primary.ID == "" {
53+
return fmt.Errorf("No ID is set")
54+
}
55+
config := testAccProvider.Meta().(*Config)
56+
_, err := config.clientPubsub.Projects.Topics.Get(rs.Primary.ID).Do()
57+
if err != nil {
58+
fmt.Errorf("Topic still present")
59+
}
60+
61+
return nil
62+
}
63+
}
64+
65+
const testAccPubsubTopic = `
66+
resource "google_pubsub_topic" "foobar" {
67+
name = "foobar"
68+
}`

0 commit comments

Comments
 (0)