Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Datasource for Pub/Sub Subscription #13296

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/6972.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-datasource
`data_source_google_pubsub_subscription`
```
30 changes: 30 additions & 0 deletions google/data_source_pubsub_subscription.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package google

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceGooglePubsubSubscription() *schema.Resource {

dsSchema := datasourceSchemaFromResourceSchema(resourcePubsubSubscription().Schema)
addRequiredFieldsToSchema(dsSchema, "name")
addOptionalFieldsToSchema(dsSchema, "project")

return &schema.Resource{
Read: dataSourceGooglePubsubSubscriptionRead,
Schema: dsSchema,
}
}

func dataSourceGooglePubsubSubscriptionRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

id, err := replaceVars(d, config, "projects/{{project}}/subscriptions/{{name}}")
if err != nil {
return fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)
return resourcePubsubSubscriptionRead(d, meta)
}
86 changes: 86 additions & 0 deletions google/data_source_pubsub_subscription_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package google

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceGooglePubsubSubscription_basic(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": randString(t, 10),
}

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPubsubSubscriptionDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccDataSourceGooglePubsubSubscription_basic(context),
Check: resource.ComposeTestCheckFunc(
checkDataSourceStateMatchesResourceState("data.google_pubsub_subscription.foo", "google_pubsub_subscription.foo"),
),
},
},
})
}

func TestAccDataSourceGooglePubsubSubscription_optionalProject(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": randString(t, 10),
}

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPubsubSubscriptionDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccDataSourceGooglePubsubSubscription_optionalProject(context),
Check: resource.ComposeTestCheckFunc(
checkDataSourceStateMatchesResourceState("data.google_pubsub_subscription.foo", "google_pubsub_subscription.foo"),
),
},
},
})
}

func testAccDataSourceGooglePubsubSubscription_basic(context map[string]interface{}) string {
return Nprintf(`
resource "google_pubsub_topic" "foo" {
name = "tf-test-pubsub-%{random_suffix}"
}

resource "google_pubsub_subscription" "foo" {
name = "tf-test-pubsub-subscription-%{random_suffix}"
topic = google_pubsub_topic.foo.name
}

data "google_pubsub_subscription" "foo" {
name = google_pubsub_subscription.foo.name
project = google_pubsub_subscription.foo.project
}
`, context)
}

func testAccDataSourceGooglePubsubSubscription_optionalProject(context map[string]interface{}) string {
return Nprintf(`
resource "google_pubsub_topic" "foo" {
name = "tf-test-pubsub-%{random_suffix}"
}

resource "google_pubsub_subscription" "foo" {
name = "tf-test-pubsub-subscription-%{random_suffix}"
topic = google_pubsub_topic.foo.name
}

data "google_pubsub_subscription" "foo" {
name = google_pubsub_subscription.foo.name
}
`, context)
}
1 change: 1 addition & 0 deletions google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,7 @@ func Provider() *schema.Provider {
"google_project": dataSourceGoogleProject(),
"google_projects": dataSourceGoogleProjects(),
"google_project_organization_policy": dataSourceGoogleProjectOrganizationPolicy(),
"google_pubsub_subscription": dataSourceGooglePubsubSubscription(),
"google_pubsub_topic": dataSourceGooglePubsubTopic(),
"google_secret_manager_secret": dataSourceSecretManagerSecret(),
"google_secret_manager_secret_version": dataSourceSecretManagerSecretVersion(),
Expand Down
35 changes: 35 additions & 0 deletions website/docs/d/pubsub_subscription.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
subcategory: "Cloud Pub/Sub"
page_title: "Google: google_pubsub_subscription"
description: |-
Get information about a Google Cloud Pub/Sub Subscription.
---

# google\_pubsub\_subscription

Get information about a Google Cloud Pub/Sub Subscription. For more information see
the [official documentation](https://cloud.google.com/pubsub/docs/)
and [API](https://cloud.google.com/pubsub/docs/apis).

## Example Usage

```hcl
data "google_pubsub_subscription" "my-pubsub-subscription" {
name = "my-pubsub-subscription"
}
```

## Argument Reference

The following arguments are supported:

* `name` - (Required) The name of the Cloud Pub/Sub Subscription.

- - -

* `project` - (Optional) The project in which the resource belongs. If it
is not provided, the provider project is used.

## Attributes Reference

See [google_pubsub_subscription](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/pubsub_subscription#argument-reference) resource for details of the available attributes.