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
118 changes: 94 additions & 24 deletions src/services/gitops.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,86 @@
import { getComponents } from './component.js';
import { createStrategy } from './config-strategy.js';
import { createConfig } from './config.js';
import { createConfig, getConfig } from './config.js';
import { getDomainById, updateDomainVersion } from './domain.js';
import { getGroupConfig } from './group-config.js';
import { createGroup, getGroupConfig } from './group-config.js';

export async function pushChanges(domainId, environment, changes) {
let domain = await getDomainById(domainId);
const validations = validateChanges(changes);
if (validations) {
return errorResponse(validations);
}

for (const change of changes) {
let domain = await getDomainById(domainId);
for (const change of changes) {
if (change.action === 'NEW') {
await processNew(domain, change, environment);
} else {
return errorResponse('Request has invalid actions', domain.lastUpdate);
}
};

domain = await updateDomainVersion(domainId);
return successResponse('Changes applied successfully', domain.lastUpdate);
}

function validateChanges(changes) {
try {
validateActions(changes);
validateDiff(changes);
} catch (e) {
return e.message;
}

return undefined;
}

function validateActions(changes) {
const validActions = ['NEW', 'CHANGED', 'DELETED'];
const hasInvalidAction = changes.some(change => !validActions.includes(change.action));

if (hasInvalidAction) {
throw new Error('Request has invalid type of change');
}
}

function validateDiff(changes) {
const validDiff = ['GROUP', 'CONFIG', 'STRATEGY'];
const hasInvalidDiff = changes.some(change => !validDiff.includes(change.diff));

if (hasInvalidDiff) {
throw new Error('Request has invalid type of diff');
}
}

async function processNew(domain, change, environment) {
if (change.diff === 'CONFIG') {
await processNewConfig(domain, change, environment);
switch (change.diff) {
case 'GROUP':
await processNewGroup(domain, change, environment);
break;
case 'CONFIG':
await processNewConfig(domain, change, environment);
break;
case 'STRATEGY':
await processNewStrategy(domain, change, environment);
break;
}
}

async function processNewGroup(domain, change, environment) {
const admin = { _id: domain.owner };
const group = await createGroup({
domain: domain._id,
name: change.content.name,
description: change.content.description,
activated: new Map().set(environment, change.content.activated),
owner: domain.owner
}, admin);

if (change.content.configs?.length) {
for (const config of change.content.configs) {
await processNewConfig(domain, {
path: [group.name],
content: config
}, environment);
}
}
}

Expand Down Expand Up @@ -49,26 +108,30 @@ async function processNewConfig(domain, change, environment) {

if (change.content.strategies?.length) {
for (const strategy of change.content.strategies) {
await createStrategy({
env: environment,
description: strategy.description,
strategy: strategy.strategy,
values: strategy.values,
operation: strategy.operation,
config: config._id,
domain: domain._id,
owner: domain.owner
}, admin);
await processNewStrategy(domain, {
path: [group.name, config.key],
content: strategy
}, environment);
}
}
}

function errorResponse(message, version) {
return {
valid: false,
message,
version
};
async function processNewStrategy(domain, change, environment) {
const path = change.path;
const content = change.content;
const admin = { _id: domain.owner };
const config = await getConfig({ domain: domain._id, key: path[1] });

await createStrategy({
env: environment,
description: content.description,
strategy: content.strategy,
values: content.values,
operation: content.operation,
config: config._id,
domain: domain._id,
owner: domain.owner
}, admin);
}

function successResponse(message, version) {
Expand All @@ -77,4 +140,11 @@ function successResponse(message, version) {
message,
version
};
}

function errorResponse(message) {
return {
valid: false,
message
};
}
Loading