Last active
April 28, 2018 05:06
-
-
Save jaycdave88/2d341d08a00eaa46e87707f2ebe87bc0 to your computer and use it in GitHub Desktop.
Datadog Slack Integrations list via Azure 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
module.exports = function(context, req) { | |
//add request-json module | |
var request = require("request-json"); | |
//function for creating the obj based on the google custom search | |
function addToObj(title,link,displayLink){ | |
return{ | |
title: title, | |
link: link | |
} | |
}; | |
//creation of the iteration of the custom search results | |
function googleCustomSearchObj(integration){ | |
return { | |
color: datadogColor, | |
title: integration.title, | |
title_link: integration.link | |
} | |
}; | |
//datadog HEX colors | |
var datadogColors = ["#FF59C7", "#AA169B","#8E06A7","#7B0298","#B804AB","#DB08E1","#9304B3","#9C0ABC"]; | |
//integraton list client creation | |
var integrationClient = request.createClient("https://app.datadoghq.com/"); | |
//google seach or CSE client creation | |
var googleCustomSearch = request.createClient("https://www.googleapis.com/"); | |
//incoming webhook to slack client creation | |
var slackHook = request.createClient( | |
"https://hooks.slack.com/services/[INFO]/[API_KEY]"); | |
// random datadog color picker; | |
var datadogColor = datadogColors[Math.floor(Math.random() * datadogColors.length)]; | |
//json response from slack | |
var slack_resp = req.body; | |
//regex to grab the data just from text= and end at & | |
var regex_text = new RegExp(/text=(.*)&/, "i"); | |
//regex to grab the user id from the slack json | |
var regex_user = new RegExp(/user_id=([a-z0-9]*)&/, "i"); | |
//regex matching | |
var integration_name = slack_resp.match(regex_text)[1]; | |
var slack_user = slack_resp.match(regex_user)[1]; | |
//replace the slack json response with a space rather then the + | |
var mutated_integration_name_response = integration_name | |
.replace(/[+]/g, " ") | |
.toLowerCase(); | |
// conditional to check if flag is associated with the slack payload and intro of control flow to only conduct one GET request. | |
if (mutated_integration_name_response.includes("-kb")) { | |
googleCustomSearch.get( | |
`customsearch/v1?c2off=1&cr=countryUS&cx=[CSE]{mutated_integration_name_response.replace("-kb", "")}&key=[API_KEY]` | |
).then(function(response){ | |
//container for the results from search | |
var googleCustomSearch = []; | |
//iteration of all the results and population of objs | |
for (var item in response.body.items) { | |
var matchingDoc = addToObj(response.body.items[item].title, response.body.items[item].link); | |
googleCustomSearch.push(matchingDoc); | |
} | |
//post back to the incomming webhook of slack | |
return slackHook.post("", { | |
text: `<@${slack_user}> - Here are some docs ...`, | |
attachments: [{ | |
color: datadogColor, | |
}].concat(googleCustomSearch.map(googleCustomSearchObj)) | |
}) | |
}).catch(function(error){ | |
return slack.post("", { | |
text: `<@${slack_user}> - Looks like an error occured: ${error}` | |
}); | |
}); | |
}else{ | |
integrationClient.get("config").then(function(response){ | |
var ffs = response.body.feature_flags; | |
var dataDogIntegration = { | |
isSupported: false, | |
name: "", | |
rawName: "" | |
}; | |
for (var key in ffs) { | |
//removing the _ characters and replacing with spaces | |
var ffs_parsed = ffs[key].replace(/_/g, " "); | |
if (mutated_integration_name_response === ffs_parsed) { | |
dataDogIntegration.isSupported = true; | |
dataDogIntegration.name = ffs_parsed; | |
dataDogIntegration.rawName = ffs[key]; | |
break; | |
} | |
} | |
if (dataDogIntegration.isSupported) { | |
return slackHook.post("", { | |
text: `<@${slack_user}> - _Yes_, *${dataDogIntegration.name}* is supported!`, | |
attachments: [ | |
{ | |
color: datadogColor, | |
title: "Core Integrations Search", | |
title_link: `https://app.datadoghq.com/account/settings#integrations/${dataDogIntegration.rawName}/` | |
}, | |
{ | |
color: datadogColor, | |
title: "Datadog Docs", | |
title_link: `https://docs.datadoghq.com/integrations/${dataDogIntegration.rawName}/` | |
}] //.concat(googleCustomSearch.map(googleCustomSearchObj)) | |
}); | |
}else{ | |
return slackHook.post("", { | |
text: `<@${slack_user}> - I could not find anything matching, *_${mutated_integration_name_response}_*` + ". Please try again or try using the `-kb` flag to search for some docs." | |
}); | |
} | |
}).catch(function(error){ | |
return slack.post("", { | |
text: `<@${slack_user}> - Looks like an error occured: ${error}` | |
}); | |
}); | |
} | |
} |
noahbeddome
commented
Apr 18, 2018
- Key/ hook should not be hard coded this should be handled with something like KMS/SSM
- There doesn't appear to be any error handling really at all
- The code is written not defensively some examples may include https://github.com/ExpanseLLC/lambda_authorizer/blob/master/lib/GoogleIdModule.js#L49
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment