-
Notifications
You must be signed in to change notification settings - Fork 2
/
integration.js
211 lines (189 loc) · 6.52 KB
/
integration.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
'use strict';
let redis = require('redis');
let _ = require('lodash');
let async = require('async');
let Logger;
let client;
let clientOptions;
/**
* This method is called once when the integration is first started. It is passed a Bunyan logging object
* that we can save and use to log data to the integration log file.
* @param logger
*/
function startup(logger) {
Logger = logger;
}
function doLookup(entities, options, cb) {
let lookupResults = [];
Logger.trace({entities: entities}, 'doLookup');
_initRedisClient(options, function(err){
if(err){
cb(err);
return;
}
async.each(entities, function (entityObj, next) {
if (entityObj.isIPv4) {
_lookupIp(entityObj, options, function (err, result) {
if (err) {
next(err);
} else {
lookupResults.push(result);
next(null);
}
});
} else {
next(null);
}
}, function (err) {
Logger.trace({lookupResults: lookupResults}, 'Lookup Results');
cb(err, lookupResults);
});
});
}
/**
* Initializes the redis client if it has not already been initialized. In addition, if the connection options
* have been changed by the user, the client is recreated to reflect the new connection options. Finally, this method
* selects the correct database as specified by the integration options.
*
* @param integrationOptions
* @param cb
* @private
*/
function _initRedisClient(integrationOptions, cb) {
if (typeof clientOptions === 'undefined') {
clientOptions = {
host: integrationOptions.host,
port: integrationOptions.port,
database: integrationOptions.database
};
}
let newOptions = {
host: integrationOptions.host,
port: integrationOptions.port,
database: integrationOptions.database
};
if (typeof client === 'undefined' || _optionsHaveChanged(clientOptions, newOptions)) {
clientOptions = newOptions;
_closeRedisClient(client, function(){
client = redis.createClient(clientOptions);
client.select(integrationOptions.database, function(err){
if(err){
Logger.error({err:err}, 'Error Changing Database');
}
Logger.info({con:clientOptions}, 'Created Redis Client');
cb(null);
});
});
}else{
cb(null);
}
}
function _closeRedisClient(myClient, cb){
if(typeof myClient !== 'undefined'){
Logger.info("Closing Existing Redis Client");
client.quit();
client.on('end', cb);
}else{
cb(null);
}
}
/**
* Compares two javascript object literals and returns true if they are the same, false if not. Is used
* to determine if a user has changed Redis connection options since the last lookup.
*
* @param options1
* @param options2
* @returns {boolean}
* @private
*/
function _optionsHaveChanged(options1, options2) {
return !_.isEqual(options1, options2);
}
function _lookupIp(entityObj, options, cb) {
_doRedisLookup(entityObj.value, function (err, result) {
if (err) {
Logger.error({err: err}, 'Error running sql statement');
cb(err);
return;
} else {
if (result) {
// In our example we are storing valid JSON as the Redis key's value. As a result, we need
// to parse it pack into a javascript object literal.
let resultAsJson = _parseRedisResult(result);
cb(null, {
// Required: This is the entity object passed into the integration doLookup method
entity: entityObj,
// Required: An object containing everything you want passed to the template
data: {
// Required: These are the tags that are displayed in your template
summary: [resultAsJson.hostname],
// Data that you want to pass back to the notification window details block
details: resultAsJson
}
});
} else {
// There was no data for this entity so we return `null` data which will cause the integration
// cache to cache this lookup as a miss.
cb(null, {entity: entityObj, data: null});
}
}
});
}
/**
* This function implements the Redis lookup logic you would like to use to retrieve data from your Redis instance.
* In the provided example we are getting the key that matches the entity's value.
*
* @param entityObj
* @param cb
* @private
*/
function _doRedisLookup(entityValue, cb){
client.get(entityValue, cb);
}
/**
* Accepts the raw output from the redis lookup command implemented in
* `_doRedisLookup()`. Should do any processing required on this data (e.g., convert
* a JSON string literal into a Javascript Object literal).
*
* @param redisResult
* @private
*/
function _parseRedisResult(redisResult){
return JSON.parse(redisResult);
}
/**
* This method is called anytime a user tries to update options for this integration via the integration
* page. This method should return errors if the options do not validate.
* @param options
*/
function validateOptions(userOptions, cb) {
Logger.debug({userOptions:userOptions}, 'Validating User Options');
let errors = [];
if (typeof userOptions.host.value !== 'string' ||
(typeof userOptions.host.value === 'string' && userOptions.host.value.length === 0)) {
errors.push({
key: 'host',
message: 'You must provide a host value'
})
}
if (typeof userOptions.port.value !== 'string' ||
(typeof userOptions.port.value === 'string' && userOptions.port.value.length === 0)) {
errors.push({
key: 'port',
message: 'You must provide the port Redis is running on'
})
}
if (typeof userOptions.database.value !== 'string' ||
(typeof userOptions.database.value === 'string' && userOptions.database.value.length === 0)) {
errors.push({
key: 'database',
message: 'You must provide the Redis database you are connecting to'
})
}
cb(null, errors);
}
module.exports = {
doLookup: doLookup,
startup: startup,
validateOptions: validateOptions
};