Skip to content

Commit

Permalink
adds google_cloud_run_service data source (#7388)
Browse files Browse the repository at this point in the history
* adds google_cloud_run_service data source

* fix: comments for PR
  • Loading branch information
kamaz authored Oct 2, 2020
1 parent 3bdd327 commit 09e2ce3
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 0 deletions.
30 changes: 30 additions & 0 deletions google/data_source_cloud_run_service.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 dataSourceGoogleCloudRunService() *schema.Resource {

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

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)
}
106 changes: 106 additions & 0 deletions google/data_source_cloud_run_service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
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_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" {
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
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
}
`, context)
}
1 change: 1 addition & 0 deletions google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
39 changes: 39 additions & 0 deletions website/docs/d/cloud_run_service.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
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 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.

## Attributes Reference

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.
10 changes: 10 additions & 0 deletions website/google.erb
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,16 @@
<li>
<a href="#">Cloud Run</a>
<ul class="nav">
<li>
<a href="#">Data Sources</a>
<ul class="nav nav-auto-expand">

<li>
<a href="/docs/providers/google/d/cloud_run_service.html">google_cloud_run_service</a>
</li>

</ul>
</li>
<li>
<a href="#">Resources</a>
<ul class="nav nav-auto-expand">
Expand Down

0 comments on commit 09e2ce3

Please sign in to comment.