Created
July 5, 2019 09:54
-
-
Save abhisek/d2969519b2084e562e799b67fd8a2bce to your computer and use it in GitHub Desktop.
Mongo Cloud Atlas Events API to Elasticsearch
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
'use strict' | |
const MONGO_ATLAS_USERNAME = 'USER' | |
const MONGO_ATLAS_APIKEY = 'APIKEY' | |
const MONGO_ATLAS_STAGING_GROUP_ID = 'ID1' | |
const MONGO_ATLAS_PRODUCTION_GROUP_ID = 'ID2' | |
const MONGO_ATLAS_EVENTS_API = 'https://cloud.mongodb.com/api/atlas/v1.0/groups/{{GROUP-ID}}/events' | |
const ELASTICSEARCH_INDEX_URL = 'https://ES-ENDPOINT' | |
const request = require('request') | |
function pushToElasticsearch(groupName, data) { | |
data.results.forEach(function (result) { | |
console.log(`Sending ${result.id} to ${groupName} Elasticsearch`) | |
result.logEnv = groupName | |
let url = ELASTICSEARCH_INDEX_URL.replace('{{OBJECT_ID}}', result.id) | |
request.put(url, | |
function (error, response, body) { | |
if (error) { | |
console.log(`Error sending to ES:${error}`) | |
} | |
}) | |
.json(result) | |
}) | |
} | |
function pushActivityForGroup(groupName, groupId) { | |
request.get(MONGO_ATLAS_EVENTS_API.replace('{{GROUP-ID}}', groupId), | |
function (error, response, body) { | |
if ((!error) && (response.statusCode === 200)) { | |
pushToElasticsearch(groupName, JSON.parse(body)) | |
} | |
else { | |
console.error(`Failed to fetch from Atlas API - Group:${groupName} Status:${response.statusCode} Error:${error}`) | |
} | |
}) | |
.auth(MONGO_ATLAS_USERNAME, MONGO_ATLAS_APIKEY, false) | |
} | |
exports.handler = function (event, context, callback) { | |
pushActivityForGroup('staging', MONGO_ATLAS_STAGING_GROUP_ID) | |
pushActivityForGroup('production', MONGO_ATLAS_PRODUCTION_GROUP_ID) | |
callback(null, 'Finished successfully') | |
}; | |
exports.handler (null, null, function () { }) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment