Skip to content

Commit

Permalink
GitOps - replaced push switch blocks with immutable process constants
Browse files Browse the repository at this point in the history
  • Loading branch information
petruki committed Oct 6, 2024
1 parent fd53541 commit 37717c9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 51 deletions.
20 changes: 8 additions & 12 deletions src/services/gitops/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import { processChanged } from './push-changed.js';
import { processDeleted } from './push-deleted.js';
import { processNew } from './push-new.js';

const CHANGE_PROCESSES = Object.freeze({
NEW: processNew,
CHANGED: processChanged,
DELETED: processDeleted
});

export const ADMIN_EMAIL = '[email protected]';

export async function pushChanges(domainId, environment, changes) {
Expand All @@ -12,18 +18,8 @@ export async function pushChanges(domainId, environment, changes) {

let domain = await getDomainById(domainId);
for (const change of changes) {
switch (change.action) {
case 'NEW':
await processNew(domain, change, environment);
break;
case 'CHANGED':
await processChanged(domain, change, environment);
break;
case 'DELETED':
await processDeleted(domain, change, environment);
break;
}
};
await CHANGE_PROCESSES[change.action](domain, change, environment);
}

domain = await updateDomainVersion(domainId);
return {
Expand Down
18 changes: 7 additions & 11 deletions src/services/gitops/push-changed.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@ import { getConfig, updateConfig } from '../config.js';
import { getGroupConfig, updateGroup } from '../group-config.js';
import { ADMIN_EMAIL } from './index.js';

const DIFF_PROCESSES = Object.freeze({
GROUP: processChangedGroup,
CONFIG: processChangedConfig,
STRATEGY: processChangedStrategy
});

export async function processChanged(domain, change, environment) {
switch (change.diff) {
case 'GROUP':
await processChangedGroup(domain, change, environment);
break;
case 'CONFIG':
await processChangedConfig(domain, change, environment);
break;
case 'STRATEGY':
await processChangedStrategy(domain, change, environment);
break;
}
await DIFF_PROCESSES[change.diff](domain, change, environment);
}

async function processChangedGroup(domain, change, environment) {
Expand Down
22 changes: 8 additions & 14 deletions src/services/gitops/push-deleted.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,15 @@ import { deleteConfig, getConfig, removeComponent } from '../config.js';
import { deleteGroup, getGroupConfig } from '../group-config.js';
import { ADMIN_EMAIL } from './index.js';

const DIFF_PROCESSES = Object.freeze({
GROUP: processGroupDeleted,
CONFIG: processConfigDeleted,
STRATEGY: processStrategyDeleted,
COMPONENT: processComponentDeleted
});

export async function processDeleted(domain, change, environment) {
switch (change.diff) {
case 'GROUP':
await processGroupDeleted(domain, change);
break;
case 'CONFIG':
await processConfigDeleted(domain, change);
break;
case 'STRATEGY':
await processStrategyDeleted(domain, change, environment);
break;
case 'COMPONENT':
await processComponentDeleted(domain, change);
break;
}
await DIFF_PROCESSES[change.diff](domain, change, environment);
}

async function processGroupDeleted(domain, change) {
Expand Down
22 changes: 8 additions & 14 deletions src/services/gitops/push-new.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,15 @@ import { addComponent, createConfig, getConfig } from '../config.js';
import { createGroup, getGroupConfig } from '../group-config.js';
import { ADMIN_EMAIL } from './index.js';

const DIFF_PROCESSES = Object.freeze({
GROUP: processNewGroup,
CONFIG: processNewConfig,
STRATEGY: processNewStrategy,
COMPONENT: processNewComponent
});

export async function processNew(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;
case 'COMPONENT':
await processNewComponent(domain, change);
break;
}
await DIFF_PROCESSES[change.diff](domain, change, environment);
}

async function processNewGroup(domain, change, environment) {
Expand Down

0 comments on commit 37717c9

Please sign in to comment.