From 06aa8717fe09659c88a4df6286db41196ad8e308 Mon Sep 17 00:00:00 2001 From: Pankov Date: Thu, 25 Oct 2018 15:38:53 +0300 Subject: [PATCH 01/15] microservice validation refactoring --- src/helpers/error-messages.js | 2 +- .../managers/microservice-manager.js | 27 +++++- src/sequelize/managers/user-manager.js | 79 ---------------- src/sequelize/models/user.js | 8 -- src/services/microservices-service.js | 91 ++++++------------- 5 files changed, 56 insertions(+), 151 deletions(-) diff --git a/src/helpers/error-messages.js b/src/helpers/error-messages.js index dbc0b9417..ec2317486 100644 --- a/src/helpers/error-messages.js +++ b/src/helpers/error-messages.js @@ -38,8 +38,8 @@ module.exports = { EXPIRED_PROVISION_KEY: 'Expired provision key', VERSION_COMMAND_NOT_FOUND: 'Version command not found', STRACE_WITHOUT_FOG: 'Can not run strace for element without fog.', - INVALID_MICROSERVICE: 'Microservice is not valid. Possible reasons: incorrect user, catalog item, flow or fog node.', INVALID_MICROSERVICE_CONFIG: "Can't create network microservice without appropriate configuration.", + INVALID_MICROSERVICE_USER: 'Current user is not allowed to create this microservice on the flow', ROUTE_NOT_FOUND: 'Route not found', INVALID_ACTION_PROPERTY: 'Unknown action property. Action can be "open" or "close"', IMAGE_SNAPSHOT_WITHOUT_FOG: 'Can not run image snapshot for element without fog.', diff --git a/src/sequelize/managers/microservice-manager.js b/src/sequelize/managers/microservice-manager.js index 46f819e8c..a9b33f54c 100644 --- a/src/sequelize/managers/microservice-manager.js +++ b/src/sequelize/managers/microservice-manager.js @@ -20,6 +20,8 @@ const StraceDiagnostics = models.StraceDiagnostics; const CatalogItem = models.CatalogItem; const CatalogItemImage = models.CatalogItemImage; const Fog = models.Fog; +const Flow = models.Flow; +const User = models.User; const Routing = models.Routing; class MicroserviceManager extends BaseManager { @@ -131,7 +133,7 @@ class MicroserviceManager extends BaseManager { as: 'destMicroservice', attributes: ['uuid'] }], - attributes: {exclude: ['id', 'source_microservice_uuid', + attributes: {exclude: ['id', 'sourceMicroserviceUuid', 'destMicroserviceUuid', 'sourceNetworkMicroserviceUuid', 'destNetworkMicroserviceUuid', 'sourceIofogUuid', 'destIofogUuid', 'connectorPortId']} @@ -141,6 +143,29 @@ class MicroserviceManager extends BaseManager { attributes: attributes }, {transaction: transaction}) } + + findMicroserviceOnGet(where, transaction) { + return Microservice.findOne({ + include: [ + { + model: Flow, + as: 'flow', + required: true, + include: [ + { + model: User, + as: 'user', + required: true, + attributes: ['id'] + } + ], + attributes: ['id'] + } + ], + where: where, + attributes: ['uuid'] + }, {transaction: transaction}) + } } const instance = new MicroserviceManager(); diff --git a/src/sequelize/managers/user-manager.js b/src/sequelize/managers/user-manager.js index 4d1d495d8..d5995d2ca 100644 --- a/src/sequelize/managers/user-manager.js +++ b/src/sequelize/managers/user-manager.js @@ -94,85 +94,6 @@ class UserManager extends BaseManager { findById(id) { return User.find({where: {id: id}}); } - - findUserOnMicroserviceCreate(where, transaction) { - return User.findOne({ - include: [ - { - model: CatalogItem, - as: 'catalogItem', - required: true, - attributes: ['id'] - }, - { - model: Flow, - as: 'flow', - required: true, - attributes: ['id'] - }, - { - model: Fog, - as: 'fog', - required: false, - attributes: ['uuid'] - } - ], - where: where, - attributes: ['id'] - }, {transaction: transaction}) - } - - findUserOnMicroserviceGet(where, transaction) { - return User.findOne({ - include: [ - { - model: Flow, - as: 'flow', - required: true, - include: [ - { - model: Microservice, - as: 'microservice', - required: true, - attributes: ['uuid'] - } - ], - attributes: ['id'] - } - ], - where: where, - attributes: ['id'], - }, {transaction: transaction}) - } - - findUserOnMicroserviceUpdate(where, transaction) { - return User.findOne({ - include: [ - { - model: Fog, - as: 'fog', - required: false, - attributes: ['uuid'] - }, - { - model: Flow, - as: 'flow', - required: true, - include: [ - { - model: Microservice, - as: 'microservice', - required: true, - attributes: ['uuid'] - } - ], - attributes: ['id'] - } - ], - where: where, - attributes: ['id'] - }, {transaction: transaction}) - } } const instance = new UserManager(); diff --git a/src/sequelize/models/user.js b/src/sequelize/models/user.js index 25a2457c1..519535f4b 100644 --- a/src/sequelize/models/user.js +++ b/src/sequelize/models/user.js @@ -47,14 +47,6 @@ module.exports = (sequelize, DataTypes) => { as: 'accessToken' }); - User.hasMany(models.CatalogItem, { - foreignKey: { - name: 'userId', - field: 'user_id' - }, - as: 'catalogItem' - }); - User.hasMany(models.Flow, { foreignKey: { name: 'userId', diff --git a/src/services/microservices-service.js b/src/services/microservices-service.js index 2b3b0f102..89239c70d 100644 --- a/src/services/microservices-service.js +++ b/src/services/microservices-service.js @@ -15,18 +15,18 @@ const TransactionDecorator = require('../decorators/transaction-decorator'); const MicroserviceManager = require('../sequelize/managers/microservice-manager'); const MicroservicePortManager = require('../sequelize/managers/microservice-port-manager'); const VolumeMappingManager = require('../sequelize/managers/volume-mapping-manager'); -const ConnectorManager = require('../sequelize/managers/connector-manager') -const ConnectorPortManager = require('../sequelize/managers/connector-port-manager') -const MicroservicePublicModeManager = require('../sequelize/managers/microservice-public-mode-manager') +const ConnectorManager = require('../sequelize/managers/connector-manager'); +const ConnectorPortManager = require('../sequelize/managers/connector-port-manager'); +const MicroservicePublicModeManager = require('../sequelize/managers/microservice-public-mode-manager'); const ChangeTrackingManager = require('../sequelize/managers/change-tracking-manager'); -const UserManager = require('../sequelize/managers/user-manager'); const FlowService = require('../services/flow-service'); +const IoFogService = require('../services/iofog-service'); const AppHelper = require('../helpers/app-helper'); const Errors = require('../helpers/errors'); const ErrorMessages = require('../helpers/error-messages'); const Validation = require('../schemas/index'); -const ConnectorService = require('../services/connector-service') -const CatalogService = require('../services/catalog-service') +const ConnectorService = require('../services/connector-service'); +const CatalogService = require('../services/catalog-service'); const _getMicroserviceByFlow = async function (flowId, user, isCLI, transaction) { await FlowService.getFlow(flowId, user, isCLI, transaction); @@ -48,7 +48,7 @@ const _getMicroserviceByFlow = async function (flowId, user, isCLI, transaction) }; const _getMicroservice = async function (microserviceUuid, user, isCLI, transaction) { - await _checkIfMicroserviceIsValidOnGet(user.id, microserviceUuid, transaction); + await _validateMicroserviceOnGet(user.id, microserviceUuid, transaction); return await MicroserviceManager.findOneWithDependencies({ uuid: microserviceUuid @@ -67,7 +67,7 @@ const _getMicroservice = async function (microserviceUuid, user, isCLI, transact const _createMicroserviceOnFog = async function (microserviceData, user, isCLI, transaction) { await Validation.validate(microserviceData, Validation.schemas.microserviceCreate); - const microservice = await _createMicroservice(microserviceData, user, transaction); + const microservice = await _createMicroservice(microserviceData, user, isCLI, transaction); if (microserviceData.ports) { //TODO _createPortMapping for each port pair @@ -90,7 +90,7 @@ const _createMicroserviceOnFog = async function (microserviceData, user, isCLI, } }; -const _createMicroservice = async function (microserviceData, user, transaction) { +const _createMicroservice = async function (microserviceData, user, isCLI, transaction) { if(microserviceData.isNetwork) { if (microserviceData.config !== undefined) { @@ -114,7 +114,14 @@ const _createMicroservice = async function (microserviceData, user, transaction) const microserviceDataCreate = AppHelper.deleteUndefinedFields(microserviceToCreate); - await _checkIfMicroserviceIsValidOnCreate(microserviceDataCreate, user.id, transaction); + //validate catalog item + await CatalogService.listCatalogItem(microserviceDataCreate.catalogItemId, user, isCLI, transaction); + //validate flow + await FlowService.getFlow(microserviceDataCreate.flowId, user, isCLI, transaction); + //validate fog node + if (microserviceDataCreate.iofogUuid) { + await IoFogService.getFog({uuid: microserviceDataCreate.iofogUuid}, user, isCLI, transaction); + } return await MicroserviceManager.create(microserviceDataCreate, transaction); }; @@ -148,8 +155,6 @@ const _createRoutes = async function (routes, microserviceUuid, user, transactio }; const _updateMicroservice = async function (microserviceUuid, microserviceData, user, isCLI, transaction) { - const microservice = await _getMicroservice(microserviceUuid, user, isCLI, transaction); - await Validation.validate(microserviceData, Validation.schemas.microserviceUpdate); if(microserviceData.isNetwork) { @@ -174,7 +179,12 @@ const _updateMicroservice = async function (microserviceUuid, microserviceData, const microserviceDataUpdate = AppHelper.deleteUndefinedFields(microserviceToUpdate); - await _checkIfMicroserviceIsValidOnUpdate(user.id, microserviceUuid, microserviceDataUpdate, transaction); + const microservice = await _getMicroservice(microserviceUuid, user, isCLI, transaction); + + //validate fog node + if (microserviceDataCreate.iofogUuid) { + await IoFogService.getFog({uuid: microserviceDataCreate.iofogUuid}, user, isCLI, transaction); + } await MicroserviceManager.update({ uuid: microserviceUuid @@ -240,57 +250,14 @@ const _deleteRoutes = async function(routes, microserviceUuid, user, transaction } }; -const _checkIfMicroserviceIsValidOnGet = async function (userId, microserviceUuid, transaction) { +const _validateMicroserviceOnGet = async function (userId, microserviceUuid, transaction) { const where = { - id: userId, - '$flow->microservice.uuid$': microserviceUuid + '$flow.user.id$': userId, + uuid: microserviceUuid }; - const user = await UserManager.findUserOnMicroserviceGet(where, transaction) - if (!user) { - throw new Errors.ValidationError(ErrorMessages.INVALID_MICROSERVICE); - } -}; - -const _checkIfMicroserviceIsValidOnUpdate = async function (userId, microserviceUuid, microserviceDataUpdate, transaction) { - let where; - if (microserviceDataUpdate.iofogUuid) { - where = { - id: userId, - '$flow->microservice.uuid$': microserviceUuid, - '$fog.uuid$': microserviceDataUpdate.iofogUuid - } - } else { - where = { - id: userId, - '$flow->microservice.uuid$': microserviceUuid, - } - } - const user = UserManager.findUserOnMicroserviceUpdate(where, transaction); - if (!user) { - throw new Errors.ValidationError(ErrorMessages.INVALID_MICROSERVICE); - } -}; - -const _checkIfMicroserviceIsValidOnCreate = async function (microserviceDataCreate, userId, transaction) { - let where; - if (microserviceDataCreate.iofogUuid) { - where = { - id: userId, - '$catalogItem.id$': microserviceDataCreate.catalogItemId, - '$flow.id$': microserviceDataCreate.flowId, - '$fog.uuid$': microserviceDataCreate.iofogUuid - } - } else { - where = { - id: userId, - '$catalogItem.id$': microserviceDataCreate.catalogItemId, - '$flow.id$': microserviceDataCreate.flowId - } - } - - const user = await UserManager.findUserOnMicroserviceCreate(where, transaction); - if (!user) { - throw new Errors.ValidationError(ErrorMessages.INVALID_MICROSERVICE); + const microservice = await MicroserviceManager.findMicroserviceOnGet(where, transaction); + if (!microservice) { + throw new Errors.ValidationError(ErrorMessages.INVALID_MICROSERVICE_USER); } }; From 93622c1dff2be91f1af4a3f3dff0be756ff13e7d Mon Sep 17 00:00:00 2001 From: Pankov Date: Thu, 25 Oct 2018 16:08:06 +0300 Subject: [PATCH 02/15] Merge branch 'new-fog-controller' of https://github.com/ioFog/FogController into new-fog-controller # Conflicts: # src/services/microservices-service.js --- src/services/microservices-service.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/services/microservices-service.js b/src/services/microservices-service.js index 170c2cc27..938be77d6 100644 --- a/src/services/microservices-service.js +++ b/src/services/microservices-service.js @@ -66,6 +66,7 @@ const _getMicroservice = async function (microserviceUuid, user, isCLI, transact if (!microservice) { throw new Errors.NotFoundError(AppHelper.formatMessage(ErrorMessages.INVALID_MICROSERVICE_UUID, microserviceUuid)) } + return microservice; }; const _createMicroserviceOnFog = async function (microserviceData, user, isCLI, transaction) { @@ -185,13 +186,13 @@ const _updateMicroservice = async function (microserviceUuid, microserviceData, const microservice = await _getMicroservice(microserviceUuid, user, isCLI, transaction); - if(microserviceDataUpdate.name){ - await isMicroserviceExist(microserviceDataUpdate.name, transaction); - } + // if(microserviceDataUpdate.name){ + // await isMicroserviceExist(microserviceDataUpdate.name, transaction); + // } //validate fog node - if (microserviceDataCreate.iofogUuid) { - await IoFogService.getFog({uuid: microserviceDataCreate.iofogUuid}, user, isCLI, transaction); + if (microserviceDataUpdate.iofogUuid) { + await IoFogService.getFog({uuid: microserviceDataUpdate.iofogUuid}, user, isCLI, transaction); } await MicroserviceManager.update({ From 0b7d6650397aa05529e8a2afe3df6409c01149cc Mon Sep 17 00:00:00 2001 From: Pankov Date: Thu, 25 Oct 2018 16:25:29 +0300 Subject: [PATCH 03/15] Merge branch 'new-fog-controller' of https://github.com/ioFog/FogController into new-fog-controller # Conflicts: # src/services/microservices-service.js --- src/services/microservices-service.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/microservices-service.js b/src/services/microservices-service.js index 7d8878edb..49c0c3ced 100644 --- a/src/services/microservices-service.js +++ b/src/services/microservices-service.js @@ -261,7 +261,7 @@ const _validateMicroserviceOnGet = async function (userId, microserviceUuid, tra }; const microservice = await MicroserviceManager.findMicroserviceOnGet(where, transaction); if (!microservice) { - throw new Errors.ValidationError(ErrorMessages.INVALID_MICROSERVICE_USER); + throw new Errors.NotFoundError(ErrorMessages.INVALID_MICROSERVICE_USER); } }; From c0755e495626b7ae4b630c6dbef3e9237d5b01e4 Mon Sep 17 00:00:00 2001 From: Pankov Date: Thu, 25 Oct 2018 17:27:18 +0300 Subject: [PATCH 04/15] Merge branch 'new-fog-controller' of https://github.com/ioFog/FogController into new-fog-controller # Conflicts: # src/services/microservices-service.js --- src/cli/catalog.js | 2 +- src/controllers/catalog-controller.js | 2 +- src/helpers/error-messages.js | 2 +- src/services/catalog-service.js | 4 ++-- src/services/microservices-service.js | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/cli/catalog.js b/src/cli/catalog.js index 1d41338e3..635cc103a 100644 --- a/src/cli/catalog.js +++ b/src/cli/catalog.js @@ -166,7 +166,7 @@ const _listCatalogItems = async function () { const _listCatalogItem = async function (obj) { logger.info(JSON.stringify(obj)); - const result = await CatalogItemService.listCatalogItem(obj.itemId, {}, true); + const result = await CatalogItemService.getCatalogItem(obj.itemId, {}, true); logger.info(JSON.stringify(result)); }; diff --git a/src/controllers/catalog-controller.js b/src/controllers/catalog-controller.js index fa47a6441..9a0791cf6 100644 --- a/src/controllers/catalog-controller.js +++ b/src/controllers/catalog-controller.js @@ -26,7 +26,7 @@ const listCatalogItemsEndPoint = async function (req, user) { const listCatalogItemEndPoint = async function (req, user) { logger.info("Catalog item id: " + req.params.id); - return await CatalogService.listCatalogItem(req.params.id, user, false); + return await CatalogService.getCatalogItem(req.params.id, user, false); }; const deleteCatalogItemEndPoint = async function (req, user) { diff --git a/src/helpers/error-messages.js b/src/helpers/error-messages.js index ec2317486..129b44a42 100644 --- a/src/helpers/error-messages.js +++ b/src/helpers/error-messages.js @@ -39,7 +39,7 @@ module.exports = { VERSION_COMMAND_NOT_FOUND: 'Version command not found', STRACE_WITHOUT_FOG: 'Can not run strace for element without fog.', INVALID_MICROSERVICE_CONFIG: "Can't create network microservice without appropriate configuration.", - INVALID_MICROSERVICE_USER: 'Current user is not allowed to create this microservice on the flow', + INVALID_MICROSERVICE_USER: 'Invalid microservice user', ROUTE_NOT_FOUND: 'Route not found', INVALID_ACTION_PROPERTY: 'Unknown action property. Action can be "open" or "close"', IMAGE_SNAPSHOT_WITHOUT_FOG: 'Can not run image snapshot for element without fog.', diff --git a/src/services/catalog-service.js b/src/services/catalog-service.js index b66698f00..7c5afbd4d 100644 --- a/src/services/catalog-service.js +++ b/src/services/catalog-service.js @@ -120,7 +120,7 @@ const listCatalogItems = async function (user, isCLI, transaction) { return await CatalogItemManager.findAllWithDependencies(where, attributes, transaction); }; -const listCatalogItem = async function (id, user, isCLI, transaction) { +const getCatalogItem = async function (id, user, isCLI, transaction) { const where = isCLI ? {id: id} : {[Op.or]: [{userId: user.id}, {userId: null}], id: id}; @@ -279,7 +279,7 @@ async function getHalCatalogItem(transaction) { module.exports = { createCatalogItem: TransactionDecorator.generateTransaction(createCatalogItem), listCatalogItems: TransactionDecorator.generateTransaction(listCatalogItems), - listCatalogItem: TransactionDecorator.generateTransaction(listCatalogItem), + getCatalogItem: TransactionDecorator.generateTransaction(getCatalogItem), deleteCatalogItem: TransactionDecorator.generateTransaction(deleteCatalogItem), updateCatalogItem: TransactionDecorator.generateTransaction(updateCatalogItem), getNetworkCatalogItem: getNetworkCatalogItem, diff --git a/src/services/microservices-service.js b/src/services/microservices-service.js index 49c0c3ced..d68231dc9 100644 --- a/src/services/microservices-service.js +++ b/src/services/microservices-service.js @@ -115,7 +115,7 @@ const _createMicroservice = async function (microserviceData, user, isCLI, trans const microserviceDataCreate = AppHelper.deleteUndefinedFields(microserviceToCreate); //validate catalog item - await CatalogService.listCatalogItem(microserviceDataCreate.catalogItemId, user, isCLI, transaction); + await CatalogService.getCatalogItem(microserviceDataCreate.catalogItemId, user, isCLI, transaction); //validate flow await FlowService.getFlow(microserviceDataCreate.flowId, user, isCLI, transaction); //validate fog node From 1fce81b25ac0d853dee6017837e1283b3cea0791 Mon Sep 17 00:00:00 2001 From: Pankov Date: Fri, 26 Oct 2018 14:34:26 +0300 Subject: [PATCH 05/15] cli microservice wip --- src/cli/microservice.js | 90 ++++++++++++++++++++++++++++++++++--- src/schemas/microservice.js | 2 - 2 files changed, 83 insertions(+), 9 deletions(-) diff --git a/src/cli/microservice.js b/src/cli/microservice.js index 699ba7251..6cd57557c 100644 --- a/src/cli/microservice.js +++ b/src/cli/microservice.js @@ -11,8 +11,13 @@ * */ -const BaseCLIHandler = require('./base-cli-handler') -const constants = require('../helpers/constants') +const BaseCLIHandler = require('./base-cli-handler'); +const constants = require('../helpers/constants'); +const logger = require('../logger'); +const MicroserviceService = require('../services/microservices-service'); +const fs = require('fs'); +const AppHelper = require('../helpers/app-helper'); +const AuthDecorator = require('../decorators/cli-decorator'); const JSON_SCHEMA = ` name: string @@ -111,7 +116,7 @@ class Microservice extends BaseCLIHandler { { name: 'user-id', alias: 'u', type: Number, description: 'User\'s id', group: [constants.CMD_ADD] - }, + } ] this.commands = { [constants.CMD_ADD]: 'Add a new microservice.', @@ -124,14 +129,16 @@ class Microservice extends BaseCLIHandler { } } - run(args) { + async run(args) { const microserviceCommand = this.parseCommandLineArgs(this.commandDefinitions, { argv: args.argv }) switch (microserviceCommand.command.command) { case constants.CMD_ADD: - return + await _executeCase(microserviceCommand, constants.CMD_ADD, _createMicroservice, false); + break; case constants.CMD_UPDATE: - return + await _executeCase(microserviceCommand, constants.CMD_UPDATE, _updateMicroservice, false); + break; case constants.CMD_REMOVE: return case constants.CMD_LIST: @@ -188,4 +195,73 @@ class Microservice extends BaseCLIHandler { } } -module.exports = new Microservice() \ No newline at end of file +const _executeCase = async function (microserviceCommand, commandName, f, isUserRequired) { + try { + const item = microserviceCommand[commandName]; + + if (isUserRequired) { + const decoratedFunction = AuthDecorator.prepareUserById(f); + decoratedFunction(item); + } else { + f(item); + } + } catch (error) { + logger.error(error.message); + } +}; + +const _createMicroservice = async function (obj) { + const microservice = obj.file + ? JSON.parse(fs.readFileSync(obj.file, 'utf8')) + : _createMicroserviceObject(obj); + + logger.info(JSON.stringify(microservice)); + + const result = await MicroserviceService.createMicroserviceOnFogWithTransaction(microservice, {}, true); + logger.info(JSON.stringify(result)); + logger.info('Microservice has been created successfully.'); +}; + +const _updateMicroservice = async function (obj) { + +} + +const _createMicroserviceObject = function (obj) { + const microserviceObj = { + name: obj.name, + config: obj.config, + catalogItemId: parseInt(obj.catalogId), + flowId: parseInt(obj.flowId), + ioFogNodeId: obj.iofogId, + rootHostAccess: AppHelper.validateBooleanCliOptions(obj.rootEnable, obj.rootDisable), + logLimit: obj.logLimit, + routes: obj.routes + }; + + if (obj.volumes) { + microserviceObj.volumeMappings = parseObjectArray(obj.volumes, 'Error during parsing of volume mapping option.'); + } + if (obj.ports) { + microserviceObj.ports = parseObjectArray(obj.ports, 'Error during parsing of port mapping option.'); + } + + return AppHelper.deleteUndefinedFields(microserviceObj); +}; + + + +const parseObjectArray = function (arr, errMsg) { + return arr.map(item => { + item = item.replace(/'/g, '"'); + let result = {}; + try { + result = JSON.parse(item); + } catch(e) { + logger.warn(errMsg); + logger.warn(e.message); + } + return result; + }) +} + +module.exports = new Microservice(); \ No newline at end of file diff --git a/src/schemas/microservice.js b/src/schemas/microservice.js index 04724dc6a..72cd89375 100644 --- a/src/schemas/microservice.js +++ b/src/schemas/microservice.js @@ -8,7 +8,6 @@ const microserviceCreate = { }, "config": {"type": "string"}, "catalogItemId": {"type": "integer"}, - "isNetwork" : {"type": "boolean"}, "flowId": {"type": "integer"}, "ioFogNodeId": {"type": "string"}, "rootHostAccess": {"type": "boolean"}, @@ -36,7 +35,6 @@ const microserviceUpdate = { "minLength": 1 }, "config": {"type": "string"}, - "isNetwork" : {"type": "boolean"}, "needUpdate" : {"type": "boolean"}, "rebuild": {"type": "boolean"}, "ioFogNodeId": {"type": "string"}, From f491bb3bcd89d5743f11440acdb34b31340ee524 Mon Sep 17 00:00:00 2001 From: Pankov Date: Fri, 26 Oct 2018 14:58:24 +0300 Subject: [PATCH 06/15] cli microservice wip --- src/cli/microservice.js | 22 ++++++++++++++++++++-- src/schemas/microservice.js | 22 +--------------------- src/services/microservices-service.js | 18 ------------------ 3 files changed, 21 insertions(+), 41 deletions(-) diff --git a/src/cli/microservice.js b/src/cli/microservice.js index 6cd57557c..20e995ac2 100644 --- a/src/cli/microservice.js +++ b/src/cli/microservice.js @@ -87,11 +87,11 @@ class Microservice extends BaseCLIHandler { }, { name: 'ports', alias: 'p', type: String, description: 'Container ports', multiple: true, - group: [constants.CMD_UPDATE, constants.CMD_ADD] + group: [constants.CMD_ADD] }, { name: 'routes', alias: 't', type: String, description: 'Microservice route(s) (receiving microservices)', multiple: true, - group: [constants.CMD_UPDATE, constants.CMD_ADD] + group: [constants.CMD_ADD] }, { name: 'add', alias: 'a', type: String, description: 'Add new route(s)', multiple: true, @@ -226,6 +226,24 @@ const _updateMicroservice = async function (obj) { } +const _updateMicroserviceObject = async function (obj) { + const microserviceObj = { + name: obj.name, + config: obj.config, + catalogItemId: parseInt(obj.catalogId), + flowId: parseInt(obj.flowId), + ioFogNodeId: obj.iofogId, + rootHostAccess: AppHelper.validateBooleanCliOptions(obj.rootEnable, obj.rootDisable), + logLimit: obj.logLimit, + }; + + if (obj.volumes) { + microserviceObj.volumeMappings = parseObjectArray(obj.volumes, 'Error during parsing of volume mapping option.'); + } + + return AppHelper.deleteUndefinedFields(microserviceObj); +} + const _createMicroserviceObject = function (obj) { const microserviceObj = { name: obj.name, diff --git a/src/schemas/microservice.js b/src/schemas/microservice.js index 72cd89375..6ba2b44c5 100644 --- a/src/schemas/microservice.js +++ b/src/schemas/microservice.js @@ -75,27 +75,7 @@ const volumeMappings = { } }; -const networkConfig = { - "id": "/networkConfig", - "type": "object", - "properties": { - "mode": {"enum": ["public", "private"]}, - "host": {"type": "string"}, - "port": {"type": "integer"}, - "cert": {"type": "string"}, - "connectioncount": {"enum": [1, 60]}, - "passcode": {"type": "string"}, - "localhost": {"enum": ["iofog"]}, - "localport": {"type": "integer"}, - "heartbeatfrequency": {"enum": [20000]}, - "heartbeatabsencethreshold": {"enum": [60000]}, - "devmode": {"type": "boolean"} - }, - "required": ["mode", "host", "port", "cert", "connectioncount", "passcode", "localhost", - "localport", "heartbeatfrequency", "heartbeatabsencethreshold", "devmode"] -}; - module.exports = { - mainSchemas: [microserviceCreate, microserviceUpdate, networkConfig, ports], + mainSchemas: [microserviceCreate, microserviceUpdate, ports], innerSchemas: [volumeMappings, ports] }; \ No newline at end of file diff --git a/src/services/microservices-service.js b/src/services/microservices-service.js index cbeb76757..735382be1 100644 --- a/src/services/microservices-service.js +++ b/src/services/microservices-service.js @@ -95,19 +95,10 @@ const _createMicroserviceOnFog = async function (microserviceData, user, isCLI, const _createMicroservice = async function (microserviceData, user, isCLI, transaction) { - if(microserviceData.isNetwork) { - if (microserviceData.config !== undefined) { - await Validation.validate(JSON.parse(microserviceData.config), Validation.schemas.networkConfig) - } else { - throw new Errors.ValidationError(ErrorMessages.INVALID_MICROSERVICE_CONFIG) - } - } - const microserviceToCreate = { uuid: AppHelper.generateRandomString(32), name: microserviceData.name, config: microserviceData.config, - isNetwork: microserviceData.isNetwork, catalogItemId: microserviceData.catalogItemId, flowId: microserviceData.flowId, iofogUuid: microserviceData.ioFogNodeId, @@ -160,18 +151,9 @@ const _createRoutes = async function (routes, microserviceUuid, user, transactio const _updateMicroservice = async function (microserviceUuid, microserviceData, user, isCLI, transaction) { await Validation.validate(microserviceData, Validation.schemas.microserviceUpdate); - if(microserviceData.isNetwork) { - if (microserviceData.config !== undefined) { - await Validation.validate(JSON.parse(microserviceData.config), Validation.schemas.networkConfig) - } else { - throw new Errors.ValidationError(ErrorMessages.INVALID_MICROSERVICE_CONFIG) - } - } - const microserviceToUpdate = { name: microserviceData.name, config: microserviceData.config, - isNetwork: microserviceData.isNetwork, needUpdate: microserviceData.needUpdate, rebuild: microserviceData.rebuild, iofogUuid: microserviceData.ioFogNodeId, From e732b3ed2085ac4d8be4d5b8ad56c9dd72a66a22 Mon Sep 17 00:00:00 2001 From: Pankov Date: Fri, 26 Oct 2018 15:02:41 +0300 Subject: [PATCH 07/15] microservices cli wip --- specs/swagger.yml | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/specs/swagger.yml b/specs/swagger.yml index 5ec9d5a26..aa8442e75 100644 --- a/specs/swagger.yml +++ b/specs/swagger.yml @@ -2897,8 +2897,6 @@ definitions: type: string catalogItemId: type: integer - isNetwork: - type: boolean flowId: type: integer ioFogNodeId: @@ -2926,8 +2924,6 @@ definitions: type: string config: type: string - isNetwork: - type: boolean needUpdate: type: boolean rebuild: @@ -2936,8 +2932,6 @@ definitions: type: string rootHostAccess: type: boolean - deleteWithCleanUp: - type: boolean logLimit: type: number imageSnapshot: @@ -3074,4 +3068,4 @@ schemes: - http - https host: 'localhost:54421' -basePath: /api/v3 +basePath: /api/v3 \ No newline at end of file From d32b759560cafde8cf20102272caf9d7b2c0c38e Mon Sep 17 00:00:00 2001 From: Pankov Date: Fri, 26 Oct 2018 19:00:12 +0300 Subject: [PATCH 08/15] cli microservices final --- specs/swagger.yml | 12 +- src/cli/catalog.js | 2 + src/cli/microservice.js | 151 ++++++++++++++++--- src/controllers/microservices-controller.js | 16 +- src/helpers/constants.js | 2 +- src/helpers/error-messages.js | 2 +- src/schemas/microservice.js | 12 +- src/services/microservices-service.js | 155 +++++++++++++------- 8 files changed, 254 insertions(+), 98 deletions(-) diff --git a/specs/swagger.yml b/specs/swagger.yml index baa73d692..bc0f6c895 100644 --- a/specs/swagger.yml +++ b/specs/swagger.yml @@ -1243,6 +1243,8 @@ paths: description: Bad Request '401': description: Not Authorized + '409': + description: Duplicate Name '500': description: Internal Server Error '/microservices/{uuid}': @@ -1312,6 +1314,8 @@ paths: description: Not Authorized '404': description: Not Found + '409': + description: Duplicate Name '500': description: Internal Server Error delete: @@ -1497,7 +1501,7 @@ paths: delete: tags: - Microservices - description: Deletes a prot mapping for microservice + description: Deletes a port mapping for microservice operationId: deleteMicroservicePortMapping parameters: - in: header @@ -2919,8 +2923,6 @@ definitions: type: string config: type: string - needUpdate: - type: boolean rebuild: type: boolean ioFogNodeId: @@ -2929,8 +2931,6 @@ definitions: type: boolean logLimit: type: number - imageSnapshot: - type: string volumeMappings: type: array items: @@ -3063,4 +3063,4 @@ schemes: - http - https host: 'localhost:54421' -basePath: /api/v3 \ No newline at end of file +basePath: /api/v3 diff --git a/src/cli/catalog.js b/src/cli/catalog.js index 635cc103a..00a9b3809 100644 --- a/src/cli/catalog.js +++ b/src/cli/catalog.js @@ -162,12 +162,14 @@ const _deleteCatalogItem = async function (obj) { const _listCatalogItems = async function () { const result = await CatalogItemService.listCatalogItems({}, true); logger.info(JSON.stringify(result)); + logger.info('Catalog items have been successfully retrieved.'); }; const _listCatalogItem = async function (obj) { logger.info(JSON.stringify(obj)); const result = await CatalogItemService.getCatalogItem(obj.itemId, {}, true); logger.info(JSON.stringify(result)); + logger.info('Catalog item has been successfully retrieved.'); }; const _createCatalogItemObject = function (catalogItem) { diff --git a/src/cli/microservice.js b/src/cli/microservice.js index 20e995ac2..c1616e556 100644 --- a/src/cli/microservice.js +++ b/src/cli/microservice.js @@ -47,7 +47,15 @@ class Microservice extends BaseCLIHandler { }, { name: 'microservice-id', alias: 'i', type: String, description: 'Microservice ID', - group: [constants.CMD_UPDATE, constants.CMD_REMOVE, constants.CMD_INFO, constants.CMD_ROUTE, constants.CMD_STRACE] + group: [constants.CMD_UPDATE, constants.CMD_REMOVE, constants.CMD_INFO, constants.CMD_PORT_MAPPING] + }, + { + name: 'dest-microservice-id', alias: 'D', type: String, description: 'Destination Microservice ID of route', + group: [constants.CMD_ROUTE] + }, + { + name: 'source-microservice-id', alias: 'S', type: String, description: 'Source Microservice ID of route', + group: [constants.CMD_ROUTE] }, { name: 'name', alias: 'n', type: String, description: 'Microservice name', @@ -55,11 +63,11 @@ class Microservice extends BaseCLIHandler { }, { name: 'catalog-id', alias: 'c', type: String, description: 'Catalog item ID', - group: [constants.CMD_UPDATE, constants.CMD_ADD] + group: [constants.CMD_ADD] }, { name: 'flow-id', alias: 'F', type: String, description: 'Application flow ID', - group: [constants.CMD_UPDATE, constants.CMD_ADD] + group: [constants.CMD_ADD] }, { name: 'iofog-id', alias: 'I', type: String, description: 'ioFog node ID', @@ -94,24 +102,48 @@ class Microservice extends BaseCLIHandler { group: [constants.CMD_ADD] }, { - name: 'add', alias: 'a', type: String, description: 'Add new route(s)', multiple: true, + name: 'add', alias: 'a', type: Boolean, description: 'Add new route(s)', group: [constants.CMD_ROUTE] }, { - name: 'remove', alias: 'm', type: String, description: 'Delete existing route(s)', multiple: true, + name: 'remove', alias: 'm', type: Boolean, description: 'Delete existing route(s)', group: [constants.CMD_ROUTE] }, { - name: 'enable', alias: 'e', type: Boolean, description: 'Enable strace option', - group: [constants.CMD_STRACE] + name: 'create', alias: 'b', type: Boolean, description: 'Add new port mapping(s)', + group: [constants.CMD_PORT_MAPPING] + }, + { + name: 'delete', alias: 'B', type: Boolean, description: 'Delete existing port mapping(s)', + group: [constants.CMD_PORT_MAPPING] + }, + { + name: 'list', alias: 'G', type: Boolean, description: 'List port mappings', + group: [constants.CMD_PORT_MAPPING] }, { - name: 'disable', alias: 'd', type: Boolean, description: 'Disable strace option', - group: [constants.CMD_STRACE] + name: 'internal', alias: 'W', type: Number, description: 'Internal port', + group: [constants.CMD_PORT_MAPPING] }, { - name: 'get', alias: 'G', type: String, description: 'Get strace data, formats: string,file', - group: [constants.CMD_STRACE] + name: 'external', alias: 'Y', type: Number, description: 'External port', + group: [constants.CMD_PORT_MAPPING] + }, + { + name: 'public', alias: 'Z', type: Boolean, description: 'Public mode of connector', + group: [constants.CMD_PORT_MAPPING] + }, + { + name: 'private', alias: 'K', type: Boolean, description: 'Private mode of connector', + group: [constants.CMD_PORT_MAPPING] + }, + { + name: 'rebuild', alias: 'w', type: Boolean, description: 'Rebuild microservice image on fog agent', + group: [constants.CMD_UPDATE] + }, + { + name: 'cleanUp', alias: 'z', type: Boolean, description: 'Delete microservice with cleanup', + group: [constants.CMD_REMOVE] }, { name: 'user-id', alias: 'u', type: Number, description: 'User\'s id', @@ -125,7 +157,7 @@ class Microservice extends BaseCLIHandler { [constants.CMD_LIST]: 'List all microservices.', [constants.CMD_INFO]: 'Get microservice settings.', [constants.CMD_ROUTE]: 'Add/Remove microservice route.', - [constants.CMD_STRACE]: 'strace option operations.', + [constants.CMD_PORT_MAPPING]: 'Add/Remove/List microservice port mapping.' } } @@ -140,13 +172,20 @@ class Microservice extends BaseCLIHandler { await _executeCase(microserviceCommand, constants.CMD_UPDATE, _updateMicroservice, false); break; case constants.CMD_REMOVE: - return + await _executeCase(microserviceCommand, constants.CMD_REMOVE, _removeMicroservice, false); + break; case constants.CMD_LIST: - return + await _executeCase(microserviceCommand, constants.CMD_LIST, _listMicroservices, false); + break; case constants.CMD_INFO: - return + await _executeCase(microserviceCommand, constants.CMD_INFO, _getMicroservice, false); + break; case constants.CMD_ROUTE: - return + await _executeCase(microserviceCommand, constants.CMD_ROUTE, _executeRouteCommand, false); + break; + case constants.CMD_PORT_MAPPING: + await _executeCase(microserviceCommand, constants.CMD_PORT_MAPPING, _executePortMappingCommand, false); + break; case constants.CMD_HELP: default: return this.help() @@ -197,7 +236,7 @@ class Microservice extends BaseCLIHandler { const _executeCase = async function (microserviceCommand, commandName, f, isUserRequired) { try { - const item = microserviceCommand[commandName]; + const item = microserviceCommand[commandName] || {}; if (isUserRequired) { const decoratedFunction = AuthDecorator.prepareUserById(f); @@ -210,6 +249,68 @@ const _executeCase = async function (microserviceCommand, commandName, f, isUse } }; +const _executeRouteCommand = async function (obj) { + logger.info(JSON.stringify(obj)); + + if (obj.add) { + await MicroserviceService.createRouteWithTransaction(obj.sourceMicroserviceId, obj.destMicroserviceId, {}, true); + logger.info(`Microservice route with source microservice ${obj.sourceMicroserviceId} and dest microservice + ${obj.destMicroserviceId} has been created successfully.`) + } else if (obj.remove) { + await MicroserviceService.deleteRouteWithTransaction(obj.sourceMicroserviceId, obj.destMicroserviceId, {}, true); + logger.info(`Microservice route with source microservice ${obj.sourceMicroserviceId} and dest microservice + ${obj.destMicroserviceId} has been removed successfully.`) + } else if (obj.add && obj.remove) { + logger.info('Please specify either "add" or "remove" operation'); + } else { + logger.info('No operation specified'); + } +}; + +const _executePortMappingCommand = async function (obj) { + logger.info(JSON.stringify(obj)); + + if (obj.create) { + + let mapping = { + internal: parseInt(obj.internal), + external: parseInt(obj.external), + publicMode: AppHelper.validateBooleanCliOptions(obj.public, obj.private) + }; + mapping = AppHelper.deleteUndefinedFields(mapping); + await MicroserviceService.createPortMappingWithTransaction(obj.microserviceId, mapping, {}, true); + logger.info('Port mapping has been create successfully'); + + } else if (obj.delete) { + await MicroserviceService.deletePortMappingWithTransaction(obj.microserviceId, obj.internal, {}, true); + logger.info('Port mapping has been deleted successfully'); + } else if (obj.list) { + await MicroserviceService.getMicroservicePortMappingListWithTransaction(obj.microserviceId, {}, true); + logger.info('Port mappings have been retrieved successfully'); + } else { + logger.info('Incorrect command usage. Please specify only one command at once'); + } +}; + +const _removeMicroservice = async function (obj) { + logger.info(JSON.stringify(obj)); + await MicroserviceService.deleteMicroserviceWithTransaction(obj.microserviceId, obj.cleanUp, {}, true); + logger.info('Microservice has been removed successfully.') +}; + +const _listMicroservices = async function () { + const result = await MicroserviceService.listMicroservicesWithTransaction({}, {}, true); + logger.info(JSON.stringify(result)); + logger.info('Microservices have been retrieved successfully.'); +}; + +const _getMicroservice = async function (obj) { + logger.info(JSON.stringify(obj)); + const result = await MicroserviceService.getMicroserviceWithTransaction(obj.microserviceId, {}, true); + logger.info(JSON.stringify(result)); + logger.info('Microservice has been retrieved successfully.'); +}; + const _createMicroservice = async function (obj) { const microservice = obj.file ? JSON.parse(fs.readFileSync(obj.file, 'utf8')) @@ -223,18 +324,24 @@ const _createMicroservice = async function (obj) { }; const _updateMicroservice = async function (obj) { + const microservice = obj.file + ? JSON.parse(fs.readFileSync(obj.file, 'utf8')) + : _updateMicroserviceObject(obj); -} + logger.info(JSON.stringify(microservice)); -const _updateMicroserviceObject = async function (obj) { + await MicroserviceService.updateMicroserviceWithTransaction(obj.microserviceId, microservice, {}, true); + logger.info('Microservice has been updated successfully.'); +}; + +const _updateMicroserviceObject = function (obj) { const microserviceObj = { name: obj.name, config: obj.config, - catalogItemId: parseInt(obj.catalogId), - flowId: parseInt(obj.flowId), ioFogNodeId: obj.iofogId, rootHostAccess: AppHelper.validateBooleanCliOptions(obj.rootEnable, obj.rootDisable), logLimit: obj.logLimit, + rebuild: obj.rebuild }; if (obj.volumes) { @@ -280,6 +387,6 @@ const parseObjectArray = function (arr, errMsg) { } return result; }) -} +}; module.exports = new Microservice(); \ No newline at end of file diff --git a/src/controllers/microservices-controller.js b/src/controllers/microservices-controller.js index d256d4cbb..f3645456b 100644 --- a/src/controllers/microservices-controller.js +++ b/src/controllers/microservices-controller.js @@ -52,45 +52,45 @@ const _deleteMicroserviceEndPoint = async function (req, user) { }; const _getMicroservicesByFlowEndPoint = async function (req, user) { - const flowId = req.query.flowId; + const data = req.query.flowId; - logger.info("Flow id:" + JSON.stringify(flowId)) + logger.info("Flow id:" + JSON.stringify(data.flowId)) - return await MicroservicesService.getMicroserviceByFlowWithTransaction(flowId, user, false) + return await MicroservicesService.listMicroservicesWithTransaction(data, user, false) }; async function _createMicroserviceRoute(req, user) { const sourceUuid = req.params.uuid const distUuid = req.params.receiverUuid logger.info(`Creating route from ${sourceUuid} to ${distUuid}`) - return await MicroservicesService.createRouteWithTransaction(sourceUuid, distUuid, user) + return await MicroservicesService.createRouteWithTransaction(sourceUuid, distUuid, user, false) } async function _deleteMicroserviceRoute(req, user) { const sourceUuid = req.params.uuid const distUuid = req.params.receiverUuid logger.info(`Creating route from ${sourceUuid} to ${distUuid}`) - return await MicroservicesService.deleteRouteWithTransaction(sourceUuid, distUuid, user) + return await MicroservicesService.deleteRouteWithTransaction(sourceUuid, distUuid, user, false) } async function _createMicroservicePortMapping(req, user) { const uuid = req.params.uuid const portMappingData = req.body logger.info(`Creating port mapping for ${uuid}`) - return await MicroservicesService.createPortMappingWithTransaction(uuid, portMappingData, user) + return await MicroservicesService.createPortMappingWithTransaction(uuid, portMappingData, user, false) } async function _deleteMicroservicePortMapping(req, user) { const uuid = req.params.uuid const internalPort = req.params.internalPort logger.info(`Deleting port mapping for ${uuid}`) - return await MicroservicesService.deletePortMappingWithTransaction(uuid, internalPort, user) + return await MicroservicesService.deletePortMappingWithTransaction(uuid, internalPort, user, false) } async function _getMicroservicePortMappingList(req, user) { const uuid = req.params.uuid logger.info(`Getting all port mappings for ${uuid}`) - return await MicroservicesService.getMicroservicePortMappingListWithTransaction(uuid, user) + return await MicroservicesService.getMicroservicePortMappingListWithTransaction(uuid, user, false) } module.exports = { diff --git a/src/helpers/constants.js b/src/helpers/constants.js index ec30f7802..d78e907d0 100644 --- a/src/helpers/constants.js +++ b/src/helpers/constants.js @@ -37,10 +37,10 @@ module.exports = { CMD_FLOW: 'flow', CMD_MICROSERVICE: 'microservice', CMD_ROUTE: 'route', + CMD_PORT_MAPPING: 'port-mapping', CMD_REGISTRY: 'registry', CMD_ACTIVATE: 'activate', CMD_SUSPEND: 'suspend', - CMD_STRACE: 'strace', CMD_DEV_MODE: 'dev-mode', CMD_TUNNEL: 'tunnel', CMD_IOFOG_REBOOT: 'reboot', diff --git a/src/helpers/error-messages.js b/src/helpers/error-messages.js index fcca78679..71dd583f4 100644 --- a/src/helpers/error-messages.js +++ b/src/helpers/error-messages.js @@ -19,7 +19,7 @@ module.exports = { UNABLE_TO_GET_ACTIVATION_CODE: 'Unable to create activation code', INVALID_FOG_NODE_ID: 'Invalid fog node id {}', INVALID_USER_EMAIL: 'Invalid user email', - INVALID_MICROSERVICE_UUID: 'Invalid microservice uuid', + INVALID_MICROSERVICE_UUID: 'Invalid microservice uuid {}', ACTIVATION_CODE_NOT_FOUND: 'Activation code not found', INVALID_OLD_PASSWORD: 'Old password is incorrect', ACCOUNT_NOT_FOUND: 'Account not found', diff --git a/src/schemas/microservice.js b/src/schemas/microservice.js index 08d5cc5e1..20012ed3d 100644 --- a/src/schemas/microservice.js +++ b/src/schemas/microservice.js @@ -35,22 +35,14 @@ const microserviceUpdate = { "minLength": 1 }, "config": {"type": "string"}, - "needUpdate" : {"type": "boolean"}, "rebuild": {"type": "boolean"}, "ioFogNodeId": {"type": "string"}, "rootHostAccess": {"type": "boolean"}, - "deleteWithCleanUp": {"type": "boolean"}, "logSize": {"type": "integer"}, - "imageSnapshot": {"type": "string"}, "volumeMappings": { "type": "array", - "items": {"$ref": "/volumeMappings"}}, - "ports": { - "type": "array", - "items": {"$ref": "/ports"}}, - "routes": { - "type": "array", - "items": {"type": "string"}} + "items": {"$ref": "/volumeMappings"} + } } }; diff --git a/src/services/microservices-service.js b/src/services/microservices-service.js index 661fd9d09..33d87ac2d 100644 --- a/src/services/microservices-service.js +++ b/src/services/microservices-service.js @@ -25,18 +25,18 @@ const AppHelper = require('../helpers/app-helper'); const Errors = require('../helpers/errors'); const ErrorMessages = require('../helpers/error-messages'); const Validation = require('../schemas/index'); -const ConnectorService = require('../services/connector-service') -const CatalogService = require('../services/catalog-service') -const RoutingManager = require('../sequelize/managers/routing-manager') +const ConnectorService = require('../services/connector-service'); +const CatalogService = require('../services/catalog-service'); +const RoutingManager = require('../sequelize/managers/routing-manager'); +const Op = require('sequelize').Op; -const _getMicroserviceByFlow = async function (flowId, user, isCLI, transaction) { - await FlowService.getFlow(flowId, user, isCLI, transaction); - - const microservice = { - flowId: flowId - }; +const _listMicroservices = async function (data, user, isCLI, transaction) { + if (!isCLI) { + await FlowService.getFlow(data.flowId, user, isCLI, transaction); + } + const where = isCLI ? {} : {flowId: data.flowId}; - return await MicroserviceManager.findAllWithDependencies(microservice, + return await MicroserviceManager.findAllWithDependencies(where, { exclude: [ 'configLastUpdated', @@ -50,9 +50,11 @@ const _getMicroserviceByFlow = async function (flowId, user, isCLI, transaction) }; const _getMicroservice = async function (microserviceUuid, user, isCLI, transaction) { - await _validateMicroserviceOnGet(user.id, microserviceUuid, transaction); + if (!isCLI) { + await _validateMicroserviceOnGet(user.id, microserviceUuid, transaction); + } - return await MicroserviceManager.findOneWithDependencies({ + const microservice = await MicroserviceManager.findOneWithDependencies({ uuid: microserviceUuid }, { @@ -60,11 +62,15 @@ const _getMicroservice = async function (microserviceUuid, user, isCLI, transact 'configLastUpdated', 'created_at', 'updated_at', - 'catalogItemId', 'updatedBy', 'flowId', 'registryId' ]}, transaction); + + if (!microservice) { + throw new Errors.NotFoundError(AppHelper.formatMessage(ErrorMessages.INVALID_MICROSERVICE_UUID, microserviceUuid)); + } + return microservice; }; const _createMicroserviceOnFog = async function (microserviceData, user, isCLI, transaction) { @@ -110,16 +116,16 @@ const _createMicroservice = async function (microserviceData, user, isCLI, trans const microserviceDataCreate = AppHelper.deleteUndefinedFields(microserviceToCreate); - await isMicroserviceExist(microserviceDataCreate.name, transaction); + await _checkForDuplicateName(microserviceDataCreate.name, {}, transaction); - //validate catalog item - await CatalogService.getCatalogItem(microserviceDataCreate.catalogItemId, user, isCLI, transaction); - //validate flow - await FlowService.getFlow(microserviceDataCreate.flowId, user, isCLI, transaction); - //validate fog node - if (microserviceDataCreate.iofogUuid) { - await IoFogService.getFog({uuid: microserviceDataCreate.iofogUuid}, user, isCLI, transaction); - } + //validate catalog item + await CatalogService.getCatalogItem(microserviceDataCreate.catalogItemId, user, isCLI, transaction); + //validate flow + await FlowService.getFlow(microserviceDataCreate.flowId, user, isCLI, transaction); + //validate fog node + if (microserviceDataCreate.iofogUuid) { + await IoFogService.getFog({uuid: microserviceDataCreate.iofogUuid}, user, isCLI, transaction); + } return await MicroserviceManager.create(microserviceDataCreate, transaction); }; @@ -158,11 +164,11 @@ const _updateMicroservice = async function (microserviceUuid, microserviceData, const microserviceToUpdate = { name: microserviceData.name, config: microserviceData.config, - needUpdate: microserviceData.needUpdate, rebuild: microserviceData.rebuild, iofogUuid: microserviceData.ioFogNodeId, rootHostAccess: microserviceData.rootHostAccess, logSize: microserviceData.logLimit, + volumeMappings: microserviceData.volumeMappings, updatedBy: user.id }; @@ -170,8 +176,8 @@ const _updateMicroservice = async function (microserviceUuid, microserviceData, const microservice = await _getMicroservice(microserviceUuid, user, isCLI, transaction); - if(microserviceDataUpdate.name){ - await isMicroserviceExist(microserviceDataUpdate.name, transaction); + if (microserviceDataUpdate.name) { + await _checkForDuplicateName(microserviceDataUpdate.name, {id: microserviceUuid}, transaction); } //validate fog node @@ -184,7 +190,7 @@ const _updateMicroservice = async function (microserviceUuid, microserviceData, }, microserviceDataUpdate, transaction); if (microserviceDataUpdate.volumeMappings) { - await _updateVolumeMappings(microserviceUuid.volumeMappings, microserviceData, transaction); + await _updateVolumeMappings(microserviceDataUpdate.volumeMappings, microserviceUuid, transaction); } if (microserviceDataUpdate.ioFogNodeId){ @@ -237,13 +243,16 @@ const _deleteMicroservice = async function (microserviceUuid, deleteWithCleanUp, await _updateChangeTracking(false, microserviceUuid, microservice.ioFogNodeId, user, isCLI, transaction) }; -const isMicroserviceExist = async function (microserviceName, transaction) { - const microservice = await MicroserviceManager.findOne({ - name: microserviceName - }, transaction); +const _checkForDuplicateName = async function (name, item, transaction) { + if (name) { + const where = item.id + ? {name: name, uuid: {[Op.ne]: item.id}} + : {name: name}; - if (microservice) { - throw new Errors.ValidationError(AppHelper.formatMessage(ErrorMessages.DUPLICATE_NAME, microserviceName)); + const result = await MicroserviceManager.findOne(where, transaction); + if (result) { + throw new Errors.DuplicatePropertyError(AppHelper.formatMessage(ErrorMessages.DUPLICATE_NAME, name)); + } } }; @@ -264,13 +273,21 @@ const _validateMicroserviceOnGet = async function (userId, microserviceUuid, tra } }; -async function _createRoute(sourceMicroserviceUuid, destMicroserviceUuid, user, transaction) { - const sourceMicroservice = await MicroserviceManager.findOne({uuid: sourceMicroserviceUuid, updatedBy: user.id}, transaction) +async function _createRoute(sourceMicroserviceUuid, destMicroserviceUuid, user, isCLI, transaction) { + const sourceWhere = isCLI + ? {uuid: sourceMicroserviceUuid} + : {uuid: sourceMicroserviceUuid, updatedBy: user.id}; + + const sourceMicroservice = await MicroserviceManager.findOne(sourceWhere, transaction); if (!sourceMicroservice) { throw new Errors.NotFoundError(AppHelper.formatMessage(ErrorMessages.INVALID_MICROSERVICE_UUID, sourceMicroserviceUuid)) } - const destMicroservice = await MicroserviceManager.findOne({uuid: destMicroserviceUuid, updatedBy: user.id}, transaction) + const destWhere = isCLI + ? {uuid: destMicroserviceUuid} + : {uuid: destMicroserviceUuid, updatedBy: user.id}; + + const destMicroservice = await MicroserviceManager.findOne(destWhere, transaction); if (!destMicroservice) { throw new Errors.NotFoundError(AppHelper.formatMessage(ErrorMessages.INVALID_MICROSERVICE_UUID, destMicroserviceUuid)) } @@ -357,7 +374,13 @@ async function _createRouteOverConnector(sourceMicroservice, destMicroservice, u 'heartbeatabsencethreshold': 60000, 'devmode': connector.devMode } - const sourceNetworkMicroservice = await _createNetworkMicroserviceForMaster(connector, ports, sourceMicroservice, sourceNetwMsConfig,networkCatalogItem, user, transaction); + const sourceNetworkMicroservice = await _createNetworkMicroserviceForMaster( + sourceMicroservice, + sourceNetwMsConfig, + networkCatalogItem, + user, + transaction + ); //create netw ms2 const destNetwMsConfig = { @@ -373,7 +396,13 @@ async function _createRouteOverConnector(sourceMicroservice, destMicroservice, u 'heartbeatabsencethreshold': 60000, 'devmode': connector.devMode } - const destNetworkMicroservice = await _createNetworkMicroserviceForMaster(connector, ports, destMicroservice, destNetwMsConfig,networkCatalogItem, user, transaction); + const destNetworkMicroservice = await _createNetworkMicroserviceForMaster( + destMicroservice, + destNetwMsConfig, + networkCatalogItem, + user, + transaction + ); //create new route const routeData = { @@ -391,7 +420,7 @@ async function _createRouteOverConnector(sourceMicroservice, destMicroservice, u await _switchOnUpdateFlagsForMicroservicesInRoute(sourceMicroservice, destMicroservice, transaction) } -async function _createNetworkMicroserviceForMaster(connector, ports, masterMicroservice, sourceNetwMsConfig, networkCatalogItem, user, transaction) { +async function _createNetworkMicroserviceForMaster(masterMicroservice, sourceNetwMsConfig, networkCatalogItem, user, transaction) { const sourceNetworkMicroserviceData = { uuid: AppHelper.generateRandomString(32), name: `Network for Element ${masterMicroservice.uuid}`, @@ -404,7 +433,7 @@ async function _createNetworkMicroserviceForMaster(connector, ports, masterMicro logSize: 50, updatedBy: user.id, configLastUpdated: Date.now() - } + }; return await MicroserviceManager.create(sourceNetworkMicroserviceData, transaction); } @@ -425,13 +454,21 @@ async function _switchOnUpdateFlagsForMicroservicesInRoute(sourceMicroservice, d await ChangeTrackingManager.update({iofogUuid: destMicroservice.iofogUuid}, updateChangeTrackingData, transaction) } -async function _deleteRoute(sourceMicroserviceUuid, destMicroserviceUuid, user, transaction) { - const sourceMicroservice = await MicroserviceManager.findOne({uuid: sourceMicroserviceUuid, updatedBy: user.id}, transaction) +async function _deleteRoute(sourceMicroserviceUuid, destMicroserviceUuid, user, isCLI, transaction) { + const sourceWhere = isCLI + ? {uuid: sourceMicroserviceUuid} + : {uuid: sourceMicroserviceUuid, updatedBy: user.id}; + + const sourceMicroservice = await MicroserviceManager.findOne(sourceWhere, transaction); if (!sourceMicroservice) { throw new Errors.NotFoundError(AppHelper.formatMessage(ErrorMessages.INVALID_MICROSERVICE_UUID, sourceMicroserviceUuid)) } - const destMicroservice = await MicroserviceManager.findOne({uuid: destMicroserviceUuid, updatedBy: user.id}, transaction) + const destWhere = isCLI + ? {uuid: destMicroserviceUuid} + : {uuid: destMicroserviceUuid, updatedBy: user.id}; + + const destMicroservice = await MicroserviceManager.findOne(destWhere, transaction); if (!destMicroservice) { throw new Errors.NotFoundError(AppHelper.formatMessage(ErrorMessages.INVALID_MICROSERVICE_UUID, destMicroserviceUuid)) } @@ -483,9 +520,14 @@ async function _deleteRouteOverConnector(route, transaction) { await ChangeTrackingManager.update({iofogUuid: route.destIofogUuid}, updateChangeTrackingData, transaction) } -async function _createPortMapping(microserviceUuid, portMappingData, user, transaction) { +async function _createPortMapping(microserviceUuid, portMappingData, user, isCLI, transaction) { await Validation.validate(portMappingData, Validation.schemas.portsCreate); - const microservice = await MicroserviceManager.findOne({uuid: microserviceUuid, updatedBy: user.id}, transaction) + + const where = isCLI + ? {uuid: microserviceUuid} + : {uuid: microserviceUuid, updatedBy: user.id}; + + const microservice = await MicroserviceManager.findOne(where, transaction) if (!microservice) { throw new Errors.NotFoundError(AppHelper.formatMessage(ErrorMessages.INVALID_MICROSERVICE_UUID, microserviceUuid)) } @@ -570,7 +612,13 @@ async function _createPortMappingOverConnector(microservice, portMappingData, us 'heartbeatabsencethreshold': 60000, 'devmode': connector.devMode } - const networkMicroservice = await _createNetworkMicroserviceForMaster(connector, ports, microservice, netwMsConfig,networkCatalogItem, user, transaction); + const networkMicroservice = await _createNetworkMicroserviceForMaster( + microservice, + netwMsConfig, + networkCatalogItem, + user, + transaction + ); //create public port mapping const mappingData = { @@ -618,8 +666,12 @@ async function _switchOnUpdateFlagsForMicroservicesForPortMapping(microservice, await ChangeTrackingManager.update({iofogUuid: microservice.iofogUuid}, updateChangeTrackingData, transaction) } -async function _deletePortMapping(microserviceUuid, internalPort, user, transaction) { - const microservice = await MicroserviceManager.findOne({uuid: microserviceUuid, updatedBy: user.id}, transaction) +async function _deletePortMapping(microserviceUuid, internalPort, user, isCLI, transaction) { + const where = isCLI + ? {uuid: microserviceUuid} + : {uuid: microserviceUuid, updatedBy: user.id} + + const microservice = await MicroserviceManager.findOne(where, transaction); if (!microservice) { throw new Errors.NotFoundError(AppHelper.formatMessage(ErrorMessages.INVALID_MICROSERVICE_UUID, microserviceUuid)) } @@ -684,8 +736,11 @@ async function _validatePorts(internal, external) { } } -async function _getPortMappingList(microserviceUuid, user, transaction) { - const microservice = await MicroserviceManager.findOne({uuid: microserviceUuid, updatedBy: user.id}, transaction) +async function _getPortMappingList(microserviceUuid, user, isCLI, transaction) { + const where = isCLI + ? {uuid: microserviceUuid} + : {uuid: microserviceUuid, updatedBy: user.id}; + const microservice = await MicroserviceManager.findOne(where, transaction) if (!microservice) { throw new Errors.NotFoundError(AppHelper.formatMessage(ErrorMessages.INVALID_MICROSERVICE_UUID, microserviceUuid)) } @@ -738,7 +793,7 @@ async function getPhysicalConections(microservice, transaction) { module.exports = { createMicroserviceOnFogWithTransaction: TransactionDecorator.generateTransaction(_createMicroserviceOnFog), - getMicroserviceByFlowWithTransaction: TransactionDecorator.generateTransaction(_getMicroserviceByFlow), + listMicroservicesWithTransaction: TransactionDecorator.generateTransaction(_listMicroservices), getMicroserviceWithTransaction: TransactionDecorator.generateTransaction(_getMicroservice), updateMicroserviceWithTransaction: TransactionDecorator.generateTransaction(_updateMicroservice), deleteMicroserviceWithTransaction: TransactionDecorator.generateTransaction(_deleteMicroservice), From ff17ccf84bc71962dae703f3604bb33987e20eaa Mon Sep 17 00:00:00 2001 From: Pankov Date: Mon, 29 Oct 2018 19:10:50 +0300 Subject: [PATCH 09/15] microservice cli refactoring --- package.json | 4 +- specs/swagger.yml | 4 +- src/cli/catalog.js | 46 +++--- src/cli/flow.js | 9 +- src/cli/index.js | 2 +- src/cli/iofog.js | 44 +++--- src/cli/microservice.js | 194 ++++++++++++++++-------- src/cli/{proxy.js => tunnel.js} | 0 src/decorators/transaction-decorator.js | 19 ++- src/helpers/app-helper.js | 9 +- src/helpers/error-messages.js | 9 +- src/services/microservices-service.js | 8 +- 12 files changed, 220 insertions(+), 128 deletions(-) rename src/cli/{proxy.js => tunnel.js} (100%) diff --git a/package.json b/package.json index 0faeab5bb..acda6a4cf 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "fs": "^0.0.1-security", "ftp": "^0.3.10", "helmet": "^3.13.0", + "json-beautify": "^1.0.1", "jsonschema": "^1.2.4", "morgan": "^1.9.1", "nconf": "^0.10.0", @@ -44,8 +45,9 @@ "nodemailer-smtp-transport": "^2.7.4", "path": "^0.12.7", "portscanner": "^2.2.0", - "retry-as-promised": "^3.1.0", + "prettyjson": "^1.2.1", "querystring": "^0.2.0", + "retry-as-promised": "^3.1.0", "sequelize": "^4.39.0", "sequelize-cli": "^4.1.1", "sqlite3": "^4.0.2", diff --git a/specs/swagger.yml b/specs/swagger.yml index bc0f6c895..b74b91eb6 100644 --- a/specs/swagger.yml +++ b/specs/swagger.yml @@ -1516,7 +1516,7 @@ paths: type: string - in: path name: internalPort - description: Receiver Microservice Id + description: Internal Port required: true type: string responses: @@ -3063,4 +3063,4 @@ schemes: - http - https host: 'localhost:54421' -basePath: /api/v3 +basePath: /api/v3 \ No newline at end of file diff --git a/src/cli/catalog.js b/src/cli/catalog.js index 00a9b3809..66905dcf9 100644 --- a/src/cli/catalog.js +++ b/src/cli/catalog.js @@ -19,26 +19,32 @@ const fs = require('fs'); const AppHelper = require('../helpers/app-helper'); const AuthDecorator = require('../decorators/cli-decorator'); -const JSON_SCHEMA = - ` name: string - description: string - category: string - publisher: string - diskRequired: number - ramRequired: number - picture: string - isPublic: boolean - registryId: number - configExample: string - images: array of objects - containerImage: string - fogTypeId: number - inputType: object - infoType: string - infoFormat: string - outputType: object - infoType: string - infoFormat: string`; +const JSON_SCHEMA = AppHelper.stringifyCliJsonSchema({ + name: "string", + description: "string", + category: "string", + images: [ + { + containerImage: "string", + fogTypeId: 1 + } + ], + publisher: "string", + diskRequired: 0, + ramRequired: 0, + picture: "string", + isPublic: true, + registryId: 0, + inputType: { + infoType: "string", + infoFormat: "string" + }, + outputType: { + infoType: "string", + infoFormat: "string" + }, + configExample: "string" +}); class Catalog extends BaseCLIHandler { constructor() { diff --git a/src/cli/flow.js b/src/cli/flow.js index b72a236be..0f8c77f00 100644 --- a/src/cli/flow.js +++ b/src/cli/flow.js @@ -19,10 +19,11 @@ const AppHelper = require('../helpers/app-helper'); const logger = require('../logger'); const fs = require('fs'); -const JSON_SCHEMA = - ` name: string - description: string - isActivated: boolean`; +const JSON_SCHEMA = AppHelper.stringifyCliJsonSchema({ + name: "string", + description: "string", + isActivated: true +}); class Flow extends BaseCLIHandler { constructor() { diff --git a/src/cli/index.js b/src/cli/index.js index c8c824af8..c57bbfc2b 100644 --- a/src/cli/index.js +++ b/src/cli/index.js @@ -16,7 +16,7 @@ const Start = require('./start') const User = require('./user') const Connector = require('./connector') const Config = require('./config') -const Proxy = require('./proxy') +const Proxy = require('./tunnel') const IOFog = require('./iofog') const Catalog = require('./catalog') const Flow = require('./flow') diff --git a/src/cli/iofog.js b/src/cli/iofog.js index c2c07e33a..944619eb8 100644 --- a/src/cli/iofog.js +++ b/src/cli/iofog.js @@ -19,28 +19,28 @@ const CliDecorator = require('../decorators/cli-decorator') const AppHelper = require('../helpers/app-helper') const FogService = require('../services/iofog-service') -const JSON_SCHEMA = - `name: string - location: string - latitude: number - longitude: number - description: string - dockerUrl: string - diskLimit: number - diskDirectory: string - memoryLimit: number - cpuLimit: number - logLimit: number - logDirectory: string - logFileCount: number - statusFrequency: number - changeFrequency: number - deviceScanFrequency: number - bluetoothEnabled: boolean - watchdogEnabled: boolean - abstractedHardwareEnabled: boolean - reboot: boolean - fogType: number` +const JSON_SCHEMA = AppHelper.stringifyCliJsonSchema({ + name: "string", + location: "string", + latitude: 0, + longitude: 0, + description: "string", + dockerUrl: "string", + diskLimit: 0, + diskDirectory: "string", + memoryLimit: 0, + cpuLimit: 0, + logLimit: 0, + logDirectory: "string", + logFileCount: 0, + statusFrequency: 0, + changeFrequency: 0, + deviceScanFrequency: 0, + bluetoothEnabled: false, + watchdogEnabled: true, + abstractedHardwareEnabled: false, + fogType: 0 +}); class IOFog extends BaseCLIHandler { constructor() { diff --git a/src/cli/microservice.js b/src/cli/microservice.js index c1616e556..07368c28c 100644 --- a/src/cli/microservice.js +++ b/src/cli/microservice.js @@ -13,26 +13,59 @@ const BaseCLIHandler = require('./base-cli-handler'); const constants = require('../helpers/constants'); +const ErrorMessages = require('../helpers/error-messages'); const logger = require('../logger'); const MicroserviceService = require('../services/microservices-service'); const fs = require('fs'); const AppHelper = require('../helpers/app-helper'); const AuthDecorator = require('../decorators/cli-decorator'); -const JSON_SCHEMA = - ` name: string - catalogItemId: string - flowId: string - ioFogNodeId: string - config: string - volumeMappings: string - logLimit: number - rootHostAccess: true - ports: object - internal: number - external: number - tunnel: boolean - routes: array of strings` +const JSON_SCHEMA_ADD = AppHelper.stringifyCliJsonSchema( + { + name: "string", + config: "string", + catalogItemId: 0, + flowId: 0, + ioFogNodeId: "string", + rootHostAccess: true, + logLimit: 0, + volumeMappings: [ + { + hostDestination: "/var/dest", + containerDestination: "/var/dest", + accessMode: "rw" + } + ], + ports: [ + { + internal: 0, + external: 0, + publicMode: true + } + ], + routes: [ + "string" + ] + } +); + +const JSON_SCHEMA_UPDATE = AppHelper.stringifyCliJsonSchema( + { + name: "string", + config: "string", + rebuild: true, + ioFogNodeId: "string", + rootHostAccess: true, + logLimit: 0, + volumeMappings: [ + { + hostDestination: "/var/dest", + containerDestination: "/var/dest", + accessMode: "rw" + } + ] + } +); class Microservice extends BaseCLIHandler { constructor() { @@ -110,33 +143,17 @@ class Microservice extends BaseCLIHandler { group: [constants.CMD_ROUTE] }, { - name: 'create', alias: 'b', type: Boolean, description: 'Add new port mapping(s)', + name: 'create', alias: 'b', type: String, description: 'Add new port mapping(s)', group: [constants.CMD_PORT_MAPPING] }, { - name: 'delete', alias: 'B', type: Boolean, description: 'Delete existing port mapping(s)', + name: 'delete', alias: 'B', type: String, description: 'Delete existing port mapping(s)', group: [constants.CMD_PORT_MAPPING] }, { name: 'list', alias: 'G', type: Boolean, description: 'List port mappings', group: [constants.CMD_PORT_MAPPING] }, - { - name: 'internal', alias: 'W', type: Number, description: 'Internal port', - group: [constants.CMD_PORT_MAPPING] - }, - { - name: 'external', alias: 'Y', type: Number, description: 'External port', - group: [constants.CMD_PORT_MAPPING] - }, - { - name: 'public', alias: 'Z', type: Boolean, description: 'Public mode of connector', - group: [constants.CMD_PORT_MAPPING] - }, - { - name: 'private', alias: 'K', type: Boolean, description: 'Private mode of connector', - group: [constants.CMD_PORT_MAPPING] - }, { name: 'rebuild', alias: 'w', type: Boolean, description: 'Rebuild microservice image on fog agent', group: [constants.CMD_UPDATE] @@ -157,7 +174,7 @@ class Microservice extends BaseCLIHandler { [constants.CMD_LIST]: 'List all microservices.', [constants.CMD_INFO]: 'Get microservice settings.', [constants.CMD_ROUTE]: 'Add/Remove microservice route.', - [constants.CMD_PORT_MAPPING]: 'Add/Remove/List microservice port mapping.' + [constants.CMD_PORT_MAPPING]: 'Create/Delete/List microservice port mapping.' } } @@ -195,9 +212,16 @@ class Microservice extends BaseCLIHandler { help() { super.help([constants.CMD_LIST], true, true, [ { - header: 'JSON File Schema', + header: 'JSON ADD File Schema', content: [ - JSON_SCHEMA, + JSON_SCHEMA_ADD, + ], + raw: true, + }, + { + header: 'JSON UPDATE File Schema', + content: [ + JSON_SCHEMA_UPDATE ], raw: true, }, @@ -213,21 +237,25 @@ class Microservice extends BaseCLIHandler { example: '$ fog-controller microservice add [other required options] --volumes /host_src:/container_src /host_bin:/container_bin', }, { - desc: '3. Ports (internal:external:tunnel)', - example: '$ fog-controller microservice add [other required options] --ports 80:8080:false 443:5443:true', + desc: '3. Port mapping (internal:external:publicMode)', + example: '$ fog-controller microservice add [other required options] --ports 80:8080:false 443:5443:false', }, { desc: '4. Add routes', - example: '$ fog-controller microservice route -i ABCD --add DEF GHI', + example: '$ fog-controller microservice route --add ABC:DEF', }, { desc: '5. Delete route', - example: '$ fog-controller microservice route -i ABC --remove DEF', + example: '$ fog-controller microservice route --remove ABC:DEF', }, { - desc: '6. Get strace data', - example: '$ fog-controller microservice strace -i ABC --get file', + desc: '6. Create port mapping', + example: '$ fog-controller microservice port-mapping --create 80:8080:false -i ABC' }, + { + desc: '7. Delete port mapping', + example: '$ fog-controller microservice port-mapping --delete 80 -i ABC' + } ], }, ]) @@ -251,15 +279,28 @@ const _executeCase = async function (microserviceCommand, commandName, f, isUse const _executeRouteCommand = async function (obj) { logger.info(JSON.stringify(obj)); - if (obj.add) { - await MicroserviceService.createRouteWithTransaction(obj.sourceMicroserviceId, obj.destMicroserviceId, {}, true); - logger.info(`Microservice route with source microservice ${obj.sourceMicroserviceId} and dest microservice - ${obj.destMicroserviceId} has been created successfully.`) + try { + const arr = obj.add.split(':'); + const sourceMicroserviceId = arr[0]; + const destMicroserviceId = arr[1]; + await MicroserviceService.createRouteWithTransaction(sourceMicroserviceId, destMicroserviceId, {}, true); + logger.info(`Microservice route with source microservice ${sourceMicroserviceId} and dest microservice + ${destMicroserviceId} has been created successfully.`) + } catch (e) { + logger.error(ErrorMessages.CLI.INVALID_ROUTE); + } } else if (obj.remove) { - await MicroserviceService.deleteRouteWithTransaction(obj.sourceMicroserviceId, obj.destMicroserviceId, {}, true); - logger.info(`Microservice route with source microservice ${obj.sourceMicroserviceId} and dest microservice - ${obj.destMicroserviceId} has been removed successfully.`) + try { + const arr = obj.add.split(':'); + const sourceMicroserviceId = arr[0]; + const destMicroserviceId = arr[1]; + await MicroserviceService.deleteRouteWithTransaction(sourceMicroserviceId, destMicroserviceId, {}, true); + logger.info(`Microservice route with source microservice ${obj.sourceMicroserviceId} and dest microservice + ${obj.destMicroserviceId} has been removed successfully.`); + } catch (e) { + logger.error(ErrorMessages.CLI.INVALID_ROUTE); + } } else if (obj.add && obj.remove) { logger.info('Please specify either "add" or "remove" operation'); } else { @@ -271,19 +312,17 @@ const _executePortMappingCommand = async function (obj) { logger.info(JSON.stringify(obj)); if (obj.create) { - - let mapping = { - internal: parseInt(obj.internal), - external: parseInt(obj.external), - publicMode: AppHelper.validateBooleanCliOptions(obj.public, obj.private) - }; - mapping = AppHelper.deleteUndefinedFields(mapping); + const mapping = parsePortMappingObject(obj.create, ErrorMessages.CLI.INVALID_PORT_MAPPING); await MicroserviceService.createPortMappingWithTransaction(obj.microserviceId, mapping, {}, true); logger.info('Port mapping has been create successfully'); - } else if (obj.delete) { - await MicroserviceService.deletePortMappingWithTransaction(obj.microserviceId, obj.internal, {}, true); - logger.info('Port mapping has been deleted successfully'); + try { + const internalPort = parseInt(obj.delete); + await MicroserviceService.deletePortMappingWithTransaction(obj.microserviceId, internalPort, {}, true); + logger.info('Port mapping has been deleted successfully'); + } catch(e) { + logger.error(ErrorMessages.CLI.INVALID_INTERNAL_PORT); + } } else if (obj.list) { await MicroserviceService.getMicroservicePortMappingListWithTransaction(obj.microserviceId, {}, true); logger.info('Port mappings have been retrieved successfully'); @@ -349,7 +388,7 @@ const _updateMicroserviceObject = function (obj) { } return AppHelper.deleteUndefinedFields(microserviceObj); -} +}; const _createMicroserviceObject = function (obj) { const microserviceObj = { @@ -364,29 +403,52 @@ const _createMicroserviceObject = function (obj) { }; if (obj.volumes) { - microserviceObj.volumeMappings = parseObjectArray(obj.volumes, 'Error during parsing of volume mapping option.'); + microserviceObj.volumeMappings = parseVolumes(obj.volumes, ErrorMessages.CLI.INVALID_VOLUME_MAPPING); } if (obj.ports) { - microserviceObj.ports = parseObjectArray(obj.ports, 'Error during parsing of port mapping option.'); + microserviceObj.ports = parsePortMappingArray(obj.ports, ErrorMessages.CLI.INVALID_PORT_MAPPING); } return AppHelper.deleteUndefinedFields(microserviceObj); }; - -const parseObjectArray = function (arr, errMsg) { +const parseVolumes = function (arr, errMsg) { return arr.map(item => { - item = item.replace(/'/g, '"'); let result = {}; try { - result = JSON.parse(item); + const props = item.split(':'); + result = { + hostDestination: props[0], + containerDestination: props[1], + accessMode: props[2] + } } catch(e) { logger.warn(errMsg); - logger.warn(e.message); } return result; }) }; +const parsePortMappingObject = function (obj, errMsg) { + let result = {}; + try { + const props = obj.split(':'); + result = { + internal: parseInt(props[0]), + external: parseInt(props[1]), + publicMode: props[2] === 'true' + } + } catch(e) { + logger.warn(errMsg); + } + return result; +} + +const parsePortMappingArray = function (arr, errMsg) { + return arr.map(obj => { + parsePortMappingObject(obj, errMsg); + }) +}; + module.exports = new Microservice(); \ No newline at end of file diff --git a/src/cli/proxy.js b/src/cli/tunnel.js similarity index 100% rename from src/cli/proxy.js rename to src/cli/tunnel.js diff --git a/src/decorators/transaction-decorator.js b/src/decorators/transaction-decorator.js index 2ffc3e7c8..2e0a6eac0 100644 --- a/src/decorators/transaction-decorator.js +++ b/src/decorators/transaction-decorator.js @@ -27,13 +27,20 @@ function transaction(f) { } } +// function generateTransaction(f) { +// return function () { +// const args = Array.prototype.slice.call(arguments); +// return retry(() => { +// const t = transaction(f); +// return t.apply(this, args); +// }, 5) +// } +// } + function generateTransaction(f) { - return function () { - const args = Array.prototype.slice.call(arguments); - return retry(() => { - const t = transaction(f); - return t.apply(this, args); - }, 5) + return function() { + const fArgs = Array.prototype.slice.call(arguments); + return f.apply(this, fArgs); } } diff --git a/src/helpers/app-helper.js b/src/helpers/app-helper.js index 8542a5232..b6f07748f 100644 --- a/src/helpers/app-helper.js +++ b/src/helpers/app-helper.js @@ -136,6 +136,12 @@ function formatMessage() { return format.apply(null, argsArray); } +function stringifyCliJsonSchema(json) { + return JSON.stringify(json, null, 2) + .replace(/{/g, "\\{") + .replace(/}/g, "\\}"); +} + module.exports = { encryptText, decryptText, @@ -150,5 +156,6 @@ module.exports = { deleteUndefinedFields, validateBooleanCliOptions, formatMessage, - findAvailablePort + findAvailablePort, + stringifyCliJsonSchema }; diff --git a/src/helpers/error-messages.js b/src/helpers/error-messages.js index 71dd583f4..49e65fa26 100644 --- a/src/helpers/error-messages.js +++ b/src/helpers/error-messages.js @@ -45,5 +45,12 @@ module.exports = { INVALID_MICROSERVICE_USER: 'Invalid microservice user or uuid', ROUTE_NOT_FOUND: 'Route not found', IMAGE_SNAPSHOT_WITHOUT_FOG: 'Can not run image snapshot for element without fog.', - FILE_DOES_NOT_EXIST: 'File does not exist.' + FILE_DOES_NOT_EXIST: 'File does not exist.', + REQUIRED_FOG_NODE: 'fog node is required.', + CLI: { + INVALID_PORT_MAPPING: 'Port mapping parsing error. Please provide valid port mapping.', + INVALID_VOLUME_MAPPING: 'Volume mapping parsing error. Please provide valid volume mapping.', + INVALID_INTERNAL_PORT: 'Internal parsing error. Please provide valid internal port.', + INVALID_ROUTE: 'Route parsing error. Please provide valid route.' + } }; diff --git a/src/services/microservices-service.js b/src/services/microservices-service.js index 94d1422ba..28e49ebca 100644 --- a/src/services/microservices-service.js +++ b/src/services/microservices-service.js @@ -81,7 +81,7 @@ const _createMicroserviceOnFog = async function (microserviceData, user, isCLI, if (microserviceData.ports) { for (const port of microserviceData.ports) { - await _createPortMapping(microservice.uuid, port, user, transaction); + await _createPortMapping(microservice.uuid, port, user, isCLI, transaction); } } if (microserviceData.volumeMappings) { @@ -528,13 +528,13 @@ async function _createPortMapping(microserviceUuid, portMappingData, user, isCLI ? {uuid: microserviceUuid} : {uuid: microserviceUuid, updatedBy: user.id}; - const microservice = await MicroserviceManager.findOne(where, transaction) + const microservice = await MicroserviceManager.findOne(where, transaction); if (!microservice) { throw new Errors.NotFoundError(AppHelper.formatMessage(ErrorMessages.INVALID_MICROSERVICE_UUID, microserviceUuid)) } if (!microservice.iofogUuid) { - throw new Errors.ValidationError('fog not set') + throw new Errors.ValidationError(AppHelper.formatMessage(ErrorMessages.REQUIRED_FOG_NODE)); } const msPorts = await MicroservicePortManager.findOne({ @@ -548,7 +548,7 @@ async function _createPortMapping(microserviceUuid, portMappingData, user, isCLI portExternal: portMappingData.external } ] - }, transaction) + }, transaction); if (msPorts) { throw new Errors.ValidationError('port mapping already exists') } From 32196423f2a0aecba68870293097c4e1a17b725b Mon Sep 17 00:00:00 2001 From: Pankov Date: Mon, 29 Oct 2018 19:35:40 +0300 Subject: [PATCH 10/15] Merge branch 'new-fog-controller' of https://github.com/ioFog/FogController into epankou/feature-microservice-cli # Conflicts: # src/cli/microservice.js # src/controllers/microservices-controller.js # src/decorators/transaction-decorator.js # src/helpers/error-messages.js # src/services/microservices-service.js --- package.json | 2 -- src/cli/microservice.js | 12 ++---------- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index acda6a4cf..3e3cbe97f 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,6 @@ "fs": "^0.0.1-security", "ftp": "^0.3.10", "helmet": "^3.13.0", - "json-beautify": "^1.0.1", "jsonschema": "^1.2.4", "morgan": "^1.9.1", "nconf": "^0.10.0", @@ -45,7 +44,6 @@ "nodemailer-smtp-transport": "^2.7.4", "path": "^0.12.7", "portscanner": "^2.2.0", - "prettyjson": "^1.2.1", "querystring": "^0.2.0", "retry-as-promised": "^3.1.0", "sequelize": "^4.39.0", diff --git a/src/cli/microservice.js b/src/cli/microservice.js index 07368c28c..88e9cd500 100644 --- a/src/cli/microservice.js +++ b/src/cli/microservice.js @@ -82,14 +82,6 @@ class Microservice extends BaseCLIHandler { name: 'microservice-id', alias: 'i', type: String, description: 'Microservice ID', group: [constants.CMD_UPDATE, constants.CMD_REMOVE, constants.CMD_INFO, constants.CMD_PORT_MAPPING] }, - { - name: 'dest-microservice-id', alias: 'D', type: String, description: 'Destination Microservice ID of route', - group: [constants.CMD_ROUTE] - }, - { - name: 'source-microservice-id', alias: 'S', type: String, description: 'Source Microservice ID of route', - group: [constants.CMD_ROUTE] - }, { name: 'name', alias: 'n', type: String, description: 'Microservice name', group: [constants.CMD_UPDATE, constants.CMD_ADD] @@ -135,11 +127,11 @@ class Microservice extends BaseCLIHandler { group: [constants.CMD_ADD] }, { - name: 'add', alias: 'a', type: Boolean, description: 'Add new route(s)', + name: 'add', alias: 'a', type: String, description: 'Add new route(s)', group: [constants.CMD_ROUTE] }, { - name: 'remove', alias: 'm', type: Boolean, description: 'Delete existing route(s)', + name: 'remove', alias: 'm', type: String, description: 'Delete existing route(s)', group: [constants.CMD_ROUTE] }, { From f662aa9048e47e5ae3660b63ed3ad56ad38f1bf0 Mon Sep 17 00:00:00 2001 From: Pankov Date: Mon, 29 Oct 2018 20:11:29 +0300 Subject: [PATCH 11/15] Merge branch 'new-fog-controller' of https://github.com/ioFog/FogController into epankou/feature-microservice-cli # Conflicts: # src/cli/microservice.js # src/controllers/microservices-controller.js # src/decorators/transaction-decorator.js # src/helpers/error-messages.js # src/services/microservices-service.js --- src/cli/microservice.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/cli/microservice.js b/src/cli/microservice.js index 88e9cd500..9f66dd8af 100644 --- a/src/cli/microservice.js +++ b/src/cli/microservice.js @@ -233,19 +233,19 @@ class Microservice extends BaseCLIHandler { example: '$ fog-controller microservice add [other required options] --ports 80:8080:false 443:5443:false', }, { - desc: '4. Add routes', + desc: '4. Add routes (sourceMicroserviceId:destMicroserviceId)', example: '$ fog-controller microservice route --add ABC:DEF', }, { - desc: '5. Delete route', + desc: '5. Delete route (sourceMicroserviceId:destMicroserviceId)', example: '$ fog-controller microservice route --remove ABC:DEF', }, { - desc: '6. Create port mapping', + desc: '6. Create port mapping (internal:external:publicMode)', example: '$ fog-controller microservice port-mapping --create 80:8080:false -i ABC' }, { - desc: '7. Delete port mapping', + desc: '7. Delete port mapping (internal)', example: '$ fog-controller microservice port-mapping --delete 80 -i ABC' } ], @@ -331,14 +331,14 @@ const _removeMicroservice = async function (obj) { const _listMicroservices = async function () { const result = await MicroserviceService.listMicroservicesWithTransaction({}, {}, true); - logger.info(JSON.stringify(result)); + logger.info(JSON.stringify(result, null, 2)); logger.info('Microservices have been retrieved successfully.'); }; const _getMicroservice = async function (obj) { logger.info(JSON.stringify(obj)); const result = await MicroserviceService.getMicroserviceWithTransaction(obj.microserviceId, {}, true); - logger.info(JSON.stringify(result)); + logger.info(JSON.stringify(result, null, 2)); logger.info('Microservice has been retrieved successfully.'); }; @@ -438,9 +438,7 @@ const parsePortMappingObject = function (obj, errMsg) { } const parsePortMappingArray = function (arr, errMsg) { - return arr.map(obj => { - parsePortMappingObject(obj, errMsg); - }) + return arr.map(obj => parsePortMappingObject(obj, errMsg)); }; module.exports = new Microservice(); \ No newline at end of file From b3b154703bd0f9b9a9618360a142f897a858b34b Mon Sep 17 00:00:00 2001 From: Pankov Date: Mon, 29 Oct 2018 20:22:59 +0300 Subject: [PATCH 12/15] Merge branch 'new-fog-controller' of https://github.com/ioFog/FogController into epankou/feature-microservice-cli # Conflicts: # src/cli/microservice.js # src/controllers/microservices-controller.js # src/decorators/transaction-decorator.js # src/helpers/error-messages.js # src/services/microservices-service.js --- src/cli/microservice.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cli/microservice.js b/src/cli/microservice.js index 9f66dd8af..d18ae4ab3 100644 --- a/src/cli/microservice.js +++ b/src/cli/microservice.js @@ -376,7 +376,7 @@ const _updateMicroserviceObject = function (obj) { }; if (obj.volumes) { - microserviceObj.volumeMappings = parseObjectArray(obj.volumes, 'Error during parsing of volume mapping option.'); + microserviceObj.volumeMappings = parseVolumes(obj.volumes, 'Error during parsing of volume mapping option.'); } return AppHelper.deleteUndefinedFields(microserviceObj); From 7dc67a33cdac4f84e83115a14d74a6dd70799be7 Mon Sep 17 00:00:00 2001 From: Pankov Date: Tue, 30 Oct 2018 11:42:50 +0300 Subject: [PATCH 13/15] Merge branch 'new-fog-controller' of https://github.com/ioFog/FogController into epankou/feature-microservice-cli # Conflicts: # src/cli/microservice.js --- src/cli/microservice.js | 24 +++++++++--------------- src/main.js | 2 -- src/sequelize/managers/base-manager.js | 7 +++++++ src/services/flow-service.js | 13 +++++-------- src/services/microservices-service.js | 25 +++++++++++++------------ 5 files changed, 34 insertions(+), 37 deletions(-) diff --git a/src/cli/microservice.js b/src/cli/microservice.js index f25b04718..a5459fe9e 100644 --- a/src/cli/microservice.js +++ b/src/cli/microservice.js @@ -175,25 +175,25 @@ class Microservice extends BaseCLIHandler { switch (microserviceCommand.command.command) { case constants.CMD_ADD: - await _executeCase(microserviceCommand, constants.CMD_ADD, _createMicroservice, false); + await _executeCase(microserviceCommand, constants.CMD_ADD, _createMicroservice); break; case constants.CMD_UPDATE: - await _executeCase(microserviceCommand, constants.CMD_UPDATE, _updateMicroservice, false); + await _executeCase(microserviceCommand, constants.CMD_UPDATE, _updateMicroservice); break; case constants.CMD_REMOVE: - await _executeCase(microserviceCommand, constants.CMD_REMOVE, _removeMicroservice, false); + await _executeCase(microserviceCommand, constants.CMD_REMOVE, _removeMicroservice); break; case constants.CMD_LIST: - await _executeCase(microserviceCommand, constants.CMD_LIST, _listMicroservices, false); + await _executeCase(microserviceCommand, constants.CMD_LIST, _listMicroservices); break; case constants.CMD_INFO: - await _executeCase(microserviceCommand, constants.CMD_INFO, _getMicroservice, false); + await _executeCase(microserviceCommand, constants.CMD_INFO, _getMicroservice); break; case constants.CMD_ROUTE: - await _executeCase(microserviceCommand, constants.CMD_ROUTE, _executeRouteCommand, false); + await _executeCase(microserviceCommand, constants.CMD_ROUTE, _executeRouteCommand); break; case constants.CMD_PORT_MAPPING: - await _executeCase(microserviceCommand, constants.CMD_PORT_MAPPING, _executePortMappingCommand, false); + await _executeCase(microserviceCommand, constants.CMD_PORT_MAPPING, _executePortMappingCommand); break; case constants.CMD_HELP: default: @@ -254,16 +254,10 @@ class Microservice extends BaseCLIHandler { } } -const _executeCase = async function (microserviceCommand, commandName, f, isUserRequired) { +const _executeCase = async function (microserviceCommand, commandName, f) { try { const item = microserviceCommand[commandName] || {}; - - if (isUserRequired) { - const decoratedFunction = AuthDecorator.prepareUserById(f); - decoratedFunction(item); - } else { - f(item); - } + await f(item); } catch (error) { logger.error(error.message); } diff --git a/src/main.js b/src/main.js index e1324f2ff..dce2424e1 100644 --- a/src/main.js +++ b/src/main.js @@ -1,5 +1,3 @@ -#!/usr/bin/env node - /* * ******************************************************************************* * * Copyright (c) 2018 Edgeworx, Inc. diff --git a/src/sequelize/managers/base-manager.js b/src/sequelize/managers/base-manager.js index 276c4cfe1..8e8af3ef9 100644 --- a/src/sequelize/managers/base-manager.js +++ b/src/sequelize/managers/base-manager.js @@ -42,6 +42,13 @@ module.exports = class BaseManager { }); } + async findAnother(object) { + + return this.getEntity().findOne({ + where: object + }); + } + async create(object, transaction) { AppHelper.checkTransaction(transaction); return this.getEntity().create(object, { diff --git a/src/services/flow-service.js b/src/services/flow-service.js index aa70d1041..3c06f0b36 100644 --- a/src/services/flow-service.js +++ b/src/services/flow-service.js @@ -17,7 +17,7 @@ const AppHelper = require('../helpers/app-helper'); const Errors = require('../helpers/errors'); const ErrorMessages = require('../helpers/error-messages'); const Validation = require('../schemas'); -const MicroserviceService = require('./microservices-service') +// const MicroserviceService = require('./microservices-service') const ChangeTrackingManager = require('../sequelize/managers/change-tracking-manager') const _createFlow = async function (flowData, user, isCLI, transaction) { @@ -103,18 +103,15 @@ const _updateFlow = async function (flowData, flowId, user, isCLI, transaction) }; const _getFlow = async function (flowId, user, isCLI, transaction) { - const whereObj = { - id: flowId, - userId: user.id - }; - const where = AppHelper.deleteUndefinedFields(whereObj); + const where = isCLI + ? {id: flowId} + : {id: flowId, userId: user.id}; const flow = await FlowManager.findOne(where, transaction); if (!flow) { throw new Errors.NotFoundError(AppHelper.formatMessage(ErrorMessages.INVALID_FLOW_ID, flowId)) } - return flow }; @@ -147,5 +144,5 @@ module.exports = { getFlowWithTransaction: TransactionDecorator.generateTransaction(_getFlow), getUserFlowsWithTransaction: TransactionDecorator.generateTransaction(_getUserFlows), getAllFlowsWithTransaction: TransactionDecorator.generateTransaction(_getAllFlows), - getFlow: _getFlow + getFlow: _getFlow, }; diff --git a/src/services/microservices-service.js b/src/services/microservices-service.js index 910a56555..d646dff6b 100644 --- a/src/services/microservices-service.js +++ b/src/services/microservices-service.js @@ -19,17 +19,16 @@ const ConnectorManager = require('../sequelize/managers/connector-manager'); const ConnectorPortManager = require('../sequelize/managers/connector-port-manager'); const MicroservicePublicModeManager = require('../sequelize/managers/microservice-public-mode-manager'); const ChangeTrackingManager = require('../sequelize/managers/change-tracking-manager'); -const FlowService = require('../services/flow-service'); const IoFogService = require('../services/iofog-service'); const AppHelper = require('../helpers/app-helper'); const Errors = require('../helpers/errors'); const ErrorMessages = require('../helpers/error-messages'); const Validation = require('../schemas/index'); const ConnectorService = require('../services/connector-service'); +const FlowService = require('../services/flow-service'); const CatalogService = require('../services/catalog-service'); const RoutingManager = require('../sequelize/managers/routing-manager'); const Op = require('sequelize').Op; -const Sequelize = require('sequelize'); const _listMicroservices = async function (flowId, user, isCLI, transaction) { if (!isCLI) { @@ -77,6 +76,8 @@ const _getMicroservice = async function (microserviceUuid, user, isCLI, transact const _createMicroserviceOnFog = async function (microserviceData, user, isCLI, transaction) { await Validation.validate(microserviceData, Validation.schemas.microserviceCreate); + await FlowService.getFlowWithTransaction(1, user, isCLI, transaction); + const microservice = await _createMicroservice(microserviceData, user, isCLI, transaction); if (microserviceData.ports) { @@ -103,7 +104,7 @@ const _createMicroserviceOnFog = async function (microserviceData, user, isCLI, const _createMicroservice = async function (microserviceData, user, isCLI, transaction) { - const microserviceToCreate = { + let microservice = { uuid: AppHelper.generateRandomString(32), name: microserviceData.name, config: microserviceData.config, @@ -115,20 +116,20 @@ const _createMicroservice = async function (microserviceData, user, isCLI, trans updatedBy: user.id }; - const microserviceDataCreate = AppHelper.deleteUndefinedFields(microserviceToCreate); + microservice = AppHelper.deleteUndefinedFields(microservice); - await _checkForDuplicateName(microserviceDataCreate.name, {}, transaction); + await _checkForDuplicateName(microservice.name, {}, transaction); //validate catalog item - await CatalogService.getCatalogItem(microserviceDataCreate.catalogItemId, user, isCLI, transaction); + await CatalogService.getCatalogItem(microservice.catalogItemId, user, isCLI, transaction); //validate flow - await FlowService.getFlow(microserviceDataCreate.flowId, user, isCLI, transaction); + await FlowService.getFlow(microservice.flowId, user, isCLI, transaction); //validate fog node - if (microserviceDataCreate.iofogUuid) { - await IoFogService.getFog({uuid: microserviceDataCreate.iofogUuid}, user, isCLI, transaction); + if (microservice.iofogUuid) { + await IoFogService.getFog({uuid: microservice.iofogUuid}, user, isCLI, transaction); } - return await MicroserviceManager.create(microserviceDataCreate, transaction); + return await MicroserviceManager.create(microservice, transaction); }; const _createMicroservicePorts = async function (ports, microserviceUuid, transaction) { @@ -250,7 +251,7 @@ const _checkForDuplicateName = async function (name, item, transaction) { ? {name: name, uuid: {[Op.ne]: item.id}} : {name: name}; - const result = await MicroserviceManager.findOne(where, transaction); + const result = await MicroserviceManager.findAnother(where); if (result) { throw new Errors.DuplicatePropertyError(AppHelper.formatMessage(ErrorMessages.DUPLICATE_NAME, name)); } @@ -813,5 +814,5 @@ module.exports = { getMicroservicePortMappingListWithTransaction: TransactionDecorator.generateTransaction(_getPortMappingList), deletePortMappingWithTransaction: TransactionDecorator.generateTransaction(_deletePortMapping), getPhysicalConections: getPhysicalConections, - getListMicroservices: _listMicroservices + getListMicroservices: _listMicroservices, }; From 308c83cae81d108e3acef45cc3339a6770d98c68 Mon Sep 17 00:00:00 2001 From: Pankov Date: Tue, 30 Oct 2018 19:49:48 +0300 Subject: [PATCH 14/15] Merge branch 'new-fog-controller' of https://github.com/ioFog/FogController into epankou/feature-microservice-cli # Conflicts: # src/cli/microservice.js --- src/cli/microservice.js | 158 +++++++++++++------------ src/helpers/constants.js | 7 +- src/main.js | 2 + src/sequelize/managers/base-manager.js | 7 -- src/sequelize/managers/flow-manager.js | 18 +++ src/services/flow-service.js | 93 ++++++++------- src/services/microservices-service.js | 22 ++-- 7 files changed, 164 insertions(+), 143 deletions(-) diff --git a/src/cli/microservice.js b/src/cli/microservice.js index a5459fe9e..9a093c5cb 100644 --- a/src/cli/microservice.js +++ b/src/cli/microservice.js @@ -80,7 +80,7 @@ class Microservice extends BaseCLIHandler { }, { name: 'microservice-id', alias: 'i', type: String, description: 'Microservice ID', - group: [constants.CMD_UPDATE, constants.CMD_REMOVE, constants.CMD_INFO, constants.CMD_PORT_MAPPING] + group: [constants.CMD_UPDATE, constants.CMD_REMOVE, constants.CMD_INFO, constants.CMD_PORT_MAPPING_CREATE, constants.CMD_PORT_MAPPING_REMOVE] }, { name: 'name', alias: 'n', type: String, description: 'Microservice name', @@ -123,28 +123,20 @@ class Microservice extends BaseCLIHandler { group: [constants.CMD_ADD] }, { - name: 'routes', alias: 't', type: String, description: 'Microservice route(s) (receiving microservices)', multiple: true, - group: [constants.CMD_ADD] + name: 'mapping', alias: 'P', type: String, description: 'Container port mapping', + group: [constants.CMD_PORT_MAPPING_CREATE] }, { - name: 'add', alias: 'a', type: String, description: 'Add new route(s)', - group: [constants.CMD_ROUTE] - }, - { - name: 'remove', alias: 'm', type: String, description: 'Delete existing route(s)', - group: [constants.CMD_ROUTE] - }, - { - name: 'create', alias: 'b', type: String, description: 'Add new port mapping(s)', - group: [constants.CMD_PORT_MAPPING] + name: 'routes', alias: 't', type: String, description: 'Microservice route(s) (receiving microservices)', multiple: true, + group: [constants.CMD_ADD] }, { - name: 'delete', alias: 'B', type: String, description: 'Delete existing port mapping(s)', - group: [constants.CMD_PORT_MAPPING] + name: 'route', alias: 'T', type: String, description: 'Microservice route (receiving microservices)', + group: [constants.CMD_ROUTE_CREATE, constants.CMD_ROUTE_REMOVE] }, { - name: 'list', alias: 'G', type: Boolean, description: 'List port mappings', - group: [constants.CMD_PORT_MAPPING] + name: 'internal-port', alias: 'b', type: String, description: 'Internal port', + group: [constants.CMD_PORT_MAPPING_REMOVE] }, { name: 'rebuild', alias: 'w', type: Boolean, description: 'Rebuild microservice image on fog agent', @@ -165,8 +157,11 @@ class Microservice extends BaseCLIHandler { [constants.CMD_REMOVE]: 'Delete a microservice.', [constants.CMD_LIST]: 'List all microservices.', [constants.CMD_INFO]: 'Get microservice settings.', - [constants.CMD_ROUTE]: 'Add/Remove microservice route.', - [constants.CMD_PORT_MAPPING]: 'Create/Delete/List microservice port mapping.' + [constants.CMD_ROUTE_CREATE]: 'Create microservice route.', + [constants.CMD_ROUTE_REMOVE]: 'Remove microservice route.', + [constants.CMD_PORT_MAPPING_CREATE]: 'Create microservice port mapping.', + [constants.CMD_PORT_MAPPING_REMOVE]: 'Remove microservice port mapping.', + [constants.CMD_PORT_MAPPING_LIST]: 'List microservice port mapping.' } } @@ -189,11 +184,20 @@ class Microservice extends BaseCLIHandler { case constants.CMD_INFO: await _executeCase(microserviceCommand, constants.CMD_INFO, _getMicroservice); break; - case constants.CMD_ROUTE: - await _executeCase(microserviceCommand, constants.CMD_ROUTE, _executeRouteCommand); + case constants.CMD_ROUTE_CREATE: + await _executeCase(microserviceCommand, constants.CMD_ROUTE_CREATE, _createRoute); + break; + case constants.CMD_ROUTE_REMOVE: + await _executeCase(microserviceCommand, constants.CMD_ROUTE_REMOVE, _removeRoute); break; - case constants.CMD_PORT_MAPPING: - await _executeCase(microserviceCommand, constants.CMD_PORT_MAPPING, _executePortMappingCommand); + case constants.CMD_PORT_MAPPING_CREATE: + await _executeCase(microserviceCommand, constants.CMD_PORT_MAPPING_CREATE, _createPortMapping); + break; + case constants.CMD_PORT_MAPPING_REMOVE: + await _executeCase(microserviceCommand, constants.CMD_PORT_MAPPING_REMOVE, _removePortMapping); + break; + case constants.CMD_PORT_MAPPING_LIST: + await _executeCase(microserviceCommand, constants.CMD_PORT_MAPPING_LIST, _listPortMappings); break; case constants.CMD_HELP: default: @@ -229,24 +233,28 @@ class Microservice extends BaseCLIHandler { example: '$ iofog-controller microservice add [other required options] --volumes /host_src:/container_src /host_bin:/container_bin', }, { - desc: '3. Port mapping (internal:external:publicMode)', + desc: '3. Port mapping (80:8080:false - internal port : external port : public mode)', example: '$ iofog-controller microservice add [other required options] --ports 80:8080:false 443:5443:false', }, { - desc: '4. Add routes (sourceMicroserviceId:destMicroserviceId)', - example: '$ iofog-controller microservice route --add ABC:DEF', + desc: '4. Add routes (ABC:DEF - source microservice id : dest microservice id)', + example: '$ iofog-controller microservice add [other required options] --routes ABC:DEF RFG:HJK' }, { - desc: '5. Delete route (sourceMicroserviceId:destMicroserviceId)', - example: '$ iofog-controller microservice route --remove ABC:DEF', + desc: '4. Add route (ABC:DEF - source microservice id : dest microservice id)', + example: '$ iofog-controller microservice route-create --route ABC:DEF', }, { - desc: '6. Create port mapping (internal:external:publicMode)', - example: '$ iofog-controller microservice port-mapping --create 80:8080:false -i ABC' + desc: '5. Delete route (ABC:DEF - source microservice id : dest microservice id)', + example: '$ iofog-controller microservice route-remove --route ABC:DEF', }, { - desc: '7. Delete port mapping (internal)', - example: '$ iofog-controller microservice port-mapping --delete 80 -i ABC' + desc: '6. Create port mapping (80:8080:false - internal port : external port : public mode, ABC - microservice)', + example: '$ iofog-controller microservice port-mapping-create --mapping 80:8080:false -i ABC' + }, + { + desc: '7. Delete port mapping (80 - internal port, ABC - microservice id)', + example: '$ iofog-controller microservice port-mapping-remove --internal-port 80 -i ABC' } ], }, @@ -263,60 +271,58 @@ const _executeCase = async function (microserviceCommand, commandName, f) { } }; -const _executeRouteCommand = async function (obj) { +const _createRoute = async function (obj) { logger.info(JSON.stringify(obj)); - if (obj.add) { - try { - const arr = obj.add.split(':'); - const sourceMicroserviceId = arr[0]; - const destMicroserviceId = arr[1]; - await MicroserviceService.createRouteWithTransaction(sourceMicroserviceId, destMicroserviceId, {}, true); - logger.info(`Microservice route with source microservice ${sourceMicroserviceId} and dest microservice + try { + const arr = obj.route.split(':'); + const sourceMicroserviceId = arr[0]; + const destMicroserviceId = arr[1]; + await MicroserviceService.createRouteWithTransaction(sourceMicroserviceId, destMicroserviceId, {}, true); + logger.info(`Microservice route with source microservice ${sourceMicroserviceId} and dest microservice ${destMicroserviceId} has been created successfully.`) - } catch (e) { - logger.error(ErrorMessages.CLI.INVALID_ROUTE); - } - } else if (obj.remove) { - try { - const arr = obj.add.split(':'); - const sourceMicroserviceId = arr[0]; - const destMicroserviceId = arr[1]; - await MicroserviceService.deleteRouteWithTransaction(sourceMicroserviceId, destMicroserviceId, {}, true); - logger.info(`Microservice route with source microservice ${obj.sourceMicroserviceId} and dest microservice + } catch (e) { + logger.error(ErrorMessages.CLI.INVALID_ROUTE); + } +}; + +const _removeRoute = async function (obj) { + logger.info(JSON.stringify(obj)); + try { + const arr = obj.port.split(':'); + const sourceMicroserviceId = arr[0]; + const destMicroserviceId = arr[1]; + await MicroserviceService.deleteRouteWithTransaction(sourceMicroserviceId, destMicroserviceId, {}, true); + logger.info(`Microservice route with source microservice ${obj.sourceMicroserviceId} and dest microservice ${obj.destMicroserviceId} has been removed successfully.`); - } catch (e) { - logger.error(ErrorMessages.CLI.INVALID_ROUTE); - } - } else if (obj.add && obj.remove) { - logger.info('Please specify either "add" or "remove" operation'); - } else { - logger.info('No operation specified'); + } catch (e) { + logger.error(ErrorMessages.CLI.INVALID_ROUTE); } }; -const _executePortMappingCommand = async function (obj) { +const _createPortMapping = async function (obj) { logger.info(JSON.stringify(obj)); + const mapping = parsePortMappingObject(obj.mapping, ErrorMessages.CLI.INVALID_PORT_MAPPING); + await MicroserviceService.createPortMappingWithTransaction(obj.microserviceId, mapping, {}, true); + logger.info('Port mapping has been create successfully'); +}; - if (obj.create) { - const mapping = parsePortMappingObject(obj.create, ErrorMessages.CLI.INVALID_PORT_MAPPING); - await MicroserviceService.createPortMappingWithTransaction(obj.microserviceId, mapping, {}, true); - logger.info('Port mapping has been create successfully'); - } else if (obj.delete) { - try { - const internalPort = parseInt(obj.delete); - await MicroserviceService.deletePortMappingWithTransaction(obj.microserviceId, internalPort, {}, true); - logger.info('Port mapping has been deleted successfully'); - } catch(e) { - logger.error(ErrorMessages.CLI.INVALID_INTERNAL_PORT); - } - } else if (obj.list) { - await MicroserviceService.getMicroservicePortMappingListWithTransaction(obj.microserviceId, {}, true); - logger.info('Port mappings have been retrieved successfully'); - } else { - logger.info('Incorrect command usage. Please specify only one command at once'); +const _removePortMapping = async function (obj) { + logger.info(JSON.stringify(obj)); + try { + const internalPort = parseInt(obj.delete); + await MicroserviceService.deletePortMappingWithTransaction(obj.microserviceId, internalPort, {}, true); + logger.info('Port mapping has been deleted successfully'); + } catch(e) { + logger.error(ErrorMessages.CLI.INVALID_INTERNAL_PORT); } }; +const _listPortMappings = async function () { + const result = await MicroserviceService.getMicroservicePortMappingListWithTransaction(obj.microserviceId, {}, true); + logger.info(JSON.stringify(result)); + logger.info('Port mappings have been retrieved successfully'); +}; + const _removeMicroservice = async function (obj) { logger.info(JSON.stringify(obj)); await MicroserviceService.deleteMicroserviceWithTransaction(obj.microserviceId, obj.cleanUp, {}, true); @@ -325,7 +331,7 @@ const _removeMicroservice = async function (obj) { const _listMicroservices = async function () { const result = await MicroserviceService.listMicroservicesWithTransaction({}, {}, true); - logger.info(JSON.stringify(result, null, 2)); + logger.info(JSON.stringify(result)); logger.info('Microservices have been retrieved successfully.'); }; diff --git a/src/helpers/constants.js b/src/helpers/constants.js index 58964cbbe..3e7eee6d1 100644 --- a/src/helpers/constants.js +++ b/src/helpers/constants.js @@ -36,8 +36,11 @@ module.exports = { CMD_CATALOG: 'catalog', CMD_FLOW: 'flow', CMD_MICROSERVICE: 'microservice', - CMD_ROUTE: 'route', - CMD_PORT_MAPPING: 'port-mapping', + CMD_ROUTE_CREATE: 'route-create', + CMD_ROUTE_REMOVE: 'route-remove', + CMD_PORT_MAPPING_CREATE: 'port-mapping-create', + CMD_PORT_MAPPING_REMOVE: 'port-mapping-remove', + CMD_PORT_MAPPING_LIST: 'port-mapping-list', CMD_REGISTRY: 'registry', CMD_ACTIVATE: 'activate', CMD_SUSPEND: 'suspend', diff --git a/src/main.js b/src/main.js index dce2424e1..e1324f2ff 100644 --- a/src/main.js +++ b/src/main.js @@ -1,3 +1,5 @@ +#!/usr/bin/env node + /* * ******************************************************************************* * * Copyright (c) 2018 Edgeworx, Inc. diff --git a/src/sequelize/managers/base-manager.js b/src/sequelize/managers/base-manager.js index 8e8af3ef9..276c4cfe1 100644 --- a/src/sequelize/managers/base-manager.js +++ b/src/sequelize/managers/base-manager.js @@ -42,13 +42,6 @@ module.exports = class BaseManager { }); } - async findAnother(object) { - - return this.getEntity().findOne({ - where: object - }); - } - async create(object, transaction) { AppHelper.checkTransaction(transaction); return this.getEntity().create(object, { diff --git a/src/sequelize/managers/flow-manager.js b/src/sequelize/managers/flow-manager.js index d993044d3..798d9aa66 100644 --- a/src/sequelize/managers/flow-manager.js +++ b/src/sequelize/managers/flow-manager.js @@ -14,12 +14,30 @@ const BaseManager = require('./base-manager'); const models = require('./../models'); const Flow = models.Flow; +const Microservice = models.Microservice; +const sequelize = require('sequelize'); class FlowManager extends BaseManager { getEntity() { return Flow } + + async findFlowMicroservices(where, transaction) { + return Flow.findOne({ + include: [ + { + model: Microservice, + as: 'microservice', + required: false, + attributes: ['iofogUuid'] + } + ], + where: where, + attributes: ['id'] + }, {transaction: transaction}) + } } + const instance = new FlowManager(); module.exports = instance; \ No newline at end of file diff --git a/src/services/flow-service.js b/src/services/flow-service.js index 3c06f0b36..9bd5bca6e 100644 --- a/src/services/flow-service.js +++ b/src/services/flow-service.js @@ -24,6 +24,7 @@ const _createFlow = async function (flowData, user, isCLI, transaction) { await Validation.validate(flowData, Validation.schemas.flowCreate); await isFlowExist(flowData.name, transaction); + await _checkForDuplicateName(flowData.name, {}, transaction); const flowToCreate = { name: flowData.name, @@ -56,50 +57,47 @@ const _deleteFlow = async function (flowId, user, isCLI, transaction) { }; const _updateFlow = async function (flowData, flowId, user, isCLI, transaction) { - await Validation.validate(flowData, Validation.schemas.flowUpdate); + await Validation.validate(flowData, Validation.schemas.flowUpdate); - const oldFlow = await _getFlow(flowId, user, isCLI, transaction); + const oldFlow = await _getFlow(flowId, user, isCLI, transaction); + if (!oldFlow) { + throw new Errors.NotFoundError(ErrorMessages.INVALID_FLOW_ID) + } + if (flowData.name) { + await _checkForDuplicateName(flowData.name, flowId, transaction); + } - if (!oldFlow) { - throw new Errors.NotFoundError(ErrorMessages.INVALID_FLOW_ID) - } - if (flowData.name !== undefined) { - await isFlowExist(flowData.name, transaction); - } + const flow = { + name: flowData.name, + description: flowData.description, + isActivated: flowData.isActivated, + updatedBy: user.id + }; - const flow = { - name: flowData.name, - description: flowData.description, - isActivated: flowData.isActivated, - updatedBy: user.id - }; - - const updateFlowData = AppHelper.deleteUndefinedFields(flow); - - const where = isCLI ? - { - id: flowId - } - : - { - id: flowId, - userId: user.id - }; + const updateFlowData = AppHelper.deleteUndefinedFields(flow); + + const where = isCLI + ? {id: flowId} + : {id: flowId, userId: user.id}; - await FlowManager.update(where, updateFlowData, transaction); - - //TODO: simplify. use manager's join query - if (oldFlow.isActivated !== flowData.isActivated) { - const msList = await MicroserviceService.getListMicroservices({flowId: flowId}, user, isCLI, transaction) - for (const ms of msList) { - const updateChangeTrackingData = { - containerConfig: true, - containerList: true, - routing: true - } - await ChangeTrackingManager.update({iofogUuid: ms.iofogUuid}, updateChangeTrackingData, transaction); - } + await FlowManager.update(where, updateFlowData, transaction); + + if (oldFlow.isActivated !== flowData.isActivated) { + const flowWithMicroservices = await FlowManager.findFlowMicroservices({id: flowId}, transaction); + const onlyUnique = (value, index, self) => self.indexOf(value) === index; + const iofogUuids = flowWithMicroservices.microservice + .map(obj => obj.iofogUuid) + .filter(onlyUnique) + .filter(val => val !== null); + for (let iofogUuid of iofogUuids) { + const updateChangeTrackingData = { + containerConfig: true, + containerList: true, + routing: true + }; + await ChangeTrackingManager.update({iofogUuid: iofogUuid}, updateChangeTrackingData, transaction); } + } }; const _getFlow = async function (flowId, user, isCLI, transaction) { @@ -127,13 +125,16 @@ const _getAllFlows = async function (isCLI, transaction) { return await FlowManager.findAll({}, transaction); }; -const isFlowExist = async function (flowName, transaction) { - const flow = await FlowManager.findOne({ - name: flowName - }, transaction); +const _checkForDuplicateName = async function (name, item, transaction) { + if (name) { + const where = item.id + ? {name: name, id: {[Op.ne]: item.id}} + : {name: name}; - if (flow) { - throw new Errors.ValidationError(AppHelper.formatMessage(ErrorMessages.DUPLICATE_NAME, flowName)); + const result = await FlowManager.findOne(where, transaction); + if (result) { + throw new Errors.DuplicatePropertyError(AppHelper.formatMessage(ErrorMessages.DUPLICATE_NAME, name)); + } } }; @@ -144,5 +145,5 @@ module.exports = { getFlowWithTransaction: TransactionDecorator.generateTransaction(_getFlow), getUserFlowsWithTransaction: TransactionDecorator.generateTransaction(_getUserFlows), getAllFlowsWithTransaction: TransactionDecorator.generateTransaction(_getAllFlows), - getFlow: _getFlow, + getFlow: _getFlow }; diff --git a/src/services/microservices-service.js b/src/services/microservices-service.js index d646dff6b..de0ce38bc 100644 --- a/src/services/microservices-service.js +++ b/src/services/microservices-service.js @@ -76,8 +76,6 @@ const _getMicroservice = async function (microserviceUuid, user, isCLI, transact const _createMicroserviceOnFog = async function (microserviceData, user, isCLI, transaction) { await Validation.validate(microserviceData, Validation.schemas.microserviceCreate); - await FlowService.getFlowWithTransaction(1, user, isCLI, transaction); - const microservice = await _createMicroservice(microserviceData, user, isCLI, transaction); if (microserviceData.ports) { @@ -104,7 +102,7 @@ const _createMicroserviceOnFog = async function (microserviceData, user, isCLI, const _createMicroservice = async function (microserviceData, user, isCLI, transaction) { - let microservice = { + let newMicroservice = { uuid: AppHelper.generateRandomString(32), name: microserviceData.name, config: microserviceData.config, @@ -116,20 +114,20 @@ const _createMicroservice = async function (microserviceData, user, isCLI, trans updatedBy: user.id }; - microservice = AppHelper.deleteUndefinedFields(microservice); + newMicroservice = AppHelper.deleteUndefinedFields(newMicroservice); - await _checkForDuplicateName(microservice.name, {}, transaction); + await _checkForDuplicateName(newMicroservice.name, {}, transaction); //validate catalog item - await CatalogService.getCatalogItem(microservice.catalogItemId, user, isCLI, transaction); + await CatalogService.getCatalogItem(newMicroservice.catalogItemId, user, isCLI, transaction); //validate flow - await FlowService.getFlow(microservice.flowId, user, isCLI, transaction); + await FlowService.getFlow(newMicroservice.flowId, user, isCLI, transaction); //validate fog node - if (microservice.iofogUuid) { - await IoFogService.getFog({uuid: microservice.iofogUuid}, user, isCLI, transaction); + if (newMicroservice.iofogUuid) { + await IoFogService.getFog({uuid: newMicroservice.iofogUuid}, user, isCLI, transaction); } - return await MicroserviceManager.create(microservice, transaction); + return await MicroserviceManager.create(newMicroservice, transaction); }; const _createMicroservicePorts = async function (ports, microserviceUuid, transaction) { @@ -251,7 +249,7 @@ const _checkForDuplicateName = async function (name, item, transaction) { ? {name: name, uuid: {[Op.ne]: item.id}} : {name: name}; - const result = await MicroserviceManager.findAnother(where); + const result = await MicroserviceManager.findOne(where, transaction); if (result) { throw new Errors.DuplicatePropertyError(AppHelper.formatMessage(ErrorMessages.DUPLICATE_NAME, name)); } @@ -814,5 +812,5 @@ module.exports = { getMicroservicePortMappingListWithTransaction: TransactionDecorator.generateTransaction(_getPortMappingList), deletePortMappingWithTransaction: TransactionDecorator.generateTransaction(_deletePortMapping), getPhysicalConections: getPhysicalConections, - getListMicroservices: _listMicroservices, + getListMicroservices: _listMicroservices }; From 051406696b20e9a14b430672783de363ae192a85 Mon Sep 17 00:00:00 2001 From: Pankov Date: Tue, 30 Oct 2018 20:12:45 +0300 Subject: [PATCH 15/15] Merge branch 'develop' of https://github.com/ioFog/FogController into epankou/feature-microservice-cli # Conflicts: # src/cli/microservice.js --- src/cli/microservice.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/cli/microservice.js b/src/cli/microservice.js index 9a093c5cb..735af77d6 100644 --- a/src/cli/microservice.js +++ b/src/cli/microservice.js @@ -80,7 +80,8 @@ class Microservice extends BaseCLIHandler { }, { name: 'microservice-id', alias: 'i', type: String, description: 'Microservice ID', - group: [constants.CMD_UPDATE, constants.CMD_REMOVE, constants.CMD_INFO, constants.CMD_PORT_MAPPING_CREATE, constants.CMD_PORT_MAPPING_REMOVE] + group: [constants.CMD_UPDATE, constants.CMD_REMOVE, constants.CMD_INFO, constants.CMD_PORT_MAPPING_CREATE, + constants.CMD_PORT_MAPPING_REMOVE, constants.CMD_PORT_MAPPING_LIST] }, { name: 'name', alias: 'n', type: String, description: 'Microservice name', @@ -288,7 +289,7 @@ const _createRoute = async function (obj) { const _removeRoute = async function (obj) { logger.info(JSON.stringify(obj)); try { - const arr = obj.port.split(':'); + const arr = obj.route.split(':'); const sourceMicroserviceId = arr[0]; const destMicroserviceId = arr[1]; await MicroserviceService.deleteRouteWithTransaction(sourceMicroserviceId, destMicroserviceId, {}, true); @@ -309,7 +310,7 @@ const _createPortMapping = async function (obj) { const _removePortMapping = async function (obj) { logger.info(JSON.stringify(obj)); try { - const internalPort = parseInt(obj.delete); + const internalPort = parseInt(obj.internalPort); await MicroserviceService.deletePortMappingWithTransaction(obj.microserviceId, internalPort, {}, true); logger.info('Port mapping has been deleted successfully'); } catch(e) { @@ -317,7 +318,7 @@ const _removePortMapping = async function (obj) { } }; -const _listPortMappings = async function () { +const _listPortMappings = async function (obj) { const result = await MicroserviceService.getMicroservicePortMappingListWithTransaction(obj.microserviceId, {}, true); logger.info(JSON.stringify(result)); logger.info('Port mappings have been retrieved successfully');