google-cloud-platformgoogle-compute-engine

Is there an API to list all google compute regions


I am looking to list all the available regions for GCP using nodejs SDK Thanks for valuable suggestions and links!


Solution

  • You can list all the zones (available for your project) for the Compute Engine. You can't do it for other products (GAE, GKE) but since everything is tied up to the compute engine it usually is the same.

    Here's the example from the documentation page describing how to do this in Node.js

    const {google} = require('googleapis');
    var compute = google.compute('v1');
    
    authorize(function(authClient) {
      var request = {
        // Project ID for this request.
        project: 'my-project',  // TODO: Update placeholder value.
    
        auth: authClient,
      };
    
      var handlePage = function(err, response) {
        if (err) {
          console.error(err);
          return;
        }
    
        var itemsPage = response['items'];
        if (!itemsPage) {
          return;
        }
        for (var i = 0; i < itemsPage.length; i++) {
          // TODO: Change code below to process each resource in `itemsPage`:
          console.log(JSON.stringify(itemsPage[i], null, 2));
        }
    
        if (response.nextPageToken) {
          request.pageToken = response.nextPageToken;
          compute.regions.list(request, handlePage);
        }
      };
    
      compute.regions.list(request, handlePage);
    });
    
    function authorize(callback) {
      google.auth.getClient({
        scopes: ['https://www.googleapis.com/auth/cloud-platform']
      }).then(client => {
        callback(client);
      }).catch(err => {
        console.error('authentication failed: ', err);
      });
    }
    

    You can also list all available zones (for compute engine) with the gcloud command: gcloud compute zones list -you can find exact syntax and it's documentation here.