@itentialopensource/adapter-meraki 0.7.3 → 0.8.2

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/utils/tbUtils.js CHANGED
@@ -100,8 +100,8 @@ module.exports = {
100
100
  *
101
101
  * @function decryptProperties
102
102
  */
103
- decryptProperties: (props, dirname, discovery) => {
104
- const propertyEncryptionClassPath = path.join(dirname, '../../../@itential/pronghorn-core/core/PropertyEncryption.js');
103
+ decryptProperties: (props, iapDir, discovery) => {
104
+ const propertyEncryptionClassPath = path.join(iapDir, 'node_modules/@itential/pronghorn-core/core/PropertyEncryption.js');
105
105
  const isEncrypted = props.pathProps.encrypted;
106
106
  const PropertyEncryption = discovery.require(propertyEncryptionClassPath, isEncrypted);
107
107
  const propertyEncryption = new PropertyEncryption({
@@ -177,12 +177,12 @@ module.exports = {
177
177
  verifyInstallationDir: (dirname, name) => {
178
178
  const pathArray = dirname.split(path.sep);
179
179
  const expectedPath = `node_modules/${name}`;
180
- const currentPath = pathArray.slice(pathArray.length - 4, pathArray.length - 1).join('/');
181
- if (expectedPath !== currentPath) {
182
- throw new Error(`adapter should be installed under ${expectedPath}`);
180
+ const currentPath = pathArray.slice(pathArray.length - 3, pathArray.length).join('/');
181
+ if (currentPath.trim() !== expectedPath.trim()) {
182
+ throw new Error(`adapter should be installed under ${expectedPath} but is installed under ${currentPath}`);
183
183
  }
184
184
 
185
- const serverFile = path.join(dirname, '../../../..', 'server.js');
185
+ const serverFile = path.join(dirname, '../../../', 'server.js');
186
186
  if (!fs.existsSync(serverFile)) {
187
187
  throw new Error(`adapter should be installed under IAP/${expectedPath}`);
188
188
  }
@@ -304,23 +304,66 @@ module.exports = {
304
304
  * @param {Object} adapterPronghorn - ./pronghorn.json in adapter dir
305
305
  * @param {Object} sampleProperties - './sampleProperties.json' in adapter dir
306
306
  */
307
- createAdapter: (pronghornProps, profileItem, sampleProperties, adapterPronghorn) => {
308
- const adapter = {
309
- mongoProps: pronghornProps.mongoProps,
310
- isEncrypted: pronghornProps.pathProps.encrypted,
311
- model: adapterPronghorn.id,
312
- name: sampleProperties.id,
313
- type: adapterPronghorn.type,
314
- properties: sampleProperties,
315
- redisProps: profileItem.redisProps,
316
- loggerProps: profileItem.loggerProps,
317
- rabbitmq: profileItem.rabbitmq
318
- };
307
+ createAdapter: function createAdapter(pronghornProps, profileItem, sampleProperties, adapterPronghorn) {
308
+ const dirname = this.getDirname();
309
+ const packagePath = `${dirname.split('node_modules')[0]}package.json`;
310
+ const info = JSON.parse(fs.readFileSync(packagePath));
311
+ const version = parseInt(info.version.split('.')[0], 10);
312
+
313
+ let adapter = {};
314
+ if (version >= 2020) {
315
+ adapter = {
316
+ isEncrypted: pronghornProps.pathProps.encrypted,
317
+ model: adapterPronghorn.id,
318
+ name: sampleProperties.id,
319
+ type: adapterPronghorn.type,
320
+ properties: sampleProperties,
321
+ loggerProps: profileItem.loggerProps
322
+ };
323
+ } else {
324
+ adapter = {
325
+ mongoProps: pronghornProps.mongoProps,
326
+ isEncrypted: pronghornProps.pathProps.encrypted,
327
+ model: adapterPronghorn.id,
328
+ name: sampleProperties.id,
329
+ type: adapterPronghorn.type,
330
+ properties: sampleProperties,
331
+ redisProps: profileItem.redisProps,
332
+ loggerProps: profileItem.loggerProps,
333
+ rabbitmq: profileItem.rabbitmq
334
+ };
335
+ adapter.mongoProps.pdb = true;
336
+ }
337
+
319
338
  adapter.loggerProps.log_filename = `adapter-${adapter.name}.log`;
320
- adapter.mongoProps.pdb = true;
321
339
  return adapter;
322
340
  },
323
341
 
342
+ getPronghornProps: function getPronghornProps(iapDir) {
343
+ console.log('Retrieving properties.json file...');
344
+ const rawProps = require(path.join(iapDir, 'properties.json'));
345
+ console.log('Decrypting properties...');
346
+ const { Discovery } = require(path.join(iapDir, 'node_modules/@itential/itential-utils'));
347
+ const discovery = new Discovery();
348
+ const pronghornProps = this.decryptProperties(rawProps, iapDir, discovery);
349
+ console.log('Found properties.\n');
350
+ return pronghornProps;
351
+ },
352
+
353
+ // get database connection and existing adapter config
354
+ getAdapterConfig: async function getAdapterConfig() {
355
+ const iapDir = this.getIAPHome();
356
+ const pronghornProps = this.getPronghornProps(iapDir);
357
+ console.log('Connecting to Database...');
358
+ const database = await this.connect(iapDir, pronghornProps);
359
+ console.log('Connection established.');
360
+ const { name } = require(path.join(__dirname, '..', 'package.json'));
361
+ const serviceItem = await database.collection(this.SERVICE_CONFIGS_COLLECTION).findOne(
362
+ { model: name }
363
+ );
364
+ return { database, serviceItem, pronghornProps };
365
+ },
366
+
324
367
  /**
325
368
  * @summary return async healthcheck result as a Promise
326
369
  *
@@ -368,5 +411,54 @@ module.exports = {
368
411
  } catch (error) {
369
412
  return false;
370
413
  }
414
+ },
415
+
416
+ /**
417
+ * @summary Used to determine the proper dirname to return in case adapter reference is
418
+ * symlinked withink IAP
419
+ * @returns the symlinked path (using pwd command) of the adapter in case properties.json
420
+ * is not found in the original path
421
+ * @function getDirname
422
+ */
423
+ getDirname: function getDirname() {
424
+ if (this.withinIAP(path.join(__dirname, '../../../../'))) {
425
+ return __dirname;
426
+ }
427
+ const { stdout } = this.systemSync('pwd', true);
428
+ return stdout.trim();
429
+ },
430
+
431
+ /**
432
+ * @summary Obtain the IAP installation directory depending on how adapter is used:
433
+ * by IAP, or by npm run CLI interface
434
+ * @returns IAP installation directory
435
+ * @function getIAPHome
436
+ */
437
+ getIAPHome: function getIAPHome() {
438
+ // if adapter started via IAP, use path injected by core
439
+ if (process.env.iap_home) return process.env.iap_home;
440
+ // adapter started via CLI `npm run <command>` so we have to be located under
441
+ // <IAP_HOME>/node_modules/@itentialopensource/<adapter>/ directory,
442
+ // use `pwd` command wihout option -P(resolving symlinks) https://linux.die.net/man/1/pwd
443
+ const { stdout } = this.systemSync('pwd', true);
444
+ if (stdout.indexOf('/node_modules') >= 0) {
445
+ return stdout.trim().split('/node_modules')[0];
446
+ }
447
+ return path.join(stdout.trim(), '../../../');
448
+ },
449
+
450
+ /**
451
+ * @summary connect to mongodb
452
+ *
453
+ * @function connect
454
+ * @param {Object} properties - pronghornProps
455
+ */
456
+ connect: async function connect(iapDir, properties) {
457
+ // Connect to Mongo
458
+ const { MongoDBConnection } = require(path.join(iapDir, 'node_modules/@itential/database'));
459
+ const connection = new MongoDBConnection(properties.mongoProps);
460
+ const database = await connection.connect(true);
461
+ return database;
371
462
  }
463
+
372
464
  };
@@ -117,30 +117,6 @@ const VerifyHealthCheckEndpoint = (serviceItem, props, scriptFlag) => {
117
117
  return { result, updatedAdapter };
118
118
  };
119
119
 
120
- const getPronghornProps = (iapDir) => {
121
- console.log('Retrieving properties.json file...');
122
- const rawProps = require(path.join(iapDir, 'properties.json'));
123
- console.log('Decrypting properties...');
124
- const { Discovery } = require('@itential/itential-utils');
125
- const discovery = new Discovery();
126
- const pronghornProps = utils.decryptProperties(rawProps, __dirname, discovery);
127
- console.log('Found properties.\n');
128
- return pronghornProps;
129
- };
130
-
131
- // get database connection and existing adapter config
132
- const getAdapterConfig = async () => {
133
- const iapDir = path.join(__dirname, '../../../../');
134
- const pronghornProps = getPronghornProps(iapDir);
135
- console.log('Connecting to Database...');
136
- const database = await basicGet.connect(pronghornProps);
137
- console.log('Connection established.');
138
- const serviceItem = await database.collection(utils.SERVICE_CONFIGS_COLLECTION).findOne(
139
- { model: name }
140
- );
141
- return { database, serviceItem, pronghornProps };
142
- };
143
-
144
120
  const offline = async () => {
145
121
  console.log('Start offline troubleshooting');
146
122
  const { updatedAdapter } = VerifyHealthCheckEndpoint({ properties: sampleProperties }, {}, true);
@@ -159,7 +135,7 @@ const offline = async () => {
159
135
 
160
136
  const troubleshoot = async (props, scriptFlag, persistFlag, adapter) => {
161
137
  // get database connection and existing adapter config
162
- const { database, serviceItem } = await getAdapterConfig();
138
+ const { database, serviceItem } = await utils.getAdapterConfig();
163
139
  // where troubleshoot should start
164
140
  if (serviceItem) {
165
141
  if (!scriptFlag || rls.keyInYN(`Start verifying the connection and authentication properties for ${name}?`)) {
@@ -211,4 +187,4 @@ const troubleshoot = async (props, scriptFlag, persistFlag, adapter) => {
211
187
  return null;
212
188
  };
213
189
 
214
- module.exports = { troubleshoot, getAdapterConfig, offline };
190
+ module.exports = { troubleshoot, offline };