diff --git a/.changelog/4360.txt b/.changelog/4360.txt new file mode 100644 index 00000000000..53942a244e4 --- /dev/null +++ b/.changelog/4360.txt @@ -0,0 +1,3 @@ +```release-note:new-datasource +`google_cloud_run_locations` +``` diff --git a/google/data_source_cloud_run_locations.go b/google/data_source_cloud_run_locations.go new file mode 100644 index 00000000000..69df596e731 --- /dev/null +++ b/google/data_source_cloud_run_locations.go @@ -0,0 +1,81 @@ +package google + +import ( + "fmt" + "log" + "sort" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceGoogleCloudRunLocations() *schema.Resource { + return &schema.Resource{ + Read: dataSourceGoogleCloudRunLocationsRead, + Schema: map[string]*schema.Schema{ + "project": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "locations": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + }, + } +} + +func dataSourceGoogleCloudRunLocationsRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + userAgent, err := generateUserAgentString(d, config.userAgent) + if err != nil { + return err + } + + project, err := getProject(d, config) + if err != nil { + return err + } + + url, err := replaceVars(d, config, "https://run.googleapis.com/v1/projects/{{project}}/locations") + if err != nil { + return err + } + + res, err := sendRequest(config, "GET", project, url, userAgent, nil) + if err != nil { + return fmt.Errorf("Error listing Cloud Run Locations : %s", err) + } + + locationsRaw := flattenCloudRunLocations(res) + + locations := make([]string, len(locationsRaw)) + for i, loc := range locationsRaw { + locations[i] = loc.(string) + } + sort.Strings(locations) + + log.Printf("[DEBUG] Received Google Cloud Run Locations: %q", locations) + + if err := d.Set("project", project); err != nil { + return fmt.Errorf("Error setting project: %s", err) + } + if err := d.Set("locations", locations); err != nil { + return fmt.Errorf("Error setting location: %s", err) + } + + d.SetId(fmt.Sprintf("projects/%s", project)) + + return nil +} + +func flattenCloudRunLocations(resp map[string]interface{}) []interface{} { + regionList := resp["locations"].([]interface{}) + regions := make([]interface{}, len(regionList)) + for i, v := range regionList { + regionObj := v.(map[string]interface{}) + regions[i] = regionObj["locationId"] + } + return regions +} diff --git a/google/data_source_cloud_run_locations_test.go b/google/data_source_cloud_run_locations_test.go new file mode 100644 index 00000000000..eb997a54732 --- /dev/null +++ b/google/data_source_cloud_run_locations_test.go @@ -0,0 +1,67 @@ +package google + +import ( + "errors" + "fmt" + "strconv" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" +) + +func TestAccDataSourceGoogleCloudRunLocations_basic(t *testing.T) { + t.Parallel() + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceGoogleCloudRunLocationsBasic, + Check: resource.ComposeTestCheckFunc( + testAccCheckGoogleCloudRunLocations("data.google_cloud_run_locations.default"), + ), + }, + }, + }) +} + +func testAccCheckGoogleCloudRunLocations(n string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Can't find cloud run locations data source: %s", n) + } + + if rs.Primary.ID == "" { + return errors.New("data source id not set") + } + + count, ok := rs.Primary.Attributes["locations.#"] + if !ok { + return errors.New("can't find 'locations' attribute") + } + + cnt, err := strconv.Atoi(count) + if err != nil { + return errors.New("failed to read number of locations") + } + if cnt < 5 { + return fmt.Errorf("expected at least 5 locations, received %d, this is most likely a bug", cnt) + } + + for i := 0; i < cnt; i++ { + idx := fmt.Sprintf("locations.%d", i) + _, ok := rs.Primary.Attributes[idx] + if !ok { + return fmt.Errorf("expected %q, location not found", idx) + } + } + return nil + } +} + +const testAccDataSourceGoogleCloudRunLocationsBasic = ` +data "google_cloud_run_locations" "default" {} +` diff --git a/google/data_source_tpu_tensorflow_versions_test.go b/google/data_source_tpu_tensorflow_versions_test.go index 0c601dee8f5..1398a99d8b8 100644 --- a/google/data_source_tpu_tensorflow_versions_test.go +++ b/google/data_source_tpu_tensorflow_versions_test.go @@ -35,12 +35,12 @@ func testAccCheckGoogleTpuTensorflowVersions(n string) resource.TestCheckFunc { } if rs.Primary.ID == "" { - return errors.New("data source ID not set.") + return errors.New("data source id not set") } count, ok := rs.Primary.Attributes["versions.#"] if !ok { - return errors.New("can't find 'names' attribute") + return errors.New("can't find 'versions' attribute") } cnt, err := strconv.Atoi(count) diff --git a/google/iam_cloud_run_service_generated_test.go b/google/iam_cloud_run_service_generated_test.go index 3ca057a5895..af752b90f6c 100644 --- a/google/iam_cloud_run_service_generated_test.go +++ b/google/iam_cloud_run_service_generated_test.go @@ -128,7 +128,7 @@ resource "google_cloud_run_service" "default" { template { spec { containers { - image = "gcr.io/cloudrun/hello" + image = "us-docker.pkg.dev/cloudrun/container/hello" } } } @@ -158,7 +158,7 @@ resource "google_cloud_run_service" "default" { template { spec { containers { - image = "gcr.io/cloudrun/hello" + image = "us-docker.pkg.dev/cloudrun/container/hello" } } } @@ -194,7 +194,7 @@ resource "google_cloud_run_service" "default" { template { spec { containers { - image = "gcr.io/cloudrun/hello" + image = "us-docker.pkg.dev/cloudrun/container/hello" } } } @@ -226,7 +226,7 @@ resource "google_cloud_run_service" "default" { template { spec { containers { - image = "gcr.io/cloudrun/hello" + image = "us-docker.pkg.dev/cloudrun/container/hello" } } } @@ -256,7 +256,7 @@ resource "google_cloud_run_service" "default" { template { spec { containers { - image = "gcr.io/cloudrun/hello" + image = "us-docker.pkg.dev/cloudrun/container/hello" } } } diff --git a/google/provider.go b/google/provider.go index d92107822fd..aac929a4e7c 100644 --- a/google/provider.go +++ b/google/provider.go @@ -628,6 +628,7 @@ func Provider() *schema.Provider { "google_cloudfunctions_function": dataSourceGoogleCloudFunctionsFunction(), "google_cloud_identity_groups": dataSourceGoogleCloudIdentityGroups(), "google_cloud_identity_group_memberships": dataSourceGoogleCloudIdentityGroupMemberships(), + "google_cloud_run_locations": dataSourceGoogleCloudRunLocations(), "google_cloud_run_service": dataSourceGoogleCloudRunService(), "google_composer_environment": dataSourceGoogleComposerEnvironment(), "google_composer_image_versions": dataSourceGoogleComposerImageVersions(), diff --git a/google/resource_cloud_run_domain_mapping_generated_test.go b/google/resource_cloud_run_domain_mapping_generated_test.go index 876c9712a16..3c692ab039c 100644 --- a/google/resource_cloud_run_domain_mapping_generated_test.go +++ b/google/resource_cloud_run_domain_mapping_generated_test.go @@ -66,7 +66,7 @@ resource "google_cloud_run_service" "default" { template { spec { containers { - image = "gcr.io/cloudrun/hello" + image = "us-docker.pkg.dev/cloudrun/container/hello" } } } diff --git a/google/resource_cloud_run_service_generated_test.go b/google/resource_cloud_run_service_generated_test.go index 98eefaac379..71e5ed94232 100644 --- a/google/resource_cloud_run_service_generated_test.go +++ b/google/resource_cloud_run_service_generated_test.go @@ -61,7 +61,7 @@ resource "google_cloud_run_service" "default" { template { spec { containers { - image = "gcr.io/cloudrun/hello" + image = "us-docker.pkg.dev/cloudrun/container/hello" } } } @@ -112,7 +112,7 @@ resource "google_cloud_run_service" "default" { template { spec { containers { - image = "gcr.io/cloudrun/hello" + image = "us-docker.pkg.dev/cloudrun/container/hello" } } @@ -177,7 +177,7 @@ resource "google_cloud_run_service" "default" { template { spec { containers { - image = "gcr.io/cloudrun/hello" + image = "us-docker.pkg.dev/cloudrun/container/hello" } } } @@ -240,7 +240,7 @@ resource "google_cloud_run_service" "default" { template { spec { containers { - image = "gcr.io/cloudrun/hello" + image = "us-docker.pkg.dev/cloudrun/container/hello" env { name = "SOURCE" value = "remote" diff --git a/google/resource_compute_region_network_endpoint_group_generated_test.go b/google/resource_compute_region_network_endpoint_group_generated_test.go index e68ac730262..cf866d3732c 100644 --- a/google/resource_compute_region_network_endpoint_group_generated_test.go +++ b/google/resource_compute_region_network_endpoint_group_generated_test.go @@ -136,7 +136,7 @@ resource "google_cloud_run_service" "cloudrun_neg" { template { spec { containers { - image = "gcr.io/cloudrun/hello" + image = "us-docker.pkg.dev/cloudrun/container/hello" } } } diff --git a/website/docs/d/cloud_run_locations.html.markdown b/website/docs/d/cloud_run_locations.html.markdown new file mode 100644 index 00000000000..e6ea9736054 --- /dev/null +++ b/website/docs/d/cloud_run_locations.html.markdown @@ -0,0 +1,79 @@ +--- +subcategory: "Cloud Run" +layout: "google" +page_title: "Google: google_cloud_run_locations" +sidebar_current: "docs-google-datasource-cloud-run-locations" +description: |- + Get Cloud Run locations available for a project. +--- + +# google\_cloud\_run\_locations + +Get Cloud Run locations available for a project. + +To get more information about Cloud Run, see: + +* [API documentation](https://cloud.google.com/run/docs/reference/rest/v1/projects.locations) +* How-to Guides + * [Official Documentation](https://cloud.google.com/run/docs/) + +## Example Usage + +```hcl +data "google_cloud_run_locations" "available" { +} +``` + +## Example Usage: Multi-regional Cloud Run deployment + +```hcl +data "google_cloud_run_locations" "available" { +} + +resource "google_cloud_run_service" "service_one" { + name = "service-one" + location = data.google_cloud_run_locations.available.locations[0] + + template { + spec { + containers { + image = "us-docker.pkg.dev/cloudrun/container/hello" + } + } + } + traffic { + percent = 100 + latest_revision = true + } +} + +resource "google_cloud_run_service" "service_two" { + name = "service-two" + location = data.google_cloud_run_locations.available.locations[1] + + template { + spec { + containers { + image = "us-docker.pkg.dev/cloudrun/container/hello"" + } + } + } + traffic { + percent = 100 + latest_revision = true + } +} +``` + +## Argument Reference + +The following arguments are supported: + +* `project` - (Optional) The project to list versions for. If it + is not provided, the provider project is used. + +## Attributes Reference + +The following attributes are exported: + +* `locations` - The list of Cloud Run locations available for the given project. diff --git a/website/docs/r/cloud_run_domain_mapping.html.markdown b/website/docs/r/cloud_run_domain_mapping.html.markdown index 80a7ec934b2..66f7f3d0fc3 100644 --- a/website/docs/r/cloud_run_domain_mapping.html.markdown +++ b/website/docs/r/cloud_run_domain_mapping.html.markdown @@ -52,7 +52,7 @@ resource "google_cloud_run_service" "default" { template { spec { containers { - image = "gcr.io/cloudrun/hello" + image = "us-docker.pkg.dev/cloudrun/container/hello" } } } diff --git a/website/docs/r/cloud_run_service.html.markdown b/website/docs/r/cloud_run_service.html.markdown index e6f395070cc..381287788eb 100644 --- a/website/docs/r/cloud_run_service.html.markdown +++ b/website/docs/r/cloud_run_service.html.markdown @@ -60,7 +60,7 @@ resource "google_cloud_run_service" "default" { template { spec { containers { - image = "gcr.io/cloudrun/hello" + image = "us-docker.pkg.dev/cloudrun/container/hello" } } } @@ -87,7 +87,7 @@ resource "google_cloud_run_service" "default" { template { spec { containers { - image = "gcr.io/cloudrun/hello" + image = "us-docker.pkg.dev/cloudrun/container/hello" } } @@ -128,7 +128,7 @@ resource "google_cloud_run_service" "default" { template { spec { containers { - image = "gcr.io/cloudrun/hello" + image = "us-docker.pkg.dev/cloudrun/container/hello" } } } @@ -167,7 +167,7 @@ resource "google_cloud_run_service" "default" { template { spec { containers { - image = "gcr.io/cloudrun/hello" + image = "us-docker.pkg.dev/cloudrun/container/hello" env { name = "SOURCE" value = "remote" @@ -204,7 +204,7 @@ resource "google_cloud_run_service" "default" { template { spec { containers { - image = "gcr.io/cloudrun/hello" + image = "us-docker.pkg.dev/cloudrun/container/hello" } } metadata { diff --git a/website/docs/r/compute_region_network_endpoint_group.html.markdown b/website/docs/r/compute_region_network_endpoint_group.html.markdown index 4e3c660914c..78fabd4c766 100644 --- a/website/docs/r/compute_region_network_endpoint_group.html.markdown +++ b/website/docs/r/compute_region_network_endpoint_group.html.markdown @@ -103,7 +103,7 @@ resource "google_cloud_run_service" "cloudrun_neg" { template { spec { containers { - image = "gcr.io/cloudrun/hello" + image = "us-docker.pkg.dev/cloudrun/container/hello" } } } diff --git a/website/google.erb b/website/google.erb index be9aa1b4c49..cc843429465 100644 --- a/website/google.erb +++ b/website/google.erb @@ -1182,6 +1182,10 @@ Data Sources