From 5809581c967346302da7e2ec10c3e0d9c13b777a Mon Sep 17 00:00:00 2001 From: Kamaz Date: Tue, 29 Sep 2020 22:12:40 +0100 Subject: [PATCH 1/2] adds google_cloud_run_service data source --- google/data_source_cloud_run_service.go | 29 ++++++++++ google/data_source_cloud_run_service_test.go | 56 +++++++++++++++++++ google/provider.go | 1 + .../docs/d/cloud_run_service.html.markdown | 42 ++++++++++++++ 4 files changed, 128 insertions(+) create mode 100644 google/data_source_cloud_run_service.go create mode 100644 google/data_source_cloud_run_service_test.go create mode 100644 website/docs/d/cloud_run_service.html.markdown diff --git a/google/data_source_cloud_run_service.go b/google/data_source_cloud_run_service.go new file mode 100644 index 00000000000..e027251131d --- /dev/null +++ b/google/data_source_cloud_run_service.go @@ -0,0 +1,29 @@ +package google + +import ( + "fmt" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceGoogleCloudRunService() *schema.Resource { + + dsSchema := datasourceSchemaFromResourceSchema(resourceCloudRunService().Schema) + addRequiredFieldsToSchema(dsSchema, "name", "location") + + return &schema.Resource{ + Read: dataSourceGoogleCloudRunServiceRead, + Schema: dsSchema, + } +} + +func dataSourceGoogleCloudRunServiceRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + + id, err := replaceVars(d, config, "locations/{{location}}/namespaces/{{project}}/services/{{name}}") + if err != nil { + return fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + return resourceCloudRunServiceRead(d, meta) +} diff --git a/google/data_source_cloud_run_service_test.go b/google/data_source_cloud_run_service_test.go new file mode 100644 index 00000000000..6021ead5249 --- /dev/null +++ b/google/data_source_cloud_run_service_test.go @@ -0,0 +1,56 @@ +package google + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccDataSourceGoogleCloudRunService_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: testAccCheckCloudRunServiceDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccDataSourceGoogleCloudRunService_basic(context), + Check: resource.ComposeTestCheckFunc( + checkDataSourceStateMatchesResourceState("data.google_cloud_run_service.foo", "google_cloud_run_service.foo"), + ), + }, + }, + }) +} + +func testAccDataSourceGoogleCloudRunService_basic(context map[string]interface{}) string { + return Nprintf(` +resource "google_cloud_run_service" "foo" { + name = "tf-test-cloudrun-srv%{random_suffix}" + location = "us-central1" + + template { + spec { + containers { + image = "gcr.io/cloudrun/hello" + } + } + } + + traffic { + percent = 100 + latest_revision = true + } +} + +data "google_cloud_run_service" "foo" { + name = google_cloud_run_service.foo.name + location = google_cloud_run_service.foo.location +} +`, context) +} diff --git a/google/provider.go b/google/provider.go index ed28144bda2..2cf91473075 100644 --- a/google/provider.go +++ b/google/provider.go @@ -564,6 +564,7 @@ func Provider() *schema.Provider { "google_client_config": dataSourceGoogleClientConfig(), "google_client_openid_userinfo": dataSourceGoogleClientOpenIDUserinfo(), "google_cloudfunctions_function": dataSourceGoogleCloudFunctionsFunction(), + "google_cloud_run_service": dataSourceGoogleCloudRunService(), "google_composer_image_versions": dataSourceGoogleComposerImageVersions(), "google_compute_address": dataSourceGoogleComputeAddress(), "google_compute_backend_service": dataSourceGoogleComputeBackendService(), diff --git a/website/docs/d/cloud_run_service.html.markdown b/website/docs/d/cloud_run_service.html.markdown new file mode 100644 index 00000000000..642e2796fa5 --- /dev/null +++ b/website/docs/d/cloud_run_service.html.markdown @@ -0,0 +1,42 @@ +--- +subcategory: "Cloud Run" +layout: "google" +page_title: "Google: google_cloud_run_service" +sidebar_current: "docs-google-cloud-run-service" +description: |- + Get information about a Google Cloud Run Service. +--- + +# google\_cloud\_run\_service + +Get information about a Google Cloud Run. For more information see +the [official documentation](https://cloud.google.com/run/docs/) +and [API](https://cloud.google.com/run/docs/apis). + +## Example Usage + +```hcl +data "google_cloud_run_service" "run-service" { + name = "my-service" + location = "us-central1" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) The name of a Cloud Function. +* `location` - (Required) The name of a Cloud Function. + +- - - + +* `project` - (Optional) The project in which the resource belongs. If it + is not provided, the provider project is used. + +* `region` - (Optional) The region in which the resource belongs. If it + is not provided, the provider region is used. + +## Attributes Reference + +TBD From b7b9041f9e3180af3365f4d1eac0b87260c5a8f4 Mon Sep 17 00:00:00 2001 From: Kamaz Date: Wed, 30 Sep 2020 19:17:12 +0100 Subject: [PATCH 2/2] fix: comments for PR --- google/data_source_cloud_run_service.go | 1 + google/data_source_cloud_run_service_test.go | 50 +++++++++++++++++++ .../docs/d/cloud_run_service.html.markdown | 9 ++-- website/google.erb | 10 ++++ 4 files changed, 64 insertions(+), 6 deletions(-) diff --git a/google/data_source_cloud_run_service.go b/google/data_source_cloud_run_service.go index e027251131d..3a6ec1beefb 100644 --- a/google/data_source_cloud_run_service.go +++ b/google/data_source_cloud_run_service.go @@ -10,6 +10,7 @@ func dataSourceGoogleCloudRunService() *schema.Resource { dsSchema := datasourceSchemaFromResourceSchema(resourceCloudRunService().Schema) addRequiredFieldsToSchema(dsSchema, "name", "location") + addOptionalFieldsToSchema(dsSchema, "project") return &schema.Resource{ Read: dataSourceGoogleCloudRunServiceRead, diff --git a/google/data_source_cloud_run_service_test.go b/google/data_source_cloud_run_service_test.go index 6021ead5249..e419bb044ed 100644 --- a/google/data_source_cloud_run_service_test.go +++ b/google/data_source_cloud_run_service_test.go @@ -28,6 +28,28 @@ func TestAccDataSourceGoogleCloudRunService_basic(t *testing.T) { }) } +func TestAccDataSourceGoogleCloudRunService_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: testAccCheckCloudRunServiceDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccDataSourceGoogleCloudRunService_optionalProject(context), + Check: resource.ComposeTestCheckFunc( + checkDataSourceStateMatchesResourceState("data.google_cloud_run_service.foo", "google_cloud_run_service.foo"), + ), + }, + }, + }) +} + func testAccDataSourceGoogleCloudRunService_basic(context map[string]interface{}) string { return Nprintf(` resource "google_cloud_run_service" "foo" { @@ -48,6 +70,34 @@ resource "google_cloud_run_service" "foo" { } } +data "google_cloud_run_service" "foo" { + name = google_cloud_run_service.foo.name + location = google_cloud_run_service.foo.location + project = google_cloud_run_service.foo.project +} +`, context) +} + +func testAccDataSourceGoogleCloudRunService_optionalProject(context map[string]interface{}) string { + return Nprintf(` +resource "google_cloud_run_service" "foo" { + name = "tf-test-cloudrun-srv%{random_suffix}" + location = "us-central1" + + template { + spec { + containers { + image = "gcr.io/cloudrun/hello" + } + } + } + + traffic { + percent = 100 + latest_revision = true + } +} + data "google_cloud_run_service" "foo" { name = google_cloud_run_service.foo.name location = google_cloud_run_service.foo.location diff --git a/website/docs/d/cloud_run_service.html.markdown b/website/docs/d/cloud_run_service.html.markdown index 642e2796fa5..27de5ace90b 100644 --- a/website/docs/d/cloud_run_service.html.markdown +++ b/website/docs/d/cloud_run_service.html.markdown @@ -26,17 +26,14 @@ data "google_cloud_run_service" "run-service" { The following arguments are supported: -* `name` - (Required) The name of a Cloud Function. -* `location` - (Required) The name of a Cloud Function. +* `name` - (Required) The name of a Cloud Run. +* `location` - (Required) The name of a Cloud Run. - - - * `project` - (Optional) The project in which the resource belongs. If it is not provided, the provider project is used. -* `region` - (Optional) The region in which the resource belongs. If it - is not provided, the provider region is used. - ## Attributes Reference -TBD +See [google_cloud_run_service](https://www.terraform.io/docs/providers/google/r/cloud_run_service.html#argument-reference) resource for details of the available attributes. diff --git a/website/google.erb b/website/google.erb index 6bfffecc70d..7acf34b39c2 100644 --- a/website/google.erb +++ b/website/google.erb @@ -900,6 +900,16 @@
  • Cloud Run