-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSupportCasesSync.js
476 lines (418 loc) · 18.6 KB
/
SupportCasesSync.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
'use strict';
var readConfig = require('read-config'),
config = readConfig('config.json');
var AWS = require("aws-sdk");
AWS.config.update({
region: config.awsRegion,
});
const EventEmitter = require('events');
class AwsSupportEmitter extends EventEmitter { };
const awsSupportEmitter = new AwsSupportEmitter();
var request = require('request');
var dateFormat = require('dateformat');
//Walking through array of accounts and invoking getAndUpdateCases with prepared params and credentials
function walkThroughAccounts() {
let accountsProcessed = 0;
config.awsIamAccountArns.forEach(function (account) {
let awsCentralAccessCreds = new AWS.TemporaryCredentials({
RoleArn: account.arn,
});
let awsSupportParams = {
afterTime: getIsoTimeBack(),
includeCommunications: true,
includeResolvedCases: true,
maxResults: 50
};
console.log('Getting records for account: ' + account.name);
getCases(awsCentralAccessCreds, awsSupportParams);
accountsProcessed++;
if (config.awsIamAccountArns.length == accountsProcessed) {
sendSnsMessage(`arn:aws:sns:${config.awsRegion}:${config.awsAccountId}:${config.snsTopicName}`, { message: "getAndUpdateCases ready" });
}
}, this);
}
//Recursive function list through AWS cases for specified account
var getCases = function (awsCentralAccessCreds, awsSupportParams) {
//us-east-1 is the region used for support cases api
let support = new AWS.Support({ apiVersion: '2013-04-15', region: 'us-east-1', credentials: awsCentralAccessCreds });
support.describeCases(awsSupportParams, function (err, data) {
if (err) {
console.log(err, err.stack);
return;
}
awsSupportEmitter.emit('get_cases_ready', data.cases, support);
//Going recursively via all pages
if (data.hasOwnProperty("nextToken")) {
console.log(`Got nextToken data.nextToken on cases listing`);
awsSupportParams.nextToken = data.nextToken;
//calling itself in nextToken exist
getCases(awsCentralAccessCreds, awsSupportParams);
}
});
};
//This event Emitter formats communications got from AWS Api and feeds the Dynamo table
awsSupportEmitter.on('get_cases_ready', function (cases, supportInstance) {
cases.forEach(function (supportCase) {
let communicationsParams = {
caseId: supportCase.caseId,
maxResults: 100
};
let i = 0;
let walkThroughCommunications = function (communicationsParams) {
supportInstance.describeCommunications(communicationsParams, function (err, allRecentCommunications) {
if (err) {
console.log(err, err.stack);
return;
}
//Reversing the communications so that the newest message has max ID and the first one goes with ID - 0
allRecentCommunications.communications.reverse();
//Feeding the communications table
allRecentCommunications.communications.forEach(function (communication) {
//Communication ID consist of case Id and sort order number
communication.CommunicationId = supportCase.caseId + '-' + i;
//Getting displayId and subject from the core case
communication.displayId = supportCase.displayId;
communication.subject = supportCase.subject;
//Assuming this is new item for which JiraUpdated supposed to be 0
communication.JiraUpdated = 0;
//The sort order which is used later to publish SQS messages in correct order
communication.Sortorder = i;
i++;
addNewItem(communication, 'CommunicationId')
});
});
}
walkThroughCommunications(communicationsParams);
}, this);
});
/*
This function will make an object containing sorted arrays of communications.
After the object it filled in - async Jira update is called for each case
*/
function processCommunications(jiraUpdated = 0) {
//Querying Dynamo table for cases ready to be added to Jira
var params = {
TableName: config.dynamoTableName,
IndexName: 'JiraUpdated-timeCreated-index',
KeyConditionExpression: 'JiraUpdated=:update_status AND timeCreated > :after_date',
ExpressionAttributeValues: {
':after_date': getIsoTimeBack(),
':update_status': jiraUpdated
}
};
var documentClient = new AWS.DynamoDB.DocumentClient();
documentClient.query(params, function (err, data) {
if (err) {
console.log(err);
return;
}
//Sorting the array
let items = data.Items.sort(function (a, b) {
return a.displayId - b.displayId || a.Sortorder - b.Sortorder;
});
let itemsProcessed = 0;
let commsObj = {}; //Object which will contain arrays of sorted communications
let lastDisplayId = null;
//Sync iteration, to keep order
for (let i = 0; i < items.length; i++) {
(function (cntr) {
let message = items[cntr];
if (message.displayId != lastDisplayId) {
commsObj[message.displayId] = new Array();
}
commsObj[message.displayId].push(message);
itemsProcessed++;
lastDisplayId = message.displayId;
if (itemsProcessed === items.length) {
for (var displayId in commsObj) {
//Assuming that jiraUpdated ==0 means query used for AWS->Jira sync. Otherwise Jira->AWS is invoked.
if (jiraUpdated == 0) {
findJiras(commsObj[displayId], displayId, updateJira);
}
else {
findJiras(commsObj[displayId], displayId, sendReply);
}
}
}
})(i);
}
});
}
//Asynchronously searches for Jira IDs by specified AWS support case ID
function findJiras(communicationsArr, displayId, callback) {
console.log(`${callback.name}: Querying Jira with AWS Support Reference: ${displayId}`);
request({
url: `${config.jiraApiHost}/search?jql='${config.jiraAwsFieldName1.name}'=${displayId} OR '${config.jiraAwsFieldName2.name}'=${displayId}`,
rejectUnauthorized: false,
headers: {
'Authorization': `Basic ${config.jiraApiKey}`,
'Content-Type': 'application/json'
}
}, function (error, response, body) {
if (error) {
console.log(error);
return;
}
// Evaluate response status code before parsing response body
// (Jira returns HTML for HTTP 401, regardless of request Content-Type, upon which JSON.parse() fails)
if (response.statusCode != 200) {
console.log(`Unable to find Jira IDs: Jira responded with HTTP ${response.statusCode}.`);
return;
}
let jsonResponse = JSON.parse(body);
if (jsonResponse.total != 0) {
jsonResponse.issues.forEach(function (issue) {
//verifying if this was second custom field reference (needed later for colorizing)
callback(communicationsArr, issue, (issue.fields[config.jiraAwsFieldName2.id] == displayId));
})
};
});
}
//synchronously update Jira with AWS comments
function updateJira(communicationsArr, issue, awsRef2) {
let jiraId = issue.key;
let i = 0;
let comment = communicationsArr[i];
(function addComment(comment) {
i++;
//Comment's BGColor should be different for different AWS references to simplify readability
if (awsRef2) {
var titleBGColor = "#2e7dba";
} else {
var titleBGColor = "#ff9900";
}
console.log(`${comment.CommunicationId}: Updating ticket: ${jiraId}`);
//Preparing comment message
let date = dateFormat(comment.timeCreated, "dddd, dS mmmm yyyy HH:MM");
//Removing default AWS text
comment.body = comment.body.split('Check out the AWS Support Knowledge Center, a knowledge base of articles and videos that answer customer questions about AWS services')[0];
let json_body = {
body: `{panel:title=AWS Case: ${comment.displayId} - ${date} - ${comment.subject}|borderStyle=solid|borderColor=#000000|titleBGColor=${titleBGColor}}${comment.body}{panel}`
}
//Sending the request
request({
url: `${config.jiraApiHost}/issue/${jiraId}/comment`,
rejectUnauthorized: false,
method: 'POST',
body: JSON.stringify(json_body),
headers: {
'Authorization': `Basic ${config.jiraApiKey}`,
'Content-Type': 'application/json'
}
}, function (error, response, body) {
if (error) {
console.log(error);
return;
}
if (response.statusCode == 201) {
console.log(`${comment.CommunicationId}: Comment successfully added to: ${jiraId}`);
//Asynchronously setting JiraUpdated so it won't appear in the next iteration
updateItem({ CommunicationId: comment.CommunicationId }, 'JiraUpdated', 1);
if (communicationsArr.length > i) {
addComment(communicationsArr[i]);
}
} else {
console.log(`${comment.CommunicationId}: Posting comment failed on: ${jiraId}`);
console.log(response);
}
});
})(comment);
}
function sendReply(communicationsArr, issue, awsRef2) {
//Getting all comments for specified issue
request({
url: `${config.jiraApiHost}/issue/${issue.key}/comment`,
rejectUnauthorized: false,
method: 'GET',
headers: {
'Authorization': `Basic ${config.jiraApiKey}`,
'Content-Type': 'application/json'
}
}, function (error, response, body) {
if (error) {
console.log(error);
return;
}
if (response.statusCode == 200) {
let jsonResponse = JSON.parse(body);
if (jsonResponse.total >= 1) {
let pattern, awsComment, accountId;
let newCommunicationId = 0;
let comment = jsonResponse.comments[0];
let i = 0;
(function processComment(comment) {
i++;
if (awsRef2) {
//caseId must be specified in case of two references.
pattern = new RegExp(`(#DearAWS\\[${communicationsArr[0].displayId}\\])((.|[\r\n])+)`, 'mgi');
}
else {
pattern = new RegExp(`(#DearAWS)((.|[\r\n])+)`, 'mgi');
}
awsComment = pattern.exec(comment.body);
if (Array.isArray(awsComment) && typeof awsComment[2] !== 'undefined') {
let commentText = awsComment[2];
//Getting account ID based on caseId
let accountIdPattern = new RegExp(`(case-)(\\d+)(-.*?)`, 'mg');
accountId = accountIdPattern.exec(communicationsArr[0].caseId);
accountId = accountId[2];
let caseInfo = `Account ID: ${accountId} \n ticket: ${communicationsArr[0].displayId} \n Comment: ${commentText}`;
console.log(`Posting AWS comment - ${caseInfo}`);
postCommentToAws(accountId, communicationsArr[0].caseId, commentText).then(function (awsResponse) {
console.log(`Case has been successfully updated in AWS! (${caseInfo})`);
console.log(commentText);
//updating dynamo so comment won't be visible on regular iterations
let now = new Date();
let date = now.toISOString();
let dynamoItem = communicationsArr[0];
dynamoItem.CommunicationId = `${dynamoItem.caseId}-${(communicationsArr.length + newCommunicationId)}`;
dynamoItem.body = commentText;
dynamoItem.timeCreated = date;
dynamoItem.JiraUpdated = 1;
dynamoItem.Sortorder = (communicationsArr.length + newCommunicationId);
addNewItem(dynamoItem, 'CommunicationId');
newCommunicationId++;
let formattedBody;
if (awsRef2) {
formattedBody = { body: `#SenttoAWS[${communicationsArr[0].displayId}]:\n${commentText}` };
}
else {
formattedBody = { body: `#SenttoAWS:\n${commentText}` };
}
//Editing Jira comment so we can see that reply has been sent to AWS
request({
url: `${config.jiraApiHost}/issue/${issue.key}/comment/${comment.id}`,
rejectUnauthorized: false,
method: 'PUT',
body: JSON.stringify(formattedBody),
headers: {
'Authorization': `Basic ${config.jiraApiKey}`,
'Content-Type': 'application/json'
}
}, function (error, response, body) {
if (error) {
console.log(error);
return;
}
if (response.statusCode == 200) {
console.log(`Comment has been replaced with #SenttoAWS for ${issue.key}`);
if (jsonResponse.comments.length > i) {
processComment(jsonResponse.comments[i]);
}
}
});
}).catch(function (error) {
console.log(error);
});
}
else {
if (jsonResponse.comments.length > i) {
processComment(jsonResponse.comments[i]);
}
}
})(comment);
}
}
});
}
//A promise function which simply posts reply to AWS support.
function postCommentToAws(accountId, caseId, comment) {
return new Promise(function (resolve, reject) {
for (var key in config.awsIamAccountArns) {
let arnObj = config.awsIamAccountArns[key];
if (arnObj.arn.indexOf(accountId) >= 0) {
console.log(`Input account ${accountId}, Found ARN ${arnObj.arn}`);
let awsCentralAccessCreds = new AWS.TemporaryCredentials({
RoleArn: arnObj.arn
});
let support = new AWS.Support({ apiVersion: '2013-04-15', region: 'us-east-1', credentials: awsCentralAccessCreds });
var params = {
communicationBody: comment,
caseId: caseId
};
support.addCommunicationToCase(params, function (err, data) {
if (err) {
return reject(err);
}
resolve(data);
});
}
};
});
}
//Helper function making date string including shift stated in config
function getIsoTimeBack() {
//Making afterTime ISO date
let date = new Date();
date.setDate(date.getDate() - config.daysToScan);
return date.toISOString();
}
//Helper function adds new item only into Dynamo table
function addNewItem(item, Id) {
let dynamoClient = new AWS.DynamoDB.DocumentClient();
let params = {
TableName: config.dynamoTableName,
ConditionExpression: `attribute_not_exists (${Id})`,
Item: item,
ReturnValues: "ALL_OLD"
}
dynamoClient.put(params, function (err, data) {
if (err) {
if (err.code == 'ConditionalCheckFailedException') {
console.log(`Item ${item[Id]} (${item.displayId}) will not be added into database since it already exist.`);
}
else {
console.error("Unable to add item. Error JSON:", JSON.stringify(err, null, 2));
}
return;
}
console.log(`Item ${item[Id]} (${item.displayId}) has been successfully added into database.`);
});
}
//Helper function adds or updates key for Dynamo item
function updateItem(hash, key, value) {
let dynamoClient = new AWS.DynamoDB.DocumentClient();
var params = {
TableName: config.dynamoTableName,
Key: hash,
UpdateExpression: 'set #a = :x',
ExpressionAttributeNames: { '#a': key },
ExpressionAttributeValues: {
':x': value
}
};
var documentClient = new AWS.DynamoDB.DocumentClient();
documentClient.update(params, function (err, data) {
if (err) {
console.log(err);
return;
}
console.log(`${JSON.stringify(hash)} has been successfully updated in Dynamo with key: ${key} and value: ${value}`);
});
}
//Helper function used to invoke JiraHandler function
function sendSnsMessage(arn, messageJson) {
var sns = new AWS.SNS();
messageJson.default = 'SnsMessage';
setTimeout(function () { //letting all dynamo operations to finish
sns.publish({
Message: JSON.stringify(messageJson),
MessageStructure: 'json',
TargetArn: arn
}, function (err, data) {
if (err) {
console.log(err.stack);
return;
}
console.log('SNS sent to: ' + arn);
console.log(data);
});
}, 5000);
}
exports.DynamoHandler = function () {
walkThroughAccounts();
}
exports.JiraHandler = function () {
processCommunications(0); //Add AWS comments (AWS->Jira)
processCommunications(1); //Open case to AWS (Jira->AWS)
}