Skip to content

Instantly share code, notes, and snippets.

@ismailbaskin
Created July 9, 2018 20:51
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ismailbaskin/1b9ec61c7fc75acc821678d805159144 to your computer and use it in GitHub Desktop.
Save ismailbaskin/1b9ec61c7fc75acc821678d805159144 to your computer and use it in GitHub Desktop.
GCP Pub/Sub to Slack message Cloud Function
const https = require('https');
const url = require('url');
const slackWebhookURL = 'https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX'; // CHANGE ME PLZ!
exports.gceAudit = (event, callback) => {
const msg = JSON.parse(Buffer.from(event.data.data, 'base64').toString());
const slackRequest = https.request({
hostname: url.parse(slackWebhookURL).hostname,
method: 'POST',
path: url.parse(slackWebhookURL).path,
headers: {
'Content-Type': 'application/json',
}
});
slackRequest.write(JSON.stringify({
'text': `*Project :* ${msg.resource.labels.project_id}
*Zone :* ${msg.resource.labels.zone}
*Name :* ${msg.protoPayload.resourceName.split('/').pop()}
*Method :* ${msg.protoPayload.methodName.split('.').pop()}
*Status :* ${msg.operation.first ? 'Started' : 'Finished'}`,
'mrkdwn': true
}));
slackRequest.end();
callback();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment