-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: feat(custom-js): implement POST /processors (#213)
* docs(image): fix outdated image in docs (#214) * chore(ci/cd): remove circleci docker_layer_caching (#218) * feat(api): add api spec for processors resource (#200) * feat(api): add api spec for processors resource * feat(api): add api spec for processors resource closes #185 fix #185 * fix(typo): typo date_time to date-time * fix(typo): typo date_time to date-time * chore(openapi3): code review comments addressed * docs(api): add download endpoint for processor files * chore(dependencies): update cassandra-driver to v4.1.0 (#210) * chore: fix rebase to master * feat(custom-js): implement POST /processors * test(processors): add tests for POST /processors (#217) * test(processors): add tests for POST /processors * style(processors): fix processors cassandra connector name * feat(processors): add js validation on POST /processors * fix(processors): add updated_at/created_at to processors resource * test(processors): add validateJavascriptContent unit-tests * test(create-test): fix processor file download error message in test
- Loading branch information
Showing
18 changed files
with
414 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/database/cassandra-handler/init-scripts/14__create_processors_table.cql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
CREATE TABLE IF NOT EXISTS processors | ||
( | ||
processor_id uuid, | ||
name text, | ||
description text, | ||
type text, | ||
file_url text, | ||
javascript text, | ||
created_at timestamp, | ||
updated_at timestamp, | ||
PRIMARY KEY (processor_id) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict'; | ||
let processorManager = require('../models/processorsManager'); | ||
|
||
module.exports.createProcessor = function (req, res, next) { | ||
return processorManager.createProcessor(req.body) | ||
.then(function (result) { | ||
return res.status(201).json(result); | ||
}) | ||
.catch(function (err) { | ||
return next(err); | ||
}); | ||
}; |
38 changes: 38 additions & 0 deletions
38
src/processors/models/database/cassandra/cassandraConnector.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
let logger = require('../../../../common/logger'); | ||
let databaseConfig = require('../../../../config/databaseConfig'); | ||
let client; | ||
|
||
const INSERT_PROCESSOR = 'INSERT INTO processors(processor_id, name, description, type, file_url, javascript, created_at, updated_at) values(?,?,?,?,?,?,?,?)'; | ||
|
||
module.exports = { | ||
init, | ||
insertProcessor | ||
}; | ||
|
||
let queryOptions = { | ||
consistency: databaseConfig.cassandraConsistency, | ||
prepare: true | ||
}; | ||
|
||
async function init(cassandraClient) { | ||
client = cassandraClient; | ||
} | ||
|
||
function insertProcessor(processorId, processorInfo) { | ||
let params = [processorId, processorInfo.name, processorInfo.description, processorInfo.type, processorInfo.file_url, processorInfo.javascript, Date.now(), Date.now()]; | ||
return executeQuery(INSERT_PROCESSOR, params, queryOptions); | ||
} | ||
|
||
function executeQuery(query, params, queryOptions) { | ||
return client.execute(query, params, { prepare: true }, queryOptions).then((result) => { | ||
logger.trace('Query result', { | ||
query: query, | ||
params: params, | ||
rows_returned: result.rowLength | ||
}); | ||
return Promise.resolve(result.rows ? result.rows : []); | ||
}).catch((exception) => { | ||
logger.error(`Cassandra query failed \n ${JSON.stringify({ query, params, queryOptions })}`, exception); | ||
return Promise.reject(new Error('Error occurred in communication with cassandra')); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict'; | ||
|
||
let databaseConfig = require('../../../config/databaseConfig'); | ||
let cassandraConnector = require('./cassandra/cassandraConnector'); | ||
let sequelizeConnector = require('./sequelize/sequelizeConnector'); | ||
let databaseConnector = databaseConfig.type.toLowerCase() === 'cassandra' ? cassandraConnector : sequelizeConnector; | ||
|
||
module.exports = { | ||
init, | ||
closeConnection, | ||
insertProcessor | ||
}; | ||
|
||
async function insertProcessor(jobId, jobInfo) { | ||
return databaseConnector.insertProcessor(jobId, jobInfo); | ||
} | ||
|
||
async function init() { | ||
return databaseConnector.init(); | ||
} | ||
|
||
function closeConnection() { | ||
return databaseConnector.closeConnection(); | ||
} |
60 changes: 60 additions & 0 deletions
60
src/processors/models/database/sequelize/sequelizeConnector.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
'use strict'; | ||
|
||
const Sequelize = require('sequelize'); | ||
let client; | ||
|
||
module.exports = { | ||
init, | ||
insertProcessor | ||
}; | ||
|
||
async function init(sequelizeClient) { | ||
client = sequelizeClient; | ||
await initSchemas(); | ||
} | ||
|
||
async function insertProcessor(processorId, processorInfo) { | ||
const processor = client.model('processor'); | ||
let params = { | ||
processor_id: processorId, | ||
name: processorInfo.name, | ||
description: processorInfo.description, | ||
type: processorInfo.type, | ||
file_url: processorInfo.file_url, | ||
javascript: processorInfo.javascript, | ||
created_at: Date.now(), | ||
updated_at: Date.now() | ||
}; | ||
return processor.create(params); | ||
} | ||
|
||
async function initSchemas() { | ||
const processorsFiles = client.define('processor', { | ||
processor_id: { | ||
type: Sequelize.DataTypes.UUID, | ||
primaryKey: true | ||
}, | ||
name: { | ||
type: Sequelize.DataTypes.TEXT('medium') | ||
}, | ||
description: { | ||
type: Sequelize.DataTypes.TEXT('long') | ||
}, | ||
type: { | ||
type: Sequelize.DataTypes.TEXT('medium') | ||
}, | ||
file_url: { | ||
type: Sequelize.DataTypes.TEXT('long') | ||
}, | ||
javascript: { | ||
type: Sequelize.DataTypes.TEXT('long') | ||
}, | ||
created_at: { | ||
type: Sequelize.DataTypes.DATE | ||
}, | ||
updated_at: { | ||
type: Sequelize.DataTypes.DATE | ||
} | ||
}); | ||
await processorsFiles.sync(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict'; | ||
|
||
const uuid = require('uuid'); | ||
|
||
const logger = require('../../common/logger'), | ||
databaseConnector = require('./database/databaseConnector'), | ||
common = require('../../common/consts.js'), | ||
fileManager = require('../../tests/models/fileManager.js'); | ||
|
||
module.exports.createProcessor = async function (processor) { | ||
let processorId = uuid.v4(); | ||
try { | ||
if (processor.type === common.PROCESSOR_TYPE_FILE_DOWNLOAD) { | ||
processor.javascript = await fileManager.downloadFile(processor.file_url); | ||
} | ||
fileManager.validateJavascriptContent(processor.javascript); | ||
await databaseConnector.insertProcessor(processorId, processor); | ||
logger.info('Processor saved successfully to database'); | ||
return processor; | ||
} catch (error) { | ||
logger.error(error, 'Error occurred trying to create new processor'); | ||
return Promise.reject(error); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
let swaggerValidator = require('express-ajv-swagger-validation'); | ||
let express = require('express'); | ||
let router = express.Router(); | ||
|
||
let processors = require('../controllers/processorController'); | ||
|
||
router.post('/', swaggerValidator.validate, processors.createProcessor); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
tests/integration-tests/processors/helpers/requestCreator.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
const request = require('supertest'), | ||
expressApp = require('../../../../src/app'); | ||
let app; | ||
module.exports = { | ||
init, | ||
createProcessor | ||
}; | ||
async function init() { | ||
try { | ||
app = await expressApp(); | ||
} catch (err){ | ||
console.log(err); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
function createProcessor(body, headers) { | ||
return request(app).post('/v1/processors') | ||
.send(body) | ||
.set(headers) | ||
.expect(function(res){ | ||
return res; | ||
}); | ||
} |
Oops, something went wrong.