@itentialopensource/adapter-alkira 0.1.3 → 0.1.5

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/CHANGELOG.md CHANGED
@@ -1,4 +1,20 @@
1
1
 
2
+ ## 0.1.5 [05-02-2023]
3
+
4
+ * Migrate adapter base with broker changes
5
+
6
+ See merge request itentialopensource/adapters/cloud/adapter-alkira!4
7
+
8
+ ---
9
+
10
+ ## 0.1.4 [04-12-2023]
11
+
12
+ * Patch/fixed get config and get count in adapterBase.json
13
+
14
+ See merge request itentialopensource/adapters/cloud/adapter-alkira!3
15
+
16
+ ---
17
+
2
18
  ## 0.1.3 [04-06-2023]
3
19
 
4
20
  * bug fix for getConfig and iapGetDeviceCount in adapterBase.js
package/adapterBase.js CHANGED
@@ -17,6 +17,8 @@ const jsonQuery = require('json-query');
17
17
  const EventEmitterCl = require('events').EventEmitter;
18
18
  const { execSync } = require('child_process');
19
19
 
20
+ const sampleProperties = require(`${__dirname}/sampleProperties.json`).properties;
21
+
20
22
  /* The schema validator */
21
23
  const AjvCl = require('ajv');
22
24
 
@@ -24,11 +26,15 @@ const AjvCl = require('ajv');
24
26
  const PropUtilCl = require('@itentialopensource/adapter-utils').PropertyUtility;
25
27
  const RequestHandlerCl = require('@itentialopensource/adapter-utils').RequestHandler;
26
28
 
29
+ const TransUtilCl = require(path.join(__dirname, 'node_modules/@itentialopensource/adapter-utils/lib/translatorUtil.js'));
30
+
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'));
30
34
 
31
35
  let propUtil = null;
36
+ let transUtil = 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
- mock = propUtil.mergeProperties(changes, mock);
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;
@@ -166,6 +176,28 @@ function getDataFromSources(loopField, sources) {
166
176
  return fieldValue;
167
177
  }
168
178
 
179
+ /*
180
+ * INTERNAL FUNCTION: update allprops device broker array with service config and sample props
181
+ */
182
+ function getDeviceBrokerArray(sampleProps, allProps) {
183
+ const brokerCallsArr = ['getDevice', 'getDevicesFiltered', 'isAlive', 'getConfig', 'getCount'];
184
+ const deviceBroker = allProps.devicebroker;
185
+ for (let i = 0; i < brokerCallsArr.length; i += 1) {
186
+ if (!allProps.devicebroker || !allProps.devicebroker[brokerCallsArr[i]] || allProps.devicebroker[brokerCallsArr[i]].length === 0 || !allProps.devicebroker[brokerCallsArr[i]][0].path) {
187
+ // if not in service config check sample props
188
+ if (!sampleProps.devicebroker || !sampleProps.devicebroker[brokerCallsArr[i]] || sampleProps.devicebroker[brokerCallsArr[i]].length === 0 || !sampleProps.devicebroker[brokerCallsArr[i]][0].path) {
189
+ deviceBroker[brokerCallsArr[i]] = [];
190
+ } else {
191
+ log.info('Updating device broker with sample props');
192
+ deviceBroker[brokerCallsArr[i]] = sampleProps.devicebroker[brokerCallsArr[i]];
193
+ }
194
+ }
195
+ }
196
+
197
+ log.info('Device broker array', JSON.stringify(deviceBroker, null, 3));
198
+ return deviceBroker;
199
+ }
200
+
169
201
  /* GENERAL ADAPTER FUNCTIONS THESE SHOULD NOT BE DIRECTLY MODIFIED */
170
202
  /* IF YOU NEED MODIFICATIONS, REDEFINE THEM IN adapter.js!!! */
171
203
  class AdapterBase extends EventEmitterCl {
@@ -185,6 +217,10 @@ class AdapterBase extends EventEmitterCl {
185
217
  this.id = prongid;
186
218
  this.propUtilInst = new PropUtilCl(prongid, __dirname);
187
219
  propUtil = this.propUtilInst;
220
+ this.transUtilInst = new TransUtilCl(prongid, this.propUtilInst);
221
+ transUtil = this.transUtilInst;
222
+ this.transUtilInst = new TransUtilCl(prongid, this.propUtilInst);
223
+ transUtil = this.transUtilInst;
188
224
  this.initProps = properties;
189
225
  this.alive = false;
190
226
  this.healthy = false;
@@ -197,6 +233,8 @@ class AdapterBase extends EventEmitterCl {
197
233
 
198
234
  // set up the properties I care about
199
235
  this.refreshProperties(properties);
236
+ // update deviceBroker based on service config and sample props
237
+ this.allProps.devicebroker = getDeviceBrokerArray(sampleProperties, this.allProps);
200
238
 
201
239
  // Instantiate the other components for this Adapter
202
240
  this.requestHandlerInst = new RequestHandlerCl(this.id, this.allProps, __dirname);
@@ -533,16 +571,17 @@ class AdapterBase extends EventEmitterCl {
533
571
  * @param {string} entity - the entity to be changed, if an action, schema or mock data file (optional)
534
572
  * @param {string} type - the type of entity file to change, (action, schema, mock) (optional)
535
573
  * @param {string} action - the action to be changed, if an action, schema or mock data file (optional)
574
+ * @param {boolean} replace - true to replace entire mock data, false to merge/append (optional)
536
575
  * @param {Callback} callback - The results of the call
537
576
  */
538
- iapUpdateAdapterConfiguration(configFile, changes, entity, type, action, callback) {
577
+ iapUpdateAdapterConfiguration(configFile, changes, entity, type, action, replace, callback) {
539
578
  const meth = 'adapterBase-iapUpdateAdapterConfiguration';
540
579
  const origin = `${this.id}-${meth}`;
541
580
  log.trace(origin);
542
581
 
543
582
  // verify the parameters are valid
544
583
  if (changes === undefined || changes === null || typeof changes !== 'object'
545
- || Object.keys(changes).length === 0) {
584
+ || Object.keys(changes).length === 0) {
546
585
  const result = {
547
586
  response: 'No configuration updates to make'
548
587
  };
@@ -627,8 +666,14 @@ class AdapterBase extends EventEmitterCl {
627
666
  if (!fs.existsSync(mpath)) {
628
667
  fs.mkdirSync(mpath);
629
668
  }
669
+ // this means we are changing a mock data file so replace is required
670
+ if (replace === undefined || replace === null || replace === '') {
671
+ const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Missing Data', ['replace'], null, null, null);
672
+ log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
673
+ return callback(null, errorObj);
674
+ }
675
+ const mres = updateMock(mpath, configFile, changes, replace);
630
676
 
631
- const mres = updateMock(mpath, configFile, changes);
632
677
  if (mres) {
633
678
  const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, `Incomplete Configuration Change: ${mres}`, [], null, null, null);
634
679
  log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
@@ -665,6 +710,10 @@ class AdapterBase extends EventEmitterCl {
665
710
  return callback(null, result);
666
711
  }
667
712
 
713
+ if (typeof this.allProps.choosepath === 'string') {
714
+ choosepath = this.allProps.choosepath;
715
+ }
716
+
668
717
  // make sure the entities directory exists
669
718
  const entitydir = path.join(__dirname, 'entities');
670
719
  if (!fs.statSync(entitydir).isDirectory()) {
@@ -689,7 +738,25 @@ class AdapterBase extends EventEmitterCl {
689
738
 
690
739
  // go through all of the actions set the appropriate info in the newActions
691
740
  for (let a = 0; a < actions.actions.length; a += 1) {
692
- if (actions.actions[a].entitypath.indexOf(apiPath) >= 0) {
741
+ if (actions.actions[a].entitypath && typeof actions.actions[a].entitypath === 'object') {
742
+ const entityKeys = Object.keys(actions.actions[a].entitypath);
743
+ if (entityKeys.length > 0) {
744
+ for (let entityKey = 0; entityKey < entityKeys.length; entityKey += 1) {
745
+ if (choosepath && entityKeys[entityKey] === choosepath && actions.actions[a].entitypath[entityKeys[entityKey]].indexOf(apiPath) >= 0) {
746
+ log.info(` Found - entity: ${entities[e]} action: ${actions.actions[a].name}`);
747
+ log.info(` method: ${actions.actions[a].method} path: ${actions.actions[a].entitypath[entityKeys[entityKey]]}`);
748
+ const fitem = {
749
+ entity: entities[e],
750
+ action: actions.actions[a].name,
751
+ method: actions.actions[a].method,
752
+ path: actions.actions[a].entitypath[entityKeys[entityKey]]
753
+ };
754
+ fitems.push(fitem);
755
+ break;
756
+ }
757
+ }
758
+ }
759
+ } else if (actions.actions[a].entitypath.indexOf(apiPath) >= 0) {
693
760
  log.info(` Found - entity: ${entities[e]} action: ${actions.actions[a].name}`);
694
761
  log.info(` method: ${actions.actions[a].method} path: ${actions.actions[a].entitypath}`);
695
762
  const fitem = {
@@ -923,7 +990,7 @@ class AdapterBase extends EventEmitterCl {
923
990
  const entityIds = [];
924
991
 
925
992
  if (entities && Object.hasOwnProperty.call(entities, 'response')
926
- && Array.isArray(entities.response)) {
993
+ && Array.isArray(entities.response)) {
927
994
  for (let e = 0; e < entities.response.length; e += 1) {
928
995
  entityIds.push(entities.response[e][key]);
929
996
  }
@@ -1161,10 +1228,10 @@ class AdapterBase extends EventEmitterCl {
1161
1228
  }
1162
1229
  if (callProps.query) {
1163
1230
  callQuery = { ...callProps.query };
1164
-
1165
1231
  // go through the query params to check for variable values
1166
1232
  const cpKeys = Object.keys(callQuery);
1167
1233
  for (let cp = 0; cp < cpKeys.length; cp += 1) {
1234
+ // if (callQuery[cpKeys[cp]].startsWith('{') && callQuery[cpKeys[cp]].endsWith('}')) {
1168
1235
  // make any necessary changes to the query params
1169
1236
  if (devResp !== null && callProps.requestFields && Object.keys(callProps.requestFields).length > 0) {
1170
1237
  const rqKeys = Object.keys(callProps.requestFields);
@@ -1178,6 +1245,7 @@ class AdapterBase extends EventEmitterCl {
1178
1245
  }
1179
1246
  }
1180
1247
  }
1248
+ // }
1181
1249
  }
1182
1250
  }
1183
1251
  if (callProps.body) {
@@ -1186,6 +1254,7 @@ class AdapterBase extends EventEmitterCl {
1186
1254
  // go through the body fields to check for variable values
1187
1255
  const cbKeys = Object.keys(callBody);
1188
1256
  for (let cb = 0; cb < cbKeys.length; cb += 1) {
1257
+ // if (callBody[cbKeys[cb]].startsWith('{') && callBody[cbKeys[cb]].endsWith('}')) {
1189
1258
  // make any necessary changes to the query params
1190
1259
  if (devResp !== null && callProps.requestFields && Object.keys(callProps.requestFields).length > 0) {
1191
1260
  const rqKeys = Object.keys(callProps.requestFields);
@@ -1200,6 +1269,7 @@ class AdapterBase extends EventEmitterCl {
1200
1269
  }
1201
1270
  }
1202
1271
  }
1272
+ // }
1203
1273
  }
1204
1274
  }
1205
1275
  if (callProps.headers) {
@@ -1208,6 +1278,7 @@ class AdapterBase extends EventEmitterCl {
1208
1278
  // go through the body fields to check for variable values
1209
1279
  const chKeys = Object.keys(callHeaders);
1210
1280
  for (let ch = 0; ch < chKeys.length; ch += 1) {
1281
+ // if (callHeaders[chKeys[ch]].startsWith('{') && callHeaders[chKeys[ch]].endsWith('}')) {
1211
1282
  // make any necessary changes to the query params
1212
1283
  if (devResp !== null && callProps.requestFields && Object.keys(callProps.requestFields).length > 0) {
1213
1284
  const rqKeys = Object.keys(callProps.requestFields);
@@ -1222,6 +1293,7 @@ class AdapterBase extends EventEmitterCl {
1222
1293
  }
1223
1294
  }
1224
1295
  }
1296
+ // }
1225
1297
  }
1226
1298
  }
1227
1299
  if (callProps.handleFailure) {
@@ -1240,7 +1312,6 @@ class AdapterBase extends EventEmitterCl {
1240
1312
  // if we received an error or their is no response on the results return an error
1241
1313
  if (error) {
1242
1314
  if (handleFail === 'fail') {
1243
- // IT IS CRASHING RIGHT BELOW HERE
1244
1315
  return callback(null, error);
1245
1316
  }
1246
1317
  return callback({}, null);
@@ -1529,7 +1600,6 @@ class AdapterBase extends EventEmitterCl {
1529
1600
  let myResult = [];
1530
1601
  results.forEach((result) => {
1531
1602
  if (Array.isArray(result)) {
1532
- // myResult = myResult.concat(result);
1533
1603
  myResult = [...myResult, ...result];
1534
1604
  } else if (Object.keys(result).length > 0) {
1535
1605
  myResult.push(result);
@@ -1686,7 +1756,7 @@ class AdapterBase extends EventEmitterCl {
1686
1756
  // Perform component calls here.
1687
1757
  callPromises.push(
1688
1758
  new Promise((resolve, reject) => {
1689
- this.iapMakeBrokerCall('getConfig', this.allProps.devicebroker.getConfig[i], [devs.list[0]], [deviceName], (callRet, callErr) => {
1759
+ this.iapMakeBrokerCall('getConfig', this.allProps.devicebroker.getConfig[i], [devs.list[0]], null, (callRet, callErr) => {
1690
1760
  // return an error
1691
1761
  if (callErr) {
1692
1762
  reject(callErr);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@itentialopensource/adapter-alkira",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "This adapter integrates with system described as: alkiraApi.",
5
5
  "main": "adapter.js",
6
6
  "wizardVersion": "2.44.15",
@@ -53,7 +53,7 @@
53
53
  "author": "Itential",
54
54
  "homepage": "https://gitlab.com/itentialopensource/adapters/cloud/adapter-alkira#readme",
55
55
  "dependencies": {
56
- "@itentialopensource/adapter-utils": "^4.48.0",
56
+ "@itentialopensource/adapter-utils": "^4.48.13",
57
57
  "ajv": "^6.12.0",
58
58
  "axios": "^0.21.0",
59
59
  "commander": "^2.20.0",
Binary file
@@ -1,10 +1,10 @@
1
1
  {
2
- "version": "0.1.2",
2
+ "version": "0.1.4",
3
3
  "configLines": 46212,
4
4
  "scriptLines": 1795,
5
- "codeLines": 40907,
5
+ "codeLines": 40946,
6
6
  "testLines": 32660,
7
7
  "testCases": 1735,
8
- "totalCodeLines": 75362,
8
+ "totalCodeLines": 75401,
9
9
  "wfTasks": 457
10
10
  }
@@ -102,87 +102,91 @@
102
102
  "devicebroker": {
103
103
  "getDevice": [
104
104
  {
105
- "path": "/get/devices/{id}",
105
+ "path": "/tenantnetworks/{tenantNetworkId}/connectors",
106
106
  "method": "GET",
107
107
  "query": {},
108
108
  "body": {},
109
109
  "headers": {},
110
110
  "handleFailure": "ignore",
111
111
  "requestFields": {
112
- "id": "name"
112
+ "tenantNetworkId": "port"
113
113
  },
114
- "responseDatakey": "",
115
114
  "responseFields": {
116
- "name": "host",
117
- "ostype": "os",
118
- "ostypePrefix": "system-",
119
- "ipaddress": "attributes.ipaddr",
120
- "port": "443"
115
+ "name": "name",
116
+ "ostype": "type",
117
+ "ostypePrefix": "connector-",
118
+ "port": "tenantNetworkId",
119
+ "ip": "id",
120
+ "ipaddress": "id"
121
121
  }
122
122
  }
123
123
  ],
124
124
  "getDevicesFiltered": [
125
125
  {
126
- "path": "/get/devices",
126
+ "path": "/tenantnetworks/{tenantNetworkId}/connectors",
127
127
  "method": "GET",
128
128
  "query": {},
129
129
  "body": {},
130
130
  "headers": {},
131
131
  "handleFailure": "ignore",
132
- "requestFields": {},
133
- "responseDatakey": "",
132
+ "requestFields": {
133
+ "tenantNetworkId": "170"
134
+ },
134
135
  "responseFields": {
135
- "name": "host",
136
- "ostype": "os",
137
- "ostypePrefix": "system-",
138
- "ipaddress": "attributes.ipaddr",
139
- "port": "443"
136
+ "name": "name",
137
+ "ostype": "type",
138
+ "ostypePrefix": "connector-",
139
+ "port": "tenantNetworkId",
140
+ "ip": "id",
141
+ "ipaddress": "id",
142
+ "connectorId": "id"
140
143
  }
141
144
  }
142
145
  ],
143
146
  "isAlive": [
144
147
  {
145
- "path": "/get/devices/{id}/status",
148
+ "path": "/tenantnetworks/{tenantNetworkId}/health/connector/{connectorId}",
146
149
  "method": "GET",
147
150
  "query": {},
148
151
  "body": {},
149
152
  "headers": {},
150
- "handleFailure": "ignore",
151
153
  "requestFields": {
152
- "id": "name"
154
+ "tenantNetworkId": "port",
155
+ "connectorId": "connectorId"
153
156
  },
154
- "responseDatakey": "",
157
+ "handleFailure": "ignore",
155
158
  "responseFields": {
156
- "status": "status",
157
- "statusValue": "online"
159
+ "status": "connectivityStatus",
160
+ "statusValue": "100"
158
161
  }
159
162
  }
160
163
  ],
161
164
  "getConfig": [
162
165
  {
163
- "path": "/get/devices/{id}/configPart1",
166
+ "path": "/tenantnetworks/{tenantNetworkId}/connectors/{connectorId}",
164
167
  "method": "GET",
165
168
  "query": {},
166
169
  "body": {},
167
170
  "headers": {},
168
- "handleFailure": "ignore",
169
171
  "requestFields": {
170
- "id": "name"
172
+ "tenantNetworkId": "port",
173
+ "connectorId": "connectorId"
171
174
  },
172
- "responseDatakey": "",
175
+ "handleFailure": "ignore",
173
176
  "responseFields": {}
174
177
  }
175
178
  ],
176
179
  "getCount": [
177
180
  {
178
- "path": "/get/devices",
181
+ "path": "/tenantnetworks/{tenantNetworkId}/connectors",
179
182
  "method": "GET",
180
183
  "query": {},
181
184
  "body": {},
182
185
  "headers": {},
186
+ "requestFields": {
187
+ "tenantNetworkId": "170"
188
+ },
183
189
  "handleFailure": "ignore",
184
- "requestFields": {},
185
- "responseDatakey": "",
186
190
  "responseFields": {}
187
191
  }
188
192
  ]
@@ -862,7 +862,7 @@ describe('[unit] Adapter Base Test', () => {
862
862
  });
863
863
  it('should return no updated if no changes are provided', (done) => {
864
864
  try {
865
- a.iapUpdateAdapterConfiguration(null, null, null, null, null, (data, error) => {
865
+ a.iapUpdateAdapterConfiguration(null, null, null, null, null, null, (data, error) => {
866
866
  try {
867
867
  assert.equal('No configuration updates to make', data.response);
868
868
  done();
@@ -878,7 +878,7 @@ describe('[unit] Adapter Base Test', () => {
878
878
  }).timeout(attemptTimeout);
879
879
  it('should throw an error if missing configuration file', (done) => {
880
880
  try {
881
- a.iapUpdateAdapterConfiguration(null, { name: 'fakeChange' }, null, null, null, (data, error) => {
881
+ a.iapUpdateAdapterConfiguration(null, { name: 'fakeChange' }, null, null, null, null, (data, error) => {
882
882
  try {
883
883
  const displayE = 'configFile is required';
884
884
  runErrorAsserts(data, error, 'AD.300', 'Test-Base-adapterBase-iapUpdateAdapterConfiguration', displayE);
@@ -895,7 +895,7 @@ describe('[unit] Adapter Base Test', () => {
895
895
  }).timeout(attemptTimeout);
896
896
  it('if not package.json, entity is required', (done) => {
897
897
  try {
898
- a.iapUpdateAdapterConfiguration('notPackage', { name: 'fakeChange' }, null, null, null, (data, error) => {
898
+ a.iapUpdateAdapterConfiguration('notPackage', { name: 'fakeChange' }, null, null, null, null, (data, error) => {
899
899
  try {
900
900
  const displayE = 'Unsupported Configuration Change or Missing Entity';
901
901
  runErrorAsserts(data, error, 'AD.999', 'Test-Base-adapterBase-iapUpdateAdapterConfiguration', displayE);
@@ -912,7 +912,7 @@ describe('[unit] Adapter Base Test', () => {
912
912
  }).timeout(attemptTimeout);
913
913
  it('if not package.json, type is required', (done) => {
914
914
  try {
915
- a.iapUpdateAdapterConfiguration('notPackage', { name: 'fakeChange' }, 'entity', null, null, (data, error) => {
915
+ a.iapUpdateAdapterConfiguration('notPackage', { name: 'fakeChange' }, 'entity', null, null, null, (data, error) => {
916
916
  try {
917
917
  const displayE = 'type is required';
918
918
  runErrorAsserts(data, error, 'AD.300', 'Test-Base-adapterBase-iapUpdateAdapterConfiguration', displayE);
@@ -929,7 +929,7 @@ describe('[unit] Adapter Base Test', () => {
929
929
  }).timeout(attemptTimeout);
930
930
  it('if not package.json, entity must be valid', (done) => {
931
931
  try {
932
- a.iapUpdateAdapterConfiguration('notPackage', { name: 'fakeChange' }, 'fakeEntity', 'fakeType', null, (data, error) => {
932
+ a.iapUpdateAdapterConfiguration('notPackage', { name: 'fakeChange' }, 'fakeEntity', 'fakeType', null, null, (data, error) => {
933
933
  try {
934
934
  const displayE = 'Incomplete Configuration Change: Invalid Entity - fakeEntity';
935
935
  runErrorAsserts(data, error, 'AD.999', 'Test-Base-adapterBase-iapUpdateAdapterConfiguration', displayE);