From 6ea06a8f022b9b81c79c0612a1a45e736b41ec34 Mon Sep 17 00:00:00 2001 From: mopp Date: Mon, 5 Oct 2020 18:59:16 +0900 Subject: [PATCH 1/2] Add data source for pubsub topics --- google/data_source_pubsub_topic.go | 30 +++++++++ google/data_source_pubsub_topic_test.go | 76 +++++++++++++++++++++++ google/provider.go | 1 + website/docs/d/pubsub_topic.html.markdown | 37 +++++++++++ website/google.erb | 10 +++ 5 files changed, 154 insertions(+) create mode 100644 google/data_source_pubsub_topic.go create mode 100644 google/data_source_pubsub_topic_test.go create mode 100644 website/docs/d/pubsub_topic.html.markdown diff --git a/google/data_source_pubsub_topic.go b/google/data_source_pubsub_topic.go new file mode 100644 index 00000000000..db5780e780e --- /dev/null +++ b/google/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/google/data_source_pubsub_topic_test.go b/google/data_source_pubsub_topic_test.go new file mode 100644 index 00000000000..9f8263df112 --- /dev/null +++ b/google/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/google/provider.go b/google/provider.go index 6789a1351c1..66b44d5de36 100644 --- a/google/provider.go +++ b/google/provider.go @@ -565,6 +565,7 @@ func Provider() *schema.Provider { "google_client_openid_userinfo": dataSourceGoogleClientOpenIDUserinfo(), "google_cloudfunctions_function": dataSourceGoogleCloudFunctionsFunction(), "google_cloud_run_service": dataSourceGoogleCloudRunService(), + "google_pubsub_topic": dataSourceGooglePubsubTopic(), "google_composer_image_versions": dataSourceGoogleComposerImageVersions(), "google_compute_address": dataSourceGoogleComputeAddress(), "google_compute_backend_service": dataSourceGoogleComputeBackendService(), diff --git a/website/docs/d/pubsub_topic.html.markdown b/website/docs/d/pubsub_topic.html.markdown new file mode 100644 index 00000000000..eeeaa18ebab --- /dev/null +++ b/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. diff --git a/website/google.erb b/website/google.erb index c20307473b3..b356b6e47b4 100644 --- a/website/google.erb +++ b/website/google.erb @@ -876,6 +876,16 @@
  • Cloud Pub/Sub