-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add google_cloud_run_service datasource (#4036)
Co-authored-by: Kamaz <[email protected]>
- Loading branch information
1 parent
73d8cdb
commit e0c59c4
Showing
4 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
third_party/terraform/data_sources/data_source_cloud_run_service.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
106
third_party/terraform/tests/data_source_cloud_run_service_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
third_party/terraform/website/docs/d/cloud_run_service.html.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |