-
Notifications
You must be signed in to change notification settings - Fork 9.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
golang pubsub SDK has been released. moved topics/subscriptions to us…
…e that Conflicts: builtin/providers/google/provider.go builtin/providers/google/resource_subscription.go builtin/providers/google/resource_subscription_test.go golang pubsub SDK has been released. moved topics/subscriptions to use that Conflicts: builtin/providers/google/provider.go builtin/providers/google/resource_subscription.go builtin/providers/google/resource_subscription_test.go file renames and add documentation files remove typo'd merge and type file move add to index page as well only need to define that once remove topic_computed schema value I think this was used at one point but is no longer. away. cleanup typo adds a couple more config values - ackDeadlineSeconds: number of seconds to wait for an ack - pushAttributes: attributes of a push subscription - pushEndpoint: target for a push subscription rearrange to better match current conventions respond to all of the comments
- Loading branch information
Showing
9 changed files
with
442 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
builtin/providers/google/resource_pubsub_subscription.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"google.golang.org/api/pubsub/v1" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourcePubsubSubscription() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourcePubsubSubscriptionCreate, | ||
Read: resourcePubsubSubscriptionRead, | ||
Delete: resourcePubsubSubscriptionDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"ack_deadline_seconds": &schema.Schema{ | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"push_config": &schema.Schema{ | ||
Type: schema.TypeList, | ||
Optional: true, | ||
ForceNew: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"attributes": &schema.Schema{ | ||
Type: schema.TypeMap, | ||
Optional: true, | ||
ForceNew: true, | ||
Elem: schema.TypeString, | ||
}, | ||
|
||
"push_endpoint": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
"topic": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
}, | ||
} | ||
} | ||
|
||
func cleanAdditionalArgs(args map[string]interface{}) map[string]string { | ||
cleaned_args := make(map[string]string) | ||
for k,v := range args { | ||
cleaned_args[k] = v.(string) | ||
} | ||
return cleaned_args | ||
} | ||
|
||
func resourcePubsubSubscriptionCreate(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
name := fmt.Sprintf("projects/%s/subscriptions/%s", config.Project, d.Get("name").(string)) | ||
computed_topic_name := fmt.Sprintf("projects/%s/topics/%s", config.Project, d.Get("topic").(string)) | ||
|
||
// process optional parameters | ||
var ackDeadlineSeconds int64 | ||
ackDeadlineSeconds = 10 | ||
if v, ok := d.GetOk("ack_deadline_seconds"); ok { | ||
ackDeadlineSeconds = v.(int64) | ||
} | ||
|
||
var subscription *pubsub.Subscription | ||
if v, ok := d.GetOk("push_config"); ok { | ||
push_configs := v.([]interface{}) | ||
|
||
if len(push_configs) > 1 { | ||
return fmt.Errorf("At most one PushConfig is allowed per subscription!") | ||
} | ||
|
||
push_config := push_configs[0].(map[string]interface{}) | ||
attributes := push_config["attributes"].(map[string]interface{}) | ||
attributesClean := cleanAdditionalArgs(attributes) | ||
pushConfig := &pubsub.PushConfig{Attributes: attributesClean, PushEndpoint: push_config["push_endpoint"].(string)} | ||
subscription = &pubsub.Subscription{AckDeadlineSeconds: ackDeadlineSeconds, Topic: computed_topic_name, PushConfig: pushConfig} | ||
} else { | ||
subscription = &pubsub.Subscription{AckDeadlineSeconds: ackDeadlineSeconds, Topic: computed_topic_name} | ||
} | ||
|
||
call := config.clientPubsub.Projects.Subscriptions.Create(name, subscription) | ||
res, err := call.Do() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId(res.Name) | ||
|
||
return nil | ||
} | ||
|
||
func resourcePubsubSubscriptionRead(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
name := d.Id() | ||
call := config.clientPubsub.Projects.Subscriptions.Get(name) | ||
_, err := call.Do() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
|
||
func resourcePubsubSubscriptionDelete(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
name := d.Id() | ||
call := config.clientPubsub.Projects.Subscriptions.Delete(name) | ||
_, err := call.Do() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
74 changes: 74 additions & 0 deletions
74
builtin/providers/google/resource_pubsub_subscription_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccPubsubSubscriptionCreate(t *testing.T) { | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckPubsubSubscriptionDestroy, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccPubsubSubscription, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccPubsubSubscriptionExists( | ||
"google_pubsub_subscription.foobar_sub"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckPubsubSubscriptionDestroy(s *terraform.State) error { | ||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "google_pubsub_subscription" { | ||
continue | ||
} | ||
|
||
config := testAccProvider.Meta().(*Config) | ||
_, err := config.clientPubsub.Projects.Subscriptions.Get(rs.Primary.ID).Do() | ||
if err != nil { | ||
fmt.Errorf("Subscription still present") | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func testAccPubsubSubscriptionExists(n string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", n) | ||
} | ||
|
||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("No ID is set") | ||
} | ||
config := testAccProvider.Meta().(*Config) | ||
_, err := config.clientPubsub.Projects.Subscriptions.Get(rs.Primary.ID).Do() | ||
if err != nil { | ||
fmt.Errorf("Subscription still present") | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
const testAccPubsubSubscription = ` | ||
resource "google_pubsub_topic" "foobar_sub" { | ||
name = "foobar_sub" | ||
} | ||
resource "google_pubsub_subscription" "foobar_sub" { | ||
name = "foobar_sub" | ||
topic = "${google_pubsub_topic.foobar_sub.name}" | ||
}` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"google.golang.org/api/pubsub/v1" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
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, | ||
}, | ||
|
||
}, | ||
} | ||
} | ||
|
||
func resourcePubsubTopicCreate(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
name := fmt.Sprintf("projects/%s/topics/%s", config.Project, d.Get("name").(string)) | ||
topic := &pubsub.Topic{} | ||
|
||
call := config.clientPubsub.Projects.Topics.Create(name, topic) | ||
res, err := call.Do() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId(res.Name) | ||
|
||
return nil | ||
} | ||
|
||
func resourcePubsubTopicRead(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
name := d.Id() | ||
call := config.clientPubsub.Projects.Topics.Get(name) | ||
_, err := call.Do() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
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 { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccPubsubTopicCreate(t *testing.T) { | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckPubsubTopicDestroy, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccPubsubTopic, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccPubsubTopicExists( | ||
"google_pubsub_topic.foobar"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckPubsubTopicDestroy(s *terraform.State) error { | ||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "google_pubsub_topic" { | ||
continue | ||
} | ||
|
||
config := testAccProvider.Meta().(*Config) | ||
_, err := config.clientPubsub.Projects.Topics.Get(rs.Primary.ID).Do() | ||
if err != nil { | ||
fmt.Errorf("Topic still present") | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func testAccPubsubTopicExists(n string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", n) | ||
} | ||
|
||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("No ID is set") | ||
} | ||
config := testAccProvider.Meta().(*Config) | ||
_, err := config.clientPubsub.Projects.Topics.Get(rs.Primary.ID).Do() | ||
if err != nil { | ||
fmt.Errorf("Topic still present") | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
const testAccPubsubTopic = ` | ||
resource "google_pubsub_topic" "foobar" { | ||
name = "foobar" | ||
}` |
Oops, something went wrong.