diff --git a/server/app/model/tags/tags.js b/server/app/model/tags/tags.js index 2d05dff28..c4439b34d 100644 --- a/server/app/model/tags/tags.js +++ b/server/app/model/tags/tags.js @@ -46,27 +46,11 @@ var TagSchema = new Schema({ }, catalystEntityType: { type: String, - enum: ['project', 'environment','bgName'], + enum: ['project', 'environment', 'businessGroup'], trim: true, required: false }, - catalystEntityMapping: [{ - catalystEntityId: { - type: String, - trim: true, - required: false - }, - catalystEntityName: { - type: String, - trim: true, - required: false - }, - tagValue: { - type: String, - trim: true, - required: false - } - }], + catalystEntityMapping: Schema.Types.Mixed, isDeleted: { type: Boolean, required: true, diff --git a/server/app/routes/v1.0/routes_providercommon.js b/server/app/routes/v1.0/routes_providercommon.js index c48cb371e..69e6ea822 100644 --- a/server/app/routes/v1.0/routes_providercommon.js +++ b/server/app/routes/v1.0/routes_providercommon.js @@ -1238,21 +1238,18 @@ module.exports.setRoutes = function (app, sessionVerificationFunc) { * @apiParam {Number} providerId Provider ID * @apiParam {Object[]} tagMappings Tag mappings * @apiParam {String} tagMappings.tagName Tag name - * @apiParam {String[]} tagMappings.tagValues Tag values * @apiSuccess {Object[]} tagMappings.catalystEntityType Catalyst entity type * @apiSuccess {String} tagNameMapping.catalystEntityMapping.catalystEntityId Catalyst entity id * @apiSuccess {String} tagNameMapping.catalystEntityMapping.tagValue Tag value * @apiParamExample {json} Request-Example: * { * "project": { - * "tagName": "application", - * "tagValues": [], + * "tagName": "application" * "catalystEntityType": "project", * "catalystEntityMapping": {} * }, * "environment": { * "tagName": "env", - * "tagValues": [], * "catalystEntityType": "environment", * "catalystEntityMapping": {} * } @@ -1525,7 +1522,7 @@ module.exports.setRoutes = function (app, sessionVerificationFunc) { app.patch('/providers/:providerId/unassigned-instances', validate(instanceValidator.get), bulkUpdateUnassignedInstances); - function getTagsList(req, res, next) { + function getTagsList(req, res, callback) { async.waterfall( [ @@ -1537,7 +1534,7 @@ module.exports.setRoutes = function (app, sessionVerificationFunc) { ], function (err, results) { if (err) { - next(err); + callback(err); } else { return res.status(200).send(results); } @@ -1545,7 +1542,7 @@ module.exports.setRoutes = function (app, sessionVerificationFunc) { ); } - function getUnassignedInstancesList(req, res, next) { + function getUnassignedInstancesList(req, res, callback) { var reqData = {}; async.waterfall( [ @@ -1571,7 +1568,7 @@ module.exports.setRoutes = function (app, sessionVerificationFunc) { ], function (err, results) { if (err) - next(err); + callback(err); else return res.status(200).send(results); }); @@ -1580,7 +1577,7 @@ module.exports.setRoutes = function (app, sessionVerificationFunc) { - function getTag(req, res, next) { + function getTag(req, res, callback) { async.waterfall( [ @@ -1594,7 +1591,7 @@ module.exports.setRoutes = function (app, sessionVerificationFunc) { ], function (err, results) { if (err) { - next(err); + callback(err); } else { return res.status(200).send(results); } @@ -1605,7 +1602,7 @@ module.exports.setRoutes = function (app, sessionVerificationFunc) { // @TODO to be implemented function createTags(req, res, next) {} - function updateTag(req, res, next) { + function updateTag(req, res, callback) { async.waterfall( [ @@ -1623,7 +1620,7 @@ module.exports.setRoutes = function (app, sessionVerificationFunc) { ], function (err, results) { if (err) { - next(err); + callback(err); } else { return res.status(200).send(results); } @@ -1631,7 +1628,7 @@ module.exports.setRoutes = function (app, sessionVerificationFunc) { ); } - function deleteTag(req, res, next) { + function deleteTag(req, res, callback) { async.waterfall( [ @@ -1644,7 +1641,7 @@ module.exports.setRoutes = function (app, sessionVerificationFunc) { ], function (err, results) { if (err) { - next(err); + callback(err); } else { return res.status(200).send(results); } @@ -1652,7 +1649,7 @@ module.exports.setRoutes = function (app, sessionVerificationFunc) { ); } - function getTagMappingsList(req, res, next) { + function getTagMappingsList(req, res, callback) { async.waterfall( [ @@ -1664,7 +1661,7 @@ module.exports.setRoutes = function (app, sessionVerificationFunc) { ], function (err, results) { if (err) { - next(err); + callback(err); } else { return res.status(200).send(results); } @@ -1672,7 +1669,7 @@ module.exports.setRoutes = function (app, sessionVerificationFunc) { ); } - function getTagMapping(req, res, next) { + function getTagMapping(req, res, callback) { async.waterfall( [ @@ -1687,7 +1684,7 @@ module.exports.setRoutes = function (app, sessionVerificationFunc) { ], function (err, results) { if (err) { - next(err); + callback(err); } else { return res.status(200).send(results); } @@ -1695,7 +1692,7 @@ module.exports.setRoutes = function (app, sessionVerificationFunc) { ); } - function addTagMappings(req, res, next) { + function addTagMappings(req, res, callback) { async.waterfall( [ @@ -1709,7 +1706,7 @@ module.exports.setRoutes = function (app, sessionVerificationFunc) { ], function (err, results) { if (err) { - next(err); + callback(err); } else { return res.status(201).send(results); } @@ -1718,7 +1715,7 @@ module.exports.setRoutes = function (app, sessionVerificationFunc) { } - function updateTagMapping(req, res, next) { + function updateTagMapping(req, res, callback) { async.waterfall( [ function (next) { @@ -1731,14 +1728,14 @@ module.exports.setRoutes = function (app, sessionVerificationFunc) { function (tag, next) { providerService.updateTagMapping(tag, req.body, next); }, - function (tag, next) { + /*function (tag, next) { providerService.getTagByNameAndProvider(req.params.providerId, tag.name, next); - }, + },*/ providerService.createTagMappingObject ], function (err, results) { if (err) { - next(err); + callback(err); } else { return res.status(200).send(results); } @@ -1746,7 +1743,7 @@ module.exports.setRoutes = function (app, sessionVerificationFunc) { ); } - function deleteTagMapping(req, res, next) { + function deleteTagMapping(req, res, callback) { async.waterfall( [ @@ -1759,7 +1756,7 @@ module.exports.setRoutes = function (app, sessionVerificationFunc) { ], function (err, results) { if (err) { - next(err); + callback(err); } else { return res.status(200).send(results); } @@ -1767,7 +1764,7 @@ module.exports.setRoutes = function (app, sessionVerificationFunc) { ); } - function getCatalystEntityMapping(req, res, next) { + function getCatalystEntityMapping(req, res, callback) { async.waterfall( [ @@ -1784,7 +1781,7 @@ module.exports.setRoutes = function (app, sessionVerificationFunc) { ], function (err, results) { if (err) { - next(err); + callback(err); } else { return res.status(200).send(results); } @@ -1792,7 +1789,7 @@ module.exports.setRoutes = function (app, sessionVerificationFunc) { ); } - function updateUnassignedInstanceTags(req, res, next) { + function updateUnassignedInstanceTags(req, res, callback) { async.waterfall( [ function (next) { @@ -1819,7 +1816,7 @@ module.exports.setRoutes = function (app, sessionVerificationFunc) { ], function (err, results) { if (err) { - next(err); + callback(err); } else { return res.status(200).send(results); } @@ -1827,7 +1824,7 @@ module.exports.setRoutes = function (app, sessionVerificationFunc) { ); } - function bulkUpdateUnassignedInstances(req, res, next) { + function bulkUpdateUnassignedInstances(req, res, callback) { async.waterfall( [ function (next) { @@ -1848,7 +1845,7 @@ module.exports.setRoutes = function (app, sessionVerificationFunc) { ], function (err, results) { if (err) { - next(err); + callback(err); } else { return res.status(200).send(results); } diff --git a/server/app/services/providerService.js b/server/app/services/providerService.js index a974f520a..cebafca3c 100644 --- a/server/app/services/providerService.js +++ b/server/app/services/providerService.js @@ -21,6 +21,7 @@ var providersModel = require('_pr/model/v2.0/providers/providers'); var gcpProviderModel = require('_pr/model/v2.0/providers/gcp-providers'); var appConfig = require('_pr/config'); var Cryptography = require('_pr/lib/utils/cryptography'); +var async = require('async'); const errorType = 'provider'; @@ -307,7 +308,8 @@ providerService.getTagMappingsByProviderId providerService.getTagByCatalystEntityTypeAndProvider = function getTagByCatalystEntityTypeAndProvider(providerId, catalystEntityType, callback) { // @TODO entity types to be moved to config - if((catalystEntityType != 'project') && (catalystEntityType != 'environment') && (catalystEntityType != 'bgName')) { + if((catalystEntityType != 'project') && (catalystEntityType != 'environment') + && (catalystEntityType != 'businessGroup')) { var err = new Error('Malformed Request'); err.status = 400; return callback(err); @@ -371,78 +373,71 @@ providerService.updateTag = function updateTag(provider, tagDetails, callback) { // @TODO Update conflict based on tag names should be handled // @TODO Nested callbacks to be handled providerService.addMultipleTagMappings = function addMultipleTagMappings(providerId, tagMappings, callback) { - if(tagMappings.length < 1) { - return callback(null, []); + if(tagMappings == null) { + return callback(null, {}); } - logger.debug(tagMappings.length); - var tagNames = []; - for(var i = 0; i < tagMappings.length; i++) { - (function(tagMapping) { + var tagsList = []; + async.forEach(Object.keys(tagMappings), + function(catalystEntityType, next1) { + var tagMapping = tagMappings[catalystEntityType]; if (!('tagName' in tagMapping) || !('catalystEntityType' in tagMapping)) { var err = new Error('Malformed Request'); err.status = 400; - return callback(err); + next1(err); } // @TODO entity types to be moved to config if ((tagMapping.catalystEntityType != 'project') - && (tagMapping.catalystEntityType != 'environment') && (tagMapping.catalystEntityType != 'bgName')) { + && (tagMapping.catalystEntityType != 'environment') + && (tagMapping.catalystEntityType != 'businessGroup')) { var err = new Error('Malformed Request'); err.status = 400; - return callback(err); + next1(err); } - var deleteParams = { - 'providerId': providerId, - 'catalystEntityType': tagMapping.catalystEntityType - }; - var deleteFields = { - 'catalystEntityType': null, - 'catalystEntityMapping': [] - }; - tagsModel.updateTag(deleteParams, deleteFields, function (err, tag) { - - if (err) { - var err = new Error('Internal server error'); - err.status = 500; - return callback(err); - } else { - - tagNames.push(tagMapping.tagName); - var params = { + async.waterfall([ + function(next2) { + var deleteParams = { 'providerId': providerId, - 'name': tagMapping.tagName - }; - var fields = { 'catalystEntityType': tagMapping.catalystEntityType }; - tagsModel.updateTag(params, fields, function (err, tag) { - if (err) { - var err = new Error('Internal server error'); - err.status = 500; - return callback(err); - } else if (!tag) { - var err = new Error('Tag not found'); - err.status = 404; - return callback(err); - } - }); - + var deleteFields = { + 'catalystEntityType': null, + 'catalystEntityMapping': {} + }; + tagsModel.updateTag(deleteParams, deleteFields, next2) + }, + function(tagDeleted, next2) { + providerService.getTagByNameAndProvider(providerId, + tagMapping.tagName, next2); + }, + function(tagDetails, next2) { + providerService.updateTagMapping(tagDetails, catalystEntityType, + tagMapping, next2); } - - }); - })(tagMappings[i]); - } - - if(tagNames.length > 0) { - return tagsModel.getTagsByProviderIdAndNames(providerId, tagNames, callback); - } else { - return callback(null, []); - } + ], function(err, tagDetails) { + if(err) { + var err = new Error('Internal server error'); + err.status = 500; + next1(err); + } else { + tagsList.push(tagDetails); + next1(null); + } + }) + }, + function(err) { + if(err) { + callback(err); + } else { + callback(null, tagsList); + } + } + ); }; -providerService.updateTagMapping = function updateTagMapping(tagDetails, tagMapping, callback) { +providerService.updateTagMapping = function updateTagMapping(tagDetails, catalystEntityType, tagMapping, callback) { if(!('tagName' in tagMapping) && !('catalystEntityMapping' in tagMapping)) { var err = new Error('Malformed Request'); err.status = 400; @@ -454,51 +449,62 @@ providerService.updateTagMapping = function updateTagMapping(tagDetails, tagMapp return callback(err); } - var catalystEntityMappingList = []; - for(var i = 0; i < tagMapping.catalystEntityMapping.length; i++) { - if(!('tagValue' in tagMapping.catalystEntityMapping[i]) - || !('catalystEntityId' in tagMapping.catalystEntityMapping[i]) - || !('catalystEntityName' in tagMapping.catalystEntityMapping[i])) { - var err = new Error('Malformed Request'); - err.status = 400; - return callback(err); - } + tagDetails.toObject(); + var catalystEntityMappings = {}; + async.forEach(Object.keys(tagMapping.catalystEntityMapping), + function(catalystEntityId, next) { + if(!('tagValue' in tagMapping.catalystEntityMapping[catalystEntityId]) + || !('catalystEntityId' in tagMapping.catalystEntityMapping[catalystEntityId]) + || !('catalystEntityName' in tagMapping.catalystEntityMapping[catalystEntityId])) { + var err = new Error('Malformed Request'); + err.status = 400; + next(err); + } - if((tagMapping.catalystEntityMapping[i].tagValue) - && (tagDetails.values.indexOf(tagMapping.catalystEntityMapping[i].tagValue) < 0)) { - var err = new Error('Tag value not found'); - err.status = 404; - return callback(err); - } + if((tagMapping.catalystEntityMapping[catalystEntityId].tagValue) + && (tagDetails.values.indexOf(tagMapping.catalystEntityMapping[catalystEntityId].tagValue) < 0)) { + var err = new Error('Tag value not found'); + err.status = 404; + next(err); + } - catalystEntityMappingList.push({ - 'tagValue': tagMapping.catalystEntityMapping[i].tagValue, - 'catalystEntityId': tagMapping.catalystEntityMapping[i].catalystEntityId, - 'catalystEntityName': tagMapping.catalystEntityMapping[i].catalystEntityName - }); - } + catalystEntityMappings[catalystEntityId] = { + 'tagValue': tagMapping.catalystEntityMapping[catalystEntityId].tagValue, + 'catalystEntityId': tagMapping.catalystEntityMapping[catalystEntityId].catalystEntityId, + 'catalystEntityName': tagMapping.catalystEntityMapping[catalystEntityId].catalystEntityName + } - var params = { - 'providerId': tagDetails.providerId, - 'name': tagDetails.name - }; - var fields = { - 'catalystEntityMapping': catalystEntityMappingList - }; - tagsModel.updateTag(params, fields, function(err, tag) { - if(err) { - var err = new Error('Internal server error'); - err.status = 500; - return callback(err); - } else if(!tag) { - var err = new Error('Tag not found'); - err.status = 404; - return callback(err); - }else { - tagDetails.catalystEntityMapping = catalystEntityMappingList; - return callback(null, tagDetails); + next(null); + }, + function(err) { + if(err) { + return callback(err); + } else { + var params = { + 'providerId': tagDetails.providerId, + 'name': tagDetails.name + }; + var fields = { + 'catalystEntityType': catalystEntityType, + 'catalystEntityMapping': catalystEntityMappings + }; + tagsModel.updateTag(params, fields, function(err, tag) { + if(err) { + var err = new Error('Internal server error'); + err.status = 500; + return callback(err); + } else if(!tag) { + var err = new Error('Tag not found'); + err.status = 404; + return callback(err); + }else { + tagDetails.catalystEntityMapping = catalystEntityMappings; + return callback(null, tagDetails); + } + }); + } } - }); + ); }; providerService.updateCatalystEntityMapping @@ -606,7 +612,8 @@ providerService.createTagsList = function createTagsList(tags, callback) { tags.forEach(function(tag) { tagsList.push({ 'name': tag.name, - 'description': tag.description?tag.description:null + 'description': tag.description?tag.description:null, + 'values': tag.values?tag.values:[] }); }); tagsListObject.tags = tagsList; @@ -624,7 +631,6 @@ providerService.createTagObject = function createTagObject(tag, callback) { }; providerService.createTagMappingList = function createTagMappingList(tags, callback) { - var tagMappingsListObject = {}; var tagMappingsList = {}; tags.forEach(function(tag) { if(tag.catalystEntityType) { @@ -634,17 +640,17 @@ providerService.createTagMappingList = function createTagMappingList(tags, callb return a.toLowerCase().localeCompare(b.toLowerCase()); }) : [], 'catalystEntityType': tag.catalystEntityType ? tag.catalystEntityType : null, - 'catalystEntityMapping': tag.catalystEntityMapping ? tag.catalystEntityMapping : [] + 'catalystEntityMapping': tag.catalystEntityMapping ? tag.catalystEntityMapping : {} }; - for (var i = 0; i < tagMapping.catalystEntityMapping.length; i++) { + + /*for (var i = 0; i < tagMapping.catalystEntityMapping.length; i++) { delete tagMapping.catalystEntityMapping[i]._id; - } + }*/ + tagMappingsList[tagMapping.catalystEntityType] = tagMapping; } }); - tagMappingsListObject.tagMappings = tagMappingsList; - return callback(null, tagMappingsList); }; @@ -655,11 +661,12 @@ providerService.createTagMappingObject = function createTagMappingObject(tag, ca return a.toLowerCase().localeCompare(b.toLowerCase()); }) : [], 'catalystEntityType': tag.catalystEntityType?tag.catalystEntityType : null, - 'catalystEntityMapping': tag.catalystEntityMapping?tag.catalystEntityMapping : [] + 'catalystEntityMapping': tag.catalystEntityMapping?tag.catalystEntityMapping : {} }; - for (var i = 0; i < tagMappingObject.catalystEntityMapping.length; i++) { + + /*for (var i = 0; i < tagMappingObject.catalystEntityMapping.length; i++) { delete tagMappingObject.catalystEntityMapping[i]._id; - } + }*/ return callback(null, tagMappingObject); }; \ No newline at end of file