0

I need to make this request from a Google Cloud Function:

POST https://compute.googleapis.com/compute/v1/projects/project-id/zones/zone/instanceGroupManagers/instance-group-name/resize?size=new-size

as per these docs: https://cloud.google.com/compute/docs/instance-groups/creating-groups-of-managed-instances#api_2

I understand how to assign a JSON service-account key and do it with a googleapi library. But in this case all I need the function to do is this single request. So, I'd like to do it simply with fetch(). How do I write the authorization?

const url = `https://compute.googleapis.com/compute/v1/projects/${projectId}/zones/${zone}/instanceGroupManagers/${instanceGroupName}/resize?size=${size}`

const response = await fetch(url, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  }
});
const responseJson = response.json();
stkvtflw
  • 12,092
  • 26
  • 78
  • 155

1 Answers1

0

You can use the google auth library to get the correct credential and to call the API

const {GoogleAuth} = require('google-auth-library');

async function main() {
  const auth = new GoogleAuth({
    scopes: 'https://www.googleapis.com/auth/cloud-platform'
  });
  const client = await auth.getClient();

  const url = `https://compute.googleapis.com/compute/v1/projects/${projectId}/zones/${zone}/instanceGroupManagers/${instanceGroupName}/resize?size=${size}`

  const res = await client.request({ url: url,
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  }
});
  console.log(res.data);
}

main().catch(console.error);

guillaume blaquiere
  • 66,369
  • 2
  • 47
  • 76