Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/middleware/gitops.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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);
Expand Down Expand Up @@ -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]');
}
Expand Down
2 changes: 1 addition & 1 deletion src/routers/gitops.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 7 additions & 9 deletions src/services/gitops/push-changed.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,30 @@ 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);
}

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);
}

Expand Down
21 changes: 1 addition & 20 deletions src/services/gitops/push-deleted.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
19 changes: 1 addition & 18 deletions src/services/gitops/push-new.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 12 additions & 8 deletions tests/gitops.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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'
}
Expand Down