-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Add Cloud Run Locations datasource #4360
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
71c3aeb
e1a9488
51eaff6
649e97c
ee373cc
d330773
0d73e9f
191a775
a08285c
b8c5483
7ae332d
b8f9f48
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason why we don't use strong types here? Locations response struct exists in run/v1 package.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, it looks like the team is using raw HTTP calls instead of adding new
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In general it's fine either way- for example I want to say the TPU service library didn't exist yet, or we hadn't imported the service library and preferred not to. |
||
| for i, v := range regionList { | ||
| regionObj := v.(map[string]interface{}) | ||
| regions[i] = regionObj["locationId"] | ||
| } | ||
| return regions | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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" {} | ||
| ` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. |
Uh oh!
There was an error while loading. Please reload this page.