I have several scenarios where I needed to list all GCP regions. (e.g. Cloud Billing API https://cloud.google.com/billing/docs/reference/rest).
I was surprised that there is no API for that.
When you try to search "list of GCP regions" you will end with one of the top results to documentation e.g. https://cloud.google.com/compute/docs/regions-zones or https://cloud.google.com/about/locations. It is not suitable for programmatic access.
I have found recently an endpoint (not API!) with the list of IP ranges for each GCP regions
https://www.gstatic.com/ipranges/cloud.json
That was the last piece of the puzzle to create my desired function.
Here is a snippet for Google Apps Script:
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
/** | |
* Get all Google Cloud Regions | |
* @author Ivan Kutil | |
* | |
* @return {array} Array of GCP regions | |
*/ | |
function getGoogleCloudRegions() { | |
const URL = "https://www.gstatic.com/ipranges/cloud.json"; | |
let json = UrlFetchApp.fetch(URL).getContentText(); | |
let data = JSON.parse(json); | |
let regions = data.prefixes.reduce( (agg, region) => { | |
if (agg.indexOf(region.scope) < 0 && region.scope != "global") { | |
agg.push(region.scope); | |
} | |
return agg; | |
}, []); | |
console.log(regions) | |
return regions; | |
} |
Tweet |