@itentialopensource/adapter-efficientip_solidserver 0.1.1 → 0.3.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 +39 -0
- package/BROKER.md +199 -0
- package/CALLS.md +1465 -0
- package/CHANGELOG.md +17 -2
- package/CODE_OF_CONDUCT.md +12 -17
- package/CONTRIBUTING.md +3 -148
- package/ENHANCE.md +69 -0
- package/PROPERTIES.md +641 -0
- package/README.md +235 -576
- package/SUMMARY.md +9 -0
- package/SYSTEMINFO.md +11 -0
- package/TROUBLESHOOT.md +47 -0
- package/adapter.js +383 -263
- package/adapterBase.js +854 -408
- package/changelogs/changelog.md +16 -0
- package/entities/.generic/action.json +110 -5
- package/entities/.generic/schema.json +6 -1
- package/error.json +6 -0
- package/metadata.json +49 -0
- package/package.json +27 -22
- package/pronghorn.json +691 -88
- package/propertiesDecorators.json +14 -0
- package/propertiesSchema.json +828 -7
- package/refs?service=git-upload-pack +0 -0
- package/report/adapter-openapi.json +41906 -0
- package/report/adapter-openapi.yaml +23138 -0
- package/report/adapterInfo.json +10 -0
- package/report/updateReport1653233995404.json +120 -0
- package/report/updateReport1691508450223.json +120 -0
- package/report/updateReport1692202927301.json +120 -0
- package/report/updateReport1694465845842.json +120 -0
- package/report/updateReport1698421858198.json +120 -0
- package/sampleProperties.json +153 -3
- package/test/integration/adapterTestBasicGet.js +3 -5
- package/test/integration/adapterTestConnectivity.js +91 -42
- package/test/integration/adapterTestIntegration.js +155 -106
- package/test/unit/adapterBaseTestUnit.js +388 -308
- package/test/unit/adapterTestUnit.js +484 -243
- package/utils/adapterInfo.js +206 -0
- package/utils/addAuth.js +94 -0
- package/utils/artifactize.js +1 -1
- package/utils/basicGet.js +1 -14
- package/utils/checkMigrate.js +1 -1
- package/utils/entitiesToDB.js +179 -0
- package/utils/findPath.js +1 -1
- package/utils/methodDocumentor.js +273 -0
- package/utils/modify.js +14 -16
- package/utils/packModificationScript.js +1 -1
- package/utils/patches2bundledDeps.js +90 -0
- package/utils/pre-commit.sh +5 -0
- package/utils/removeHooks.js +20 -0
- package/utils/taskMover.js +309 -0
- package/utils/tbScript.js +129 -53
- package/utils/tbUtils.js +125 -25
- package/utils/testRunner.js +17 -17
- package/utils/troubleshootingAdapter.js +10 -31
- package/workflows/README.md +0 -3
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/* @copyright Itential, LLC 2019 */
|
|
3
|
+
/* eslint global-require:warn */
|
|
4
|
+
/* eslint import/no-dynamic-require:warn */
|
|
5
|
+
/* eslint prefer-destructuring:warn */
|
|
6
|
+
|
|
7
|
+
const path = require('path');
|
|
8
|
+
const fs = require('fs-extra');
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* This script will determine the information about the adapter and store
|
|
12
|
+
* it into a file in the adapter.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* get adapter information
|
|
17
|
+
*/
|
|
18
|
+
function adapterInfo() {
|
|
19
|
+
// set the base pase of the adapter - tool shoud be one level up in utils
|
|
20
|
+
let adaptdir = __dirname;
|
|
21
|
+
const infoRes = {};
|
|
22
|
+
|
|
23
|
+
if (adaptdir.endsWith('/utils')) {
|
|
24
|
+
adaptdir = adaptdir.substring(0, adaptdir.length - 6);
|
|
25
|
+
}
|
|
26
|
+
const pack = require(`${adaptdir}/package.json`);
|
|
27
|
+
infoRes.version = pack.version;
|
|
28
|
+
|
|
29
|
+
let configCount = 0;
|
|
30
|
+
if (fs.existsSync(`${adaptdir}/pronghorn.json`)) {
|
|
31
|
+
const cFile = fs.readFileSync(`${adaptdir}/pronghorn.json`, 'utf8');
|
|
32
|
+
configCount += cFile.split('\n').length;
|
|
33
|
+
} else {
|
|
34
|
+
console.log('Missing - pronghorn.json');
|
|
35
|
+
}
|
|
36
|
+
if (fs.existsSync(`${adaptdir}/propertiesSchema.json`)) {
|
|
37
|
+
const cFile = fs.readFileSync(`${adaptdir}/propertiesSchema.json`, 'utf8');
|
|
38
|
+
configCount += cFile.split('\n').length;
|
|
39
|
+
} else {
|
|
40
|
+
console.log('Missing - propertiesSchema.json');
|
|
41
|
+
}
|
|
42
|
+
if (fs.existsSync(`${adaptdir}/error.json`)) {
|
|
43
|
+
const cFile = fs.readFileSync(`${adaptdir}/error.json`, 'utf8');
|
|
44
|
+
configCount += cFile.split('\n').length;
|
|
45
|
+
} else {
|
|
46
|
+
console.log('Missing - error.json');
|
|
47
|
+
}
|
|
48
|
+
const entitydir = path.join(adaptdir, '/entities');
|
|
49
|
+
if (fs.existsSync(entitydir) && fs.statSync(entitydir).isDirectory()) {
|
|
50
|
+
const entities = fs.readdirSync(entitydir);
|
|
51
|
+
// need to go through each entity in the entities directory
|
|
52
|
+
for (let e = 0; e < entities.length; e += 1) {
|
|
53
|
+
if (fs.statSync(`${entitydir}/${entities[e]}`).isDirectory()) {
|
|
54
|
+
const cfiles = fs.readdirSync(entitydir);
|
|
55
|
+
for (let c = 0; c < cfiles.length; c += 1) {
|
|
56
|
+
if (cfiles[c].endsWith('.json')) {
|
|
57
|
+
const ccFile = fs.readFileSync(`${entitydir}/${entities[e]}/${cfiles[c]}`, 'utf8');
|
|
58
|
+
configCount += ccFile.split('\n').length;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
} else {
|
|
64
|
+
console.log('Could not find the entities directory');
|
|
65
|
+
}
|
|
66
|
+
infoRes.configLines = configCount;
|
|
67
|
+
|
|
68
|
+
let scodeCount = 0;
|
|
69
|
+
if (fs.existsSync(`${adaptdir}/utils/artifactize.js`)) {
|
|
70
|
+
const sFile = fs.readFileSync(`${adaptdir}/utils/artifactize.js`, 'utf8');
|
|
71
|
+
scodeCount += sFile.split('\n').length;
|
|
72
|
+
} else {
|
|
73
|
+
console.log('Missing - utils/artifactize.js');
|
|
74
|
+
}
|
|
75
|
+
if (fs.existsSync(`${adaptdir}/utils/basicGet.js`)) {
|
|
76
|
+
const sFile = fs.readFileSync(`${adaptdir}/utils/basicGet.js`, 'utf8');
|
|
77
|
+
scodeCount += sFile.split('\n').length;
|
|
78
|
+
} else {
|
|
79
|
+
console.log('Missing - utils/basicGet.js');
|
|
80
|
+
}
|
|
81
|
+
if (fs.existsSync(`${adaptdir}/utils/checkMigrate.js`)) {
|
|
82
|
+
const sFile = fs.readFileSync(`${adaptdir}/utils/checkMigrate.js`, 'utf8');
|
|
83
|
+
scodeCount += sFile.split('\n').length;
|
|
84
|
+
} else {
|
|
85
|
+
console.log('Missing - utils/checkMigrate.js');
|
|
86
|
+
}
|
|
87
|
+
if (fs.existsSync(`${adaptdir}/utils/findPath.js`)) {
|
|
88
|
+
const sFile = fs.readFileSync(`${adaptdir}/utils/findPath.js`, 'utf8');
|
|
89
|
+
scodeCount += sFile.split('\n').length;
|
|
90
|
+
} else {
|
|
91
|
+
console.log('Missing - utils/findPath.js');
|
|
92
|
+
}
|
|
93
|
+
if (fs.existsSync(`${adaptdir}/utils/modify.js`)) {
|
|
94
|
+
const sFile = fs.readFileSync(`${adaptdir}/utils/modify.js`, 'utf8');
|
|
95
|
+
scodeCount += sFile.split('\n').length;
|
|
96
|
+
} else {
|
|
97
|
+
console.log('Missing - utils/modify.js');
|
|
98
|
+
}
|
|
99
|
+
if (fs.existsSync(`${adaptdir}/utils/packModificationScript.js`)) {
|
|
100
|
+
const sFile = fs.readFileSync(`${adaptdir}/utils/packModificationScript.js`, 'utf8');
|
|
101
|
+
scodeCount += sFile.split('\n').length;
|
|
102
|
+
} else {
|
|
103
|
+
console.log('Missing - utils/packModificationScript.js');
|
|
104
|
+
}
|
|
105
|
+
if (fs.existsSync(`${adaptdir}/utils/setup.js`)) {
|
|
106
|
+
const sFile = fs.readFileSync(`${adaptdir}/utils/setup.js`, 'utf8');
|
|
107
|
+
scodeCount += sFile.split('\n').length;
|
|
108
|
+
} else {
|
|
109
|
+
console.log('Missing - utils/setup.js');
|
|
110
|
+
}
|
|
111
|
+
if (fs.existsSync(`${adaptdir}/utils/tbScript.js`)) {
|
|
112
|
+
const sFile = fs.readFileSync(`${adaptdir}/utils/tbScript.js`, 'utf8');
|
|
113
|
+
scodeCount += sFile.split('\n').length;
|
|
114
|
+
} else {
|
|
115
|
+
console.log('Missing - utils/tbScript.js');
|
|
116
|
+
}
|
|
117
|
+
if (fs.existsSync(`${adaptdir}/utils/tbUtils.js`)) {
|
|
118
|
+
const sFile = fs.readFileSync(`${adaptdir}/utils/tbUtils.js`, 'utf8');
|
|
119
|
+
scodeCount += sFile.split('\n').length;
|
|
120
|
+
} else {
|
|
121
|
+
console.log('Missing - utils/tbUtils.js');
|
|
122
|
+
}
|
|
123
|
+
if (fs.existsSync(`${adaptdir}/utils/testRunner.js`)) {
|
|
124
|
+
const sFile = fs.readFileSync(`${adaptdir}/utils/testRunner.js`, 'utf8');
|
|
125
|
+
scodeCount += sFile.split('\n').length;
|
|
126
|
+
} else {
|
|
127
|
+
console.log('Missing - utils/testRunner.js');
|
|
128
|
+
}
|
|
129
|
+
if (fs.existsSync(`${adaptdir}/utils/troubleshootingAdapter.js`)) {
|
|
130
|
+
const sFile = fs.readFileSync(`${adaptdir}/utils/troubleshootingAdapter.js`, 'utf8');
|
|
131
|
+
scodeCount += sFile.split('\n').length;
|
|
132
|
+
} else {
|
|
133
|
+
console.log('Missing - utils/troubleshootingAdapter.js');
|
|
134
|
+
}
|
|
135
|
+
infoRes.scriptLines = scodeCount;
|
|
136
|
+
|
|
137
|
+
let codeCount = 0;
|
|
138
|
+
if (fs.existsSync(`${adaptdir}/adapter.js`)) {
|
|
139
|
+
const aFile = fs.readFileSync(`${adaptdir}/adapter.js`, 'utf8');
|
|
140
|
+
codeCount += aFile.split('\n').length;
|
|
141
|
+
} else {
|
|
142
|
+
console.log('Missing - utils/adapter.js');
|
|
143
|
+
}
|
|
144
|
+
if (fs.existsSync(`${adaptdir}/adapterBase.js`)) {
|
|
145
|
+
const aFile = fs.readFileSync(`${adaptdir}/adapterBase.js`, 'utf8');
|
|
146
|
+
codeCount += aFile.split('\n').length;
|
|
147
|
+
} else {
|
|
148
|
+
console.log('Missing - utils/adapterBase.js');
|
|
149
|
+
}
|
|
150
|
+
infoRes.codeLines = codeCount;
|
|
151
|
+
|
|
152
|
+
let tcodeCount = 0;
|
|
153
|
+
let ttestCount = 0;
|
|
154
|
+
if (fs.existsSync(`${adaptdir}/test/integration/adapterTestBasicGet.js`)) {
|
|
155
|
+
const tFile = fs.readFileSync(`${adaptdir}/test/integration/adapterTestBasicGet.js`, 'utf8');
|
|
156
|
+
tcodeCount += tFile.split('\n').length;
|
|
157
|
+
ttestCount += tFile.split('it(\'').length;
|
|
158
|
+
} else {
|
|
159
|
+
console.log('Missing - test/integration/adapterTestBasicGet.js');
|
|
160
|
+
}
|
|
161
|
+
if (fs.existsSync(`${adaptdir}/test/integration/adapterTestConnectivity.js`)) {
|
|
162
|
+
const tFile = fs.readFileSync(`${adaptdir}/test/integration/adapterTestConnectivity.js`, 'utf8');
|
|
163
|
+
tcodeCount += tFile.split('\n').length;
|
|
164
|
+
ttestCount += tFile.split('it(\'').length;
|
|
165
|
+
} else {
|
|
166
|
+
console.log('Missing - test/integration/adapterTestConnectivity.js');
|
|
167
|
+
}
|
|
168
|
+
if (fs.existsSync(`${adaptdir}/test/integration/adapterTestIntegration.js`)) {
|
|
169
|
+
const tFile = fs.readFileSync(`${adaptdir}/test/integration/adapterTestIntegration.js`, 'utf8');
|
|
170
|
+
tcodeCount += tFile.split('\n').length;
|
|
171
|
+
ttestCount += tFile.split('it(\'').length;
|
|
172
|
+
} else {
|
|
173
|
+
console.log('Missing - test/integration/adapterTestIntegration.js');
|
|
174
|
+
}
|
|
175
|
+
if (fs.existsSync(`${adaptdir}/test/unit/adapterBaseTestUnit.js`)) {
|
|
176
|
+
const tFile = fs.readFileSync(`${adaptdir}/test/unit/adapterBaseTestUnit.js`, 'utf8');
|
|
177
|
+
tcodeCount += tFile.split('\n').length;
|
|
178
|
+
ttestCount += tFile.split('it(\'').length;
|
|
179
|
+
} else {
|
|
180
|
+
console.log('Missing - test/unit/adapterBaseTestUnit.js');
|
|
181
|
+
}
|
|
182
|
+
if (fs.existsSync(`${adaptdir}/test/unit/adapterTestUnit.js`)) {
|
|
183
|
+
const tFile = fs.readFileSync(`${adaptdir}/test/unit/adapterTestUnit.js`, 'utf8');
|
|
184
|
+
tcodeCount += tFile.split('\n').length;
|
|
185
|
+
ttestCount += tFile.split('it(\'').length;
|
|
186
|
+
} else {
|
|
187
|
+
console.log('Missing - test/unit/adapterTestUnit.js');
|
|
188
|
+
}
|
|
189
|
+
infoRes.testLines = tcodeCount;
|
|
190
|
+
infoRes.testCases = ttestCount;
|
|
191
|
+
infoRes.totalCodeLines = scodeCount + codeCount + tcodeCount;
|
|
192
|
+
|
|
193
|
+
if (fs.existsSync(`${adaptdir}/pronghorn.json`)) {
|
|
194
|
+
// Read the entity schema from the file system
|
|
195
|
+
const phFile = path.join(adaptdir, '/pronghorn.json');
|
|
196
|
+
const prong = require(phFile);
|
|
197
|
+
infoRes.wfTasks = prong.methods.length;
|
|
198
|
+
} else {
|
|
199
|
+
console.log('Missing - pronghorn.json');
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
console.log(JSON.stringify(infoRes));
|
|
203
|
+
fs.writeFileSync(`${adaptdir}/report/adapterInfo.json`, JSON.stringify(infoRes, null, 2));
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
adapterInfo();
|
package/utils/addAuth.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/* eslint-disable no-plusplus */
|
|
2
|
+
/* eslint global-require: warn */
|
|
3
|
+
/* eslint import/no-dynamic-require: warn */
|
|
4
|
+
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const fs = require('fs');
|
|
7
|
+
const rls = require('readline-sync');
|
|
8
|
+
|
|
9
|
+
function getQuestions(props, obj) {
|
|
10
|
+
const questions = props.map((p) => `${p}: ${(obj[p] !== undefined) ? `(${obj[p]})` : ''} `);
|
|
11
|
+
return questions;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// function outputs each property for user to edit/confirm
|
|
15
|
+
// props are the fields that need to be changed depending on what the user selects
|
|
16
|
+
// obj is the JSON object that's being updated
|
|
17
|
+
function confirm(props, obj) {
|
|
18
|
+
// create array of questions
|
|
19
|
+
const updatedObj = obj;
|
|
20
|
+
getQuestions(props, obj).forEach((q) => {
|
|
21
|
+
const answer = rls.question(q);
|
|
22
|
+
// only update the field if the answer is NOT and empty string
|
|
23
|
+
if (answer) {
|
|
24
|
+
updatedObj[q.split(':')[0].trim()] = answer;
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
return updatedObj;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const updateBasicAuth = (auth) => {
|
|
31
|
+
const propsToUpdate = ['username', 'password', 'auth_field', 'auth_field_format'];
|
|
32
|
+
return confirm(propsToUpdate, auth);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const updateStaticTokenAuth = (auth) => {
|
|
36
|
+
const propsToUpdate = ['token', 'auth_field', 'auth_field_format'];
|
|
37
|
+
return confirm(propsToUpdate, auth);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
function updateTokenSchemas(user, pw, token) {
|
|
41
|
+
let schemaPath = path.join(__dirname, '..', 'entities/.system/schemaTokenReq.json');
|
|
42
|
+
const reqSchema = require(schemaPath);
|
|
43
|
+
reqSchema.properties.username.external_name = user;
|
|
44
|
+
reqSchema.properties.password.external_name = pw;
|
|
45
|
+
fs.writeFileSync(schemaPath, JSON.stringify(reqSchema, null, 2));
|
|
46
|
+
schemaPath = path.join(__dirname, '..', 'entities/.system/schemaTokenResp.json');
|
|
47
|
+
const respSchema = require(schemaPath);
|
|
48
|
+
respSchema.properties.token.external_name = token;
|
|
49
|
+
fs.writeFileSync(schemaPath, JSON.stringify(respSchema, null, 2));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function updateRequestToken(auth) {
|
|
53
|
+
const propsToUpdate = [
|
|
54
|
+
'username',
|
|
55
|
+
'password',
|
|
56
|
+
'auth_field',
|
|
57
|
+
'auth_field_format',
|
|
58
|
+
'token_user_field',
|
|
59
|
+
'token_password_field',
|
|
60
|
+
'token_result_field',
|
|
61
|
+
'token_URI_path'
|
|
62
|
+
];
|
|
63
|
+
const newAuth = confirm(propsToUpdate, auth);
|
|
64
|
+
updateTokenSchemas(newAuth.token_user_field, newAuth.token_password_field, newAuth.token_result_field);
|
|
65
|
+
|
|
66
|
+
return newAuth;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// prompt users to pick an auth method from the list above
|
|
70
|
+
const addAuthInfo = (props) => {
|
|
71
|
+
const authOptions = [
|
|
72
|
+
'basic user_password',
|
|
73
|
+
'static_token',
|
|
74
|
+
'request_token',
|
|
75
|
+
'no_authentication'
|
|
76
|
+
];
|
|
77
|
+
const newProps = confirm(['host', 'port', 'base_path'], props);
|
|
78
|
+
|
|
79
|
+
const newAuthMethod = authOptions[rls.keyInSelect(authOptions, 'Which authentication method?')];
|
|
80
|
+
newProps.authentication.auth_method = newAuthMethod;
|
|
81
|
+
|
|
82
|
+
if (newAuthMethod === 'basic user_password') {
|
|
83
|
+
newProps.authentication = updateBasicAuth(newProps.authentication);
|
|
84
|
+
} else if (newAuthMethod === 'static_token') {
|
|
85
|
+
newProps.authentication = updateStaticTokenAuth(newProps.authentication);
|
|
86
|
+
} else if (newAuthMethod === 'request_token') {
|
|
87
|
+
newProps.authentication = updateRequestToken(newProps.authentication);
|
|
88
|
+
}
|
|
89
|
+
console.log('Connectivity and authentication properties have been configured');
|
|
90
|
+
console.log('If you want to make changes, rerun this script to reinstall the adapter');
|
|
91
|
+
return newProps;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
module.exports = { addAuthInfo };
|
package/utils/artifactize.js
CHANGED
package/utils/basicGet.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
/* eslint import/no-extraneous-dependencies: warn */
|
|
5
5
|
/* eslint global-require: warn */
|
|
6
6
|
/* eslint import/no-unresolved: warn */
|
|
7
|
+
/* eslint import/no-dynamic-require: warn */
|
|
7
8
|
|
|
8
9
|
const winston = require('winston');
|
|
9
10
|
|
|
@@ -43,20 +44,6 @@ const basicGet = {
|
|
|
43
44
|
adapter.id,
|
|
44
45
|
adapterProps
|
|
45
46
|
);
|
|
46
|
-
},
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* @summary connect to mongodb
|
|
50
|
-
*
|
|
51
|
-
* @function connect
|
|
52
|
-
* @param {Object} properties - pronghornProps
|
|
53
|
-
*/
|
|
54
|
-
connect: async function connect(properties) {
|
|
55
|
-
// Connect to Mongo
|
|
56
|
-
const { MongoDBConnection } = require('@itential/database');
|
|
57
|
-
const connection = new MongoDBConnection(properties.mongoProps);
|
|
58
|
-
const database = await connection.connect(true);
|
|
59
|
-
return database;
|
|
60
47
|
}
|
|
61
48
|
};
|
|
62
49
|
|
package/utils/checkMigrate.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const { execSync } = require('child_process');
|
|
2
|
+
const fs = require('fs');
|
|
2
3
|
const semver = require('semver');
|
|
3
4
|
const axios = require('axios');
|
|
4
|
-
const fs = require('fs');
|
|
5
5
|
const packageJson = require('../package.json');
|
|
6
6
|
|
|
7
7
|
const localEngineVer = packageJson.engineVersion;
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/* @copyright Itential, LLC 2021 */
|
|
2
|
+
|
|
3
|
+
// Set globals
|
|
4
|
+
/* global log */
|
|
5
|
+
|
|
6
|
+
/* eslint import/no-dynamic-require: warn */
|
|
7
|
+
/* eslint global-require: warn */
|
|
8
|
+
/* eslint no-unused-vars: warn */
|
|
9
|
+
/* eslint import/no-unresolved: warn */
|
|
10
|
+
/* eslint no-promise-executor-return: warn */
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* This script is used to read through an adapter's entities files
|
|
14
|
+
* and then creates documents and enters them into the IAP mongodb
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const fs = require('fs');
|
|
18
|
+
const path = require('path');
|
|
19
|
+
const utils = require('./tbUtils');
|
|
20
|
+
|
|
21
|
+
// get the pronghorn database information
|
|
22
|
+
const getPronghornProps = async (iapDir) => {
|
|
23
|
+
log.trace('Retrieving properties.json file...');
|
|
24
|
+
const rawProps = require(path.join(iapDir, 'properties.json'));
|
|
25
|
+
log.trace('Decrypting properties...');
|
|
26
|
+
const { PropertyEncryption } = require('@itential/itential-utils');
|
|
27
|
+
const propertyEncryption = new PropertyEncryption();
|
|
28
|
+
const pronghornProps = await propertyEncryption.decryptProps(rawProps);
|
|
29
|
+
log.trace('Found properties.\n');
|
|
30
|
+
return pronghornProps;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Function used to take a file path to a entity directory and build
|
|
35
|
+
* a document that corresponds to the entity files.
|
|
36
|
+
*/
|
|
37
|
+
const buildDoc = (pathstring) => {
|
|
38
|
+
let files = fs.readdirSync(pathstring);
|
|
39
|
+
|
|
40
|
+
// load the mockdatafiles
|
|
41
|
+
const mockdatafiles = {};
|
|
42
|
+
if (files.includes('mockdatafiles') && fs.lstatSync(`${pathstring}/mockdatafiles`).isDirectory()) {
|
|
43
|
+
fs.readdirSync(`${pathstring}/mockdatafiles`).forEach((file) => {
|
|
44
|
+
if (file.split('.').pop() === 'json') {
|
|
45
|
+
const mockpath = `${pathstring}/mockdatafiles/${file}`;
|
|
46
|
+
const data = JSON.parse(fs.readFileSync(mockpath));
|
|
47
|
+
mockdatafiles[mockpath.split('/').pop()] = data;
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// load the action data
|
|
53
|
+
let actions;
|
|
54
|
+
if (files.includes('action.json')) {
|
|
55
|
+
actions = JSON.parse(fs.readFileSync(`${pathstring}/action.json`));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Load schema.json and other schemas in remaining json files
|
|
59
|
+
files = files.filter((f) => (f !== 'action.json') && f.endsWith('.json'));
|
|
60
|
+
const schema = [];
|
|
61
|
+
files.forEach((file) => {
|
|
62
|
+
const data = JSON.parse(fs.readFileSync(`${pathstring}/${file}`));
|
|
63
|
+
schema.push({
|
|
64
|
+
name: file,
|
|
65
|
+
schema: data
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// return the data
|
|
70
|
+
return {
|
|
71
|
+
actions: actions.actions,
|
|
72
|
+
schema,
|
|
73
|
+
mockdatafiles
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Function used to get the database from the options or a provided directory
|
|
79
|
+
*/
|
|
80
|
+
const optionsHandler = (options) => {
|
|
81
|
+
// if the database properties were provided in the options - return them
|
|
82
|
+
if (options.pronghornProps) {
|
|
83
|
+
if (typeof options.pronghornProps === 'string') {
|
|
84
|
+
return JSON.parse(options.pronghornProps);
|
|
85
|
+
}
|
|
86
|
+
return new Promise((resolve, reject) => resolve(options.pronghornProps));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// if the directory was provided, get the pronghorn props from the directory
|
|
90
|
+
if (options.iapDir) {
|
|
91
|
+
return getPronghornProps(options.iapDir);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// if nothing was provided, error
|
|
95
|
+
return new Promise((resolve, reject) => reject(new Error('Neither pronghornProps nor iapDir defined in options!')));
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Function used to put the adapter configuration into the provided database
|
|
100
|
+
*/
|
|
101
|
+
const moveEntitiesToDB = async (targetPath, options) => {
|
|
102
|
+
// set local variables
|
|
103
|
+
let myOpts = options;
|
|
104
|
+
let myPath = targetPath;
|
|
105
|
+
|
|
106
|
+
// if we got a string parse into a JSON object
|
|
107
|
+
if (typeof myOpts === 'string') {
|
|
108
|
+
myOpts = JSON.parse(myOpts);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// if there is no target collection - set the collection to the default
|
|
112
|
+
if (!myOpts.targetCollection) {
|
|
113
|
+
myOpts.targetCollection = 'adapter_configs';
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// if there is no id error since we need an id for the entities
|
|
117
|
+
if (!myOpts.id) {
|
|
118
|
+
throw new Error('Adapter ID required!');
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// get the pronghorn database properties
|
|
122
|
+
return optionsHandler(options).then(async (currentProps) => {
|
|
123
|
+
// Check valid filepath provided
|
|
124
|
+
if (!myPath) {
|
|
125
|
+
// if no path use the current directory without the utils
|
|
126
|
+
myPath = path.join(__dirname, '../');
|
|
127
|
+
} else if (myPath.slice(-1) === '/') {
|
|
128
|
+
myPath = myPath.slice(0, -1);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// verify set the entity path
|
|
132
|
+
const entitiesPath = `${myPath}/entities`;
|
|
133
|
+
if (!fs.existsSync(entitiesPath)) {
|
|
134
|
+
throw new Error(`Entities path does not exist in filesystem: ${entitiesPath}`);
|
|
135
|
+
} else {
|
|
136
|
+
log.trace('Target found on filesystem');
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Get adapter details
|
|
140
|
+
if (!fs.existsSync(`${myPath}/pronghorn.json`)) {
|
|
141
|
+
throw new Error(`pronghorn.json does not exist in path: ${myPath}`);
|
|
142
|
+
} else {
|
|
143
|
+
log.trace('pronghorn.json found on filesystem');
|
|
144
|
+
}
|
|
145
|
+
const adapterData = JSON.parse(fs.readFileSync(`${myPath}/pronghorn.json`));
|
|
146
|
+
|
|
147
|
+
// Load files from the filesystem
|
|
148
|
+
const docs = [];
|
|
149
|
+
const entities = fs.readdirSync(entitiesPath);
|
|
150
|
+
entities.forEach((entity) => {
|
|
151
|
+
const entityPath = `${entitiesPath}/${entity}`;
|
|
152
|
+
const isDir = fs.lstatSync(entitiesPath).isDirectory();
|
|
153
|
+
|
|
154
|
+
// Build doc for entity
|
|
155
|
+
if (isDir) {
|
|
156
|
+
let doc = buildDoc(entityPath);
|
|
157
|
+
doc = {
|
|
158
|
+
id: myOpts.id,
|
|
159
|
+
type: adapterData.id,
|
|
160
|
+
entity,
|
|
161
|
+
...doc
|
|
162
|
+
};
|
|
163
|
+
docs.push(doc);
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
// Upload documents to db collection
|
|
168
|
+
const db = await utils.connect(currentProps).catch((err) => { console.error(err); throw err; });
|
|
169
|
+
if (!db) {
|
|
170
|
+
console.error('Error occured when connectiong to database', currentProps);
|
|
171
|
+
throw new Error('Database not found');
|
|
172
|
+
}
|
|
173
|
+
const collection = db.collection(myOpts.targetCollection);
|
|
174
|
+
const res = await collection.insertMany(docs, { checkKeys: false }).catch((err) => { console.error(err); throw err; });
|
|
175
|
+
return res;
|
|
176
|
+
});
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
module.exports = { moveEntitiesToDB };
|
package/utils/findPath.js
CHANGED