apf-node-common 1.0.109 → 1.0.111
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ApplicationContextService.js +103 -103
- package/CoreUtils.js +59 -59
- package/Logger/index.js +93 -93
- package/Logger/loggerTransports.js +137 -137
- package/SSMConfig.js +44 -44
- package/ScheduleCalculationService.js +0 -0
- package/auditlog/AuditLogger.js +58 -58
- package/auditlog/AuditLoggerRepository.js +38 -38
- package/auditlog/AuditLoggerService.js +37 -37
- package/config/SSMParameters.js +16 -16
- package/constants/CommonMessages.js +14 -14
- package/constants/Frequency.js +8 -8
- package/constants/TimeZone.js +11 -11
- package/constants/UserType.js +9 -9
- package/exception/CustomException.js +36 -36
- package/exception/SendResponse.js +139 -139
- package/index.js +187 -187
- package/package.json +29 -29
- package/test/AWSUtilityIntegrationTest.js +94 -94
- package/test/FrequencyValidatorTest.js +0 -0
- package/test/LambdaCommunicationServiceTest.js +83 -83
- package/test/ScheduleCalculationServiceTest.js +0 -0
- package/utils/HashIds.js +139 -139
- package/utils/NumberFormatter.js +253 -253
- package/utils/aws/AESEncryptionUsingKMS.js +106 -106
- package/utils/aws/AWSAPIKeyGenerator.js +307 -307
- package/utils/aws/AWSS3Utils.js +128 -128
- package/utils/aws/AWSSMSUtils.js +63 -63
- package/utils/aws/AWSSNSBasedEmailDispatcher.js +37 -37
- package/utils/aws/AWSSNSBasedSMSDispatcher.js +37 -37
- package/utils/aws/AWSSNSUtils.js +35 -38
- package/utils/aws/LambdaCommunicationService.js +232 -232
- package/utils/enumHelper.js +7 -7
- package/utils/thirdparty/URLShorteningService.js +25 -25
- package/validation/CoreValidations.js +45 -45
- package/validation/FrequencyValidator.js +0 -0
- package/validation/SchemaValidation.js +106 -106
|
@@ -1,308 +1,308 @@
|
|
|
1
|
-
const AWS = require('aws-sdk');
|
|
2
|
-
|
|
3
|
-
const awsEnv = (process.env.NODE_ENVIRONMENT || 'Test');
|
|
4
|
-
const awsRegion = (process.env.AWS_DEFAULT_REGION || 'us-east-2');
|
|
5
|
-
AWS.config.update({ region: awsRegion });
|
|
6
|
-
const SSMConfig = require("../../SSMConfig");
|
|
7
|
-
const ssmParam = require('../../config/SSMParameters')['ssm']
|
|
8
|
-
const keyTypeVal = "API_KEY"; //constant
|
|
9
|
-
const gateway = new AWS.APIGateway();
|
|
10
|
-
module.exports = {
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* This Api used to create Usage Plan in Aws
|
|
14
|
-
* @param {object} usagePlanData
|
|
15
|
-
* {
|
|
16
|
-
* name: 'String Value'
|
|
17
|
-
* description: 'String Value'
|
|
18
|
-
* period: 'String value'
|
|
19
|
-
* env: 'string Value'
|
|
20
|
-
* apiId: 'string Value'
|
|
21
|
-
* offset: Integer
|
|
22
|
-
* limit: Integer
|
|
23
|
-
* rateLimit: Integer
|
|
24
|
-
* burstLimit: Integer
|
|
25
|
-
* }
|
|
26
|
-
*
|
|
27
|
-
* Example= {
|
|
28
|
-
* "name": "TEstSanal",
|
|
29
|
-
* "description": "check create or not",
|
|
30
|
-
* "period": "DAY",
|
|
31
|
-
* "env": "Test",
|
|
32
|
-
* "apiId": "k5c5nbh1p9",
|
|
33
|
-
* "offset": 0,
|
|
34
|
-
* "rateLimit": 100,
|
|
35
|
-
* "burstLimit": 200
|
|
36
|
-
* }
|
|
37
|
-
*/
|
|
38
|
-
|
|
39
|
-
async createUsagePlan(usagePlanData) {
|
|
40
|
-
console.log('AWSUsagePlane : createUsagePlan Start')
|
|
41
|
-
try {
|
|
42
|
-
let params = {
|
|
43
|
-
"name": usagePlanData.name,
|
|
44
|
-
"description": usagePlanData.description,
|
|
45
|
-
"apiStages": [{
|
|
46
|
-
"stage": usagePlanData.env,
|
|
47
|
-
"apiId": usagePlanData.apiId
|
|
48
|
-
}],
|
|
49
|
-
"quota": {
|
|
50
|
-
"period": usagePlanData.period,
|
|
51
|
-
"offset": usagePlanData.offset,
|
|
52
|
-
"limit": usagePlanData.limit
|
|
53
|
-
},
|
|
54
|
-
"throttle": {
|
|
55
|
-
"rateLimit": usagePlanData.rateLimit,
|
|
56
|
-
"burstLimit": usagePlanData.burstLimit
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
let response = await gateway.createUsagePlan(params).promise();
|
|
61
|
-
|
|
62
|
-
console.log(`Create Usage plane ${response}`)
|
|
63
|
-
console.log("AWSUsagePlane : createUsagePlan End ");
|
|
64
|
-
return response
|
|
65
|
-
} catch (error) {
|
|
66
|
-
console.log('error occured while create Usage Plane');
|
|
67
|
-
console.log("AWSUsagePlane: createUsePlane Error : " + error.message);
|
|
68
|
-
throw error;
|
|
69
|
-
}
|
|
70
|
-
},
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* This Api Used to get ApiKey
|
|
74
|
-
* @param {object} usagePlanData
|
|
75
|
-
* {
|
|
76
|
-
* customerId: 'STRING Value',
|
|
77
|
-
* includeValues: Boolean,
|
|
78
|
-
* limit: Integer
|
|
79
|
-
* nameQuery: 'String Value',
|
|
80
|
-
* position: 'String Value'
|
|
81
|
-
* }
|
|
82
|
-
*
|
|
83
|
-
* Example:-
|
|
84
|
-
* usagePlanData = {
|
|
85
|
-
customerId: 'b38DNdLj',
|
|
86
|
-
includeValues: false|| true,
|
|
87
|
-
limit: 25,
|
|
88
|
-
nameQuery: 'sanal',
|
|
89
|
-
position: null
|
|
90
|
-
}
|
|
91
|
-
*/
|
|
92
|
-
|
|
93
|
-
async getApiKey(usagePlanData) {
|
|
94
|
-
try {
|
|
95
|
-
console.log('AWSApiGatewayApiKey : getApiKey Start')
|
|
96
|
-
var params = {
|
|
97
|
-
customerId: usagePlanData.customerId,
|
|
98
|
-
includeValues: usagePlanData.includeValues,
|
|
99
|
-
limit: usagePlanData.limit,
|
|
100
|
-
nameQuery: usagePlanData.nameQuery,
|
|
101
|
-
position: usagePlanData.position
|
|
102
|
-
}
|
|
103
|
-
let apiKeyResult = await gateway.getApiKeys(params).promise();
|
|
104
|
-
console.log('AWSApiGatewayApiKey : getApiKey End')
|
|
105
|
-
return apiKeyResult.items[0];
|
|
106
|
-
} catch (error) {
|
|
107
|
-
console.log('AWSApiGatewayApiKey : getApiKey error occured: ' + error)
|
|
108
|
-
console.log('AWSApiGatewayApiKey : getApiKey error occured:' + error.message);
|
|
109
|
-
throw error
|
|
110
|
-
}
|
|
111
|
-
},
|
|
112
|
-
/**
|
|
113
|
-
* This Api Used to delete ApiKey
|
|
114
|
-
* @param {object} usagePlanData
|
|
115
|
-
* {
|
|
116
|
-
* customerId: 'STRING Value',
|
|
117
|
-
* includeValues: Boolean,
|
|
118
|
-
* limit: Integer
|
|
119
|
-
* nameQuery: 'String Value',
|
|
120
|
-
* position: 'String Value'
|
|
121
|
-
* }
|
|
122
|
-
*
|
|
123
|
-
* Example:-
|
|
124
|
-
* usagePlanData = {
|
|
125
|
-
customerId: 'b38DNdLj',
|
|
126
|
-
includeValues: false|| true,
|
|
127
|
-
limit: 25,
|
|
128
|
-
nameQuery: 'sanal',
|
|
129
|
-
position: null
|
|
130
|
-
}
|
|
131
|
-
*/
|
|
132
|
-
|
|
133
|
-
async deleteApiKey(usagePlanData) {
|
|
134
|
-
try {
|
|
135
|
-
console.log('AWSApiGatewayApiKey : deleteApiKey Start')
|
|
136
|
-
let getApiKeyData = await this.getApiKey(usagePlanData);
|
|
137
|
-
var params = {
|
|
138
|
-
apiKey: getApiKeyData.id
|
|
139
|
-
}
|
|
140
|
-
let deleteApiKeyResponse = await gateway.deleteApiKey(params).promise();
|
|
141
|
-
console.log('AWSApiGatewayApiKey : deleteApiKey Start')
|
|
142
|
-
return deleteApiKeyResponse;
|
|
143
|
-
|
|
144
|
-
} catch (error) {
|
|
145
|
-
console.log('AWSApiGatewayApiKey : deleteApiKey error occured: ' + error)
|
|
146
|
-
console.log('AWSApiGatewayApiKey : deleteApiKey error occured:' + error.message);
|
|
147
|
-
throw error
|
|
148
|
-
}
|
|
149
|
-
},
|
|
150
|
-
/**
|
|
151
|
-
* This Api used to crate ApiKey
|
|
152
|
-
* @param {object} keyData
|
|
153
|
-
* {
|
|
154
|
-
* customerId: 'String Value',
|
|
155
|
-
* name: 'String Value',
|
|
156
|
-
* description: 'String Value',
|
|
157
|
-
* stageKeys: [{
|
|
158
|
-
* "restApiId": 'String Value',
|
|
159
|
-
* "stageName": 'String Value'
|
|
160
|
-
* }],
|
|
161
|
-
* isAutoGeneratedValue: boolean,
|
|
162
|
-
* value: 'String Value'
|
|
163
|
-
*}
|
|
164
|
-
*
|
|
165
|
-
* Example-
|
|
166
|
-
* customerId: '3kW17a2n',
|
|
167
|
-
name: 'sample Name',
|
|
168
|
-
description: 'Sample Description',
|
|
169
|
-
stageKeys: [{
|
|
170
|
-
"restApiId": 'wOdLYWGD',
|
|
171
|
-
"stageName": 'Test'
|
|
172
|
-
}],
|
|
173
|
-
isAutoGeneratedValue: true,
|
|
174
|
-
value: 'DV66HY8-2GR41PN-MEV6DSE-RV6PRQZ'
|
|
175
|
-
*/
|
|
176
|
-
|
|
177
|
-
async createApiKey(keyData) {
|
|
178
|
-
try {
|
|
179
|
-
console.log('AWSApiGatewayApiKey : createApiKey Start');
|
|
180
|
-
params = {
|
|
181
|
-
customerId: keyData.customerId,
|
|
182
|
-
name: keyData.name,
|
|
183
|
-
description: keyData.description,
|
|
184
|
-
enabled: true,
|
|
185
|
-
generateDistinctId: true,
|
|
186
|
-
stageKeys: []
|
|
187
|
-
}
|
|
188
|
-
if (!keyData.isAutoGeneratedValue) {
|
|
189
|
-
params.value = keyData.value;
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
keyData.stageKeys.forEach(element => {
|
|
193
|
-
params.stageKeys.push({
|
|
194
|
-
restApiId: element.restApiId,
|
|
195
|
-
stageName: element.stageName
|
|
196
|
-
});
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
const key = await gateway.createApiKey(params).promise();
|
|
200
|
-
console.log('AWSApiGatewayApiKey : createApiKey End');
|
|
201
|
-
return key;
|
|
202
|
-
} catch (error) {
|
|
203
|
-
console.log('AWSApiGatewayApiKey : createApiKey error occured:' + error);
|
|
204
|
-
console.log('AWSApiGatewayApiKey : createApiKey error occured:' + error.message);
|
|
205
|
-
throw error
|
|
206
|
-
}
|
|
207
|
-
},
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
* This Api used add ApiKey in usage plan
|
|
211
|
-
* @param {object} usagePlanData
|
|
212
|
-
* {
|
|
213
|
-
* customerId: 'String Value',
|
|
214
|
-
* includeValues: boolean,
|
|
215
|
-
* limit: Integer,
|
|
216
|
-
* name: 'String Value',
|
|
217
|
-
* position: 'String Value',
|
|
218
|
-
* description: 'String Value',
|
|
219
|
-
* restApiId: 'String Value',
|
|
220
|
-
* isAutoGeneratedValue: Boolean,
|
|
221
|
-
* value: 'String Value'
|
|
222
|
-
* }
|
|
223
|
-
*
|
|
224
|
-
* Example-
|
|
225
|
-
* const usagePlanData = {
|
|
226
|
-
customerId: 'b38DNdLj',
|
|
227
|
-
includeValues: false,
|
|
228
|
-
limit: 25,
|
|
229
|
-
name: 'sanal',
|
|
230
|
-
position: null,
|
|
231
|
-
description: 'api key',
|
|
232
|
-
restApiId: "k5c5nbh1p9",
|
|
233
|
-
stageName: "Test",
|
|
234
|
-
isAutoGeneratedValue: false,
|
|
235
|
-
value: '5M14FFM-R8JMVZQ-GBS4GMP-J7KHG4F'
|
|
236
|
-
}
|
|
237
|
-
*/
|
|
238
|
-
async addApiKey(usagePlanData) {
|
|
239
|
-
|
|
240
|
-
try {
|
|
241
|
-
console.log('AWSApiGatewayUsagePlan : addApiKey Start');
|
|
242
|
-
const ssmConfig = await SSMConfig.ssmConfig(ssmParam);
|
|
243
|
-
const usagePlanIdVal = ssmConfig.usagePlanId;
|
|
244
|
-
validate(usagePlanData)
|
|
245
|
-
|
|
246
|
-
let deleteApiKeyData = {
|
|
247
|
-
customerId: usagePlanData.customerId,
|
|
248
|
-
includeValues: false,
|
|
249
|
-
limit: usagePlanData.limit,
|
|
250
|
-
nameQuery: usagePlanData.name,
|
|
251
|
-
position: usagePlanData.position
|
|
252
|
-
}
|
|
253
|
-
let keyData = {
|
|
254
|
-
customerId: usagePlanData.customerId,
|
|
255
|
-
name: usagePlanData.name,
|
|
256
|
-
description: usagePlanData.description,
|
|
257
|
-
stageKeys: [{
|
|
258
|
-
"restApiId": usagePlanData.restApiId,
|
|
259
|
-
"stageName": usagePlanData.stageName
|
|
260
|
-
}],
|
|
261
|
-
isAutoGeneratedValue: usagePlanData.isAutoGeneratedValue,
|
|
262
|
-
value: usagePlanData.value
|
|
263
|
-
}
|
|
264
|
-
console.log("deleteApiKeyData: ", deleteApiKeyData)
|
|
265
|
-
console.log("keyData: ", keyData)
|
|
266
|
-
let isKeyPresent = await this.getApiKey(deleteApiKeyData);
|
|
267
|
-
if (isKeyPresent) {
|
|
268
|
-
let deleteApiKeyResponse = await this.deleteApiKey(deleteApiKeyData);
|
|
269
|
-
}
|
|
270
|
-
let generateApiKeyResponse = await this.createApiKey(keyData);
|
|
271
|
-
let params = {
|
|
272
|
-
keyId: generateApiKeyResponse.id,
|
|
273
|
-
keyType: keyTypeVal,
|
|
274
|
-
usagePlanId: usagePlanIdVal
|
|
275
|
-
}
|
|
276
|
-
let addUsagePlanApiKeyResponse = await gateway.createUsagePlanKey(params).promise();
|
|
277
|
-
console.log('AWSApiGatewayUsagePlan : addApiKey End');
|
|
278
|
-
return addUsagePlanApiKeyResponse;
|
|
279
|
-
} catch (error) {
|
|
280
|
-
console.log('AWSApiGatewayUsagePlan : addUsagePlanApiKey error occured:' + error);
|
|
281
|
-
console.log('AWSApiGatewayUsagePlan : addUsagePlanApiKey error occured:' + error.message);
|
|
282
|
-
throw error
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
function validate(usagePlanData) {
|
|
289
|
-
|
|
290
|
-
if (!usagePlanData) {
|
|
291
|
-
throw new Error('usagePlanData is required');
|
|
292
|
-
}
|
|
293
|
-
if (!usagePlanData.customerId) {
|
|
294
|
-
throw new Error('customerId is required');
|
|
295
|
-
}
|
|
296
|
-
if (!usagePlanData.name) {
|
|
297
|
-
throw new Error('name is required');
|
|
298
|
-
}
|
|
299
|
-
if (!usagePlanData.limit) {
|
|
300
|
-
throw new Error('limit is required');
|
|
301
|
-
}
|
|
302
|
-
if (!usagePlanData.restApiId) {
|
|
303
|
-
throw new Error('restApiId is required');
|
|
304
|
-
}
|
|
305
|
-
if (!usagePlanData.stageName) {
|
|
306
|
-
throw new Error('stageName is required');
|
|
307
|
-
}
|
|
1
|
+
const AWS = require('aws-sdk');
|
|
2
|
+
|
|
3
|
+
const awsEnv = (process.env.NODE_ENVIRONMENT || 'Test');
|
|
4
|
+
const awsRegion = (process.env.AWS_DEFAULT_REGION || 'us-east-2');
|
|
5
|
+
AWS.config.update({ region: awsRegion });
|
|
6
|
+
const SSMConfig = require("../../SSMConfig");
|
|
7
|
+
const ssmParam = require('../../config/SSMParameters')['ssm']
|
|
8
|
+
const keyTypeVal = "API_KEY"; //constant
|
|
9
|
+
const gateway = new AWS.APIGateway();
|
|
10
|
+
module.exports = {
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* This Api used to create Usage Plan in Aws
|
|
14
|
+
* @param {object} usagePlanData
|
|
15
|
+
* {
|
|
16
|
+
* name: 'String Value'
|
|
17
|
+
* description: 'String Value'
|
|
18
|
+
* period: 'String value'
|
|
19
|
+
* env: 'string Value'
|
|
20
|
+
* apiId: 'string Value'
|
|
21
|
+
* offset: Integer
|
|
22
|
+
* limit: Integer
|
|
23
|
+
* rateLimit: Integer
|
|
24
|
+
* burstLimit: Integer
|
|
25
|
+
* }
|
|
26
|
+
*
|
|
27
|
+
* Example= {
|
|
28
|
+
* "name": "TEstSanal",
|
|
29
|
+
* "description": "check create or not",
|
|
30
|
+
* "period": "DAY",
|
|
31
|
+
* "env": "Test",
|
|
32
|
+
* "apiId": "k5c5nbh1p9",
|
|
33
|
+
* "offset": 0,
|
|
34
|
+
* "rateLimit": 100,
|
|
35
|
+
* "burstLimit": 200
|
|
36
|
+
* }
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
async createUsagePlan(usagePlanData) {
|
|
40
|
+
console.log('AWSUsagePlane : createUsagePlan Start')
|
|
41
|
+
try {
|
|
42
|
+
let params = {
|
|
43
|
+
"name": usagePlanData.name,
|
|
44
|
+
"description": usagePlanData.description,
|
|
45
|
+
"apiStages": [{
|
|
46
|
+
"stage": usagePlanData.env,
|
|
47
|
+
"apiId": usagePlanData.apiId
|
|
48
|
+
}],
|
|
49
|
+
"quota": {
|
|
50
|
+
"period": usagePlanData.period,
|
|
51
|
+
"offset": usagePlanData.offset,
|
|
52
|
+
"limit": usagePlanData.limit
|
|
53
|
+
},
|
|
54
|
+
"throttle": {
|
|
55
|
+
"rateLimit": usagePlanData.rateLimit,
|
|
56
|
+
"burstLimit": usagePlanData.burstLimit
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
let response = await gateway.createUsagePlan(params).promise();
|
|
61
|
+
|
|
62
|
+
console.log(`Create Usage plane ${response}`)
|
|
63
|
+
console.log("AWSUsagePlane : createUsagePlan End ");
|
|
64
|
+
return response
|
|
65
|
+
} catch (error) {
|
|
66
|
+
console.log('error occured while create Usage Plane');
|
|
67
|
+
console.log("AWSUsagePlane: createUsePlane Error : " + error.message);
|
|
68
|
+
throw error;
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* This Api Used to get ApiKey
|
|
74
|
+
* @param {object} usagePlanData
|
|
75
|
+
* {
|
|
76
|
+
* customerId: 'STRING Value',
|
|
77
|
+
* includeValues: Boolean,
|
|
78
|
+
* limit: Integer
|
|
79
|
+
* nameQuery: 'String Value',
|
|
80
|
+
* position: 'String Value'
|
|
81
|
+
* }
|
|
82
|
+
*
|
|
83
|
+
* Example:-
|
|
84
|
+
* usagePlanData = {
|
|
85
|
+
customerId: 'b38DNdLj',
|
|
86
|
+
includeValues: false|| true,
|
|
87
|
+
limit: 25,
|
|
88
|
+
nameQuery: 'sanal',
|
|
89
|
+
position: null
|
|
90
|
+
}
|
|
91
|
+
*/
|
|
92
|
+
|
|
93
|
+
async getApiKey(usagePlanData) {
|
|
94
|
+
try {
|
|
95
|
+
console.log('AWSApiGatewayApiKey : getApiKey Start')
|
|
96
|
+
var params = {
|
|
97
|
+
customerId: usagePlanData.customerId,
|
|
98
|
+
includeValues: usagePlanData.includeValues,
|
|
99
|
+
limit: usagePlanData.limit,
|
|
100
|
+
nameQuery: usagePlanData.nameQuery,
|
|
101
|
+
position: usagePlanData.position
|
|
102
|
+
}
|
|
103
|
+
let apiKeyResult = await gateway.getApiKeys(params).promise();
|
|
104
|
+
console.log('AWSApiGatewayApiKey : getApiKey End')
|
|
105
|
+
return apiKeyResult.items[0];
|
|
106
|
+
} catch (error) {
|
|
107
|
+
console.log('AWSApiGatewayApiKey : getApiKey error occured: ' + error)
|
|
108
|
+
console.log('AWSApiGatewayApiKey : getApiKey error occured:' + error.message);
|
|
109
|
+
throw error
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
/**
|
|
113
|
+
* This Api Used to delete ApiKey
|
|
114
|
+
* @param {object} usagePlanData
|
|
115
|
+
* {
|
|
116
|
+
* customerId: 'STRING Value',
|
|
117
|
+
* includeValues: Boolean,
|
|
118
|
+
* limit: Integer
|
|
119
|
+
* nameQuery: 'String Value',
|
|
120
|
+
* position: 'String Value'
|
|
121
|
+
* }
|
|
122
|
+
*
|
|
123
|
+
* Example:-
|
|
124
|
+
* usagePlanData = {
|
|
125
|
+
customerId: 'b38DNdLj',
|
|
126
|
+
includeValues: false|| true,
|
|
127
|
+
limit: 25,
|
|
128
|
+
nameQuery: 'sanal',
|
|
129
|
+
position: null
|
|
130
|
+
}
|
|
131
|
+
*/
|
|
132
|
+
|
|
133
|
+
async deleteApiKey(usagePlanData) {
|
|
134
|
+
try {
|
|
135
|
+
console.log('AWSApiGatewayApiKey : deleteApiKey Start')
|
|
136
|
+
let getApiKeyData = await this.getApiKey(usagePlanData);
|
|
137
|
+
var params = {
|
|
138
|
+
apiKey: getApiKeyData.id
|
|
139
|
+
}
|
|
140
|
+
let deleteApiKeyResponse = await gateway.deleteApiKey(params).promise();
|
|
141
|
+
console.log('AWSApiGatewayApiKey : deleteApiKey Start')
|
|
142
|
+
return deleteApiKeyResponse;
|
|
143
|
+
|
|
144
|
+
} catch (error) {
|
|
145
|
+
console.log('AWSApiGatewayApiKey : deleteApiKey error occured: ' + error)
|
|
146
|
+
console.log('AWSApiGatewayApiKey : deleteApiKey error occured:' + error.message);
|
|
147
|
+
throw error
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
/**
|
|
151
|
+
* This Api used to crate ApiKey
|
|
152
|
+
* @param {object} keyData
|
|
153
|
+
* {
|
|
154
|
+
* customerId: 'String Value',
|
|
155
|
+
* name: 'String Value',
|
|
156
|
+
* description: 'String Value',
|
|
157
|
+
* stageKeys: [{
|
|
158
|
+
* "restApiId": 'String Value',
|
|
159
|
+
* "stageName": 'String Value'
|
|
160
|
+
* }],
|
|
161
|
+
* isAutoGeneratedValue: boolean,
|
|
162
|
+
* value: 'String Value'
|
|
163
|
+
*}
|
|
164
|
+
*
|
|
165
|
+
* Example-
|
|
166
|
+
* customerId: '3kW17a2n',
|
|
167
|
+
name: 'sample Name',
|
|
168
|
+
description: 'Sample Description',
|
|
169
|
+
stageKeys: [{
|
|
170
|
+
"restApiId": 'wOdLYWGD',
|
|
171
|
+
"stageName": 'Test'
|
|
172
|
+
}],
|
|
173
|
+
isAutoGeneratedValue: true,
|
|
174
|
+
value: 'DV66HY8-2GR41PN-MEV6DSE-RV6PRQZ'
|
|
175
|
+
*/
|
|
176
|
+
|
|
177
|
+
async createApiKey(keyData) {
|
|
178
|
+
try {
|
|
179
|
+
console.log('AWSApiGatewayApiKey : createApiKey Start');
|
|
180
|
+
params = {
|
|
181
|
+
customerId: keyData.customerId,
|
|
182
|
+
name: keyData.name,
|
|
183
|
+
description: keyData.description,
|
|
184
|
+
enabled: true,
|
|
185
|
+
generateDistinctId: true,
|
|
186
|
+
stageKeys: []
|
|
187
|
+
}
|
|
188
|
+
if (!keyData.isAutoGeneratedValue) {
|
|
189
|
+
params.value = keyData.value;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
keyData.stageKeys.forEach(element => {
|
|
193
|
+
params.stageKeys.push({
|
|
194
|
+
restApiId: element.restApiId,
|
|
195
|
+
stageName: element.stageName
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
const key = await gateway.createApiKey(params).promise();
|
|
200
|
+
console.log('AWSApiGatewayApiKey : createApiKey End');
|
|
201
|
+
return key;
|
|
202
|
+
} catch (error) {
|
|
203
|
+
console.log('AWSApiGatewayApiKey : createApiKey error occured:' + error);
|
|
204
|
+
console.log('AWSApiGatewayApiKey : createApiKey error occured:' + error.message);
|
|
205
|
+
throw error
|
|
206
|
+
}
|
|
207
|
+
},
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* This Api used add ApiKey in usage plan
|
|
211
|
+
* @param {object} usagePlanData
|
|
212
|
+
* {
|
|
213
|
+
* customerId: 'String Value',
|
|
214
|
+
* includeValues: boolean,
|
|
215
|
+
* limit: Integer,
|
|
216
|
+
* name: 'String Value',
|
|
217
|
+
* position: 'String Value',
|
|
218
|
+
* description: 'String Value',
|
|
219
|
+
* restApiId: 'String Value',
|
|
220
|
+
* isAutoGeneratedValue: Boolean,
|
|
221
|
+
* value: 'String Value'
|
|
222
|
+
* }
|
|
223
|
+
*
|
|
224
|
+
* Example-
|
|
225
|
+
* const usagePlanData = {
|
|
226
|
+
customerId: 'b38DNdLj',
|
|
227
|
+
includeValues: false,
|
|
228
|
+
limit: 25,
|
|
229
|
+
name: 'sanal',
|
|
230
|
+
position: null,
|
|
231
|
+
description: 'api key',
|
|
232
|
+
restApiId: "k5c5nbh1p9",
|
|
233
|
+
stageName: "Test",
|
|
234
|
+
isAutoGeneratedValue: false,
|
|
235
|
+
value: '5M14FFM-R8JMVZQ-GBS4GMP-J7KHG4F'
|
|
236
|
+
}
|
|
237
|
+
*/
|
|
238
|
+
async addApiKey(usagePlanData) {
|
|
239
|
+
|
|
240
|
+
try {
|
|
241
|
+
console.log('AWSApiGatewayUsagePlan : addApiKey Start');
|
|
242
|
+
const ssmConfig = await SSMConfig.ssmConfig(ssmParam);
|
|
243
|
+
const usagePlanIdVal = ssmConfig.usagePlanId;
|
|
244
|
+
validate(usagePlanData)
|
|
245
|
+
|
|
246
|
+
let deleteApiKeyData = {
|
|
247
|
+
customerId: usagePlanData.customerId,
|
|
248
|
+
includeValues: false,
|
|
249
|
+
limit: usagePlanData.limit,
|
|
250
|
+
nameQuery: usagePlanData.name,
|
|
251
|
+
position: usagePlanData.position
|
|
252
|
+
}
|
|
253
|
+
let keyData = {
|
|
254
|
+
customerId: usagePlanData.customerId,
|
|
255
|
+
name: usagePlanData.name,
|
|
256
|
+
description: usagePlanData.description,
|
|
257
|
+
stageKeys: [{
|
|
258
|
+
"restApiId": usagePlanData.restApiId,
|
|
259
|
+
"stageName": usagePlanData.stageName
|
|
260
|
+
}],
|
|
261
|
+
isAutoGeneratedValue: usagePlanData.isAutoGeneratedValue,
|
|
262
|
+
value: usagePlanData.value
|
|
263
|
+
}
|
|
264
|
+
console.log("deleteApiKeyData: ", deleteApiKeyData)
|
|
265
|
+
console.log("keyData: ", keyData)
|
|
266
|
+
let isKeyPresent = await this.getApiKey(deleteApiKeyData);
|
|
267
|
+
if (isKeyPresent) {
|
|
268
|
+
let deleteApiKeyResponse = await this.deleteApiKey(deleteApiKeyData);
|
|
269
|
+
}
|
|
270
|
+
let generateApiKeyResponse = await this.createApiKey(keyData);
|
|
271
|
+
let params = {
|
|
272
|
+
keyId: generateApiKeyResponse.id,
|
|
273
|
+
keyType: keyTypeVal,
|
|
274
|
+
usagePlanId: usagePlanIdVal
|
|
275
|
+
}
|
|
276
|
+
let addUsagePlanApiKeyResponse = await gateway.createUsagePlanKey(params).promise();
|
|
277
|
+
console.log('AWSApiGatewayUsagePlan : addApiKey End');
|
|
278
|
+
return addUsagePlanApiKeyResponse;
|
|
279
|
+
} catch (error) {
|
|
280
|
+
console.log('AWSApiGatewayUsagePlan : addUsagePlanApiKey error occured:' + error);
|
|
281
|
+
console.log('AWSApiGatewayUsagePlan : addUsagePlanApiKey error occured:' + error.message);
|
|
282
|
+
throw error
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function validate(usagePlanData) {
|
|
289
|
+
|
|
290
|
+
if (!usagePlanData) {
|
|
291
|
+
throw new Error('usagePlanData is required');
|
|
292
|
+
}
|
|
293
|
+
if (!usagePlanData.customerId) {
|
|
294
|
+
throw new Error('customerId is required');
|
|
295
|
+
}
|
|
296
|
+
if (!usagePlanData.name) {
|
|
297
|
+
throw new Error('name is required');
|
|
298
|
+
}
|
|
299
|
+
if (!usagePlanData.limit) {
|
|
300
|
+
throw new Error('limit is required');
|
|
301
|
+
}
|
|
302
|
+
if (!usagePlanData.restApiId) {
|
|
303
|
+
throw new Error('restApiId is required');
|
|
304
|
+
}
|
|
305
|
+
if (!usagePlanData.stageName) {
|
|
306
|
+
throw new Error('stageName is required');
|
|
307
|
+
}
|
|
308
308
|
}
|