-
-
Save fokosun/d6f52407e3a94d4495a4e99831e70a73 to your computer and use it in GitHub Desktop.
GCP Pub/Sub to Slack message Cloud Function
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
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