diff --git a/third_party/terraform/data_sources/data_source_pubsub_topic.go b/third_party/terraform/data_sources/data_source_pubsub_topic.go new file mode 100644 index 000000000000..db5780e780e3 --- /dev/null +++ b/third_party/terraform/data_sources/data_source_pubsub_topic.go @@ -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) +} diff --git a/third_party/terraform/tests/data_source_pubsub_topic_test.go b/third_party/terraform/tests/data_source_pubsub_topic_test.go new file mode 100644 index 000000000000..9f8263df112f --- /dev/null +++ b/third_party/terraform/tests/data_source_pubsub_topic_test.go @@ -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) +} diff --git a/third_party/terraform/utils/provider.go.erb b/third_party/terraform/utils/provider.go.erb index be33297fd868..5fd90b8047a7 100644 --- a/third_party/terraform/utils/provider.go.erb +++ b/third_party/terraform/utils/provider.go.erb @@ -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(), @@ -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(), + "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(), @@ -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), @@ -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), diff --git a/third_party/terraform/website/docs/d/pubsub_topic.html.markdown b/third_party/terraform/website/docs/d/pubsub_topic.html.markdown new file mode 100644 index 000000000000..eeeaa18ebab3 --- /dev/null +++ b/third_party/terraform/website/docs/d/pubsub_topic.html.markdown @@ -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.