Skip to content

Commit 9dfc4f1

Browse files
Adding Datasource for Pub/Sub Subscription (#6972) (#13296)
* Adding Datasource for Pub/Sub Subscription * Removing unintended spaces * Added documentation for the datasource * Update mmv1/third_party/terraform/website/docs/d/pubsub_subscription.html.markdown Co-authored-by: Sarah French <[email protected]> Co-authored-by: Sarah French <[email protected]> Signed-off-by: Modular Magician <[email protected]> Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Sarah French <[email protected]>
1 parent 5f1f0dd commit 9dfc4f1

File tree

5 files changed

+155
-0
lines changed

5 files changed

+155
-0
lines changed

.changelog/6972.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-datasource
2+
`data_source_google_pubsub_subscription`
3+
```
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package google
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
)
8+
9+
func dataSourceGooglePubsubSubscription() *schema.Resource {
10+
11+
dsSchema := datasourceSchemaFromResourceSchema(resourcePubsubSubscription().Schema)
12+
addRequiredFieldsToSchema(dsSchema, "name")
13+
addOptionalFieldsToSchema(dsSchema, "project")
14+
15+
return &schema.Resource{
16+
Read: dataSourceGooglePubsubSubscriptionRead,
17+
Schema: dsSchema,
18+
}
19+
}
20+
21+
func dataSourceGooglePubsubSubscriptionRead(d *schema.ResourceData, meta interface{}) error {
22+
config := meta.(*Config)
23+
24+
id, err := replaceVars(d, config, "projects/{{project}}/subscriptions/{{name}}")
25+
if err != nil {
26+
return fmt.Errorf("Error constructing id: %s", err)
27+
}
28+
d.SetId(id)
29+
return resourcePubsubSubscriptionRead(d, meta)
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package google
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccDataSourceGooglePubsubSubscription_basic(t *testing.T) {
10+
t.Parallel()
11+
12+
context := map[string]interface{}{
13+
"random_suffix": randString(t, 10),
14+
}
15+
16+
vcrTest(t, resource.TestCase{
17+
PreCheck: func() { testAccPreCheck(t) },
18+
Providers: testAccProviders,
19+
CheckDestroy: testAccCheckPubsubSubscriptionDestroyProducer(t),
20+
Steps: []resource.TestStep{
21+
{
22+
Config: testAccDataSourceGooglePubsubSubscription_basic(context),
23+
Check: resource.ComposeTestCheckFunc(
24+
checkDataSourceStateMatchesResourceState("data.google_pubsub_subscription.foo", "google_pubsub_subscription.foo"),
25+
),
26+
},
27+
},
28+
})
29+
}
30+
31+
func TestAccDataSourceGooglePubsubSubscription_optionalProject(t *testing.T) {
32+
t.Parallel()
33+
34+
context := map[string]interface{}{
35+
"random_suffix": randString(t, 10),
36+
}
37+
38+
vcrTest(t, resource.TestCase{
39+
PreCheck: func() { testAccPreCheck(t) },
40+
Providers: testAccProviders,
41+
CheckDestroy: testAccCheckPubsubSubscriptionDestroyProducer(t),
42+
Steps: []resource.TestStep{
43+
{
44+
Config: testAccDataSourceGooglePubsubSubscription_optionalProject(context),
45+
Check: resource.ComposeTestCheckFunc(
46+
checkDataSourceStateMatchesResourceState("data.google_pubsub_subscription.foo", "google_pubsub_subscription.foo"),
47+
),
48+
},
49+
},
50+
})
51+
}
52+
53+
func testAccDataSourceGooglePubsubSubscription_basic(context map[string]interface{}) string {
54+
return Nprintf(`
55+
resource "google_pubsub_topic" "foo" {
56+
name = "tf-test-pubsub-%{random_suffix}"
57+
}
58+
59+
resource "google_pubsub_subscription" "foo" {
60+
name = "tf-test-pubsub-subscription-%{random_suffix}"
61+
topic = google_pubsub_topic.foo.name
62+
}
63+
64+
data "google_pubsub_subscription" "foo" {
65+
name = google_pubsub_subscription.foo.name
66+
project = google_pubsub_subscription.foo.project
67+
}
68+
`, context)
69+
}
70+
71+
func testAccDataSourceGooglePubsubSubscription_optionalProject(context map[string]interface{}) string {
72+
return Nprintf(`
73+
resource "google_pubsub_topic" "foo" {
74+
name = "tf-test-pubsub-%{random_suffix}"
75+
}
76+
77+
resource "google_pubsub_subscription" "foo" {
78+
name = "tf-test-pubsub-subscription-%{random_suffix}"
79+
topic = google_pubsub_topic.foo.name
80+
}
81+
82+
data "google_pubsub_subscription" "foo" {
83+
name = google_pubsub_subscription.foo.name
84+
}
85+
`, context)
86+
}

google/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,7 @@ func Provider() *schema.Provider {
895895
"google_project": dataSourceGoogleProject(),
896896
"google_projects": dataSourceGoogleProjects(),
897897
"google_project_organization_policy": dataSourceGoogleProjectOrganizationPolicy(),
898+
"google_pubsub_subscription": dataSourceGooglePubsubSubscription(),
898899
"google_pubsub_topic": dataSourceGooglePubsubTopic(),
899900
"google_secret_manager_secret": dataSourceSecretManagerSecret(),
900901
"google_secret_manager_secret_version": dataSourceSecretManagerSecretVersion(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
subcategory: "Cloud Pub/Sub"
3+
page_title: "Google: google_pubsub_subscription"
4+
description: |-
5+
Get information about a Google Cloud Pub/Sub Subscription.
6+
---
7+
8+
# google\_pubsub\_subscription
9+
10+
Get information about a Google Cloud Pub/Sub Subscription. For more information see
11+
the [official documentation](https://cloud.google.com/pubsub/docs/)
12+
and [API](https://cloud.google.com/pubsub/docs/apis).
13+
14+
## Example Usage
15+
16+
```hcl
17+
data "google_pubsub_subscription" "my-pubsub-subscription" {
18+
name = "my-pubsub-subscription"
19+
}
20+
```
21+
22+
## Argument Reference
23+
24+
The following arguments are supported:
25+
26+
* `name` - (Required) The name of the Cloud Pub/Sub Subscription.
27+
28+
- - -
29+
30+
* `project` - (Optional) The project in which the resource belongs. If it
31+
is not provided, the provider project is used.
32+
33+
## Attributes Reference
34+
35+
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.

0 commit comments

Comments
 (0)