Skip to content

Commit 56b36bf

Browse files
Add Cloud Run Locations datasource (#4360) (#8192)
Co-authored-by: upodroid <cy@borg.dev> Signed-off-by: Modular Magician <magic-modules@google.com> Co-authored-by: upodroid <cy@borg.dev>
1 parent 0270dd0 commit 56b36bf

14 files changed

+255
-20
lines changed

.changelog/4360.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-datasource
2+
`google_cloud_run_locations`
3+
```
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package google
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"sort"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
)
10+
11+
func dataSourceGoogleCloudRunLocations() *schema.Resource {
12+
return &schema.Resource{
13+
Read: dataSourceGoogleCloudRunLocationsRead,
14+
Schema: map[string]*schema.Schema{
15+
"project": {
16+
Type: schema.TypeString,
17+
Optional: true,
18+
Computed: true,
19+
},
20+
"locations": {
21+
Type: schema.TypeList,
22+
Computed: true,
23+
Elem: &schema.Schema{Type: schema.TypeString},
24+
},
25+
},
26+
}
27+
}
28+
29+
func dataSourceGoogleCloudRunLocationsRead(d *schema.ResourceData, meta interface{}) error {
30+
config := meta.(*Config)
31+
userAgent, err := generateUserAgentString(d, config.userAgent)
32+
if err != nil {
33+
return err
34+
}
35+
36+
project, err := getProject(d, config)
37+
if err != nil {
38+
return err
39+
}
40+
41+
url, err := replaceVars(d, config, "https://run.googleapis.com/v1/projects/{{project}}/locations")
42+
if err != nil {
43+
return err
44+
}
45+
46+
res, err := sendRequest(config, "GET", project, url, userAgent, nil)
47+
if err != nil {
48+
return fmt.Errorf("Error listing Cloud Run Locations : %s", err)
49+
}
50+
51+
locationsRaw := flattenCloudRunLocations(res)
52+
53+
locations := make([]string, len(locationsRaw))
54+
for i, loc := range locationsRaw {
55+
locations[i] = loc.(string)
56+
}
57+
sort.Strings(locations)
58+
59+
log.Printf("[DEBUG] Received Google Cloud Run Locations: %q", locations)
60+
61+
if err := d.Set("project", project); err != nil {
62+
return fmt.Errorf("Error setting project: %s", err)
63+
}
64+
if err := d.Set("locations", locations); err != nil {
65+
return fmt.Errorf("Error setting location: %s", err)
66+
}
67+
68+
d.SetId(fmt.Sprintf("projects/%s", project))
69+
70+
return nil
71+
}
72+
73+
func flattenCloudRunLocations(resp map[string]interface{}) []interface{} {
74+
regionList := resp["locations"].([]interface{})
75+
regions := make([]interface{}, len(regionList))
76+
for i, v := range regionList {
77+
regionObj := v.(map[string]interface{})
78+
regions[i] = regionObj["locationId"]
79+
}
80+
return regions
81+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package google
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"strconv"
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
11+
)
12+
13+
func TestAccDataSourceGoogleCloudRunLocations_basic(t *testing.T) {
14+
t.Parallel()
15+
16+
vcrTest(t, resource.TestCase{
17+
PreCheck: func() { testAccPreCheck(t) },
18+
Providers: testAccProviders,
19+
Steps: []resource.TestStep{
20+
{
21+
Config: testAccDataSourceGoogleCloudRunLocationsBasic,
22+
Check: resource.ComposeTestCheckFunc(
23+
testAccCheckGoogleCloudRunLocations("data.google_cloud_run_locations.default"),
24+
),
25+
},
26+
},
27+
})
28+
}
29+
30+
func testAccCheckGoogleCloudRunLocations(n string) resource.TestCheckFunc {
31+
return func(s *terraform.State) error {
32+
rs, ok := s.RootModule().Resources[n]
33+
if !ok {
34+
return fmt.Errorf("Can't find cloud run locations data source: %s", n)
35+
}
36+
37+
if rs.Primary.ID == "" {
38+
return errors.New("data source id not set")
39+
}
40+
41+
count, ok := rs.Primary.Attributes["locations.#"]
42+
if !ok {
43+
return errors.New("can't find 'locations' attribute")
44+
}
45+
46+
cnt, err := strconv.Atoi(count)
47+
if err != nil {
48+
return errors.New("failed to read number of locations")
49+
}
50+
if cnt < 5 {
51+
return fmt.Errorf("expected at least 5 locations, received %d, this is most likely a bug", cnt)
52+
}
53+
54+
for i := 0; i < cnt; i++ {
55+
idx := fmt.Sprintf("locations.%d", i)
56+
_, ok := rs.Primary.Attributes[idx]
57+
if !ok {
58+
return fmt.Errorf("expected %q, location not found", idx)
59+
}
60+
}
61+
return nil
62+
}
63+
}
64+
65+
const testAccDataSourceGoogleCloudRunLocationsBasic = `
66+
data "google_cloud_run_locations" "default" {}
67+
`

google/data_source_tpu_tensorflow_versions_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ func testAccCheckGoogleTpuTensorflowVersions(n string) resource.TestCheckFunc {
3535
}
3636

3737
if rs.Primary.ID == "" {
38-
return errors.New("data source ID not set.")
38+
return errors.New("data source id not set")
3939
}
4040

4141
count, ok := rs.Primary.Attributes["versions.#"]
4242
if !ok {
43-
return errors.New("can't find 'names' attribute")
43+
return errors.New("can't find 'versions' attribute")
4444
}
4545

4646
cnt, err := strconv.Atoi(count)

google/iam_cloud_run_service_generated_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ resource "google_cloud_run_service" "default" {
128128
template {
129129
spec {
130130
containers {
131-
image = "gcr.io/cloudrun/hello"
131+
image = "us-docker.pkg.dev/cloudrun/container/hello"
132132
}
133133
}
134134
}
@@ -158,7 +158,7 @@ resource "google_cloud_run_service" "default" {
158158
template {
159159
spec {
160160
containers {
161-
image = "gcr.io/cloudrun/hello"
161+
image = "us-docker.pkg.dev/cloudrun/container/hello"
162162
}
163163
}
164164
}
@@ -194,7 +194,7 @@ resource "google_cloud_run_service" "default" {
194194
template {
195195
spec {
196196
containers {
197-
image = "gcr.io/cloudrun/hello"
197+
image = "us-docker.pkg.dev/cloudrun/container/hello"
198198
}
199199
}
200200
}
@@ -226,7 +226,7 @@ resource "google_cloud_run_service" "default" {
226226
template {
227227
spec {
228228
containers {
229-
image = "gcr.io/cloudrun/hello"
229+
image = "us-docker.pkg.dev/cloudrun/container/hello"
230230
}
231231
}
232232
}
@@ -256,7 +256,7 @@ resource "google_cloud_run_service" "default" {
256256
template {
257257
spec {
258258
containers {
259-
image = "gcr.io/cloudrun/hello"
259+
image = "us-docker.pkg.dev/cloudrun/container/hello"
260260
}
261261
}
262262
}

google/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,7 @@ func Provider() *schema.Provider {
628628
"google_cloudfunctions_function": dataSourceGoogleCloudFunctionsFunction(),
629629
"google_cloud_identity_groups": dataSourceGoogleCloudIdentityGroups(),
630630
"google_cloud_identity_group_memberships": dataSourceGoogleCloudIdentityGroupMemberships(),
631+
"google_cloud_run_locations": dataSourceGoogleCloudRunLocations(),
631632
"google_cloud_run_service": dataSourceGoogleCloudRunService(),
632633
"google_composer_environment": dataSourceGoogleComposerEnvironment(),
633634
"google_composer_image_versions": dataSourceGoogleComposerImageVersions(),

google/resource_cloud_run_domain_mapping_generated_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ resource "google_cloud_run_service" "default" {
6666
template {
6767
spec {
6868
containers {
69-
image = "gcr.io/cloudrun/hello"
69+
image = "us-docker.pkg.dev/cloudrun/container/hello"
7070
}
7171
}
7272
}

google/resource_cloud_run_service_generated_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ resource "google_cloud_run_service" "default" {
6161
template {
6262
spec {
6363
containers {
64-
image = "gcr.io/cloudrun/hello"
64+
image = "us-docker.pkg.dev/cloudrun/container/hello"
6565
}
6666
}
6767
}
@@ -112,7 +112,7 @@ resource "google_cloud_run_service" "default" {
112112
template {
113113
spec {
114114
containers {
115-
image = "gcr.io/cloudrun/hello"
115+
image = "us-docker.pkg.dev/cloudrun/container/hello"
116116
}
117117
}
118118
@@ -177,7 +177,7 @@ resource "google_cloud_run_service" "default" {
177177
template {
178178
spec {
179179
containers {
180-
image = "gcr.io/cloudrun/hello"
180+
image = "us-docker.pkg.dev/cloudrun/container/hello"
181181
}
182182
}
183183
}
@@ -240,7 +240,7 @@ resource "google_cloud_run_service" "default" {
240240
template {
241241
spec {
242242
containers {
243-
image = "gcr.io/cloudrun/hello"
243+
image = "us-docker.pkg.dev/cloudrun/container/hello"
244244
env {
245245
name = "SOURCE"
246246
value = "remote"

google/resource_compute_region_network_endpoint_group_generated_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ resource "google_cloud_run_service" "cloudrun_neg" {
136136
template {
137137
spec {
138138
containers {
139-
image = "gcr.io/cloudrun/hello"
139+
image = "us-docker.pkg.dev/cloudrun/container/hello"
140140
}
141141
}
142142
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---
2+
subcategory: "Cloud Run"
3+
layout: "google"
4+
page_title: "Google: google_cloud_run_locations"
5+
sidebar_current: "docs-google-datasource-cloud-run-locations"
6+
description: |-
7+
Get Cloud Run locations available for a project.
8+
---
9+
10+
# google\_cloud\_run\_locations
11+
12+
Get Cloud Run locations available for a project.
13+
14+
To get more information about Cloud Run, see:
15+
16+
* [API documentation](https://cloud.google.com/run/docs/reference/rest/v1/projects.locations)
17+
* How-to Guides
18+
* [Official Documentation](https://cloud.google.com/run/docs/)
19+
20+
## Example Usage
21+
22+
```hcl
23+
data "google_cloud_run_locations" "available" {
24+
}
25+
```
26+
27+
## Example Usage: Multi-regional Cloud Run deployment
28+
29+
```hcl
30+
data "google_cloud_run_locations" "available" {
31+
}
32+
33+
resource "google_cloud_run_service" "service_one" {
34+
name = "service-one"
35+
location = data.google_cloud_run_locations.available.locations[0]
36+
37+
template {
38+
spec {
39+
containers {
40+
image = "us-docker.pkg.dev/cloudrun/container/hello"
41+
}
42+
}
43+
}
44+
traffic {
45+
percent = 100
46+
latest_revision = true
47+
}
48+
}
49+
50+
resource "google_cloud_run_service" "service_two" {
51+
name = "service-two"
52+
location = data.google_cloud_run_locations.available.locations[1]
53+
54+
template {
55+
spec {
56+
containers {
57+
image = "us-docker.pkg.dev/cloudrun/container/hello""
58+
}
59+
}
60+
}
61+
traffic {
62+
percent = 100
63+
latest_revision = true
64+
}
65+
}
66+
```
67+
68+
## Argument Reference
69+
70+
The following arguments are supported:
71+
72+
* `project` - (Optional) The project to list versions for. If it
73+
is not provided, the provider project is used.
74+
75+
## Attributes Reference
76+
77+
The following attributes are exported:
78+
79+
* `locations` - The list of Cloud Run locations available for the given project.

0 commit comments

Comments
 (0)