diff --git a/src/middleware/gitops.js b/src/middleware/gitops.js index 707f9b8..a4d0031 100644 --- a/src/middleware/gitops.js +++ b/src/middleware/gitops.js @@ -5,8 +5,7 @@ const PATH_CONSTRAINTS_NEW = { GROUP: 0, CONFIG: 1, STRATEGY: 2, - COMPONENT: 2, - STRATEGY_VALUE: 3 + COMPONENT: 2 }; const PATH_CONSTRAINTS_CHANGED = { @@ -19,10 +18,11 @@ const PATH_CONSTRAINTS_DELETED = { GROUP: 1, CONFIG: 2, STRATEGY: 3, - COMPONENT: 2, - STRATEGY_VALUE: 3 + COMPONENT: 2 }; +const CONTENT_TYPE_ARRAY = ['COMPONENT']; + export async function featureFlag(req, res, next) { try { await checkGitopsIntegration(req.domain); @@ -77,7 +77,7 @@ function validatePathForElement(changes) { function validateChangesContent(changes) { for (const change of changes) { - if (['COMPONENT', 'STRATEGY_VALUE'].includes(change.diff)) { + if (CONTENT_TYPE_ARRAY.includes(change.diff)) { if (!Array.isArray(change.content)) { throw new Error('Request has invalid content type [object]'); } diff --git a/src/routers/gitops.js b/src/routers/gitops.js index 3b62833..50496ee 100644 --- a/src/routers/gitops.js +++ b/src/routers/gitops.js @@ -16,7 +16,7 @@ router.post('/gitops/v1/push', gitopsAuth, featureFlag, [ .custom(value => ['NEW', 'CHANGED', 'DELETED'].includes(value)) .withMessage('Request has invalid type of action'), body('changes.*.diff') - .custom(value => ['GROUP', 'CONFIG', 'STRATEGY', 'STRATEGY_VALUE', 'COMPONENT'].includes(value)) + .custom(value => ['GROUP', 'CONFIG', 'STRATEGY', 'COMPONENT'].includes(value)) .withMessage('Request has invalid type of diff'), ], validate, validateChanges, async (req, res) => { try { diff --git a/src/services/gitops/push-changed.js b/src/services/gitops/push-changed.js index bf9de3f..21f7c60 100644 --- a/src/services/gitops/push-changed.js +++ b/src/services/gitops/push-changed.js @@ -29,15 +29,13 @@ async function processChangedGroup(domain, change, environment) { } async function processChangedConfig(domain, change, environment) { - const path = change.path; const content = change.content; const admin = { _id: domain.owner, email: ADMIN_EMAIL }; - const group = await getGroupConfig({ domain: domain._id, name: path[0] }); - const config = await getConfig({ domain: domain._id, group: group._id, key: content.key }); + const config = await getConfig({ domain: domain._id, key: content.key }); await updateConfig(config._id, { - description: getChangedValue(content.description, group.description), - activated: new Map().set(environment, getChangedValue(content.activated, group.activated.get(environment))) + description: getChangedValue(content.description, config.description), + activated: new Map().set(environment, getChangedValue(content.activated, config.activated.get(environment))) }, admin); } @@ -45,16 +43,16 @@ async function processChangedStrategy(domain, change, environment) { const path = change.path; const content = change.content; const admin = { _id: domain.owner, email: ADMIN_EMAIL }; - const group = await getGroupConfig({ domain: domain._id, name: path[0] }); - const config = await getConfig({ domain: domain._id, group: group._id, key: path[1] }); + const config = await getConfig({ domain: domain._id, key: path[1] }); const strategies = await getStrategies({ config: config._id }); const strategy = strategies.find(strategy => strategy.strategy === path[2] && strategy.activated.has(environment)); await updateStrategy(strategy._id, { - description: getChangedValue(content.description, group.description), + description: getChangedValue(content.description, strategy.description), operation: getChangedValue(content.operation, strategy.operation), - activated: new Map().set(environment, getChangedValue(content.activated, group.activated.get(environment))) + activated: new Map().set(environment, getChangedValue(content.activated, strategy.activated.get(environment))), + values: getChangedValue(content.values, strategy.values) }, admin); } diff --git a/src/services/gitops/push-deleted.js b/src/services/gitops/push-deleted.js index b84caff..9ef2546 100644 --- a/src/services/gitops/push-deleted.js +++ b/src/services/gitops/push-deleted.js @@ -1,5 +1,5 @@ import { getComponents } from '../component.js'; -import { deleteStrategy, getStrategies, removeVal } from '../config-strategy.js'; +import { deleteStrategy, getStrategies } from '../config-strategy.js'; import { deleteConfig, getConfig, removeComponent } from '../config.js'; import { deleteGroup, getGroupConfig } from '../group-config.js'; import { ADMIN_EMAIL } from './index.js'; @@ -15,9 +15,6 @@ export async function processDeleted(domain, change, environment) { case 'STRATEGY': await processStrategyDeleted(domain, change, environment); break; - case 'STRATEGY_VALUE': - await processStrategyValueDeleted(domain, change, environment); - break; case 'COMPONENT': await processComponentDeleted(domain, change); break; @@ -53,22 +50,6 @@ async function processStrategyDeleted(domain, change, environment) { await deleteStrategy(strategy._id, admin); } -async function processStrategyValueDeleted(domain, change, environment) { - const path = change.path; - const admin = { _id: domain.owner, email: ADMIN_EMAIL }; - const group = await getGroupConfig({ domain: domain._id, name: path[0] }); - const config = await getConfig({ domain: domain._id, group: group._id, key: path[1] }); - - const strategies = await getStrategies({ config: config._id }); - const strategy = strategies.find(strategy => strategy.strategy === path[2] && strategy.activated.get(environment)); - - for (const value of change.content) { - if (strategy.values.includes(value)) { - await removeVal(strategy._id, { value }, admin); - } - } -} - async function processComponentDeleted(domain, change) { const path = change.path; const content = change.content; diff --git a/src/services/gitops/push-new.js b/src/services/gitops/push-new.js index 4481915..cb5463a 100644 --- a/src/services/gitops/push-new.js +++ b/src/services/gitops/push-new.js @@ -1,5 +1,5 @@ import { getComponents } from '../component.js'; -import { createStrategy, getStrategies, updateStrategy } from '../config-strategy.js'; +import { createStrategy } from '../config-strategy.js'; import { addComponent, createConfig, getConfig } from '../config.js'; import { createGroup, getGroupConfig } from '../group-config.js'; import { ADMIN_EMAIL } from './index.js'; @@ -15,9 +15,6 @@ export async function processNew(domain, change, environment) { case 'STRATEGY': await processNewStrategy(domain, change, environment); break; - case 'STRATEGY_VALUE': - await processNewStrategyValue(domain, change); - break; case 'COMPONENT': await processNewComponent(domain, change); break; @@ -95,20 +92,6 @@ async function processNewStrategy(domain, change, environment) { }, admin, getNewValue(content.activated, true)); } -async function processNewStrategyValue(domain, change) { - const path = change.path; - const content = change.content; - const admin = { _id: domain.owner, email: ADMIN_EMAIL }; - const config = await getConfig({ domain: domain._id, key: path[1] }); - - const strategies = await getStrategies({ config: config._id }); - const strategy = strategies.find(strategy => strategy.strategy === path[2]); - - await updateStrategy(strategy._id, { - values: [...strategy.values, ...content] - }, admin); -} - async function processNewComponent(domain, change) { const path = change.path; const content = change.content; diff --git a/tests/gitops.test.js b/tests/gitops.test.js index 7bdd2d2..5b29aa8 100644 --- a/tests/gitops.test.js +++ b/tests/gitops.test.js @@ -299,10 +299,12 @@ describe('GitOps - Push New', () => { .send({ environment: EnvType.DEFAULT, changes: [{ - action: 'NEW', - diff: 'STRATEGY_VALUE', + action: 'CHANGED', + diff: 'STRATEGY', path: ['Group Test', 'TEST_CONFIG_KEY', StrategiesType.VALUE], - content: ['USER_4'] + content: { + values: ['USER_1', 'USER_2', 'USER_3', 'USER_4'] + } }] }) .expect(200); @@ -494,10 +496,12 @@ describe('GitOps - Push Deleted', () => { .send({ environment: EnvType.DEFAULT, changes: [{ - action: 'DELETED', - diff: 'STRATEGY_VALUE', + action: 'CHANGED', + diff: 'STRATEGY', path: ['Group Test', 'TEST_CONFIG_KEY', StrategiesType.VALUE], - content: ['USER_2'] + content: { + values: ['USER_1', 'USER_3'] + } }] }) .expect(200); @@ -653,8 +657,8 @@ describe('GitOps - Push Changes - Errors (invalid requests)', () => { environment: EnvType.DEFAULT, changes: [{ action: 'NEW', - diff: 'STRATEGY_VALUE', - path: ['Group Test', 'TEST_CONFIG_KEY', StrategiesType.VALUE], + diff: 'COMPONENT', + path: ['Group Test', 'TEST_CONFIG_KEY'], content: { value: 'SHOULD_BE_ARRAY' }