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

Add data source for pubsub topics #4062

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
30 changes: 30 additions & 0 deletions third_party/terraform/data_sources/data_source_pubsub_topic.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 dataSourceGooglePubsubTopic() *schema.Resource {

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

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

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

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

import (
"testing"

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

func TestAccDataSourceGooglePubsubTopic_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: testAccCheckPubsubTopicDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccDataSourceGooglePubsubTopic_basic(context),
Check: resource.ComposeTestCheckFunc(
checkDataSourceStateMatchesResourceState("data.google_pubsub_topic.foo", "google_pubsub_topic.foo"),
),
},
},
})
}

func TestAccDataSourceGooglePubsubTopic_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: testAccCheckPubsubTopicDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccDataSourceGooglePubsubTopic_optionalProject(context),
Check: resource.ComposeTestCheckFunc(
checkDataSourceStateMatchesResourceState("data.google_pubsub_topic.foo", "google_pubsub_topic.foo"),
),
},
},
})
}

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

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

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

data "google_pubsub_topic" "foo" {
name = google_pubsub_topic.foo.name
}
`, context)
}
11 changes: 6 additions & 5 deletions third_party/terraform/utils/provider.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func Provider() *schema.Provider {
"google_compute_default_service_account": dataSourceGoogleComputeDefaultServiceAccount(),
"google_compute_forwarding_rule": dataSourceGoogleComputeForwardingRule(),
"google_compute_global_address": dataSourceGoogleComputeGlobalAddress(),
"google_compute_global_forwarding_rule": dataSourceGoogleComputeGlobalForwardingRule(),
"google_compute_global_forwarding_rule": dataSourceGoogleComputeGlobalForwardingRule(),
"google_compute_image": dataSourceGoogleComputeImage(),
"google_compute_instance": dataSourceGoogleComputeInstance(),
"google_compute_instance_serial_port": dataSourceGoogleComputeInstanceSerialPort(),
Expand Down Expand Up @@ -228,10 +228,11 @@ func Provider() *schema.Provider {
"google_project": dataSourceGoogleProject(),
"google_projects": dataSourceGoogleProjects(),
"google_project_organization_policy": dataSourceGoogleProjectOrganizationPolicy(),
"google_secret_manager_secret_version": dataSourceSecretManagerSecretVersion(),
"google_pubsub_topic": dataSourceGooglePubsubTopic(),
ScottSuarez marked this conversation as resolved.
Show resolved Hide resolved
"google_secret_manager_secret_version": dataSourceSecretManagerSecretVersion(),
"google_service_account": dataSourceGoogleServiceAccount(),
"google_service_account_access_token": dataSourceGoogleServiceAccountAccessToken(),
"google_service_account_id_token": dataSourceGoogleServiceAccountIdToken(),
"google_service_account_id_token": dataSourceGoogleServiceAccountIdToken(),
"google_service_account_key": dataSourceGoogleServiceAccountKey(),
"google_sql_ca_certs": dataSourceGoogleSQLCaCerts(),
"google_sql_database_instance": dataSourceSqlDatabaseInstance(),
Expand Down Expand Up @@ -315,7 +316,7 @@ end # products.each do
"google_bigtable_table": resourceBigtableTable(),
"google_bigtable_table_iam_binding": ResourceIamBinding(IamBigtableTableSchema, NewBigtableTableUpdater, BigtableTableIdParseFunc),
"google_bigtable_table_iam_member": ResourceIamMember(IamBigtableTableSchema, NewBigtableTableUpdater, BigtableTableIdParseFunc),
"google_bigtable_table_iam_policy": ResourceIamPolicy(IamBigtableTableSchema, NewBigtableTableUpdater, BigtableTableIdParseFunc),
"google_bigtable_table_iam_policy": ResourceIamPolicy(IamBigtableTableSchema, NewBigtableTableUpdater, BigtableTableIdParseFunc),
"google_bigquery_dataset_iam_binding": ResourceIamBinding(IamBigqueryDatasetSchema, NewBigqueryDatasetIamUpdater, BigqueryDatasetIdParseFunc),
"google_bigquery_dataset_iam_member": ResourceIamMember(IamBigqueryDatasetSchema, NewBigqueryDatasetIamUpdater, BigqueryDatasetIdParseFunc),
"google_bigquery_dataset_iam_policy": ResourceIamPolicy(IamBigqueryDatasetSchema, NewBigqueryDatasetIamUpdater, BigqueryDatasetIdParseFunc),
Expand Down Expand Up @@ -345,7 +346,7 @@ end # products.each do
"google_container_registry": resourceContainerRegistry(),
"google_dataflow_job": resourceDataflowJob(),
<% unless version == 'ga' -%>
"google_dataflow_flex_template_job": resourceDataflowFlexTemplateJob(),
"google_dataflow_flex_template_job": resourceDataflowFlexTemplateJob(),
<% end -%>
"google_dataproc_cluster": resourceDataprocCluster(),
"google_dataproc_cluster_iam_binding": ResourceIamBinding(IamDataprocClusterSchema, NewDataprocClusterUpdater, DataprocClusterIdParseFunc),
Expand Down
37 changes: 37 additions & 0 deletions third_party/terraform/website/docs/d/pubsub_topic.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
subcategory: "Cloud Pub/Sub"
layout: "google"
page_title: "Google: google_pubsub_topic"
sidebar_current: "docs-google-datasource-pubsub-topic"
description: |-
Get information about a Google Cloud Pub/Sub Topic.
---

# google\_pubsub\_topic

Get information about a Google Cloud Pub/Sub Topic. 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_topic" "my-pubsub-topic" {
name = "my-pubsub-topic"
}
```

## Argument Reference

The following arguments are supported:

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

- - -

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

## Attributes Reference

See [google_pubsub_topic](https://www.terraform.io/docs/providers/google/r/pubsub_topic.html#argument-reference) resource for details of the available attributes.