Skip to content

Commit

Permalink
Add google_cloud_run_service datasource (#4036)
Browse files Browse the repository at this point in the history
Co-authored-by: Kamaz <[email protected]>
  • Loading branch information
rileykarson and kamaz authored Oct 2, 2020
1 parent 73d8cdb commit e0c59c4
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 0 deletions.
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 third_party/terraform/tests/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 third_party/terraform/utils/provider.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ func Provider() *schema.Provider {
"google_cloud_identity_groups": dataSourceGoogleCloudIdentityGroups(),
"google_cloud_identity_group_memberships": dataSourceGoogleCloudIdentityGroupMemberships(),
<% end -%>
"google_cloud_run_service": dataSourceGoogleCloudRunService(),
"google_composer_image_versions": dataSourceGoogleComposerImageVersions(),
"google_compute_address": dataSourceGoogleComputeAddress(),
"google_compute_backend_service": dataSourceGoogleComputeBackendService(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
subcategory: "Cloud Run"
layout: "google"
page_title: "Google: google_cloud_run_service"
sidebar_current: "docs-google-datasource-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 the Cloud Run Service.

* `location` - (Required) The location of the cloud run instance. eg us-central1

- - -

* `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.

0 comments on commit e0c59c4

Please sign in to comment.