From c9def1614fa46c3b7e0b26fa14618f486a53a020 Mon Sep 17 00:00:00 2001 From: mopp Date: Mon, 5 Oct 2020 18:59:16 +0900 Subject: [PATCH] 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 | 858 +++++++++++----------- 5 files changed, 578 insertions(+), 424 deletions(-) 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..7ed76168361 --- /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 dataSourceGooglePubsubService() *schema.Resource { + + dsSchema := datasourceSchemaFromResourceSchema(resourcePubsubTopic().Schema) + addRequiredFieldsToSchema(dsSchema, "name") + addOptionalFieldsToSchema(dsSchema, "project") + + return &schema.Resource{ + Read: dataSourceGooglePubsubServiceRead, + Schema: dsSchema, + } +} + +func dataSourceGooglePubsubServiceRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + + id, err := replaceVars(d, config, "namespaces/{{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..5556c379659 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": dataSourceGooglePubsubService(), "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..c3991c6cff8 100644 --- a/website/google.erb +++ b/website/google.erb @@ -49,19 +49,19 @@
  • Resources
  • @@ -73,31 +73,31 @@
  • Resources
  • @@ -109,35 +109,35 @@
  • Resources
  • @@ -149,41 +149,41 @@
  • Data Sources
  • Resources
  • @@ -195,11 +195,11 @@
  • Resources
  • @@ -211,19 +211,19 @@
  • Resources
  • @@ -235,59 +235,59 @@
  • Resources
  • @@ -299,57 +299,57 @@
  • Data Sources
  • Resources
  • @@ -361,19 +361,19 @@
  • Resources
  • @@ -385,31 +385,31 @@
  • Resources
  • @@ -421,11 +421,11 @@
  • Resources
  • @@ -437,21 +437,21 @@
  • Data Sources
  • Resources
  • @@ -463,33 +463,33 @@
  • Data Sources
  • Resources
  • @@ -501,11 +501,11 @@
  • Resources
  • @@ -517,15 +517,15 @@
  • Resources
  • @@ -537,25 +537,25 @@
  • Data Sources
  • Resources
  • @@ -567,39 +567,39 @@
  • Resources
  • @@ -611,15 +611,15 @@
  • Data Sources
  • @@ -631,15 +631,15 @@
  • Resources
  • @@ -651,57 +651,57 @@
  • Data Sources
  • Resources
  • @@ -713,161 +713,161 @@
  • Data Sources
  • Resources
  • @@ -876,26 +876,36 @@
  • Cloud Pub/Sub @@ -907,29 +917,29 @@
  • Data Sources
  • Resources
  • @@ -941,41 +951,41 @@
  • Data Sources
  • Resources
  • @@ -987,11 +997,11 @@
  • Resources
  • @@ -1003,15 +1013,15 @@
  • Resources
  • @@ -1023,23 +1033,23 @@
  • Resources
  • @@ -1051,69 +1061,69 @@
  • Data Sources
  • Resources
  • @@ -1125,21 +1135,21 @@
  • Data Sources
  • Resources
  • @@ -1151,11 +1161,11 @@
  • Resources
  • @@ -1167,409 +1177,409 @@
  • Data Sources
  • Resources
  • @@ -1581,33 +1591,33 @@
  • Data Sources
  • Resources
  • @@ -1619,27 +1629,27 @@
  • Resources
  • @@ -1651,23 +1661,23 @@
  • Resources
  • @@ -1679,15 +1689,15 @@
  • Resources
  • @@ -1699,27 +1709,27 @@
  • Resources
  • @@ -1731,11 +1741,11 @@
  • Resources
  • @@ -1747,19 +1757,19 @@
  • Resources
  • @@ -1771,11 +1781,11 @@
  • Resources
  • @@ -1787,15 +1797,15 @@
  • Data Sources
  • @@ -1807,11 +1817,11 @@
  • Resources
  • @@ -1823,37 +1833,37 @@
  • Data Sources
  • Resources
  • @@ -1865,35 +1875,35 @@
  • Resources
  • @@ -1905,43 +1915,43 @@
  • Resources
  • @@ -1953,29 +1963,29 @@
  • Data Sources
  • Resources
  • @@ -1987,11 +1997,11 @@
  • Resources
  • @@ -2003,15 +2013,15 @@
  • Resources
  • @@ -2023,21 +2033,21 @@
  • Data Sources
  • Resources
  • @@ -2049,11 +2059,11 @@
  • Resources
  • @@ -2065,11 +2075,11 @@
  • Resources
  • @@ -2081,11 +2091,11 @@
  • Resources
  • @@ -2097,11 +2107,11 @@
  • Resources
  • @@ -2113,19 +2123,19 @@
  • Resources
  • @@ -2137,29 +2147,29 @@
  • Data Sources
  • Resources
  • @@ -2171,11 +2181,11 @@
  • Resources
  • @@ -2187,11 +2197,11 @@
  • Resources
  • @@ -2203,11 +2213,11 @@
  • Resources
  • @@ -2219,21 +2229,21 @@
  • Data Sources
  • Resources