Skip to content

Commit 4dd8719

Browse files
feat(cloudrunv2): add datasource for google_cloud_run_v2_service (#9284) (#16290)
[upstream:ea658db3eaaefdee3eef74ab6b0382d541c5cbbe] Signed-off-by: Modular Magician <[email protected]>
1 parent 3488b9e commit 4dd8719

File tree

5 files changed

+206
-0
lines changed

5 files changed

+206
-0
lines changed

.changelog/9284.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-datasource
2+
`google_cloud_run_v2_service`
3+
```

google/provider/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,7 @@ func DatasourceMapWithErrors() (map[string]*schema.Resource, error) {
804804
"google_cloud_run_locations": cloudrun.DataSourceGoogleCloudRunLocations(),
805805
"google_cloud_run_service": cloudrun.DataSourceGoogleCloudRunService(),
806806
"google_cloud_run_v2_job": cloudrunv2.DataSourceGoogleCloudRunV2Job(),
807+
"google_cloud_run_v2_service": cloudrunv2.DataSourceGoogleCloudRunV2Service(),
807808
"google_composer_environment": composer.DataSourceGoogleComposerEnvironment(),
808809
"google_composer_image_versions": composer.DataSourceGoogleComposerImageVersions(),
809810
"google_compute_address": compute.DataSourceGoogleComputeAddress(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
package cloudrunv2
4+
5+
import (
6+
"fmt"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
10+
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
11+
)
12+
13+
func DataSourceGoogleCloudRunV2Service() *schema.Resource {
14+
dsSchema := tpgresource.DatasourceSchemaFromResourceSchema(ResourceCloudRunV2Service().Schema)
15+
tpgresource.AddRequiredFieldsToSchema(dsSchema, "name")
16+
tpgresource.AddOptionalFieldsToSchema(dsSchema, "location")
17+
18+
// Set 'Optional' schema elements
19+
tpgresource.AddOptionalFieldsToSchema(dsSchema, "project")
20+
21+
return &schema.Resource{
22+
Read: dataSourceGoogleCloudRunV2ServiceRead,
23+
Schema: dsSchema,
24+
}
25+
}
26+
27+
func dataSourceGoogleCloudRunV2ServiceRead(d *schema.ResourceData, meta interface{}) error {
28+
config := meta.(*transport_tpg.Config)
29+
30+
id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/locations/{{location}}/services/{{name}}")
31+
if err != nil {
32+
return fmt.Errorf("Error constructing id: %s", err)
33+
}
34+
35+
d.SetId(id)
36+
37+
err = resourceCloudRunV2ServiceRead(d, meta)
38+
if err != nil {
39+
return err
40+
}
41+
42+
if d.Id() == "" {
43+
return fmt.Errorf("%s not found", id)
44+
}
45+
return nil
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
package cloudrunv2_test
4+
5+
import (
6+
"fmt"
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
10+
"github.com/hashicorp/terraform-provider-google/google/acctest"
11+
"github.com/hashicorp/terraform-provider-google/google/envvar"
12+
)
13+
14+
func TestAccDataSourceGoogleCloudRunV2Service_basic(t *testing.T) {
15+
t.Parallel()
16+
17+
project := envvar.GetTestProjectFromEnv()
18+
19+
name := fmt.Sprintf("tf-test-cloud-run-v2-service-%d", acctest.RandInt(t))
20+
location := "us-central1"
21+
id := fmt.Sprintf("projects/%s/locations/%s/services/%s", project, location, name)
22+
23+
acctest.VcrTest(t, resource.TestCase{
24+
PreCheck: func() { acctest.AccTestPreCheck(t) },
25+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
26+
Steps: []resource.TestStep{
27+
{
28+
Config: testAccDataSourceGoogleCloudRunV2Service_basic(name, location),
29+
Check: resource.ComposeTestCheckFunc(
30+
resource.TestCheckResourceAttr("data.google_cloud_run_v2_service.hello", "id", id),
31+
resource.TestCheckResourceAttr("data.google_cloud_run_v2_service.hello", "name", name),
32+
resource.TestCheckResourceAttr("data.google_cloud_run_v2_service.hello", "location", location),
33+
),
34+
},
35+
},
36+
})
37+
}
38+
39+
func testAccDataSourceGoogleCloudRunV2Service_basic(name, location string) string {
40+
return fmt.Sprintf(`
41+
resource "google_cloud_run_v2_service" "hello" {
42+
name = "%s"
43+
location = "%s"
44+
ingress = "INGRESS_TRAFFIC_ALL"
45+
46+
template {
47+
containers {
48+
image = "us-docker.pkg.dev/cloudrun/container/hello"
49+
}
50+
}
51+
}
52+
53+
data "google_cloud_run_v2_service" "hello" {
54+
name = google_cloud_run_v2_service.hello.name
55+
location = google_cloud_run_v2_service.hello.location
56+
}
57+
`, name, location)
58+
}
59+
60+
func TestAccDataSourceGoogleCloudRunV2Service_bindIAMPermission(t *testing.T) {
61+
t.Parallel()
62+
63+
project := envvar.GetTestProjectFromEnv()
64+
65+
name := fmt.Sprintf("tf-test-cloud-run-v2-service-%d", acctest.RandInt(t))
66+
location := "us-central1"
67+
id := fmt.Sprintf("projects/%s/locations/%s/services/%s", project, location, name)
68+
69+
acctest.VcrTest(t, resource.TestCase{
70+
PreCheck: func() { acctest.AccTestPreCheck(t) },
71+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
72+
Steps: []resource.TestStep{
73+
{
74+
Config: testAccDataSourceGoogleCloudRunV2Service_bindIAMPermission(name, location),
75+
Check: resource.ComposeTestCheckFunc(
76+
resource.TestCheckResourceAttr("data.google_cloud_run_v2_service.hello", "id", id),
77+
resource.TestCheckResourceAttr("data.google_cloud_run_v2_service.hello", "name", name),
78+
resource.TestCheckResourceAttr("data.google_cloud_run_v2_service.hello", "location", location),
79+
),
80+
},
81+
},
82+
})
83+
}
84+
85+
func testAccDataSourceGoogleCloudRunV2Service_bindIAMPermission(name, location string) string {
86+
return fmt.Sprintf(`
87+
resource "google_cloud_run_v2_service" "hello" {
88+
name = "%s"
89+
location = "%s"
90+
ingress = "INGRESS_TRAFFIC_ALL"
91+
92+
template {
93+
containers {
94+
image = "us-docker.pkg.dev/cloudrun/container/hello"
95+
}
96+
}
97+
}
98+
99+
data "google_cloud_run_v2_service" "hello" {
100+
name = google_cloud_run_v2_service.hello.name
101+
location = google_cloud_run_v2_service.hello.location
102+
}
103+
104+
resource "google_service_account" "foo" {
105+
account_id = "foo-service-account"
106+
display_name = "foo-service-account"
107+
}
108+
109+
resource "google_cloud_run_v2_service_iam_binding" "foo_run_invoker" {
110+
name = data.google_cloud_run_v2_service.hello.name
111+
location = data.google_cloud_run_v2_service.hello.location
112+
113+
role = "roles/run.invoker"
114+
members = [
115+
"serviceAccount:${google_service_account.foo.email}",
116+
]
117+
}
118+
`, name, location)
119+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
subcategory: "Cloud Run"
3+
description: |-
4+
Get information about a Google Cloud Run v2 Service.
5+
---
6+
7+
# google\_cloud\_run\_v2\_service
8+
9+
Get information about a Google Cloud Run v2 Service. For more information see
10+
the [official documentation](https://cloud.google.com/run/docs/)
11+
and [API](https://cloud.google.com/run/docs/apis).
12+
13+
## Example Usage
14+
15+
```hcl
16+
data "google_cloud_run_v2_service" "my_service" {
17+
name = "my-service"
18+
location = "us-central1"
19+
}
20+
```
21+
22+
## Argument Reference
23+
24+
The following arguments are supported:
25+
26+
* `name` - (Required) The name of the Cloud Run v2 Service.
27+
28+
* `location` - (Required) The location of the instance. eg us-central1
29+
30+
- - -
31+
32+
* `project` - (Optional) The project in which the resource belongs. If it
33+
is not provided, the provider project is used.
34+
35+
## Attributes Reference
36+
37+
See [google_cloud_run_v2_service](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/cloud_run_v2_service#argument-reference) resource for details of the available attributes.

0 commit comments

Comments
 (0)