@itentialopensource/adapter-netbox 0.7.1 → 0.8.0

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/adapter.js CHANGED
@@ -95,10 +95,18 @@ class Netbox extends AdapterBaseCl {
95
95
  }
96
96
 
97
97
  /**
98
- * @getWorkflowFunctions
99
- */
100
- getWorkflowFunctions(inIgnore) {
101
- let myIgnore = ['hasEntities', 'hasDevices'];
98
+ * @iapGetAdapterWorkflowFunctions
99
+ */
100
+ iapGetAdapterWorkflowFunctions(inIgnore) {
101
+ let myIgnore = [
102
+ 'healthCheck',
103
+ 'iapGetAdapterWorkflowFunctions',
104
+ 'iapHasAdapterEntity',
105
+ 'iapVerifyAdapterCapability',
106
+ 'iapUpdateAdapterEntityCache',
107
+ 'hasEntities',
108
+ 'getAuthorization'
109
+ ];
102
110
  if (!inIgnore && Array.isArray(inIgnore)) {
103
111
  myIgnore = inIgnore;
104
112
  } else if (!inIgnore && typeof inIgnore === 'string') {
@@ -109,15 +117,15 @@ class Netbox extends AdapterBaseCl {
109
117
  // you can add specific methods that you do not want to be workflow functions to ignore like below
110
118
  // myIgnore.push('myMethodNotInWorkflow');
111
119
 
112
- return super.getWorkflowFunctions(myIgnore);
120
+ return super.iapGetAdapterWorkflowFunctions(myIgnore);
113
121
  }
114
122
 
115
123
  /**
116
- * updateAdapterConfiguration is used to update any of the adapter configuration files. This
124
+ * iapUpdateAdapterConfiguration is used to update any of the adapter configuration files. This
117
125
  * allows customers to make changes to adapter configuration without having to be on the
118
126
  * file system.
119
127
  *
120
- * @function updateAdapterConfiguration
128
+ * @function iapUpdateAdapterConfiguration
121
129
  * @param {string} configFile - the name of the file being updated (required)
122
130
  * @param {Object} changes - an object containing all of the changes = formatted like the configuration file (required)
123
131
  * @param {string} entity - the entity to be changed, if an action, schema or mock data file (optional)
@@ -125,36 +133,42 @@ class Netbox extends AdapterBaseCl {
125
133
  * @param {string} action - the action to be changed, if an action, schema or mock data file (optional)
126
134
  * @param {Callback} callback - The results of the call
127
135
  */
128
- updateAdapterConfiguration(configFile, changes, entity, type, action, callback) {
129
- const origin = `${this.id}-adapter-updateAdapterConfiguration`;
136
+ iapUpdateAdapterConfiguration(configFile, changes, entity, type, action, callback) {
137
+ const meth = 'adapter-iapUpdateAdapterConfiguration';
138
+ const origin = `${this.id}-${meth}`;
130
139
  log.trace(origin);
131
- super.updateAdapterConfiguration(configFile, changes, entity, type, action, callback);
140
+
141
+ super.iapUpdateAdapterConfiguration(configFile, changes, entity, type, action, callback);
132
142
  }
133
143
 
134
144
  /**
135
145
  * See if the API path provided is found in this adapter
136
146
  *
137
- * @function findPath
147
+ * @function iapFindAdapterPath
138
148
  * @param {string} apiPath - the api path to check on
139
149
  * @param {Callback} callback - The results of the call
140
150
  */
141
- findPath(apiPath, callback) {
142
- const origin = `${this.id}-adapter-findPath`;
151
+ iapFindAdapterPath(apiPath, callback) {
152
+ const meth = 'adapter-iapFindAdapterPath';
153
+ const origin = `${this.id}-${meth}`;
143
154
  log.trace(origin);
144
- super.findPath(apiPath, callback);
155
+
156
+ super.iapFindAdapterPath(apiPath, callback);
145
157
  }
146
158
 
147
159
  /**
148
160
  * @summary Suspends adapter
149
161
  *
150
- * @function suspend
162
+ * @function iapSuspendAdapter
151
163
  * @param {Callback} callback - callback function
152
164
  */
153
- suspend(mode, callback) {
154
- const origin = `${this.id}-adapter-suspend`;
165
+ iapSuspendAdapter(mode, callback) {
166
+ const meth = 'adapter-iapSuspendAdapter';
167
+ const origin = `${this.id}-${meth}`;
155
168
  log.trace(origin);
169
+
156
170
  try {
157
- return super.suspend(mode, callback);
171
+ return super.iapSuspendAdapter(mode, callback);
158
172
  } catch (error) {
159
173
  log.error(`${origin}: ${error}`);
160
174
  return callback(null, error);
@@ -164,14 +178,16 @@ class Netbox extends AdapterBaseCl {
164
178
  /**
165
179
  * @summary Unsuspends adapter
166
180
  *
167
- * @function unsuspend
181
+ * @function iapUnsuspendAdapter
168
182
  * @param {Callback} callback - callback function
169
183
  */
170
- unsuspend(callback) {
171
- const origin = `${this.id}-adapter-unsuspend`;
184
+ iapUnsuspendAdapter(callback) {
185
+ const meth = 'adapter-iapUnsuspendAdapter';
186
+ const origin = `${this.id}-${meth}`;
172
187
  log.trace(origin);
188
+
173
189
  try {
174
- return super.unsuspend(callback);
190
+ return super.iapUnsuspendAdapter(callback);
175
191
  } catch (error) {
176
192
  log.error(`${origin}: ${error}`);
177
193
  return callback(null, error);
@@ -181,29 +197,33 @@ class Netbox extends AdapterBaseCl {
181
197
  /**
182
198
  * @summary Get the Adaoter Queue
183
199
  *
184
- * @function getQueue
200
+ * @function iapGetAdapterQueue
185
201
  * @param {Callback} callback - callback function
186
202
  */
187
- getQueue(callback) {
188
- const origin = `${this.id}-adapter-getQueue`;
203
+ iapGetAdapterQueue(callback) {
204
+ const meth = 'adapter-iapGetAdapterQueue';
205
+ const origin = `${this.id}-${meth}`;
189
206
  log.trace(origin);
190
- return super.getQueue(callback);
207
+
208
+ return super.iapGetAdapterQueue(callback);
191
209
  }
192
210
 
193
211
  /**
194
212
  * @summary Runs troubleshoot scripts for adapter
195
213
  *
196
- * @function troubleshoot
214
+ * @function iapTroubleshootAdapter
197
215
  * @param {Object} props - the connection, healthcheck and authentication properties
198
216
  *
199
217
  * @param {boolean} persistFlag - whether the adapter properties should be updated
200
218
  * @param {Callback} callback - The results of the call
201
219
  */
202
- troubleshoot(props, persistFlag, callback) {
203
- const origin = `${this.id}-adapter-troubleshoot`;
220
+ iapTroubleshootAdapter(props, persistFlag, callback) {
221
+ const meth = 'adapter-iapTroubleshootAdapter';
222
+ const origin = `${this.id}-${meth}`;
204
223
  log.trace(origin);
224
+
205
225
  try {
206
- return super.troubleshoot(props, persistFlag, this, callback);
226
+ return super.iapTroubleshootAdapter(props, persistFlag, this, callback);
207
227
  } catch (error) {
208
228
  log.error(`${origin}: ${error}`);
209
229
  return callback(null, error);
@@ -213,15 +233,17 @@ class Netbox extends AdapterBaseCl {
213
233
  /**
214
234
  * @summary runs healthcheck script for adapter
215
235
  *
216
- * @function runHealthcheck
236
+ * @function iapRunAdapterHealthcheck
217
237
  * @param {Adapter} adapter - adapter instance to troubleshoot
218
238
  * @param {Callback} callback - callback function
219
239
  */
220
- runHealthcheck(callback) {
221
- const origin = `${this.id}-adapter-runHealthcheck`;
240
+ iapRunAdapterHealthcheck(callback) {
241
+ const meth = 'adapter-iapRunAdapterHealthcheck';
242
+ const origin = `${this.id}-${meth}`;
222
243
  log.trace(origin);
244
+
223
245
  try {
224
- return super.runHealthcheck(this, callback);
246
+ return super.iapRunAdapterHealthcheck(this, callback);
225
247
  } catch (error) {
226
248
  log.error(`${origin}: ${error}`);
227
249
  return callback(null, error);
@@ -231,14 +253,16 @@ class Netbox extends AdapterBaseCl {
231
253
  /**
232
254
  * @summary runs connectivity check script for adapter
233
255
  *
234
- * @function runConnectivity
256
+ * @function iapRunAdapterConnectivity
235
257
  * @param {Callback} callback - callback function
236
258
  */
237
- runConnectivity(callback) {
238
- const origin = `${this.id}-adapter-runConnectivity`;
259
+ iapRunAdapterConnectivity(callback) {
260
+ const meth = 'adapter-iapRunAdapterConnectivity';
261
+ const origin = `${this.id}-${meth}`;
239
262
  log.trace(origin);
263
+
240
264
  try {
241
- return super.runConnectivity(callback);
265
+ return super.iapRunAdapterConnectivity(callback);
242
266
  } catch (error) {
243
267
  log.error(`${origin}: ${error}`);
244
268
  return callback(null, error);
@@ -248,14 +272,16 @@ class Netbox extends AdapterBaseCl {
248
272
  /**
249
273
  * @summary runs basicGet script for adapter
250
274
  *
251
- * @function runBasicGet
275
+ * @function iapRunAdapterBasicGet
252
276
  * @param {Callback} callback - callback function
253
277
  */
254
- runBasicGet(callback) {
255
- const origin = `${this.id}-adapter-runBasicGet`;
278
+ iapRunAdapterBasicGet(callback) {
279
+ const meth = 'adapter-iapRunAdapterBasicGet';
280
+ const origin = `${this.id}-${meth}`;
256
281
  log.trace(origin);
282
+
257
283
  try {
258
- return super.runBasicGet(callback);
284
+ return super.iapRunAdapterBasicGet(callback);
259
285
  } catch (error) {
260
286
  log.error(`${origin}: ${error}`);
261
287
  return callback(null, error);
@@ -265,45 +291,48 @@ class Netbox extends AdapterBaseCl {
265
291
  /**
266
292
  * @summary moves entites into Mongo DB
267
293
  *
268
- * @function moveEntitiesToDB
294
+ * @function iapMoveAdapterEntitiesToDB
269
295
  * @param {getCallback} callback - a callback function to return the result (Generics)
270
296
  * or the error
271
297
  */
272
- moveEntitiesToDB(callback) {
273
- const origin = `${this.id}-adapter-moveEntitiesToDB`;
298
+ iapMoveAdapterEntitiesToDB(callback) {
299
+ const meth = 'adapter-iapMoveAdapterEntitiesToDB';
300
+ const origin = `${this.id}-${meth}`;
274
301
  log.trace(origin);
302
+
275
303
  try {
276
- return super.moveEntitiesToDB(callback);
304
+ return super.iapMoveAdapterEntitiesToDB(callback);
277
305
  } catch (err) {
278
306
  log.error(`${origin}: ${err}`);
279
307
  return callback(null, err);
280
308
  }
281
309
  }
282
310
 
311
+ /* BROKER CALLS */
283
312
  /**
284
313
  * @summary Determines if this adapter supports the specific entity
285
314
  *
286
- * @function hasEntity
315
+ * @function iapHasAdapterEntity
287
316
  * @param {String} entityType - the entity type to check for
288
317
  * @param {String/Array} entityId - the specific entity we are looking for
289
318
  *
290
319
  * @param {Callback} callback - An array of whether the adapter can has the
291
320
  * desired capability or an error
292
321
  */
293
- hasEntity(entityType, entityId, callback) {
294
- const origin = `${this.id}-adapter-hasEntity`;
322
+ iapHasAdapterEntity(entityType, entityId, callback) {
323
+ const origin = `${this.id}-adapter-iapHasAdapterEntity`;
295
324
  log.trace(origin);
296
325
 
297
326
  // Make the call -
298
- // verifyCapability(entityType, actionType, entityId, callback)
299
- return this.verifyCapability(entityType, null, entityId, callback);
327
+ // iapVerifyAdapterCapability(entityType, actionType, entityId, callback)
328
+ return this.iapVerifyAdapterCapability(entityType, null, entityId, callback);
300
329
  }
301
330
 
302
331
  /**
303
332
  * @summary Provides a way for the adapter to tell north bound integrations
304
333
  * whether the adapter supports type, action and specific entity
305
334
  *
306
- * @function verifyCapability
335
+ * @function iapVerifyAdapterCapability
307
336
  * @param {String} entityType - the entity type to check for
308
337
  * @param {String} actionType - the action type to check for
309
338
  * @param {String/Array} entityId - the specific entity we are looking for
@@ -311,15 +340,15 @@ class Netbox extends AdapterBaseCl {
311
340
  * @param {Callback} callback - An array of whether the adapter can has the
312
341
  * desired capability or an error
313
342
  */
314
- verifyCapability(entityType, actionType, entityId, callback) {
315
- const meth = 'adapterBase-verifyCapability';
343
+ iapVerifyAdapterCapability(entityType, actionType, entityId, callback) {
344
+ const meth = 'adapterBase-iapVerifyAdapterCapability';
316
345
  const origin = `${this.id}-${meth}`;
317
346
  log.trace(origin);
318
347
 
319
348
  // if caching
320
349
  if (this.caching) {
321
- // Make the call - verifyCapability(entityType, actionType, entityId, callback)
322
- return this.requestHandlerInst.verifyCapability(entityType, actionType, entityId, (results, error) => {
350
+ // Make the call - iapVerifyAdapterCapability(entityType, actionType, entityId, callback)
351
+ return this.requestHandlerInst.iapVerifyAdapterCapability(entityType, actionType, entityId, (results, error) => {
323
352
  if (error) {
324
353
  return callback(null, error);
325
354
  }
@@ -337,7 +366,7 @@ class Netbox extends AdapterBaseCl {
337
366
  }
338
367
 
339
368
  // need to check the cache again since it has been updated
340
- return this.requestHandlerInst.verifyCapability(entityType, actionType, entityId, (vcapable, verror) => {
369
+ return this.requestHandlerInst.iapVerifyAdapterCapability(entityType, actionType, entityId, (vcapable, verror) => {
341
370
  if (verror) {
342
371
  return callback(null, verror);
343
372
  }
@@ -370,7 +399,7 @@ class Netbox extends AdapterBaseCl {
370
399
  // if no entity id
371
400
  if (!entityId) {
372
401
  // need to check the cache again since it has been updated
373
- return this.requestHandlerInst.verifyCapability(entityType, actionType, null, (vcapable, verror) => {
402
+ return this.requestHandlerInst.iapVerifyAdapterCapability(entityType, actionType, null, (vcapable, verror) => {
374
403
  if (verror) {
375
404
  return callback(null, verror);
376
405
  }
@@ -391,7 +420,7 @@ class Netbox extends AdapterBaseCl {
391
420
  }
392
421
 
393
422
  // need to check the cache again since it has been updated
394
- return this.requestHandlerInst.verifyCapability(entityType, actionType, null, (vcapable, verror) => {
423
+ return this.requestHandlerInst.iapVerifyAdapterCapability(entityType, actionType, null, (vcapable, verror) => {
395
424
  if (verror) {
396
425
  return callback(null, verror);
397
426
  }
@@ -432,11 +461,11 @@ class Netbox extends AdapterBaseCl {
432
461
  /**
433
462
  * @summary Updates the cache for all entities by call the get All entity method
434
463
  *
435
- * @function updateEntityCache
464
+ * @function iapUpdateAdapterEntityCache
436
465
  *
437
466
  */
438
- updateEntityCache() {
439
- const origin = `${this.id}-adapter-updateEntityCache`;
467
+ iapUpdateAdapterEntityCache() {
468
+ const origin = `${this.id}-adapter-iapUpdateAdapterEntityCache`;
440
469
  log.trace(origin);
441
470
 
442
471
  if (this.caching) {
@@ -449,6 +478,140 @@ class Netbox extends AdapterBaseCl {
449
478
  }
450
479
  }
451
480
 
481
+ /**
482
+ * @summary Determines if this adapter supports any in a list of entities
483
+ *
484
+ * @function hasEntities
485
+ * @param {String} entityType - the entity type to check for
486
+ * @param {Array} entityList - the list of entities we are looking for
487
+ *
488
+ * @param {Callback} callback - A map where the entity is the key and the
489
+ * value is true or false
490
+ */
491
+ hasEntities(entityType, entityList, callback) {
492
+ const meth = 'adapter-hasEntities';
493
+ const origin = `${this.id}-${meth}`;
494
+ log.trace(origin);
495
+
496
+ try {
497
+ return super.hasEntities(entityType, entityList, callback);
498
+ } catch (err) {
499
+ log.error(`${origin}: ${err}`);
500
+ return callback(null, err);
501
+ }
502
+ }
503
+
504
+ /**
505
+ * @summary Get Appliance that match the deviceName
506
+ *
507
+ * @function getDevice
508
+ * @param {String} deviceName - the deviceName to find (required)
509
+ *
510
+ * @param {getCallback} callback - a callback function to return the result
511
+ * (appliance) or the error
512
+ */
513
+ getDevice(deviceName, callback) {
514
+ const meth = 'adapter-getDevice';
515
+ const origin = `${this.id}-${meth}`;
516
+ log.trace(origin);
517
+
518
+ try {
519
+ return super.getDevice(deviceName, callback);
520
+ } catch (err) {
521
+ log.error(`${origin}: ${err}`);
522
+ return callback(null, err);
523
+ }
524
+ }
525
+
526
+ /**
527
+ * @summary Get Appliances that match the filter
528
+ *
529
+ * @function getDevicesFiltered
530
+ * @param {Object} options - the data to use to filter the appliances (optional)
531
+ *
532
+ * @param {getCallback} callback - a callback function to return the result
533
+ * (appliances) or the error
534
+ */
535
+ getDevicesFiltered(options, callback) {
536
+ const meth = 'adapter-getDevicesFiltered';
537
+ const origin = `${this.id}-${meth}`;
538
+ log.trace(origin);
539
+
540
+ try {
541
+ return super.getDevicesFiltered(options, callback);
542
+ } catch (err) {
543
+ log.error(`${origin}: ${err}`);
544
+ return callback(null, err);
545
+ }
546
+ }
547
+
548
+ /**
549
+ * @summary Gets the status for the provided appliance
550
+ *
551
+ * @function isAlive
552
+ * @param {String} deviceName - the deviceName of the appliance. (required)
553
+ *
554
+ * @param {configCallback} callback - callback function to return the result
555
+ * (appliance isAlive) or the error
556
+ */
557
+ isAlive(deviceName, callback) {
558
+ const meth = 'adapter-isAlive';
559
+ const origin = `${this.id}-${meth}`;
560
+ log.trace(origin);
561
+
562
+ try {
563
+ return super.isAlive(deviceName, callback);
564
+ } catch (err) {
565
+ log.error(`${origin}: ${err}`);
566
+ return callback(null, err);
567
+ }
568
+ }
569
+
570
+ /**
571
+ * @summary Gets a config for the provided Appliance
572
+ *
573
+ * @function getConfig
574
+ * @param {String} deviceName - the deviceName of the appliance. (required)
575
+ * @param {String} format - the desired format of the config. (optional)
576
+ *
577
+ * @param {configCallback} callback - callback function to return the result
578
+ * (appliance config) or the error
579
+ */
580
+ getConfig(deviceName, format, callback) {
581
+ const meth = 'adapter-getConfig';
582
+ const origin = `${this.id}-${meth}`;
583
+ log.trace(origin);
584
+
585
+ try {
586
+ return super.getConfig(deviceName, format, callback);
587
+ } catch (err) {
588
+ log.error(`${origin}: ${err}`);
589
+ return callback(null, err);
590
+ }
591
+ }
592
+
593
+ /**
594
+ * @summary Gets the device count from the system
595
+ *
596
+ * @function iapGetDeviceCount
597
+ *
598
+ * @param {getCallback} callback - callback function to return the result
599
+ * (count) or the error
600
+ */
601
+ iapGetDeviceCount(callback) {
602
+ const meth = 'adapter-iapGetDeviceCount';
603
+ const origin = `${this.id}-${meth}`;
604
+ log.trace(origin);
605
+
606
+ try {
607
+ return super.iapGetDeviceCount(callback);
608
+ } catch (err) {
609
+ log.error(`${origin}: ${err}`);
610
+ return callback(null, err);
611
+ }
612
+ }
613
+
614
+ /* GENERIC ADAPTER REQUEST - allows extension of adapter without new calls being added */
452
615
  /**
453
616
  * Makes the requested generic call
454
617
  *
@@ -559,79 +722,23 @@ class Netbox extends AdapterBaseCl {
559
722
  }
560
723
  }
561
724
 
562
- /* BROKER CALLS */
563
- /**
564
- * @summary Determines if this adapter supports any in a list of entities
565
- *
566
- * @function hasEntities
567
- * @param {String} entityType - the entity type to check for
568
- * @param {Array} entityList - the list of entities we are looking for
569
- *
570
- * @param {Callback} callback - A map where the entity is the key and the
571
- * value is true or false
572
- */
573
- hasEntities(entityType, entityList, callback) {
574
- const origin = `${this.id}-adapter-hasEntities`;
575
- log.trace(origin);
576
-
577
- switch (entityType) {
578
- case 'Device':
579
- return this.hasDevices(entityList, callback);
580
- default:
581
- return callback(null, `${this.id} does not support entity ${entityType}`);
582
- }
583
- }
584
-
585
725
  /**
586
- * @summary Helper method for hasEntities for the specific device case
726
+ * Makes the requested generic call with no base path or version
587
727
  *
588
- * @param {Array} deviceList - array of unique device identifiers
589
- * @param {Callback} callback - A map where the device is the key and the
590
- * value is true or false
591
- */
592
- hasDevices(deviceList, callback) {
593
- const origin = `${this.id}-adapter-hasDevices`;
594
- log.trace(origin);
595
-
596
- const findings = deviceList.reduce((map, device) => {
597
- // eslint-disable-next-line no-param-reassign
598
- map[device] = false;
599
- log.debug(`In reduce: ${JSON.stringify(map)}`);
600
- return map;
601
- }, {});
602
- const apiCalls = deviceList.map((device) => new Promise((resolve) => {
603
- this.getDevice(device, (result, error) => {
604
- if (error) {
605
- log.debug(`In map error: ${JSON.stringify(device)}`);
606
- return resolve({ name: device, found: false });
607
- }
608
- log.debug(`In map: ${JSON.stringify(device)}`);
609
- return resolve({ name: device, found: true });
610
- });
611
- }));
612
- Promise.all(apiCalls).then((results) => {
613
- results.forEach((device) => {
614
- findings[device.name] = device.found;
615
- });
616
- log.debug(`FINDINGS: ${JSON.stringify(findings)}`);
617
- return callback(findings);
618
- }).catch((errors) => {
619
- log.error('Unable to do device lookup.');
620
- return callback(null, { code: 503, message: 'Unable to do device lookup.', error: errors });
621
- });
622
- }
623
-
624
- /**
625
- * @summary Get Appliance that match the deviceName
626
- *
627
- * @function getDevice
628
- * @param {String} deviceName - the deviceName to find (required)
629
- *
630
- * @param {getCallback} callback - a callback function to return the result
631
- * (appliance) or the error
728
+ * @function genericAdapterRequestNoBasePath
729
+ * @param {String} uriPath - the path of the api call - do not include the host, port, base path or version (required)
730
+ * @param {String} restMethod - the rest method (GET, POST, PUT, PATCH, DELETE) (required)
731
+ * @param {Object} queryData - the parameters to be put on the url (optional).
732
+ * Can be a stringified Object.
733
+ * @param {Object} requestBody - the body to add to the request (optional).
734
+ * Can be a stringified Object.
735
+ * @param {Object} addlHeaders - additional headers to be put on the call (optional).
736
+ * Can be a stringified Object.
737
+ * @param {getCallback} callback - a callback function to return the result (Generics)
738
+ * or the error
632
739
  */
633
- getDevice(deviceName, callback) {
634
- const meth = 'adapter-getDevice';
740
+ genericAdapterRequestNoBasePath(uriPath, restMethod, queryData, requestBody, addlHeaders, callback) {
741
+ const meth = 'adapter-genericAdapterRequestNoBasePath';
635
742
  const origin = `${this.id}-${meth}`;
636
743
  log.trace(origin);
637
744
 
@@ -642,365 +749,81 @@ class Netbox extends AdapterBaseCl {
642
749
  }
643
750
 
644
751
  /* HERE IS WHERE YOU VALIDATE DATA */
645
- if (deviceName === undefined || deviceName === null || deviceName === '' || deviceName.length === 0) {
646
- const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Missing Data', ['deviceName'], null, null, null);
752
+ if (uriPath === undefined || uriPath === null || uriPath === '') {
753
+ const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Missing Data', ['uriPath'], null, null, null);
647
754
  log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
648
755
  return callback(null, errorObj);
649
756
  }
650
-
651
- try {
652
- // need to get the device so we can convert the deviceName to an id
653
- // !! if we can do a lookup by name the getDevicesFiltered may not be necessary
654
- const opts = {
655
- filter: {
656
- name: deviceName
657
- }
658
- };
659
- return this.getDevicesFiltered(opts, (devs, ferr) => {
660
- // if we received an error or their is no response on the results return an error
661
- if (ferr) {
662
- return callback(null, ferr);
663
- }
664
- if (devs.list.length < 1) {
665
- const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, `Did Not Find Device ${deviceName}`, [], null, null, null);
666
- log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
667
- return callback(null, errorObj);
668
- }
669
- // get the uuid from the device
670
- const { uuid } = devs.list[0];
671
-
672
- // !! using Generic makes it easier on the Adapter Builder (just need to change the path)
673
- // !! you can also replace with a specific call if that is easier
674
- const uriPath = `/call/toget/device/${uuid}`;
675
- return this.genericAdapterRequest(uriPath, 'GET', {}, {}, {}, (result, error) => {
676
- // if we received an error or their is no response on the results return an error
677
- if (error) {
678
- return callback(null, error);
679
- }
680
- if (!result.response || !result.response.applianceMo) {
681
- const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Invalid Response', ['getDevice'], null, null, null);
682
- log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
683
- return callback(null, errorObj);
684
- }
685
-
686
- // return the response
687
- // !! format the data we send back
688
- // !! these fields are config manager fields you need to map to the data we receive
689
- const thisDevice = result.response;
690
- thisDevice.name = thisDevice.systemName;
691
- thisDevice.ostype = `System-${thisDevice.systemType}`;
692
- thisDevice.port = thisDevice.systemPort;
693
- thisDevice.ipaddress = thisDevice.systemIP;
694
- return callback(thisDevice);
695
- });
696
- });
697
- } catch (ex) {
698
- const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Caught Exception', null, null, null, ex);
757
+ if (restMethod === undefined || restMethod === null || restMethod === '') {
758
+ const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Missing Data', ['restMethod'], null, null, null);
699
759
  log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
700
760
  return callback(null, errorObj);
701
761
  }
702
- }
703
762
 
704
- /**
705
- * @summary Get Appliances that match the filter
706
- *
707
- * @function getDevicesFiltered
708
- * @param {Object} options - the data to use to filter the appliances (optional)
709
- *
710
- * @param {getCallback} callback - a callback function to return the result
711
- * (appliances) or the error
712
- */
713
- getDevicesFiltered(options, callback) {
714
- const meth = 'adapter-getDevicesFiltered';
715
- const origin = `${this.id}-${meth}`;
716
- log.trace(origin);
717
-
718
- // verify the required fields have been provided
719
- if (options === undefined || options === null || options === '' || options.length === 0) {
720
- const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Missing Data', ['options'], null, null, null);
721
- log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
722
- return callback(null, errorObj);
763
+ /* HERE IS WHERE YOU SET THE DATA TO PASS INTO REQUEST */
764
+ // remove any leading / and split the uripath into path variables
765
+ let myPath = uriPath;
766
+ while (myPath.indexOf('/') === 0) {
767
+ myPath = myPath.substring(1);
723
768
  }
724
- log.debug(`Device Filter Options: ${JSON.stringify(options)}`);
725
-
726
- // TODO - get pagination working
727
- // const nextToken = options.start;
728
- // const maxResults = options.limit;
769
+ const pathVars = myPath.split('/');
770
+ const queryParamsAvailable = queryData;
771
+ const queryParams = {};
772
+ const bodyVars = requestBody;
729
773
 
730
- // set up the filter of Device Names
731
- let filterName = [];
732
- if (options && options.filter && options.filter.name) {
733
- // when this hack is removed, remove the lint ignore above
734
- if (Array.isArray(options.filter.name)) {
735
- // eslint-disable-next-line prefer-destructuring
736
- filterName = options.filter.name;
737
- } else {
738
- filterName = [options.filter.name];
774
+ // loop in template. long callback arg name to avoid identifier conflicts
775
+ Object.keys(queryParamsAvailable).forEach((thisKeyInQueryParamsAvailable) => {
776
+ if (queryParamsAvailable[thisKeyInQueryParamsAvailable] !== undefined && queryParamsAvailable[thisKeyInQueryParamsAvailable] !== null
777
+ && queryParamsAvailable[thisKeyInQueryParamsAvailable] !== '') {
778
+ queryParams[thisKeyInQueryParamsAvailable] = queryParamsAvailable[thisKeyInQueryParamsAvailable];
739
779
  }
740
- }
741
-
742
- // TODO - get sort and order working
743
- /*
744
- if (options && options.sort) {
745
- reqObj.uriOptions.sort = JSON.stringify(options.sort);
746
- }
747
- if (options && options.order) {
748
- reqObj.uriOptions.order = options.order;
749
- }
750
- */
751
- try {
752
- // !! using Generic makes it easier on the Adapter Builder (just need to change the path)
753
- // !! you can also replace with a specific call if that is easier
754
- const uriPath = '/call/toget/devices';
755
- return this.genericAdapterRequest(uriPath, 'GET', {}, {}, {}, (result, error) => {
756
- // if we received an error or their is no response on the results return an error
757
- if (error) {
758
- return callback(null, error);
759
- }
760
- if (!result.response) {
761
- const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Invalid Response', ['getDevicesFiltered'], null, null, null);
762
- log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
763
- return callback(null, errorObj);
764
- }
765
-
766
- // !! go through the response - may have to look for sub object
767
- // handle an array of devices
768
- if (Array.isArray(result.response)) {
769
- const myDevices = [];
770
-
771
- for (let d = 0; d < result.response.length; d += 1) {
772
- // !! format the data we send back
773
- // !! these fields are config manager fields you need to map to the data we receive
774
- const thisDevice = result.response;
775
- thisDevice.name = thisDevice.systemName;
776
- thisDevice.ostype = `System-${thisDevice.systemType}`;
777
- thisDevice.port = thisDevice.systemPort;
778
- thisDevice.ipaddress = thisDevice.systemIP;
779
-
780
- // if there is no filter - return the device
781
- if (filterName.length === 0) {
782
- myDevices.push(thisDevice);
783
- } else {
784
- // if we have to match a filter
785
- let found = false;
786
- for (let f = 0; f < filterName.length; f += 1) {
787
- if (thisDevice.name.indexOf(filterName[f]) >= 0) {
788
- found = true;
789
- break;
790
- }
791
- }
792
- // matching device
793
- if (found) {
794
- myDevices.push(thisDevice);
795
- }
796
- }
797
- }
798
- log.debug(`${origin}: Found #${myDevices.length} devices.`);
799
- log.debug(`Devices: ${JSON.stringify(myDevices)}`);
800
- return callback({ total: myDevices.length, list: myDevices });
801
- }
802
- // handle a single device response
803
- // !! format the data we send back
804
- // !! these fields are config manager fields you need to map to the data we receive
805
- const thisDevice = result.response;
806
- thisDevice.name = thisDevice.systemName;
807
- thisDevice.ostype = `System-${thisDevice.systemType}`;
808
- thisDevice.port = thisDevice.systemPort;
809
- thisDevice.ipaddress = thisDevice.systemIP;
810
-
811
- // if there is no filter - return the device
812
- if (filterName.length === 0) {
813
- log.debug(`${origin}: Found #1 device.`);
814
- log.debug(`Device: ${JSON.stringify(thisDevice)}`);
815
- return callback({ total: 1, list: [thisDevice] });
816
- }
817
-
818
- // if there is a filter need to check for matching device
819
- let found = false;
820
- for (let f = 0; f < filterName.length; f += 1) {
821
- if (thisDevice.name.indexOf(filterName[f]) >= 0) {
822
- found = true;
823
- break;
824
- }
825
- }
826
- // matching device
827
- if (found) {
828
- log.debug(`${origin}: Found #1 device.`);
829
- log.debug(`Device Found: ${JSON.stringify(thisDevice)}`);
830
- return callback({ total: 1, list: [thisDevice] });
831
- }
832
- // not a matching device
833
- log.debug(`${origin}: No matching device found.`);
834
- return callback({ total: 0, list: [] });
835
- });
836
- } catch (ex) {
837
- const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Caught Exception', null, null, null, ex);
838
- log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
839
- return callback(null, errorObj);
840
- }
841
- }
842
-
843
- /**
844
- * @summary Gets the status for the provided appliance
845
- *
846
- * @function isAlive
847
- * @param {String} deviceName - the deviceName of the appliance. (required)
848
- *
849
- * @param {configCallback} callback - callback function to return the result
850
- * (appliance isAlive) or the error
851
- */
852
- isAlive(deviceName, callback) {
853
- const meth = 'adapter-isAlive';
854
- const origin = `${this.id}-${meth}`;
855
- log.trace(origin);
856
-
857
- // verify the required fields have been provided
858
- if (deviceName === undefined || deviceName === null || deviceName === '' || deviceName.length === 0) {
859
- const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Missing Data', ['deviceName'], null, null, null);
860
- log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
861
- return callback(null, errorObj);
862
- }
863
-
864
- try {
865
- // need to get the device so we can convert the deviceName to an id
866
- // !! if we can do a lookup by name the getDevicesFiltered may not be necessary
867
- const opts = {
868
- filter: {
869
- name: deviceName
870
- }
871
- };
872
- return this.getDevicesFiltered(opts, (devs, ferr) => {
873
- // if we received an error or their is no response on the results return an error
874
- if (ferr) {
875
- return callback(null, ferr);
876
- }
877
- if (devs.list.length < 1) {
878
- const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, `Did Not Find Device ${deviceName}`, [], null, null, null);
879
- log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
880
- return callback(null, errorObj);
881
- }
882
- // get the uuid from the device
883
- const { uuid } = devs.list[0];
884
-
885
- // !! using Generic makes it easier on the Adapter Builder (just need to change the path)
886
- // !! you can also replace with a specific call if that is easier
887
- const uriPath = `/call/toget/status/${uuid}`;
888
- return this.genericAdapterRequest(uriPath, 'GET', {}, {}, {}, (result, error) => {
889
- // if we received an error or their is no response on the results return an error
890
- if (error) {
891
- return callback(null, error);
892
- }
893
- // !! should update this to make sure we are checking for the appropriate object/field
894
- if (!result.response || !result.response.returnObj || !Object.hasOwnProperty.call(result.response.returnObj, 'statusField')) {
895
- const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Invalid Response', ['isAlive'], null, null, null);
896
- log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
897
- return callback(null, errorObj);
898
- }
780
+ });
899
781
 
900
- // !! return the response - Update to the appropriate object/field
901
- return callback(!result.response.returnObj.statusField);
902
- });
903
- });
904
- } catch (ex) {
905
- const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Caught Exception', null, null, null, ex);
906
- log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
907
- return callback(null, errorObj);
782
+ // set up the request object - payload, uriPathVars, uriQuery, uriOptions, addlHeaders
783
+ const reqObj = {
784
+ payload: bodyVars,
785
+ uriPathVars: pathVars,
786
+ uriQuery: queryParams,
787
+ uriOptions: {}
788
+ };
789
+ // add headers if provided
790
+ if (addlHeaders) {
791
+ reqObj.addlHeaders = addlHeaders;
908
792
  }
909
- }
910
-
911
- /**
912
- * @summary Gets a config for the provided Appliance
913
- *
914
- * @function getConfig
915
- * @param {String} deviceName - the deviceName of the appliance. (required)
916
- * @param {String} format - the desired format of the config. (optional)
917
- *
918
- * @param {configCallback} callback - callback function to return the result
919
- * (appliance config) or the error
920
- */
921
- getConfig(deviceName, format, callback) {
922
- const meth = 'adapter-getConfig';
923
- const origin = `${this.id}-${meth}`;
924
- log.trace(origin);
925
793
 
926
- // verify the required fields have been provided
927
- if (deviceName === undefined || deviceName === null || deviceName === '' || deviceName.length === 0) {
928
- const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Missing Data', ['deviceName'], null, null, null);
929
- log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
930
- return callback(null, errorObj);
794
+ // determine the call and return flag
795
+ let action = 'getGenericsNoBase';
796
+ let returnF = true;
797
+ if (restMethod.toUpperCase() === 'POST') {
798
+ action = 'createGenericNoBase';
799
+ } else if (restMethod.toUpperCase() === 'PUT') {
800
+ action = 'updateGenericNoBase';
801
+ } else if (restMethod.toUpperCase() === 'PATCH') {
802
+ action = 'patchGenericNoBase';
803
+ } else if (restMethod.toUpperCase() === 'DELETE') {
804
+ action = 'deleteGenericNoBase';
805
+ returnF = false;
931
806
  }
932
807
 
933
808
  try {
934
- // need to get the device so we can convert the deviceName to an id
935
- // !! if we can do a lookup by name the getDevicesFiltered may not be necessary
936
- const opts = {
937
- filter: {
938
- name: deviceName
939
- }
940
- };
941
- return this.getDevicesFiltered(opts, (devs, ferr) => {
942
- // if we received an error or their is no response on the results return an error
943
- if (ferr) {
944
- return callback(null, ferr);
809
+ // Make the call -
810
+ // identifyRequest(entity, action, requestObj, returnDataFlag, callback)
811
+ return this.requestHandlerInst.identifyRequest('.generic', action, reqObj, returnF, (irReturnData, irReturnError) => {
812
+ // if we received an error or their is no response on the results
813
+ // return an error
814
+ if (irReturnError) {
815
+ /* HERE IS WHERE YOU CAN ALTER THE ERROR MESSAGE */
816
+ return callback(null, irReturnError);
945
817
  }
946
- if (devs.list.length < 1) {
947
- const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, `Did Not Find Device ${deviceName}`, [], null, null, null);
818
+ if (!Object.hasOwnProperty.call(irReturnData, 'response')) {
819
+ const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Invalid Response', ['genericAdapterRequestNoBasePath'], null, null, null);
948
820
  log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
949
821
  return callback(null, errorObj);
950
822
  }
951
- // get the uuid from the device
952
- const { uuid } = devs.list[0];
953
823
 
954
- // !! using Generic makes it easier on the Adapter Builder (just need to change the path)
955
- // !! you can also replace with a specific call if that is easier
956
- const uriPath = `/call/toget/config/${uuid}`;
957
- return this.genericAdapterRequest(uriPath, 'GET', {}, {}, {}, (result, error) => {
958
- // if we received an error or their is no response on the results return an error
959
- if (error) {
960
- return callback(null, error);
961
- }
962
-
963
- // return the result
964
- const newResponse = {
965
- response: JSON.stringify(result.response, null, 2)
966
- };
967
- return callback(newResponse);
968
- });
969
- });
970
- } catch (ex) {
971
- const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Caught Exception', null, null, null, ex);
972
- log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
973
- return callback(null, errorObj);
974
- }
975
- }
976
-
977
- /**
978
- * @summary Gets the device count from the system
979
- *
980
- * @function getCount
981
- *
982
- * @param {getCallback} callback - callback function to return the result
983
- * (count) or the error
984
- */
985
- getCount(callback) {
986
- const meth = 'adapter-getCount';
987
- const origin = `${this.id}-${meth}`;
988
- log.trace(origin);
989
-
990
- // verify the required fields have been provided
991
-
992
- try {
993
- // !! using Generic makes it easier on the Adapter Builder (just need to change the path)
994
- // !! you can also replace with a specific call if that is easier
995
- const uriPath = '/call/toget/count';
996
- return this.genericAdapterRequest(uriPath, 'GET', {}, {}, {}, (result, error) => {
997
- // if we received an error or their is no response on the results return an error
998
- if (error) {
999
- return callback(null, error);
1000
- }
1001
-
1002
- // return the result
1003
- return callback({ count: result.response });
824
+ /* HERE IS WHERE YOU CAN ALTER THE RETURN DATA */
825
+ // return the response
826
+ return callback(irReturnData, null);
1004
827
  });
1005
828
  } catch (ex) {
1006
829
  const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Caught Exception', null, null, null, ex);