@itentialopensource/adapter-winston_syslog 1.0.1

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.
Files changed (45) hide show
  1. package/.eslintignore +5 -0
  2. package/.eslintrc.js +18 -0
  3. package/.jshintrc +3 -0
  4. package/CHANGELOG.md +9 -0
  5. package/LICENSE +201 -0
  6. package/README.md +285 -0
  7. package/adapter.js +250 -0
  8. package/adapterBase.js +1782 -0
  9. package/entities/.generic/action.json +214 -0
  10. package/entities/.generic/schema.json +28 -0
  11. package/entities/.system/action.json +50 -0
  12. package/entities/.system/mockdatafiles/getToken-default.json +3 -0
  13. package/entities/.system/mockdatafiles/healthcheck-default.json +3 -0
  14. package/entities/.system/schema.json +19 -0
  15. package/entities/.system/schemaTokenReq.json +53 -0
  16. package/entities/.system/schemaTokenResp.json +53 -0
  17. package/error.json +190 -0
  18. package/package.json +87 -0
  19. package/pronghorn.json +93 -0
  20. package/propertiesDecorators.json +14 -0
  21. package/propertiesSchema.json +1243 -0
  22. package/refs?service=git-upload-pack +0 -0
  23. package/report/adapterInfo.json +10 -0
  24. package/sampleProperties.json +60 -0
  25. package/test/integration/adapterTestIntegration.js +329 -0
  26. package/test/unit/adapterBaseTestUnit.js +949 -0
  27. package/test/unit/adapterTestUnit.js +1207 -0
  28. package/utils/adapterInfo.js +206 -0
  29. package/utils/addAuth.js +94 -0
  30. package/utils/artifactize.js +146 -0
  31. package/utils/basicGet.js +50 -0
  32. package/utils/checkMigrate.js +63 -0
  33. package/utils/entitiesToDB.js +179 -0
  34. package/utils/findPath.js +74 -0
  35. package/utils/modify.js +154 -0
  36. package/utils/packModificationScript.js +35 -0
  37. package/utils/patches2bundledDeps.js +90 -0
  38. package/utils/pre-commit.sh +30 -0
  39. package/utils/removeHooks.js +20 -0
  40. package/utils/setup.js +33 -0
  41. package/utils/tbScript.js +184 -0
  42. package/utils/tbUtils.js +469 -0
  43. package/utils/testRunner.js +298 -0
  44. package/utils/troubleshootingAdapter.js +190 -0
  45. package/workflows/README.md +3 -0
package/adapter.js ADDED
@@ -0,0 +1,250 @@
1
+ /* @copyright Itential, LLC 2019 (pre-modifications) */
2
+
3
+ /* eslint import/no-dynamic-require: warn */
4
+ /* eslint object-curly-newline: warn */
5
+ /* eslint no-unused-expressions: warn */
6
+
7
+ // Set globals
8
+ /* global log */
9
+
10
+ /* Required libraries. */
11
+ const path = require('path');
12
+
13
+ /* Fetch in the other needed components for the this Adaptor */
14
+ const AdapterBaseCl = require(path.join(__dirname, 'adapterBase.js'));
15
+
16
+ /* Require winston_syslog from dependencies. */
17
+ const winston = require('winston');
18
+ require('winston-syslog').Syslog;
19
+
20
+ /**
21
+ * This is the adapter/interface into Winston_syslog
22
+ */
23
+
24
+ /* GENERAL ADAPTER FUNCTIONS */
25
+ class WinstonSyslog extends AdapterBaseCl {
26
+ /**
27
+ * WinstonSyslog Adapter
28
+ * @constructor
29
+ */
30
+ /* Working on changing the way we do Emit methods due to size and time constrainsts
31
+ constructor(prongid, properties) {
32
+ // Instantiate the AdapterBase super class
33
+ super(prongid, properties);
34
+
35
+ const restFunctionNames = this.iapGetAdapterWorkflowFunctions();
36
+
37
+ // Dynamically bind emit functions
38
+ for (let i = 0; i < restFunctionNames.length; i += 1) {
39
+ // Bind function to have name fnNameEmit for fnName
40
+ const version = restFunctionNames[i].match(/__v[0-9]+/);
41
+ const baseFnName = restFunctionNames[i].replace(/__v[0-9]+/, '');
42
+ const fnNameEmit = version ? `${baseFnName}Emit${version}` : `${baseFnName}Emit`;
43
+ this[fnNameEmit] = function (...args) {
44
+ // extract the callback
45
+ const callback = args[args.length - 1];
46
+ // slice the callback from args so we can insert our own
47
+ const functionArgs = args.slice(0, args.length - 1);
48
+ // create a random name for the listener
49
+ const eventName = `${restFunctionNames[i]}:${Math.random().toString(36)}`;
50
+ // tell the calling class to start listening
51
+ callback({ event: eventName, status: 'received' });
52
+ // store parent for use of this context later
53
+ const parent = this;
54
+ // store emission function
55
+ const func = function (val, err) {
56
+ parent.removeListener(eventName, func);
57
+ parent.emit(eventName, val, err);
58
+ };
59
+ // Use apply to call the function in a specific context
60
+ this[restFunctionNames[i]].apply(this, functionArgs.concat([func])); // eslint-disable-line prefer-spread
61
+ };
62
+ }
63
+
64
+ // Uncomment if you have things to add to the constructor like using your own properties.
65
+ // Otherwise the constructor in the adapterBase will be used.
66
+ // Capture my own properties - they need to be defined in propertiesSchema.json
67
+ // if (this.allProps && this.allProps.myownproperty) {
68
+ // mypropvariable = this.allProps.myownproperty;
69
+ // }
70
+ }
71
+ */
72
+
73
+ /**
74
+ * @callback healthCallback
75
+ * @param {Object} reqObj - the request to send into the healthcheck
76
+ * @param {Callback} callback - The results of the call
77
+ */
78
+ healthCheck(reqObj, callback) {
79
+ // you can modify what is passed into the healthcheck by changing things in the newReq
80
+ let newReq = null;
81
+ if (reqObj) {
82
+ newReq = Object.assign(...reqObj);
83
+ }
84
+ super.healthCheck(newReq, callback);
85
+ }
86
+
87
+ /**
88
+ * @callback healthCallback
89
+ * @param {Object} result - the result of the get request (contains an id and a status)
90
+ */
91
+ /**
92
+ * @callback getCallback
93
+ * @param {Object} result - the result of the get request (entity/ies)
94
+ * @param {String} error - any error that occurred
95
+ */
96
+ /**
97
+ * @callback createCallback
98
+ * @param {Object} item - the newly created entity
99
+ * @param {String} error - any error that occurred
100
+ */
101
+ /**
102
+ * @callback updateCallback
103
+ * @param {String} status - the status of the update action
104
+ * @param {String} error - any error that occurred
105
+ */
106
+ /**
107
+ * @callback deleteCallback
108
+ * @param {String} status - the status of the delete action
109
+ * @param {String} error - any error that occurred
110
+ */
111
+
112
+ /**
113
+ * @function postLog
114
+ * @pronghornType method
115
+ * @name postLog
116
+ * @summary Post a log message
117
+ *
118
+ * @param {string} severity - The log level of the message. One of: (
119
+ * debug, info, notice, warning, err, crit, alert, emerg)
120
+ * @param {string} facility - Where to send the log message
121
+ * @param {string} message - The text body of the log message
122
+ * @param {getCallback} callback - a callback function to return the result
123
+ * @return {object} result - A JSON Object containing status, code, and the result
124
+ *
125
+ * @roles admin
126
+ * @task true
127
+ */
128
+ /* YOU CAN CHANGE THE PARAMETERS YOU TAKE IN HERE AND IN THE pronghorn.json FILE */
129
+ postLog(severity, facility, message, callback) {
130
+ const meth = 'adapter-postLog';
131
+ const origin = `${this.id}-${meth}`;
132
+ log.trace(origin);
133
+
134
+ if (this.suspended && this.suspendMode === 'error') {
135
+ const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'AD.600', [], null, null, null);
136
+ log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
137
+ return callback(null, errorObj);
138
+ }
139
+
140
+ /* HERE IS WHERE YOU VALIDATE DATA */
141
+ if (severity === undefined || severity === null || severity === '') {
142
+ const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Missing Data', ['severity'], null, null, null);
143
+ log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
144
+ return callback(null, errorObj);
145
+ }
146
+
147
+ if (facility === undefined || facility === null || facility === '') {
148
+ const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Missing Data', ['facility'], null, null, null);
149
+ log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
150
+ return callback(null, errorObj);
151
+ }
152
+
153
+ if (message === undefined || message === null || message === '') {
154
+ const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Missing Data', ['message'], null, null, null);
155
+ log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
156
+ return callback(null, errorObj);
157
+ }
158
+
159
+ const validSev = [
160
+ 'debug',
161
+ 'info',
162
+ 'notice',
163
+ 'warning',
164
+ 'error',
165
+ 'crit',
166
+ 'alert',
167
+ 'emerg'
168
+ ];
169
+
170
+ if (!validSev.includes(severity)) {
171
+ const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Invalid input: invalid severity', ['postLog'], null, null, null);
172
+ log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
173
+ return callback(null, errorObj);
174
+ }
175
+
176
+ const validFacilities = [
177
+ 'local0',
178
+ 'local1',
179
+ 'local2',
180
+ 'local3',
181
+ 'local4',
182
+ 'local5',
183
+ 'local6',
184
+ 'local7'
185
+ ];
186
+
187
+ if (!validFacilities.includes(facility)) {
188
+ const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Invalid input: invalid facility', ['postLog'], null, null, null);
189
+ log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
190
+ return callback(null, errorObj);
191
+ }
192
+
193
+ if (typeof message !== 'string') {
194
+ const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Invalid input: message must be a string', ['postLog'], null, null, null);
195
+ log.error(`${origin}: ${errorObj.IAPerror.displayString}`);
196
+ return callback(null, errorObj);
197
+ }
198
+
199
+ /* HERE IS WHERE YOU SET THE DATA TO PASS INTO REQUEST */
200
+ try {
201
+ // Make the call -
202
+ log.debug(`${origin} - Inside winston_syslog try`);
203
+ const {
204
+ host,
205
+ port,
206
+ logProtocol,
207
+ type,
208
+ minLogLevel
209
+ } = this.allProps;
210
+
211
+ log.debug(`${origin} - Got winston_syslog props`);
212
+
213
+ const transport = new winston.transports.Syslog({
214
+ host,
215
+ port,
216
+ type,
217
+ facility,
218
+ protocol: logProtocol,
219
+ localhost: 'Itential'
220
+ });
221
+ log.debug(`${origin} - Created winston_syslog transport`);
222
+
223
+ const logger = winston.createLogger({
224
+ levels: winston.config.syslog.levels,
225
+ level: minLogLevel || 'info',
226
+ transports: [transport]
227
+ });
228
+
229
+ log.debug(`${origin} - Created winston_syslog logger`);
230
+
231
+ logger[severity](message);
232
+ log.debug(`${origin} - Sent winston log message`);
233
+ const successObj = {
234
+ status: 'success',
235
+ code: 200,
236
+ facility
237
+ };
238
+ transport.close();
239
+ log.debug(`${origin} - Closed winston_syslog transport`);
240
+ // return the response
241
+ return callback(successObj, null);
242
+ } catch (ex) {
243
+ const errorObj = this.requestHandlerInst.formatErrorObject(this.id, meth, 'Caught Exception', null, null, null, ex);
244
+ log.error(`${origin}: ${errorObj.IAPerror.stack}`);
245
+ return callback(null, errorObj);
246
+ }
247
+ }
248
+ }
249
+
250
+ module.exports = WinstonSyslog;