@itentialopensource/adapter-checkpoint_management 0.5.0 → 0.7.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/AUTH.md +19 -16
- package/CALLS.md +12 -0
- package/CHANGELOG.md +16 -0
- package/CONTRIBUTING.md +1 -160
- package/ENHANCE.md +2 -2
- package/README.md +23 -18
- package/SYSTEMINFO.md +15 -3
- package/adapter.js +161 -333
- package/adapterBase.js +465 -898
- package/changelogs/changelog.md +111 -0
- package/metadata.json +52 -0
- package/package.json +25 -25
- package/pronghorn.json +460 -136
- package/propertiesSchema.json +358 -31
- package/refs?service=git-upload-pack +0 -0
- package/report/adapterInfo.json +8 -8
- package/report/updateReport1691152555900.json +120 -0
- package/report/updateReport1692203274399.json +120 -0
- package/sampleProperties.json +153 -34
- package/test/integration/adapterTestBasicGet.js +2 -4
- package/test/integration/adapterTestConnectivity.js +91 -42
- package/test/integration/adapterTestIntegration.js +130 -2
- package/test/unit/adapterBaseTestUnit.js +388 -313
- package/test/unit/adapterTestUnit.js +332 -112
- package/utils/adapterInfo.js +1 -1
- package/utils/addAuth.js +1 -1
- package/utils/artifactize.js +1 -1
- package/utils/checkMigrate.js +1 -1
- package/utils/entitiesToDB.js +2 -2
- package/utils/findPath.js +1 -1
- package/utils/methodDocumentor.js +225 -0
- package/utils/modify.js +13 -15
- package/utils/packModificationScript.js +1 -1
- package/utils/pre-commit.sh +2 -0
- package/utils/taskMover.js +309 -0
- package/utils/tbScript.js +89 -34
- package/utils/tbUtils.js +41 -21
- package/utils/testRunner.js +1 -1
- package/utils/troubleshootingAdapter.js +9 -6
- package/workflows/README.md +0 -3
package/adapterBase.js
CHANGED
|
@@ -11,14 +11,18 @@
|
|
|
11
11
|
/* eslint prefer-destructuring: warn */
|
|
12
12
|
|
|
13
13
|
/* Required libraries. */
|
|
14
|
-
const fs = require('fs-extra');
|
|
15
14
|
const path = require('path');
|
|
16
|
-
const jsonQuery = require('json-query');
|
|
17
|
-
const EventEmitterCl = require('events').EventEmitter;
|
|
18
15
|
const { execSync } = require('child_process');
|
|
16
|
+
const { spawnSync } = require('child_process');
|
|
17
|
+
const EventEmitterCl = require('events').EventEmitter;
|
|
18
|
+
const fs = require('fs-extra');
|
|
19
|
+
const jsonQuery = require('json-query');
|
|
20
|
+
|
|
21
|
+
const sampleProperties = require(`${__dirname}/sampleProperties.json`).properties;
|
|
19
22
|
|
|
20
23
|
/* The schema validator */
|
|
21
24
|
const AjvCl = require('ajv');
|
|
25
|
+
const { Test } = require('mocha');
|
|
22
26
|
|
|
23
27
|
/* Fetch in the other needed components for the this Adaptor */
|
|
24
28
|
const PropUtilCl = require('@itentialopensource/adapter-utils').PropertyUtility;
|
|
@@ -27,8 +31,10 @@ const RequestHandlerCl = require('@itentialopensource/adapter-utils').RequestHan
|
|
|
27
31
|
const entitiesToDB = require(path.join(__dirname, 'utils/entitiesToDB'));
|
|
28
32
|
const troubleshootingAdapter = require(path.join(__dirname, 'utils/troubleshootingAdapter'));
|
|
29
33
|
const tbUtils = require(path.join(__dirname, 'utils/tbUtils'));
|
|
34
|
+
const taskMover = require(path.join(__dirname, 'utils/taskMover'));
|
|
30
35
|
|
|
31
36
|
let propUtil = null;
|
|
37
|
+
let choosepath = null;
|
|
32
38
|
|
|
33
39
|
/*
|
|
34
40
|
* INTERNAL FUNCTION: force fail the adapter - generally done to cause restart
|
|
@@ -101,7 +107,7 @@ function updateSchema(entityPath, configFile, changes) {
|
|
|
101
107
|
/*
|
|
102
108
|
* INTERNAL FUNCTION: update the mock data file
|
|
103
109
|
*/
|
|
104
|
-
function updateMock(mockPath, configFile, changes) {
|
|
110
|
+
function updateMock(mockPath, configFile, changes, replace) {
|
|
105
111
|
// if the mock file does not exist - create it
|
|
106
112
|
const mockFile = path.join(mockPath, `/${configFile}`);
|
|
107
113
|
if (!fs.existsSync(mockFile)) {
|
|
@@ -113,7 +119,11 @@ function updateMock(mockPath, configFile, changes) {
|
|
|
113
119
|
let mock = require(path.resolve(mockPath, configFile));
|
|
114
120
|
|
|
115
121
|
// merge the changes into the mock file
|
|
116
|
-
|
|
122
|
+
if (replace === true) {
|
|
123
|
+
mock = changes;
|
|
124
|
+
} else {
|
|
125
|
+
mock = propUtil.mergeProperties(changes, mock);
|
|
126
|
+
}
|
|
117
127
|
|
|
118
128
|
fs.writeFileSync(mockFile, JSON.stringify(mock, null, 2));
|
|
119
129
|
return null;
|
|
@@ -145,27 +155,6 @@ function updatePackage(changes) {
|
|
|
145
155
|
return null;
|
|
146
156
|
}
|
|
147
157
|
|
|
148
|
-
/*
|
|
149
|
-
* INTERNAL FUNCTION: get data from source(s) - nested
|
|
150
|
-
*/
|
|
151
|
-
function getDataFromSources(loopField, sources) {
|
|
152
|
-
let fieldValue = loopField;
|
|
153
|
-
|
|
154
|
-
// go through the sources to find the field
|
|
155
|
-
for (let s = 0; s < sources.length; s += 1) {
|
|
156
|
-
// find the field value using jsonquery
|
|
157
|
-
const nestedValue = jsonQuery(loopField, { data: sources[s] }).value;
|
|
158
|
-
|
|
159
|
-
// if we found in source - set and no need to check other sources
|
|
160
|
-
if (nestedValue) {
|
|
161
|
-
fieldValue = nestedValue;
|
|
162
|
-
break;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
return fieldValue;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
158
|
/* GENERAL ADAPTER FUNCTIONS THESE SHOULD NOT BE DIRECTLY MODIFIED */
|
|
170
159
|
/* IF YOU NEED MODIFICATIONS, REDEFINE THEM IN adapter.js!!! */
|
|
171
160
|
class AdapterBase extends EventEmitterCl {
|
|
@@ -255,7 +244,7 @@ class AdapterBase extends EventEmitterCl {
|
|
|
255
244
|
this.allProps = this.propUtilInst.mergeProperties(properties, defProps);
|
|
256
245
|
|
|
257
246
|
// validate the entity against the schema
|
|
258
|
-
const ajvInst = new AjvCl();
|
|
247
|
+
const ajvInst = new AjvCl({ strictSchema: false, allowUnionTypes: true });
|
|
259
248
|
const validate = ajvInst.compile(propertiesSchema);
|
|
260
249
|
const result = validate(this.allProps);
|
|
261
250
|
|
|
@@ -379,9 +368,15 @@ class AdapterBase extends EventEmitterCl {
|
|
|
379
368
|
this.emit('OFFLINE', { id: this.id });
|
|
380
369
|
this.emit('DEGRADED', { id: this.id });
|
|
381
370
|
this.healthy = false;
|
|
382
|
-
|
|
383
|
-
|
|
371
|
+
if (typeof error === 'object') {
|
|
372
|
+
log.error(`${origin}: HEALTH CHECK - Error ${JSON.stringify(error)}`);
|
|
373
|
+
} else {
|
|
374
|
+
log.error(`${origin}: HEALTH CHECK - Error ${error}`);
|
|
375
|
+
}
|
|
376
|
+
} else if (typeof error === 'object') {
|
|
384
377
|
// still log but set the level to trace
|
|
378
|
+
log.trace(`${origin}: HEALTH CHECK - Still Errors ${JSON.stringify(error)}`);
|
|
379
|
+
} else {
|
|
385
380
|
log.trace(`${origin}: HEALTH CHECK - Still Errors ${error}`);
|
|
386
381
|
}
|
|
387
382
|
|
|
@@ -427,6 +422,40 @@ class AdapterBase extends EventEmitterCl {
|
|
|
427
422
|
return myfunctions;
|
|
428
423
|
}
|
|
429
424
|
|
|
425
|
+
/**
|
|
426
|
+
* iapGetAdapterWorkflowFunctions is used to get all of the workflow function in the adapter
|
|
427
|
+
* @param {array} ignoreThese - additional methods to ignore (optional)
|
|
428
|
+
*
|
|
429
|
+
* @function iapGetAdapterWorkflowFunctions
|
|
430
|
+
*/
|
|
431
|
+
iapGetAdapterWorkflowFunctions(ignoreThese) {
|
|
432
|
+
const myfunctions = this.getAllFunctions();
|
|
433
|
+
const wffunctions = [];
|
|
434
|
+
|
|
435
|
+
// remove the functions that should not be in a Workflow
|
|
436
|
+
for (let m = 0; m < myfunctions.length; m += 1) {
|
|
437
|
+
if (myfunctions[m] === 'checkActionFiles') {
|
|
438
|
+
// got to the second tier (adapterBase)
|
|
439
|
+
break;
|
|
440
|
+
}
|
|
441
|
+
if (!(myfunctions[m].endsWith('Emit') || myfunctions[m].match(/Emit__v[0-9]+/))) {
|
|
442
|
+
let found = false;
|
|
443
|
+
if (ignoreThese && Array.isArray(ignoreThese)) {
|
|
444
|
+
for (let i = 0; i < ignoreThese.length; i += 1) {
|
|
445
|
+
if (myfunctions[m].toUpperCase() === ignoreThese[i].toUpperCase()) {
|
|
446
|
+
found = true;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
if (!found) {
|
|
451
|
+
wffunctions.push(myfunctions[m]);
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
return wffunctions;
|
|
457
|
+
}
|
|
458
|
+
|
|
430
459
|
/**
|
|
431
460
|
* checkActionFiles is used to update the validation of the action files.
|
|
432
461
|
*
|
|
@@ -482,40 +511,6 @@ class AdapterBase extends EventEmitterCl {
|
|
|
482
511
|
return this.requestHandlerInst.encryptProperty(property, technique, callback);
|
|
483
512
|
}
|
|
484
513
|
|
|
485
|
-
/**
|
|
486
|
-
* iapGetAdapterWorkflowFunctions is used to get all of the workflow function in the adapter
|
|
487
|
-
* @param {array} ignoreThese - additional methods to ignore (optional)
|
|
488
|
-
*
|
|
489
|
-
* @function iapGetAdapterWorkflowFunctions
|
|
490
|
-
*/
|
|
491
|
-
iapGetAdapterWorkflowFunctions(ignoreThese) {
|
|
492
|
-
const myfunctions = this.getAllFunctions();
|
|
493
|
-
const wffunctions = [];
|
|
494
|
-
|
|
495
|
-
// remove the functions that should not be in a Workflow
|
|
496
|
-
for (let m = 0; m < myfunctions.length; m += 1) {
|
|
497
|
-
if (myfunctions[m] === 'addEntityCache') {
|
|
498
|
-
// got to the second tier (adapterBase)
|
|
499
|
-
break;
|
|
500
|
-
}
|
|
501
|
-
if (!(myfunctions[m].endsWith('Emit') || myfunctions[m].match(/Emit__v[0-9]+/))) {
|
|
502
|
-
let found = false;
|
|
503
|
-
if (ignoreThese && Array.isArray(ignoreThese)) {
|
|
504
|
-
for (let i = 0; i < ignoreThese.length; i += 1) {
|
|
505
|
-
if (myfunctions[m].toUpperCase() === ignoreThese[i].toUpperCase()) {
|
|
506
|
-
found = true;
|
|
507
|
-
}
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
if (!found) {
|
|
511
|
-
wffunctions.push(myfunctions[m]);
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
return wffunctions;
|
|
517
|
-
}
|
|
518
|
-
|
|
519
514
|
/**
|
|
520
515
|
* iapUpdateAdapterConfiguration is used to update any of the adapter configuration files. This
|
|
521
516
|
* allows customers to make changes to adapter configuration without having to be on the
|
|
@@ -527,16 +522,17 @@ class AdapterBase extends EventEmitterCl {
|
|
|
527
522
|
* @param {string} entity - the entity to be changed, if an action, schema or mock data file (optional)
|
|
528
523
|
* @param {string} type - the type of entity file to change, (action, schema, mock) (optional)
|
|
529
524
|
* @param {string} action - the action to be changed, if an action, schema or mock data file (optional)
|
|
525
|
+
* @param {boolean} replace - true to replace entire mock data, false to merge/append (optional)
|
|
530
526
|
* @param {Callback} callback - The results of the call
|
|
531
527
|
*/
|
|
532
|
-
iapUpdateAdapterConfiguration(configFile, changes, entity, type, action, callback) {
|
|
528
|
+
iapUpdateAdapterConfiguration(configFile, changes, entity, type, action, replace, callback) {
|
|
533
529
|
const meth = 'adapterBase-iapUpdateAdapterConfiguration';
|
|
534
530
|
const origin = `${this.id}-${meth}`;
|
|
535
531
|
log.trace(origin);
|
|
536
532
|
|
|
537
533
|
// verify the parameters are valid
|
|
538
534
|
if (changes === undefined || changes === null || typeof changes !== 'object'
|
|
539
|
-
|
|
535
|
+
|| Object.keys(changes).length === 0) {
|
|
540
536
|
const result = {
|
|
541
537
|
response: 'No configuration updates to make'
|
|
542
538
|
};
|
|
@@ -621,8 +617,14 @@ class AdapterBase extends EventEmitterCl {
|
|
|
621
617
|
if (!fs.existsSync(mpath)) {
|
|
622
618
|
fs.mkdirSync(mpath);
|
|
623
619
|
}
|
|
620
|
+
// this means we are changing a mock data file so replace is required
|
|
621
|
+
if (replace === undefined || replace === null || replace === '') {
|
|
622
|
+
const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Missing Data', ['replace'], null, null, null);
|
|
623
|
+
log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
624
|
+
return callback(null, errorObj);
|
|
625
|
+
}
|
|
626
|
+
const mres = updateMock(mpath, configFile, changes, replace);
|
|
624
627
|
|
|
625
|
-
const mres = updateMock(mpath, configFile, changes);
|
|
626
628
|
if (mres) {
|
|
627
629
|
const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, `Incomplete Configuration Change: ${mres}`, [], null, null, null);
|
|
628
630
|
log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
@@ -639,6 +641,86 @@ class AdapterBase extends EventEmitterCl {
|
|
|
639
641
|
return callback(null, errorObj);
|
|
640
642
|
}
|
|
641
643
|
|
|
644
|
+
/**
|
|
645
|
+
* @summary Suspends the adapter
|
|
646
|
+
* @param {Callback} callback - The adapater suspension status
|
|
647
|
+
* @function iapSuspendAdapter
|
|
648
|
+
*/
|
|
649
|
+
iapSuspendAdapter(mode, callback) {
|
|
650
|
+
const origin = `${this.id}-adapterBase-iapSuspendAdapter`;
|
|
651
|
+
if (this.suspended) {
|
|
652
|
+
throw new Error(`${origin}: Adapter is already suspended`);
|
|
653
|
+
}
|
|
654
|
+
try {
|
|
655
|
+
this.suspended = true;
|
|
656
|
+
this.suspendMode = mode;
|
|
657
|
+
if (this.suspendMode === 'pause') {
|
|
658
|
+
const props = JSON.parse(JSON.stringify(this.initProps));
|
|
659
|
+
// To suspend adapter, enable throttling and set concurrent max to 0
|
|
660
|
+
props.throttle.throttle_enabled = true;
|
|
661
|
+
props.throttle.concurrent_max = 0;
|
|
662
|
+
this.refreshProperties(props);
|
|
663
|
+
}
|
|
664
|
+
return callback({ suspended: true });
|
|
665
|
+
} catch (error) {
|
|
666
|
+
return callback(null, error);
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
/**
|
|
671
|
+
* @summary Unsuspends the adapter
|
|
672
|
+
* @param {Callback} callback - The adapater suspension status
|
|
673
|
+
*
|
|
674
|
+
* @function iapUnsuspendAdapter
|
|
675
|
+
*/
|
|
676
|
+
iapUnsuspendAdapter(callback) {
|
|
677
|
+
const origin = `${this.id}-adapterBase-iapUnsuspendAdapter`;
|
|
678
|
+
if (!this.suspended) {
|
|
679
|
+
throw new Error(`${origin}: Adapter is not suspended`);
|
|
680
|
+
}
|
|
681
|
+
if (this.suspendMode === 'pause') {
|
|
682
|
+
const props = JSON.parse(JSON.stringify(this.initProps));
|
|
683
|
+
// To unsuspend adapter, keep throttling enabled and begin processing queued requests in order
|
|
684
|
+
props.throttle.throttle_enabled = true;
|
|
685
|
+
props.throttle.concurrent_max = 1;
|
|
686
|
+
this.refreshProperties(props);
|
|
687
|
+
setTimeout(() => {
|
|
688
|
+
this.getQueue((q, error) => {
|
|
689
|
+
// console.log("Items in queue: " + String(q.length))
|
|
690
|
+
if (q.length === 0) {
|
|
691
|
+
// if queue is empty, return to initial properties state
|
|
692
|
+
this.refreshProperties(this.initProps);
|
|
693
|
+
this.suspended = false;
|
|
694
|
+
return callback({ suspended: false });
|
|
695
|
+
}
|
|
696
|
+
// recursive call to check queue again every second
|
|
697
|
+
return this.iapUnsuspendAdapter(callback);
|
|
698
|
+
});
|
|
699
|
+
}, 1000);
|
|
700
|
+
} else {
|
|
701
|
+
this.suspended = false;
|
|
702
|
+
callback({ suspend: false });
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
/**
|
|
707
|
+
* iapGetAdapterQueue is used to get information for all of the requests currently in the queue.
|
|
708
|
+
*
|
|
709
|
+
* @function iapGetAdapterQueue
|
|
710
|
+
* @param {Callback} callback - a callback function to return the result (Queue) or the error
|
|
711
|
+
*/
|
|
712
|
+
iapGetAdapterQueue(callback) {
|
|
713
|
+
const origin = `${this.id}-adapterBase-iapGetAdapterQueue`;
|
|
714
|
+
log.trace(origin);
|
|
715
|
+
|
|
716
|
+
return this.requestHandlerInst.getQueue(callback);
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
/* ********************************************** */
|
|
720
|
+
/* */
|
|
721
|
+
/* EXPOSES ADAPTER SCRIPTS */
|
|
722
|
+
/* */
|
|
723
|
+
/* ********************************************** */
|
|
642
724
|
/**
|
|
643
725
|
* See if the API path provided is found in this adapter
|
|
644
726
|
*
|
|
@@ -659,6 +741,10 @@ class AdapterBase extends EventEmitterCl {
|
|
|
659
741
|
return callback(null, result);
|
|
660
742
|
}
|
|
661
743
|
|
|
744
|
+
if (typeof this.allProps.choosepath === 'string') {
|
|
745
|
+
choosepath = this.allProps.choosepath;
|
|
746
|
+
}
|
|
747
|
+
|
|
662
748
|
// make sure the entities directory exists
|
|
663
749
|
const entitydir = path.join(__dirname, 'entities');
|
|
664
750
|
if (!fs.statSync(entitydir).isDirectory()) {
|
|
@@ -683,7 +769,25 @@ class AdapterBase extends EventEmitterCl {
|
|
|
683
769
|
|
|
684
770
|
// go through all of the actions set the appropriate info in the newActions
|
|
685
771
|
for (let a = 0; a < actions.actions.length; a += 1) {
|
|
686
|
-
if (actions.actions[a].entitypath.
|
|
772
|
+
if (actions.actions[a].entitypath && typeof actions.actions[a].entitypath === 'object') {
|
|
773
|
+
const entityKeys = Object.keys(actions.actions[a].entitypath);
|
|
774
|
+
if (entityKeys.length > 0) {
|
|
775
|
+
for (let entityKey = 0; entityKey < entityKeys.length; entityKey += 1) {
|
|
776
|
+
if (choosepath && entityKeys[entityKey] === choosepath && actions.actions[a].entitypath[entityKeys[entityKey]].indexOf(apiPath) >= 0) {
|
|
777
|
+
log.info(` Found - entity: ${entities[e]} action: ${actions.actions[a].name}`);
|
|
778
|
+
log.info(` method: ${actions.actions[a].method} path: ${actions.actions[a].entitypath[entityKeys[entityKey]]}`);
|
|
779
|
+
const fitem = {
|
|
780
|
+
entity: entities[e],
|
|
781
|
+
action: actions.actions[a].name,
|
|
782
|
+
method: actions.actions[a].method,
|
|
783
|
+
path: actions.actions[a].entitypath[entityKeys[entityKey]]
|
|
784
|
+
};
|
|
785
|
+
fitems.push(fitem);
|
|
786
|
+
break;
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
} else if (actions.actions[a].entitypath.indexOf(apiPath) >= 0) {
|
|
687
791
|
log.info(` Found - entity: ${entities[e]} action: ${actions.actions[a].name}`);
|
|
688
792
|
log.info(` method: ${actions.actions[a].method} path: ${actions.actions[a].entitypath}`);
|
|
689
793
|
const fitem = {
|
|
@@ -722,81 +826,6 @@ class AdapterBase extends EventEmitterCl {
|
|
|
722
826
|
return callback(result, null);
|
|
723
827
|
}
|
|
724
828
|
|
|
725
|
-
/**
|
|
726
|
-
* @summary Suspends the adapter
|
|
727
|
-
* @param {Callback} callback - The adapater suspension status
|
|
728
|
-
* @function iapSuspendAdapter
|
|
729
|
-
*/
|
|
730
|
-
iapSuspendAdapter(mode, callback) {
|
|
731
|
-
const origin = `${this.id}-adapterBase-iapSuspendAdapter`;
|
|
732
|
-
if (this.suspended) {
|
|
733
|
-
throw new Error(`${origin}: Adapter is already suspended`);
|
|
734
|
-
}
|
|
735
|
-
try {
|
|
736
|
-
this.suspended = true;
|
|
737
|
-
this.suspendMode = mode;
|
|
738
|
-
if (this.suspendMode === 'pause') {
|
|
739
|
-
const props = JSON.parse(JSON.stringify(this.initProps));
|
|
740
|
-
// To suspend adapter, enable throttling and set concurrent max to 0
|
|
741
|
-
props.throttle.throttle_enabled = true;
|
|
742
|
-
props.throttle.concurrent_max = 0;
|
|
743
|
-
this.refreshProperties(props);
|
|
744
|
-
}
|
|
745
|
-
return callback({ suspended: true });
|
|
746
|
-
} catch (error) {
|
|
747
|
-
return callback(null, error);
|
|
748
|
-
}
|
|
749
|
-
}
|
|
750
|
-
|
|
751
|
-
/**
|
|
752
|
-
* @summary Unsuspends the adapter
|
|
753
|
-
* @param {Callback} callback - The adapater suspension status
|
|
754
|
-
*
|
|
755
|
-
* @function iapUnsuspendAdapter
|
|
756
|
-
*/
|
|
757
|
-
iapUnsuspendAdapter(callback) {
|
|
758
|
-
const origin = `${this.id}-adapterBase-iapUnsuspendAdapter`;
|
|
759
|
-
if (!this.suspended) {
|
|
760
|
-
throw new Error(`${origin}: Adapter is not suspended`);
|
|
761
|
-
}
|
|
762
|
-
if (this.suspendMode === 'pause') {
|
|
763
|
-
const props = JSON.parse(JSON.stringify(this.initProps));
|
|
764
|
-
// To unsuspend adapter, keep throttling enabled and begin processing queued requests in order
|
|
765
|
-
props.throttle.throttle_enabled = true;
|
|
766
|
-
props.throttle.concurrent_max = 1;
|
|
767
|
-
this.refreshProperties(props);
|
|
768
|
-
setTimeout(() => {
|
|
769
|
-
this.getQueue((q, error) => {
|
|
770
|
-
// console.log("Items in queue: " + String(q.length))
|
|
771
|
-
if (q.length === 0) {
|
|
772
|
-
// if queue is empty, return to initial properties state
|
|
773
|
-
this.refreshProperties(this.initProps);
|
|
774
|
-
this.suspended = false;
|
|
775
|
-
return callback({ suspended: false });
|
|
776
|
-
}
|
|
777
|
-
// recursive call to check queue again every second
|
|
778
|
-
return this.iapUnsuspendAdapter(callback);
|
|
779
|
-
});
|
|
780
|
-
}, 1000);
|
|
781
|
-
} else {
|
|
782
|
-
this.suspended = false;
|
|
783
|
-
callback({ suspend: false });
|
|
784
|
-
}
|
|
785
|
-
}
|
|
786
|
-
|
|
787
|
-
/**
|
|
788
|
-
* iapGetAdapterQueue is used to get information for all of the requests currently in the queue.
|
|
789
|
-
*
|
|
790
|
-
* @function iapGetAdapterQueue
|
|
791
|
-
* @param {Callback} callback - a callback function to return the result (Queue) or the error
|
|
792
|
-
*/
|
|
793
|
-
iapGetAdapterQueue(callback) {
|
|
794
|
-
const origin = `${this.id}-adapterBase-iapGetAdapterQueue`;
|
|
795
|
-
log.trace(origin);
|
|
796
|
-
|
|
797
|
-
return this.requestHandlerInst.getQueue(callback);
|
|
798
|
-
}
|
|
799
|
-
|
|
800
829
|
/**
|
|
801
830
|
* @summary runs troubleshoot scripts for adapter
|
|
802
831
|
*
|
|
@@ -831,7 +860,7 @@ class AdapterBase extends EventEmitterCl {
|
|
|
831
860
|
if (result) {
|
|
832
861
|
return callback(result);
|
|
833
862
|
}
|
|
834
|
-
return callback(null,
|
|
863
|
+
return callback(null, 'Healthcheck failed');
|
|
835
864
|
} catch (error) {
|
|
836
865
|
return callback(null, error);
|
|
837
866
|
}
|
|
@@ -846,8 +875,7 @@ class AdapterBase extends EventEmitterCl {
|
|
|
846
875
|
*/
|
|
847
876
|
async iapRunAdapterConnectivity(callback) {
|
|
848
877
|
try {
|
|
849
|
-
const {
|
|
850
|
-
const { host } = serviceItem.properties.properties;
|
|
878
|
+
const { host } = this.allProps;
|
|
851
879
|
const result = tbUtils.runConnectivity(host, false);
|
|
852
880
|
if (result.failCount > 0) {
|
|
853
881
|
return callback(null, result);
|
|
@@ -899,156 +927,89 @@ class AdapterBase extends EventEmitterCl {
|
|
|
899
927
|
}
|
|
900
928
|
|
|
901
929
|
/**
|
|
902
|
-
* @
|
|
903
|
-
*
|
|
904
|
-
* @function addEntityCache
|
|
905
|
-
* @param {String} entityType - the type of the entities
|
|
906
|
-
* @param {Array} data - the list of entities
|
|
907
|
-
* @param {String} key - unique key for the entities
|
|
930
|
+
* @function iapDeactivateTasks
|
|
908
931
|
*
|
|
909
|
-
* @param {
|
|
910
|
-
*
|
|
932
|
+
* @param {Array} tasks - List of tasks to deactivate
|
|
933
|
+
* @param {Callback} callback
|
|
911
934
|
*/
|
|
912
|
-
|
|
913
|
-
const meth = 'adapterBase-
|
|
935
|
+
iapDeactivateTasks(tasks, callback) {
|
|
936
|
+
const meth = 'adapterBase-iapDeactivateTasks';
|
|
914
937
|
const origin = `${this.id}-${meth}`;
|
|
915
938
|
log.trace(origin);
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
entityIds.push(entities.response[e][key]);
|
|
924
|
-
}
|
|
939
|
+
let data;
|
|
940
|
+
try {
|
|
941
|
+
data = taskMover.deactivateTasks(__dirname, tasks);
|
|
942
|
+
} catch (ex) {
|
|
943
|
+
taskMover.rollbackChanges(__dirname);
|
|
944
|
+
taskMover.deleteBackups(__dirname);
|
|
945
|
+
return callback(null, ex);
|
|
925
946
|
}
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
if (error) {
|
|
930
|
-
return callback(null, error);
|
|
931
|
-
}
|
|
932
|
-
if (!loaded) {
|
|
933
|
-
const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Entity Cache Not Loading', [entityType], null, null, null);
|
|
934
|
-
log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
935
|
-
return callback(null, errorObj);
|
|
936
|
-
}
|
|
937
|
-
|
|
938
|
-
return callback(loaded);
|
|
939
|
-
});
|
|
940
|
-
}
|
|
947
|
+
taskMover.deleteBackups(__dirname);
|
|
948
|
+
return callback(data, null);
|
|
949
|
+
}
|
|
941
950
|
|
|
942
951
|
/**
|
|
943
|
-
* @
|
|
944
|
-
*
|
|
945
|
-
* @function entityInList
|
|
946
|
-
* @param {String/Array} entityId - the specific entity we are looking for
|
|
947
|
-
* @param {Array} data - the list of entities
|
|
952
|
+
* @function iapActivateTasks
|
|
948
953
|
*
|
|
949
|
-
* @param {
|
|
950
|
-
*
|
|
954
|
+
* @param {Array} tasks - List of tasks to deactivate
|
|
955
|
+
* @param {Callback} callback
|
|
951
956
|
*/
|
|
952
|
-
|
|
953
|
-
const
|
|
957
|
+
iapActivateTasks(tasks, callback) {
|
|
958
|
+
const meth = 'adapterBase-iapActivateTasks';
|
|
959
|
+
const origin = `${this.id}-${meth}`;
|
|
954
960
|
log.trace(origin);
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
resEntity.push(true);
|
|
963
|
-
} else {
|
|
964
|
-
resEntity.push(false);
|
|
965
|
-
}
|
|
966
|
-
}
|
|
967
|
-
|
|
968
|
-
return resEntity;
|
|
961
|
+
let data;
|
|
962
|
+
try {
|
|
963
|
+
data = taskMover.activateTasks(__dirname, tasks);
|
|
964
|
+
} catch (ex) {
|
|
965
|
+
taskMover.rollbackChanges(__dirname);
|
|
966
|
+
taskMover.deleteBackups(__dirname);
|
|
967
|
+
return callback(null, ex);
|
|
969
968
|
}
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
return [data.includes(entityId)];
|
|
969
|
+
taskMover.deleteBackups(__dirname);
|
|
970
|
+
return callback(data, null);
|
|
973
971
|
}
|
|
974
972
|
|
|
973
|
+
/* ********************************************** */
|
|
974
|
+
/* */
|
|
975
|
+
/* EXPOSES CACHE CALLS */
|
|
976
|
+
/* */
|
|
977
|
+
/* ********************************************** */
|
|
975
978
|
/**
|
|
976
|
-
* @summary
|
|
979
|
+
* @summary Populate the cache for the given entities
|
|
977
980
|
*
|
|
978
|
-
* @function
|
|
979
|
-
* @param {Array}
|
|
980
|
-
*
|
|
981
|
-
* @
|
|
982
|
-
* desired capability or an error
|
|
981
|
+
* @function iapPopulateEntityCache
|
|
982
|
+
* @param {String/Array of Strings} entityType - the entity type(s) to populate
|
|
983
|
+
* @param {Callback} callback - whether the cache was updated or not for each entity type
|
|
984
|
+
* @returns return of the callback
|
|
983
985
|
*/
|
|
984
|
-
|
|
985
|
-
const
|
|
986
|
-
const origin = `${this.id}-${meth}`;
|
|
986
|
+
iapPopulateEntityCache(entityTypes, callback) {
|
|
987
|
+
const origin = `${this.myid}-adapterBase-iapPopulateEntityCache`;
|
|
987
988
|
log.trace(origin);
|
|
988
|
-
let locResults = results;
|
|
989
989
|
|
|
990
|
-
|
|
991
|
-
const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Entity Cache Not Loading', ['unknown'], null, null, null);
|
|
992
|
-
log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
993
|
-
this.repeatCacheCount += 1;
|
|
994
|
-
return callback(null, errorObj);
|
|
995
|
-
}
|
|
996
|
-
|
|
997
|
-
// if an error occured, return the error
|
|
998
|
-
if (locResults && locResults[0] === 'error') {
|
|
999
|
-
const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Error Verifying Entity Cache', null, null, null, null);
|
|
1000
|
-
log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
1001
|
-
return callback(null, errorObj);
|
|
1002
|
-
}
|
|
1003
|
-
|
|
1004
|
-
// go through the response and change to true/false
|
|
1005
|
-
if (locResults) {
|
|
1006
|
-
// if not an array, just convert the return
|
|
1007
|
-
if (!Array.isArray(locResults)) {
|
|
1008
|
-
if (locResults === 'found') {
|
|
1009
|
-
locResults = [true];
|
|
1010
|
-
} else {
|
|
1011
|
-
locResults = [false];
|
|
1012
|
-
}
|
|
1013
|
-
} else {
|
|
1014
|
-
const temp = [];
|
|
1015
|
-
|
|
1016
|
-
// go through each element in the array to convert
|
|
1017
|
-
for (let r = 0; r < locResults.length; r += 1) {
|
|
1018
|
-
if (locResults[r] === 'found') {
|
|
1019
|
-
temp.push(true);
|
|
1020
|
-
} else {
|
|
1021
|
-
temp.push(false);
|
|
1022
|
-
}
|
|
1023
|
-
}
|
|
1024
|
-
locResults = temp;
|
|
1025
|
-
}
|
|
1026
|
-
}
|
|
1027
|
-
|
|
1028
|
-
// return the results
|
|
1029
|
-
return callback(locResults);
|
|
990
|
+
return this.requestHandlerInst.populateEntityCache(entityTypes, callback);
|
|
1030
991
|
}
|
|
1031
992
|
|
|
1032
993
|
/**
|
|
1033
|
-
* @summary
|
|
1034
|
-
* all of the capabilities for the current adapter
|
|
994
|
+
* @summary Retrieves data from cache for specified entity type
|
|
1035
995
|
*
|
|
1036
|
-
* @function
|
|
1037
|
-
*
|
|
1038
|
-
* @
|
|
996
|
+
* @function iapRetrieveEntitiesCache
|
|
997
|
+
* @param {String} entityType - entity of which to retrieve
|
|
998
|
+
* @param {Object} options - settings of which data to return and how to return it
|
|
999
|
+
* @param {Callback} callback - the data if it was retrieved
|
|
1039
1000
|
*/
|
|
1040
|
-
|
|
1041
|
-
const origin = `${this.
|
|
1001
|
+
iapRetrieveEntitiesCache(entityType, options, callback) {
|
|
1002
|
+
const origin = `${this.myid}-adapterBase-iapRetrieveEntitiesCache`;
|
|
1042
1003
|
log.trace(origin);
|
|
1043
1004
|
|
|
1044
|
-
|
|
1045
|
-
try {
|
|
1046
|
-
return this.requestHandlerInst.getAllCapabilities();
|
|
1047
|
-
} catch (e) {
|
|
1048
|
-
return [];
|
|
1049
|
-
}
|
|
1005
|
+
return this.requestHandlerInst.retrieveEntitiesCache(entityType, options, callback);
|
|
1050
1006
|
}
|
|
1051
1007
|
|
|
1008
|
+
/* ********************************************** */
|
|
1009
|
+
/* */
|
|
1010
|
+
/* EXPOSES BROKER CALLS */
|
|
1011
|
+
/* */
|
|
1012
|
+
/* ********************************************** */
|
|
1052
1013
|
/**
|
|
1053
1014
|
* @summary Determines if this adapter supports any in a list of entities
|
|
1054
1015
|
*
|
|
@@ -1060,716 +1021,322 @@ class AdapterBase extends EventEmitterCl {
|
|
|
1060
1021
|
* value is true or false
|
|
1061
1022
|
*/
|
|
1062
1023
|
hasEntities(entityType, entityList, callback) {
|
|
1063
|
-
const origin = `${this.id}-
|
|
1024
|
+
const origin = `${this.id}-adapterBase-hasEntities`;
|
|
1064
1025
|
log.trace(origin);
|
|
1065
1026
|
|
|
1066
|
-
|
|
1067
|
-
case 'Device':
|
|
1068
|
-
return this.hasDevices(entityList, callback);
|
|
1069
|
-
default:
|
|
1070
|
-
return callback(null, `${this.id} does not support entity ${entityType}`);
|
|
1071
|
-
}
|
|
1027
|
+
return this.requestHandlerInst.hasEntities(entityType, entityList, callback);
|
|
1072
1028
|
}
|
|
1073
1029
|
|
|
1074
1030
|
/**
|
|
1075
|
-
* @summary
|
|
1031
|
+
* @summary Get Appliance that match the deviceName
|
|
1076
1032
|
*
|
|
1077
|
-
* @
|
|
1078
|
-
* @param {
|
|
1079
|
-
*
|
|
1033
|
+
* @function getDevice
|
|
1034
|
+
* @param {String} deviceName - the deviceName to find (required)
|
|
1035
|
+
*
|
|
1036
|
+
* @param {getCallback} callback - a callback function to return the result
|
|
1037
|
+
* (appliance) or the error
|
|
1080
1038
|
*/
|
|
1081
|
-
|
|
1082
|
-
const origin = `${this.id}-
|
|
1039
|
+
getDevice(deviceName, callback) {
|
|
1040
|
+
const origin = `${this.id}-adapterBase-getDevice`;
|
|
1083
1041
|
log.trace(origin);
|
|
1084
1042
|
|
|
1085
|
-
|
|
1086
|
-
// eslint-disable-next-line no-param-reassign
|
|
1087
|
-
map[device] = false;
|
|
1088
|
-
log.debug(`In reduce: ${JSON.stringify(map)}`);
|
|
1089
|
-
return map;
|
|
1090
|
-
}, {});
|
|
1091
|
-
const apiCalls = deviceList.map((device) => new Promise((resolve) => {
|
|
1092
|
-
this.getDevice(device, (result, error) => {
|
|
1093
|
-
if (error) {
|
|
1094
|
-
log.debug(`In map error: ${JSON.stringify(device)}`);
|
|
1095
|
-
return resolve({ name: device, found: false });
|
|
1096
|
-
}
|
|
1097
|
-
log.debug(`In map: ${JSON.stringify(device)}`);
|
|
1098
|
-
return resolve({ name: device, found: true });
|
|
1099
|
-
});
|
|
1100
|
-
}));
|
|
1101
|
-
Promise.all(apiCalls).then((results) => {
|
|
1102
|
-
results.forEach((device) => {
|
|
1103
|
-
findings[device.name] = device.found;
|
|
1104
|
-
});
|
|
1105
|
-
log.debug(`FINDINGS: ${JSON.stringify(findings)}`);
|
|
1106
|
-
return callback(findings);
|
|
1107
|
-
}).catch((errors) => {
|
|
1108
|
-
log.error('Unable to do device lookup.');
|
|
1109
|
-
return callback(null, { code: 503, message: 'Unable to do device lookup.', error: errors });
|
|
1110
|
-
});
|
|
1043
|
+
return this.requestHandlerInst.getDevice(deviceName, callback);
|
|
1111
1044
|
}
|
|
1112
1045
|
|
|
1113
1046
|
/**
|
|
1114
|
-
* @summary
|
|
1047
|
+
* @summary Get Appliances that match the filter
|
|
1115
1048
|
*
|
|
1116
|
-
* @function
|
|
1117
|
-
* @param {
|
|
1118
|
-
* @param {object} callProps - the proeprties for the broker call (required)
|
|
1119
|
-
* @param {object} devResp - the device details to extract needed inputs (required)
|
|
1120
|
-
* @param {string} filterName - any filter to search on (required)
|
|
1049
|
+
* @function getDevicesFiltered
|
|
1050
|
+
* @param {Object} options - the data to use to filter the appliances (optional)
|
|
1121
1051
|
*
|
|
1122
|
-
* @param {getCallback} callback - a callback function to return the result
|
|
1052
|
+
* @param {getCallback} callback - a callback function to return the result
|
|
1053
|
+
* (appliances) or the error
|
|
1123
1054
|
*/
|
|
1124
|
-
|
|
1125
|
-
const
|
|
1126
|
-
const origin = `${this.id}-${meth}`;
|
|
1055
|
+
getDevicesFiltered(options, callback) {
|
|
1056
|
+
const origin = `${this.id}-adapterBase-getDevicesFiltered`;
|
|
1127
1057
|
log.trace(origin);
|
|
1128
1058
|
|
|
1129
|
-
|
|
1130
|
-
let uriPath = '';
|
|
1131
|
-
let uriMethod = 'GET';
|
|
1132
|
-
let callQuery = {};
|
|
1133
|
-
let callBody = {};
|
|
1134
|
-
let callHeaders = {};
|
|
1135
|
-
let handleFail = 'fail';
|
|
1136
|
-
let ostypePrefix = '';
|
|
1137
|
-
let statusValue = 'true';
|
|
1138
|
-
if (callProps.path) {
|
|
1139
|
-
uriPath = `${callProps.path}`;
|
|
1140
|
-
|
|
1141
|
-
// make any necessary changes to the path
|
|
1142
|
-
if (devResp !== null && callProps.requestFields && Object.keys(callProps.requestFields).length > 0) {
|
|
1143
|
-
const rqKeys = Object.keys(callProps.requestFields);
|
|
1144
|
-
|
|
1145
|
-
// get the field from the provided device
|
|
1146
|
-
for (let rq = 0; rq < rqKeys.length; rq += 1) {
|
|
1147
|
-
const fieldValue = getDataFromSources(callProps.requestFields[rqKeys[rq]], devResp);
|
|
1148
|
-
|
|
1149
|
-
// put the value into the path - if it has been specified in the path
|
|
1150
|
-
uriPath = uriPath.replace(`{${rqKeys[rq]}}`, fieldValue);
|
|
1151
|
-
}
|
|
1152
|
-
}
|
|
1153
|
-
}
|
|
1154
|
-
if (callProps.method) {
|
|
1155
|
-
uriMethod = callProps.method;
|
|
1156
|
-
}
|
|
1157
|
-
if (callProps.query) {
|
|
1158
|
-
callQuery = callProps.query;
|
|
1159
|
-
|
|
1160
|
-
// go through the query params to check for variable values
|
|
1161
|
-
const cpKeys = Object.keys(callQuery);
|
|
1162
|
-
for (let cp = 0; cp < cpKeys.length; cp += 1) {
|
|
1163
|
-
if (callQuery[cpKeys[cp]].startsWith('{') && callQuery[cpKeys[cp]].endsWith('}')) {
|
|
1164
|
-
// make any necessary changes to the query params
|
|
1165
|
-
if (devResp !== null && callProps.requestFields && Object.keys(callProps.requestFields).length > 0) {
|
|
1166
|
-
const rqKeys = Object.keys(callProps.requestFields);
|
|
1167
|
-
|
|
1168
|
-
// get the field from the provided device
|
|
1169
|
-
for (let rq = 0; rq < rqKeys.length; rq += 1) {
|
|
1170
|
-
if (cpKeys[cp] === rqKeys[rq]) {
|
|
1171
|
-
const fieldValue = getDataFromSources(callProps.requestFields[rqKeys[rq]], devResp);
|
|
1172
|
-
|
|
1173
|
-
// put the value into the query - if it has been specified in the query
|
|
1174
|
-
callQuery[cpKeys[cp]] = fieldValue;
|
|
1175
|
-
}
|
|
1176
|
-
}
|
|
1177
|
-
}
|
|
1178
|
-
}
|
|
1179
|
-
}
|
|
1180
|
-
}
|
|
1181
|
-
if (callProps.body) {
|
|
1182
|
-
callBody = callProps.body;
|
|
1183
|
-
|
|
1184
|
-
// go through the body fields to check for variable values
|
|
1185
|
-
const cbKeys = Object.keys(callBody);
|
|
1186
|
-
for (let cb = 0; cb < cbKeys.length; cb += 1) {
|
|
1187
|
-
if (callBody[cbKeys[cb]].startsWith('{') && callBody[cbKeys[cb]].endsWith('}')) {
|
|
1188
|
-
// make any necessary changes to the query params
|
|
1189
|
-
if (devResp !== null && callProps.requestFields && Object.keys(callProps.requestFields).length > 0) {
|
|
1190
|
-
const rqKeys = Object.keys(callProps.requestFields);
|
|
1191
|
-
|
|
1192
|
-
// get the field from the provided device
|
|
1193
|
-
for (let rq = 0; rq < rqKeys.length; rq += 1) {
|
|
1194
|
-
if (cbKeys[cb] === rqKeys[rq]) {
|
|
1195
|
-
const fieldValue = getDataFromSources(callProps.requestFields[rqKeys[rq]], devResp);
|
|
1196
|
-
|
|
1197
|
-
// put the value into the query - if it has been specified in the query
|
|
1198
|
-
callBody[cbKeys[cb]] = fieldValue;
|
|
1199
|
-
}
|
|
1200
|
-
}
|
|
1201
|
-
}
|
|
1202
|
-
}
|
|
1203
|
-
}
|
|
1204
|
-
}
|
|
1205
|
-
if (callProps.headers) {
|
|
1206
|
-
callHeaders = callProps.headers;
|
|
1207
|
-
|
|
1208
|
-
// go through the body fields to check for variable values
|
|
1209
|
-
const chKeys = Object.keys(callHeaders);
|
|
1210
|
-
for (let ch = 0; ch < chKeys.length; ch += 1) {
|
|
1211
|
-
if (callHeaders[chKeys[ch]].startsWith('{') && callHeaders[chKeys[ch]].endsWith('}')) {
|
|
1212
|
-
// make any necessary changes to the query params
|
|
1213
|
-
if (devResp !== null && callProps.requestFields && Object.keys(callProps.requestFields).length > 0) {
|
|
1214
|
-
const rqKeys = Object.keys(callProps.requestFields);
|
|
1215
|
-
|
|
1216
|
-
// get the field from the provided device
|
|
1217
|
-
for (let rq = 0; rq < rqKeys.length; rq += 1) {
|
|
1218
|
-
if (chKeys[ch] === rqKeys[rq]) {
|
|
1219
|
-
const fieldValue = getDataFromSources(callProps.requestFields[rqKeys[rq]], devResp);
|
|
1220
|
-
|
|
1221
|
-
// put the value into the query - if it has been specified in the query
|
|
1222
|
-
callHeaders[chKeys[ch]] = fieldValue;
|
|
1223
|
-
}
|
|
1224
|
-
}
|
|
1225
|
-
}
|
|
1226
|
-
}
|
|
1227
|
-
}
|
|
1228
|
-
}
|
|
1229
|
-
if (callProps.handleFailure) {
|
|
1230
|
-
handleFail = callProps.handleFailure;
|
|
1231
|
-
}
|
|
1232
|
-
if (callProps.responseFields && callProps.responseFields.ostypePrefix) {
|
|
1233
|
-
ostypePrefix = callProps.responseFields.ostypePrefix;
|
|
1234
|
-
}
|
|
1235
|
-
if (callProps.responseFields && callProps.responseFields.statusValue) {
|
|
1236
|
-
statusValue = callProps.responseFields.statusValue;
|
|
1237
|
-
}
|
|
1238
|
-
|
|
1239
|
-
// !! using Generic makes it easier on the Adapter Builder (just need to change the path)
|
|
1240
|
-
// !! you can also replace with a specific call if that is easier
|
|
1241
|
-
return this.genericAdapterRequest(uriPath, uriMethod, callQuery, callBody, callHeaders, (result, error) => {
|
|
1242
|
-
// if we received an error or their is no response on the results return an error
|
|
1243
|
-
if (error) {
|
|
1244
|
-
if (handleFail === 'fail') {
|
|
1245
|
-
return callback(null, error);
|
|
1246
|
-
}
|
|
1247
|
-
return callback({}, null);
|
|
1248
|
-
}
|
|
1249
|
-
if (!result.response) {
|
|
1250
|
-
if (handleFail === 'fail') {
|
|
1251
|
-
const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Invalid Response', [brokCall], null, null, null);
|
|
1252
|
-
log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
1253
|
-
return callback(null, errorObj);
|
|
1254
|
-
}
|
|
1255
|
-
return callback({}, null);
|
|
1256
|
-
}
|
|
1257
|
-
|
|
1258
|
-
// get the response piece we care about from the response
|
|
1259
|
-
const myResult = result;
|
|
1260
|
-
if (callProps.responseDatakey) {
|
|
1261
|
-
myResult.response = jsonQuery(callProps.responseDatakey, { data: myResult.response }).value;
|
|
1262
|
-
}
|
|
1263
|
-
|
|
1264
|
-
// get the keys for the response fields
|
|
1265
|
-
let rfKeys = [];
|
|
1266
|
-
if (callProps.responseFields && Object.keys(callProps.responseFields).length > 0) {
|
|
1267
|
-
rfKeys = Object.keys(callProps.responseFields);
|
|
1268
|
-
}
|
|
1269
|
-
|
|
1270
|
-
// if we got an array returned (e.g. getDevicesFitered)
|
|
1271
|
-
if (Array.isArray(myResult.response)) {
|
|
1272
|
-
const listDevices = [];
|
|
1273
|
-
for (let a = 0; a < myResult.response.length; a += 1) {
|
|
1274
|
-
const thisDevice = myResult.response[a];
|
|
1275
|
-
for (let rf = 0; rf < rfKeys.length; rf += 1) {
|
|
1276
|
-
if (rfKeys[rf] !== 'ostypePrefix') {
|
|
1277
|
-
let fieldValue = getDataFromSources(callProps.responseFields[rfKeys[rf]], [thisDevice, devResp, callProps.requestFields]);
|
|
1278
|
-
|
|
1279
|
-
// if the field is ostype - need to add prefix
|
|
1280
|
-
if (rfKeys[rf] === 'ostype' && typeof fieldValue === 'string') {
|
|
1281
|
-
fieldValue = ostypePrefix + fieldValue;
|
|
1282
|
-
}
|
|
1283
|
-
// if there is a status to set, set it
|
|
1284
|
-
if (rfKeys[rf] === 'status') {
|
|
1285
|
-
// if really looking for just a good response
|
|
1286
|
-
if (callProps.responseFields[rfKeys[rf]] === 'return2xx' && myResult.icode === statusValue.toString()) {
|
|
1287
|
-
thisDevice.isAlive = true;
|
|
1288
|
-
} else if (fieldValue.toString() === statusValue.toString()) {
|
|
1289
|
-
thisDevice.isAlive = true;
|
|
1290
|
-
} else {
|
|
1291
|
-
thisDevice.isAlive = false;
|
|
1292
|
-
}
|
|
1293
|
-
}
|
|
1294
|
-
// if we found a good value
|
|
1295
|
-
thisDevice[rfKeys[rf]] = fieldValue;
|
|
1296
|
-
}
|
|
1297
|
-
}
|
|
1298
|
-
|
|
1299
|
-
// if there is no filter - add the device to the list
|
|
1300
|
-
if (!filterName || filterName.length === 0) {
|
|
1301
|
-
listDevices.push(thisDevice);
|
|
1302
|
-
} else {
|
|
1303
|
-
// if we have to match a filter
|
|
1304
|
-
let found = false;
|
|
1305
|
-
for (let f = 0; f < filterName.length; f += 1) {
|
|
1306
|
-
if (thisDevice.name.indexOf(filterName[f]) >= 0) {
|
|
1307
|
-
found = true;
|
|
1308
|
-
break;
|
|
1309
|
-
}
|
|
1310
|
-
}
|
|
1311
|
-
// matching device
|
|
1312
|
-
if (found) {
|
|
1313
|
-
listDevices.push(thisDevice);
|
|
1314
|
-
}
|
|
1315
|
-
}
|
|
1316
|
-
}
|
|
1317
|
-
|
|
1318
|
-
// return the array of devices
|
|
1319
|
-
return callback(listDevices, null);
|
|
1320
|
-
}
|
|
1321
|
-
|
|
1322
|
-
// if this is not an array - just about everything else, just handle as a single object
|
|
1323
|
-
let thisDevice = myResult.response;
|
|
1324
|
-
for (let rf = 0; rf < rfKeys.length; rf += 1) {
|
|
1325
|
-
// skip ostypePrefix since it is not a field
|
|
1326
|
-
if (rfKeys[rf] !== 'ostypePrefix') {
|
|
1327
|
-
let fieldValue = getDataFromSources(callProps.responseFields[rfKeys[rf]], [thisDevice, devResp, callProps.requestFields]);
|
|
1328
|
-
|
|
1329
|
-
// if the field is ostype - need to add prefix
|
|
1330
|
-
if (rfKeys[rf] === 'ostype' && typeof fieldValue === 'string') {
|
|
1331
|
-
fieldValue = ostypePrefix + fieldValue;
|
|
1332
|
-
}
|
|
1333
|
-
// if there is a status to set, set it
|
|
1334
|
-
if (rfKeys[rf] === 'status') {
|
|
1335
|
-
// if really looking for just a good response
|
|
1336
|
-
if (callProps.responseFields[rfKeys[rf]] === 'return2xx' && myResult.icode === statusValue.toString()) {
|
|
1337
|
-
thisDevice.isAlive = true;
|
|
1338
|
-
} else if (fieldValue.toString() === statusValue.toString()) {
|
|
1339
|
-
thisDevice.isAlive = true;
|
|
1340
|
-
} else {
|
|
1341
|
-
thisDevice.isAlive = false;
|
|
1342
|
-
}
|
|
1343
|
-
}
|
|
1344
|
-
// if we found a good value
|
|
1345
|
-
thisDevice[rfKeys[rf]] = fieldValue;
|
|
1346
|
-
}
|
|
1347
|
-
}
|
|
1348
|
-
|
|
1349
|
-
// if there is a filter - check the device is in the list
|
|
1350
|
-
if (filterName && filterName.length > 0) {
|
|
1351
|
-
let found = false;
|
|
1352
|
-
for (let f = 0; f < filterName.length; f += 1) {
|
|
1353
|
-
if (thisDevice.name.indexOf(filterName[f]) >= 0) {
|
|
1354
|
-
found = true;
|
|
1355
|
-
break;
|
|
1356
|
-
}
|
|
1357
|
-
}
|
|
1358
|
-
// no matching device - clear the device
|
|
1359
|
-
if (!found) {
|
|
1360
|
-
thisDevice = {};
|
|
1361
|
-
}
|
|
1362
|
-
}
|
|
1363
|
-
|
|
1364
|
-
return callback(thisDevice, null);
|
|
1365
|
-
});
|
|
1366
|
-
} catch (e) {
|
|
1367
|
-
const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Caught Exception', null, null, null, e);
|
|
1368
|
-
log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
1369
|
-
return callback(null, errorObj);
|
|
1370
|
-
}
|
|
1059
|
+
return this.requestHandlerInst.getDevicesFiltered(options, callback);
|
|
1371
1060
|
}
|
|
1372
1061
|
|
|
1373
1062
|
/**
|
|
1374
|
-
* @summary
|
|
1063
|
+
* @summary Gets the status for the provided appliance
|
|
1375
1064
|
*
|
|
1376
|
-
* @function
|
|
1377
|
-
* @param {String} deviceName - the deviceName
|
|
1065
|
+
* @function isAlive
|
|
1066
|
+
* @param {String} deviceName - the deviceName of the appliance. (required)
|
|
1378
1067
|
*
|
|
1379
|
-
* @param {
|
|
1380
|
-
*
|
|
1068
|
+
* @param {configCallback} callback - callback function to return the result
|
|
1069
|
+
* (appliance isAlive) or the error
|
|
1381
1070
|
*/
|
|
1382
|
-
|
|
1383
|
-
const
|
|
1384
|
-
const origin = `${this.id}-${meth}`;
|
|
1071
|
+
isAlive(deviceName, callback) {
|
|
1072
|
+
const origin = `${this.id}-adapterBase-isAlive`;
|
|
1385
1073
|
log.trace(origin);
|
|
1386
1074
|
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Missing Properties', ['devicebroker.getDevice.path'], null, null, null);
|
|
1390
|
-
log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
1391
|
-
return callback(null, errorObj);
|
|
1392
|
-
}
|
|
1393
|
-
|
|
1394
|
-
/* HERE IS WHERE YOU VALIDATE DATA */
|
|
1395
|
-
if (deviceName === undefined || deviceName === null || deviceName === '' || deviceName.length === 0) {
|
|
1396
|
-
const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Missing Data', ['deviceName'], null, null, null);
|
|
1397
|
-
log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
1398
|
-
return callback(null, errorObj);
|
|
1399
|
-
}
|
|
1400
|
-
|
|
1401
|
-
try {
|
|
1402
|
-
// need to get the device so we can convert the deviceName to an id
|
|
1403
|
-
// !! if we can do a lookup by name the getDevicesFiltered may not be necessary
|
|
1404
|
-
const opts = {
|
|
1405
|
-
filter: {
|
|
1406
|
-
name: deviceName
|
|
1407
|
-
}
|
|
1408
|
-
};
|
|
1409
|
-
return this.getDevicesFiltered(opts, (devs, ferr) => {
|
|
1410
|
-
// if we received an error or their is no response on the results return an error
|
|
1411
|
-
if (ferr) {
|
|
1412
|
-
return callback(null, ferr);
|
|
1413
|
-
}
|
|
1414
|
-
if (devs.list.length < 1) {
|
|
1415
|
-
const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, `Did Not Find Device ${deviceName}`, [], null, null, null);
|
|
1416
|
-
log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
1417
|
-
return callback(null, errorObj);
|
|
1418
|
-
}
|
|
1419
|
-
|
|
1420
|
-
const callPromises = [];
|
|
1421
|
-
for (let i = 0; i < this.allProps.devicebroker.getDevice.length; i += 1) {
|
|
1422
|
-
// Perform component calls here.
|
|
1423
|
-
callPromises.push(
|
|
1424
|
-
new Promise((resolve, reject) => {
|
|
1425
|
-
this.iapMakeBrokerCall('getDevice', this.allProps.devicebroker.getDevice[i], [devs.list[0]], null, (callRet, callErr) => {
|
|
1426
|
-
// return an error
|
|
1427
|
-
if (callErr) {
|
|
1428
|
-
reject(callErr);
|
|
1429
|
-
} else {
|
|
1430
|
-
// return the data
|
|
1431
|
-
resolve(callRet);
|
|
1432
|
-
}
|
|
1433
|
-
});
|
|
1434
|
-
})
|
|
1435
|
-
);
|
|
1436
|
-
}
|
|
1075
|
+
return this.requestHandlerInst.isAlive(deviceName, callback);
|
|
1076
|
+
}
|
|
1437
1077
|
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1078
|
+
/**
|
|
1079
|
+
* @summary Gets a config for the provided Appliance
|
|
1080
|
+
*
|
|
1081
|
+
* @function getConfig
|
|
1082
|
+
* @param {String} deviceName - the deviceName of the appliance. (required)
|
|
1083
|
+
* @param {String} format - the desired format of the config. (optional)
|
|
1084
|
+
*
|
|
1085
|
+
* @param {configCallback} callback - callback function to return the result
|
|
1086
|
+
* (appliance config) or the error
|
|
1087
|
+
*/
|
|
1088
|
+
getConfig(deviceName, format, callback) {
|
|
1089
|
+
const origin = `${this.id}-adapterBase-getConfig`;
|
|
1090
|
+
log.trace(origin);
|
|
1444
1091
|
|
|
1445
|
-
|
|
1446
|
-
});
|
|
1447
|
-
});
|
|
1448
|
-
} catch (ex) {
|
|
1449
|
-
const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Caught Exception', null, null, null, ex);
|
|
1450
|
-
log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
1451
|
-
return callback(null, errorObj);
|
|
1452
|
-
}
|
|
1092
|
+
return this.requestHandlerInst.getConfig(deviceName, format, callback);
|
|
1453
1093
|
}
|
|
1454
1094
|
|
|
1455
1095
|
/**
|
|
1456
|
-
* @summary
|
|
1096
|
+
* @summary Gets the device count from the system
|
|
1457
1097
|
*
|
|
1458
|
-
* @function
|
|
1459
|
-
* @param {Object} options - the data to use to filter the appliances (optional)
|
|
1098
|
+
* @function iapGetDeviceCount
|
|
1460
1099
|
*
|
|
1461
|
-
* @param {getCallback} callback -
|
|
1462
|
-
*
|
|
1100
|
+
* @param {getCallback} callback - callback function to return the result
|
|
1101
|
+
* (count) or the error
|
|
1463
1102
|
*/
|
|
1464
|
-
|
|
1465
|
-
const
|
|
1466
|
-
const origin = `${this.id}-${meth}`;
|
|
1103
|
+
iapGetDeviceCount(callback) {
|
|
1104
|
+
const origin = `${this.id}-adapterBase-iapGetDeviceCount`;
|
|
1467
1105
|
log.trace(origin);
|
|
1468
1106
|
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Missing Properties', ['devicebroker.getDevicesFiltered.path'], null, null, null);
|
|
1472
|
-
log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
1473
|
-
return callback(null, errorObj);
|
|
1474
|
-
}
|
|
1107
|
+
return this.requestHandlerInst.iapGetDeviceCount(callback);
|
|
1108
|
+
}
|
|
1475
1109
|
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1110
|
+
/* ********************************************** */
|
|
1111
|
+
/* */
|
|
1112
|
+
/* EXPOSES GENERIC HANDLER */
|
|
1113
|
+
/* */
|
|
1114
|
+
/* ********************************************** */
|
|
1115
|
+
/**
|
|
1116
|
+
* Makes the requested generic call
|
|
1117
|
+
*
|
|
1118
|
+
* @function iapExpandedGenericAdapterRequest
|
|
1119
|
+
* @param {Object} metadata - metadata for the call (optional).
|
|
1120
|
+
* Can be a stringified Object.
|
|
1121
|
+
* @param {String} uriPath - the path of the api call - do not include the host, port, base path or version (optional)
|
|
1122
|
+
* @param {String} restMethod - the rest method (GET, POST, PUT, PATCH, DELETE) (optional)
|
|
1123
|
+
* @param {Object} pathVars - the parameters to be put within the url path (optional).
|
|
1124
|
+
* Can be a stringified Object.
|
|
1125
|
+
* @param {Object} queryData - the parameters to be put on the url (optional).
|
|
1126
|
+
* Can be a stringified Object.
|
|
1127
|
+
* @param {Object} requestBody - the body to add to the request (optional).
|
|
1128
|
+
* Can be a stringified Object.
|
|
1129
|
+
* @param {Object} addlHeaders - additional headers to be put on the call (optional).
|
|
1130
|
+
* Can be a stringified Object.
|
|
1131
|
+
* @param {getCallback} callback - a callback function to return the result (Generics)
|
|
1132
|
+
* or the error
|
|
1133
|
+
*/
|
|
1134
|
+
iapExpandedGenericAdapterRequest(metadata, uriPath, restMethod, pathVars, queryData, requestBody, addlHeaders, callback) {
|
|
1135
|
+
const origin = `${this.myid}-adapterBase-iapExpandedGenericAdapterRequest`;
|
|
1136
|
+
log.trace(origin);
|
|
1483
1137
|
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
// const nextToken = options.start;
|
|
1487
|
-
// const maxResults = options.limit;
|
|
1488
|
-
|
|
1489
|
-
// set up the filter of Device Names
|
|
1490
|
-
let filterName = [];
|
|
1491
|
-
if (options && options.filter && options.filter.name) {
|
|
1492
|
-
// when this hack is removed, remove the lint ignore above
|
|
1493
|
-
if (Array.isArray(options.filter.name)) {
|
|
1494
|
-
// eslint-disable-next-line prefer-destructuring
|
|
1495
|
-
filterName = options.filter.name;
|
|
1496
|
-
} else {
|
|
1497
|
-
filterName = [options.filter.name];
|
|
1498
|
-
}
|
|
1499
|
-
}
|
|
1138
|
+
return this.requestHandlerInst.expandedGenericAdapterRequest(metadata, uriPath, restMethod, pathVars, queryData, requestBody, addlHeaders, callback);
|
|
1139
|
+
}
|
|
1500
1140
|
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
} else {
|
|
1520
|
-
// return the data
|
|
1521
|
-
resolve(callRet);
|
|
1522
|
-
}
|
|
1523
|
-
});
|
|
1524
|
-
})
|
|
1525
|
-
);
|
|
1526
|
-
}
|
|
1141
|
+
/**
|
|
1142
|
+
* Makes the requested generic call
|
|
1143
|
+
*
|
|
1144
|
+
* @function genericAdapterRequest
|
|
1145
|
+
* @param {String} uriPath - the path of the api call - do not include the host, port, base path or version (required)
|
|
1146
|
+
* @param {String} restMethod - the rest method (GET, POST, PUT, PATCH, DELETE) (required)
|
|
1147
|
+
* @param {Object} queryData - the parameters to be put on the url (optional).
|
|
1148
|
+
* Can be a stringified Object.
|
|
1149
|
+
* @param {Object} requestBody - the body to add to the request (optional).
|
|
1150
|
+
* Can be a stringified Object.
|
|
1151
|
+
* @param {Object} addlHeaders - additional headers to be put on the call (optional).
|
|
1152
|
+
* Can be a stringified Object.
|
|
1153
|
+
* @param {getCallback} callback - a callback function to return the result (Generics)
|
|
1154
|
+
* or the error
|
|
1155
|
+
*/
|
|
1156
|
+
genericAdapterRequest(uriPath, restMethod, queryData, requestBody, addlHeaders, callback) {
|
|
1157
|
+
const origin = `${this.myid}-adapterBase-genericAdapterRequest`;
|
|
1158
|
+
log.trace(origin);
|
|
1527
1159
|
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
let myResult = [];
|
|
1531
|
-
results.forEach((result) => {
|
|
1532
|
-
if (Array.isArray(result)) {
|
|
1533
|
-
myResult = [...myResult, ...result];
|
|
1534
|
-
} else if (Object.keys(result).length > 0) {
|
|
1535
|
-
myResult.push(result);
|
|
1536
|
-
}
|
|
1537
|
-
});
|
|
1160
|
+
return this.requestHandlerInst.genericAdapterRequest(uriPath, restMethod, queryData, requestBody, addlHeaders, callback);
|
|
1161
|
+
}
|
|
1538
1162
|
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1163
|
+
/**
|
|
1164
|
+
* Makes the requested generic call with no base path or version
|
|
1165
|
+
*
|
|
1166
|
+
* @function genericAdapterRequestNoBasePath
|
|
1167
|
+
* @param {String} uriPath - the path of the api call - do not include the host, port, base path or version (required)
|
|
1168
|
+
* @param {String} restMethod - the rest method (GET, POST, PUT, PATCH, DELETE) (required)
|
|
1169
|
+
* @param {Object} queryData - the parameters to be put on the url (optional).
|
|
1170
|
+
* Can be a stringified Object.
|
|
1171
|
+
* @param {Object} requestBody - the body to add to the request (optional).
|
|
1172
|
+
* Can be a stringified Object.
|
|
1173
|
+
* @param {Object} addlHeaders - additional headers to be put on the call (optional).
|
|
1174
|
+
* Can be a stringified Object.
|
|
1175
|
+
* @param {getCallback} callback - a callback function to return the result (Generics)
|
|
1176
|
+
* or the error
|
|
1177
|
+
*/
|
|
1178
|
+
genericAdapterRequestNoBasePath(uriPath, restMethod, queryData, requestBody, addlHeaders, callback) {
|
|
1179
|
+
const origin = `${this.myid}-adapterBase-genericAdapterRequestNoBasePath`;
|
|
1180
|
+
log.trace(origin);
|
|
1181
|
+
|
|
1182
|
+
return this.requestHandlerInst.genericAdapterRequestNoBasePath(uriPath, restMethod, queryData, requestBody, addlHeaders, callback);
|
|
1548
1183
|
}
|
|
1549
1184
|
|
|
1185
|
+
/* ********************************************** */
|
|
1186
|
+
/* */
|
|
1187
|
+
/* EXPOSES INVENTORY CALLS */
|
|
1188
|
+
/* */
|
|
1189
|
+
/* ********************************************** */
|
|
1550
1190
|
/**
|
|
1551
|
-
* @summary
|
|
1191
|
+
* @summary run the adapter lint script to return the results.
|
|
1552
1192
|
*
|
|
1553
|
-
* @function
|
|
1554
|
-
* @param {String} deviceName - the deviceName of the appliance. (required)
|
|
1193
|
+
* @function iapRunAdapterLint
|
|
1555
1194
|
*
|
|
1556
|
-
* @
|
|
1557
|
-
* (appliance isAlive) or the error
|
|
1195
|
+
* @return {Object} - containing the results of the lint call.
|
|
1558
1196
|
*/
|
|
1559
|
-
|
|
1560
|
-
const meth = 'adapterBase-
|
|
1197
|
+
iapRunAdapterLint(callback) {
|
|
1198
|
+
const meth = 'adapterBase-iapRunAdapterLint';
|
|
1561
1199
|
const origin = `${this.id}-${meth}`;
|
|
1562
1200
|
log.trace(origin);
|
|
1201
|
+
let command = null;
|
|
1563
1202
|
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Missing Properties', ['devicebroker.isAlive.path'], null, null, null);
|
|
1567
|
-
log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
1568
|
-
return callback(null, errorObj);
|
|
1569
|
-
}
|
|
1570
|
-
|
|
1571
|
-
// verify the required fields have been provided
|
|
1572
|
-
if (deviceName === undefined || deviceName === null || deviceName === '' || deviceName.length === 0) {
|
|
1573
|
-
const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Missing Data', ['deviceName'], null, null, null);
|
|
1574
|
-
log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
1575
|
-
return callback(null, errorObj);
|
|
1576
|
-
}
|
|
1577
|
-
|
|
1578
|
-
try {
|
|
1579
|
-
// need to get the device so we can convert the deviceName to an id
|
|
1580
|
-
// !! if we can do a lookup by name the getDevicesFiltered may not be necessary
|
|
1581
|
-
const opts = {
|
|
1582
|
-
filter: {
|
|
1583
|
-
name: deviceName
|
|
1584
|
-
}
|
|
1585
|
-
};
|
|
1586
|
-
return this.getDevicesFiltered(opts, (devs, ferr) => {
|
|
1587
|
-
// if we received an error or their is no response on the results return an error
|
|
1588
|
-
if (ferr) {
|
|
1589
|
-
return callback(null, ferr);
|
|
1590
|
-
}
|
|
1591
|
-
if (devs.list.length < 1) {
|
|
1592
|
-
const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, `Did Not Find Device ${deviceName}`, [], null, null, null);
|
|
1593
|
-
log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
1594
|
-
return callback(null, errorObj);
|
|
1595
|
-
}
|
|
1203
|
+
if (fs.existsSync('package.json')) {
|
|
1204
|
+
const packageData = require('./package.json');
|
|
1596
1205
|
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
this.iapMakeBrokerCall('isAlive', this.allProps.devicebroker.isAlive[i], [devs.list[0]], null, (callRet, callErr) => {
|
|
1603
|
-
// return an error
|
|
1604
|
-
if (callErr) {
|
|
1605
|
-
reject(callErr);
|
|
1606
|
-
} else {
|
|
1607
|
-
// return the data
|
|
1608
|
-
resolve(callRet);
|
|
1609
|
-
}
|
|
1610
|
-
});
|
|
1611
|
-
})
|
|
1612
|
-
);
|
|
1613
|
-
}
|
|
1206
|
+
// check if 'test', 'test:unit', 'test:integration' exists in package.json file
|
|
1207
|
+
if (!packageData.scripts || !packageData.scripts['lint:errors']) {
|
|
1208
|
+
log.error('The required script does not exist in the package.json file');
|
|
1209
|
+
return callback(null, 'The required script does not exist in the package.json file');
|
|
1210
|
+
}
|
|
1614
1211
|
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
let myResult = {};
|
|
1618
|
-
results.forEach((result) => {
|
|
1619
|
-
myResult = { ...myResult, ...result };
|
|
1620
|
-
});
|
|
1212
|
+
// execute 'npm run lint:errors' command
|
|
1213
|
+
command = spawnSync('npm', ['run', 'lint:errors'], { cwd: __dirname, encoding: 'utf-8' });
|
|
1621
1214
|
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
1632
|
-
return callback(null, errorObj);
|
|
1215
|
+
// analyze and format the response
|
|
1216
|
+
const result = {
|
|
1217
|
+
status: 'SUCCESS'
|
|
1218
|
+
};
|
|
1219
|
+
if (command.status !== 0) {
|
|
1220
|
+
result.status = 'FAILED';
|
|
1221
|
+
result.output = command.stdout;
|
|
1222
|
+
}
|
|
1223
|
+
return callback(result);
|
|
1633
1224
|
}
|
|
1225
|
+
|
|
1226
|
+
log.error('Package Not Found');
|
|
1227
|
+
return callback(null, 'Package Not Found');
|
|
1634
1228
|
}
|
|
1635
1229
|
|
|
1636
1230
|
/**
|
|
1637
|
-
* @summary
|
|
1231
|
+
* @summary run the adapter test scripts (baseunit and unit) to return the results.
|
|
1232
|
+
* can not run integration as there can be implications with that.
|
|
1638
1233
|
*
|
|
1639
|
-
* @function
|
|
1640
|
-
* @param {String} deviceName - the deviceName of the appliance. (required)
|
|
1641
|
-
* @param {String} format - the desired format of the config. (optional)
|
|
1234
|
+
* @function iapRunAdapterTests
|
|
1642
1235
|
*
|
|
1643
|
-
* @
|
|
1644
|
-
* (appliance config) or the error
|
|
1236
|
+
* @return {Object} - containing the results of the baseunit and unit tests.
|
|
1645
1237
|
*/
|
|
1646
|
-
|
|
1647
|
-
const meth = 'adapterBase-
|
|
1238
|
+
iapRunAdapterTests(callback) {
|
|
1239
|
+
const meth = 'adapterBase-iapRunAdapterTests';
|
|
1648
1240
|
const origin = `${this.id}-${meth}`;
|
|
1649
1241
|
log.trace(origin);
|
|
1242
|
+
let basecommand = null;
|
|
1243
|
+
let command = null;
|
|
1650
1244
|
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Missing Properties', ['devicebroker.getConfig.path'], null, null, null);
|
|
1654
|
-
log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
1655
|
-
return callback(null, errorObj);
|
|
1656
|
-
}
|
|
1245
|
+
if (fs.existsSync('package.json')) {
|
|
1246
|
+
const packageData = require('./package.json');
|
|
1657
1247
|
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
}
|
|
1248
|
+
// check if 'test', 'test:unit', 'test:integration' exists in package.json file
|
|
1249
|
+
if (!packageData.scripts || !packageData.scripts['test:baseunit'] || !packageData.scripts['test:unit']) {
|
|
1250
|
+
log.error('The required scripts do not exist in the package.json file');
|
|
1251
|
+
return callback(null, 'The required scripts do not exist in the package.json file');
|
|
1252
|
+
}
|
|
1664
1253
|
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
}
|
|
1254
|
+
// run baseunit test
|
|
1255
|
+
basecommand = spawnSync('npm', ['run', 'test:baseunit'], { cwd: __dirname, encoding: 'utf-8' });
|
|
1256
|
+
|
|
1257
|
+
// analyze and format the response to baseunit
|
|
1258
|
+
const baseresult = {
|
|
1259
|
+
status: 'SUCCESS'
|
|
1672
1260
|
};
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
}
|
|
1678
|
-
if (devs.list.length < 1) {
|
|
1679
|
-
const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, `Did Not Find Device ${deviceName}`, [], null, null, null);
|
|
1680
|
-
log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
1681
|
-
return callback(null, errorObj);
|
|
1682
|
-
}
|
|
1261
|
+
if (basecommand.status !== 0) {
|
|
1262
|
+
baseresult.status = 'FAILED';
|
|
1263
|
+
baseresult.output = basecommand.stdout;
|
|
1264
|
+
}
|
|
1683
1265
|
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
// Perform component calls here.
|
|
1687
|
-
callPromises.push(
|
|
1688
|
-
new Promise((resolve, reject) => {
|
|
1689
|
-
this.iapMakeBrokerCall('getConfig', this.allProps.devicebroker.getConfig[i], [devs.list[0]], null, (callRet, callErr) => {
|
|
1690
|
-
// return an error
|
|
1691
|
-
if (callErr) {
|
|
1692
|
-
reject(callErr);
|
|
1693
|
-
} else {
|
|
1694
|
-
// return the data
|
|
1695
|
-
resolve(callRet);
|
|
1696
|
-
}
|
|
1697
|
-
});
|
|
1698
|
-
})
|
|
1699
|
-
);
|
|
1700
|
-
}
|
|
1266
|
+
// run unit test
|
|
1267
|
+
command = spawnSync('npm', ['run', 'test:unit'], { cwd: __dirname, encoding: 'utf-8' });
|
|
1701
1268
|
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
}
|
|
1716
|
-
|
|
1717
|
-
const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Caught Exception', null, null, null, ex);
|
|
1718
|
-
log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
1719
|
-
return callback(null, errorObj);
|
|
1269
|
+
// analyze and format the response to unit
|
|
1270
|
+
const unitresult = {
|
|
1271
|
+
status: 'SUCCESS'
|
|
1272
|
+
};
|
|
1273
|
+
if (command.status !== 0) {
|
|
1274
|
+
unitresult.status = 'FAILED';
|
|
1275
|
+
unitresult.output = command.stdout;
|
|
1276
|
+
}
|
|
1277
|
+
|
|
1278
|
+
// format the response and return it
|
|
1279
|
+
const result = {
|
|
1280
|
+
base: baseresult,
|
|
1281
|
+
unit: unitresult
|
|
1282
|
+
};
|
|
1283
|
+
return callback(result);
|
|
1720
1284
|
}
|
|
1285
|
+
|
|
1286
|
+
log.error('Package Not Found');
|
|
1287
|
+
return callback(null, 'Package Not Found');
|
|
1721
1288
|
}
|
|
1722
1289
|
|
|
1723
1290
|
/**
|
|
1724
|
-
* @summary
|
|
1291
|
+
* @summary provide inventory information abbout the adapter
|
|
1725
1292
|
*
|
|
1726
|
-
* @function
|
|
1293
|
+
* @function iapGetAdapterInventory
|
|
1727
1294
|
*
|
|
1728
|
-
* @
|
|
1729
|
-
* (count) or the error
|
|
1295
|
+
* @return {Object} - containing the adapter inventory information
|
|
1730
1296
|
*/
|
|
1731
|
-
|
|
1732
|
-
const meth = 'adapterBase-
|
|
1297
|
+
iapGetAdapterInventory(callback) {
|
|
1298
|
+
const meth = 'adapterBase-iapGetAdapterInventory';
|
|
1733
1299
|
const origin = `${this.id}-${meth}`;
|
|
1734
1300
|
log.trace(origin);
|
|
1735
1301
|
|
|
1736
|
-
// make sure we are set up for device broker getCount
|
|
1737
|
-
if (!this.allProps.devicebroker || !this.allProps.devicebroker.getCount || this.allProps.devicebroker.getCount.length === 0 || !this.allProps.devicebroker.getCount[0].path) {
|
|
1738
|
-
const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Missing Properties', ['devicebroker.getCount.path'], null, null, null);
|
|
1739
|
-
log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
|
|
1740
|
-
return callback(null, errorObj);
|
|
1741
|
-
}
|
|
1742
|
-
|
|
1743
|
-
// verify the required fields have been provided
|
|
1744
|
-
|
|
1745
1302
|
try {
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1303
|
+
// call to the adapter utils to get inventory
|
|
1304
|
+
return this.requestHandlerInst.getAdapterInventory((res, error) => {
|
|
1305
|
+
const adapterInv = res;
|
|
1306
|
+
|
|
1307
|
+
// get all of the tasks
|
|
1308
|
+
const allTasks = this.getAllFunctions();
|
|
1309
|
+
adapterInv.totalTasks = allTasks.length;
|
|
1310
|
+
|
|
1311
|
+
// get all of the possible workflow tasks
|
|
1312
|
+
const myIgnore = [
|
|
1313
|
+
'healthCheck',
|
|
1314
|
+
'iapGetAdapterWorkflowFunctions',
|
|
1315
|
+
'hasEntities'
|
|
1316
|
+
];
|
|
1317
|
+
adapterInv.totalWorkflowTasks = this.iapGetAdapterWorkflowFunctions(myIgnore).length;
|
|
1318
|
+
|
|
1319
|
+
// TODO: CACHE
|
|
1320
|
+
// CONFIRM CACHE
|
|
1321
|
+
// GET CACHE ENTITIES
|
|
1322
|
+
|
|
1323
|
+
// get the Device Count
|
|
1324
|
+
return this.iapGetDeviceCount((devres, deverror) => {
|
|
1325
|
+
// if call failed assume not broker integrated
|
|
1326
|
+
if (deverror) {
|
|
1327
|
+
adapterInv.brokerDefined = false;
|
|
1328
|
+
adapterInv.deviceCount = -1;
|
|
1329
|
+
} else {
|
|
1330
|
+
// broker confirmed
|
|
1331
|
+
adapterInv.brokerDefined = true;
|
|
1332
|
+
adapterInv.deviceCount = 0;
|
|
1333
|
+
if (devres && devres.count) {
|
|
1334
|
+
adapterInv.deviceCount = devres.count;
|
|
1335
|
+
}
|
|
1336
|
+
}
|
|
1763
1337
|
|
|
1764
|
-
|
|
1765
|
-
return Promise.all(callPromises).then((results) => {
|
|
1766
|
-
let myResult = {};
|
|
1767
|
-
results.forEach((result) => {
|
|
1768
|
-
myResult = { ...myResult, ...result };
|
|
1338
|
+
return callback(adapterInv);
|
|
1769
1339
|
});
|
|
1770
|
-
|
|
1771
|
-
// return the result
|
|
1772
|
-
return callback({ count: myResult.length });
|
|
1773
1340
|
});
|
|
1774
1341
|
} catch (ex) {
|
|
1775
1342
|
const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Caught Exception', null, null, null, ex);
|