Skip to content

Instantly share code, notes, and snippets.

@dhavaln
Last active August 22, 2019 06:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhavaln/7a8a2f233112cf73b09299efd26374ea to your computer and use it in GitHub Desktop.
Save dhavaln/7a8a2f233112cf73b09299efd26374ea to your computer and use it in GitHub Desktop.
Compute calls from Cloud Function

Test Values

Make sure you change the project and firewall resource Ids of your environment.

Local Testing

gcloud beta auth application-default login
const {auth, Compute} = require('google-auth-library');
const {google} = require('googleapis');
var compute = google.compute('v1');
function getFirewallDetail(authClient, cb){
var request = {
project: 'project id',
firewall: 'firewall resource id',
auth: authClient,
};
console.log('Getting Firewall Entries');
compute.firewalls.get(request, cb);
}
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);
});
}
updateFirewall = (req, res) => {
authorize((client)=>{
getFirewallDetail(client, (err, response) => {
if (err) {
if(res) res.status(200).send(err);
return;
}
console.log(response.data)
if(res) res.status(200).send(response.data);
});
});
};
exports.updateFirewall = updateFirewall;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment