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
61 changes: 35 additions & 26 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@
"express-rate-limit": "^7.4.1",
"express-validator": "^7.2.0",
"graphql": "^16.9.0",
"graphql-http": "^1.22.1",
"graphql-http": "^1.22.2",
"graphql-tag": "^2.12.6",
"helmet": "^8.0.0",
"jsonwebtoken": "^9.0.2",
"moment": "^2.30.1",
"mongodb": "^6.10.0",
"mongoose": "^8.8.0",
"mongoose": "^8.8.1",
"pino": "^9.5.0",
"pino-pretty": "^11.3.0",
"swagger-ui-express": "^5.0.1",
Expand Down
76 changes: 76 additions & 0 deletions requests/Switcher API.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -5254,6 +5254,44 @@
},
"response": []
},
{
"name": "GitOps Push - Change Relay",
"request": {
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "{{gitopsToken}}",
"type": "string"
}
]
},
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\r\n \"environment\": \"default\",\r\n \"changes\": [\r\n {\r\n \"action\": \"CHANGED\",\r\n \"diff\": \"CONFIG\",\r\n \"path\": [\r\n \"Group Test\",\r\n \"NEW_SWITCHER_2\"\r\n ],\r\n \"content\": {\r\n \"relay\": {\r\n \"type\": \"NOTIFICATION\",\r\n \"method\": \"POST\",\r\n \"endpoint\": \"https://localhost:3000\",\r\n \"description\": \"Push message to log\",\r\n \"activated\": true\r\n }\r\n }\r\n }\r\n ]\r\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{url}}/gitops/v1/push",
"host": [
"{{url}}"
],
"path": [
"gitops",
"v1",
"push"
]
}
},
"response": []
},
{
"name": "GitOps Push - Change Strategy",
"request": {
Expand Down Expand Up @@ -5406,6 +5444,44 @@
},
"response": []
},
{
"name": "GitOps Push - New Switcher / Relay",
"request": {
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "{{gitopsToken}}",
"type": "string"
}
]
},
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\r\n \"environment\": \"default\",\r\n \"changes\": [\r\n {\r\n \"action\": \"NEW\",\r\n \"diff\": \"CONFIG\",\r\n \"path\": [\r\n \"New Group\"\r\n ],\r\n \"content\": {\r\n \"key\": \"NEW_SWITCHER_RELAY\",\r\n \"description\": \"New Switcher Relay\",\r\n \"activated\": true,\r\n \"components\": [\r\n \"switcher-playground\"\r\n ],\r\n \"relay\": {\r\n \"type\": \"NOTIFICATION\",\r\n \"method\": \"POST\",\r\n \"endpoint\": \"https://localhost:3000\",\r\n \"description\": \"Push message to log\",\r\n \"activated\": true\r\n }\r\n }\r\n }\r\n ]\r\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{url}}/gitops/v1/push",
"host": [
"{{url}}"
],
"path": [
"gitops",
"v1",
"push"
]
}
},
"response": []
},
{
"name": "GitOps Push - New Switcher / Strategy",
"request": {
Expand Down
5 changes: 4 additions & 1 deletion src/client/configuration-resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ export async function resolveFlatConfig(source, context) {
configs = source.config;
domainId = configs[0].domain;
} else if (source.group) {
configs = await getConfigs({ group: source.group.map(g => g._id) }, true);
configs = await getConfigs({
domain: source.group[0].domain,
group: source.group.map(g => g._id)
}, true);
domainId = source.group[0].domain;
}

Expand Down
11 changes: 8 additions & 3 deletions src/services/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ export async function getConfigById(id, populateAdmin = false) {
export async function getConfig(where) {
const query = Config.findOne();

if (where.domain) query.where('domain', where.domain);
if (where.key) query.where('key', where.key);
query.where('key', where.key);
query.where('domain', where.domain);

if (where.group) query.where('group', where.group);

return query.exec();
Expand All @@ -39,9 +40,10 @@ export async function getConfig(where) {
export async function getConfigs(where, lean = false) {
const query = Config.find();

query.where('domain', where.domain);

if (where.id) query.where('_id', where.id);
if (where.key) query.where('key', where.key);
if (where.domain) query.where('domain', where.domain);
if (where.group) query.where('group', where.group);
if (lean) query.lean();

Expand Down Expand Up @@ -70,6 +72,9 @@ export async function createConfig(args, admin) {
key: args.key,
domain: group.domain
});

// validates relay
isRelayValid(args.relay);

if (config) {
throw new BadRequestError(`Config ${config.key} already exists`);
Expand Down
22 changes: 20 additions & 2 deletions src/services/gitops/push-changed.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getStrategies, updateStrategy } from '../config-strategy.js';
import { getConfig, updateConfig } from '../config.js';
import { getConfig, updateConfig, updateConfigRelay } from '../config.js';
import { getGroupConfig, updateGroup } from '../group-config.js';
import { ADMIN_EMAIL } from './index.js';

Expand Down Expand Up @@ -27,12 +27,16 @@ async function processChangedGroup(domain, change, environment) {
async function processChangedConfig(domain, change, environment) {
const content = change.content;
const admin = { _id: domain.owner, email: ADMIN_EMAIL };
const config = await getConfig({ domain: domain._id, key: content.key });
const config = await getConfig({ domain: domain._id, key: change.path[1] });

await updateConfig(config._id, {
description: getChangedValue(content.description, config.description),
activated: new Map().set(environment, getChangedValue(content.activated, config.activated.get(environment)))
}, admin);

if (content.relay) {
await updateConfigRelay(config._id, processRelay(content.relay, config.relay, environment), admin);
}
}

async function processChangedStrategy(domain, change, environment) {
Expand All @@ -52,6 +56,20 @@ async function processChangedStrategy(domain, change, environment) {
}, admin);
}

function processRelay(content, configRelay, environment) {
return {
type: getChangedValue(content.type, configRelay.type),
method: getChangedValue(content.method, configRelay.method),
description: getChangedValue(content.description, configRelay.description),
activated: {
[environment]: getChangedValue(content.activated, configRelay.activated?.get(environment))
},
endpoint: {
[environment]: getChangedValue(content.endpoint, configRelay.endpoint?.get(environment))
}
};
}

function getChangedValue(changeValue, defaultValue) {
return changeValue !== undefined ? changeValue : defaultValue;
}
32 changes: 23 additions & 9 deletions src/services/gitops/push-new.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,35 @@ async function processNewConfig(domain, change, environment) {
const admin = { _id: domain.owner };
const group = await getGroupConfig({ domain: domain._id, name: path[0] });

let componentIds;
if (content.components?.length) {
const components = await getComponents({ domain: domain._id, name: { $in: content.components } });
componentIds = components.map(component => component._id);
}

const config = await createConfig({
const newConfig = {
domain: domain._id,
group: group._id,
key: content.key,
description: getNewValue(content.description, ''),
activated: new Map().set(environment, getNewValue(content.activated, true)),
owner: domain.owner,
components: componentIds
}, admin);
};

if (content.components?.length) {
const components = await getComponents({ domain: domain._id, name: { $in: content.components } });
newConfig.components = components.map(component => component._id);
}

if (content.relay) {
newConfig.relay = {
type: content.relay.type,
method: content.relay.method,
endpoint: {
[environment]: content.relay.endpoint
},
description: content.relay.description,
activated: {
[environment]: getNewValue(content.relay.activated, true)
}
};
}

const config = await createConfig(newConfig, admin);

if (content.strategies?.length) {
for (const strategy of content.strategies) {
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/db_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const configPrdQADocument = {
key: keyConfigPrdQA,
description: 'Test config 2 - Off in PRD and ON in QA',
activated: {
[`${EnvType.DEFAULT}`]: false,
[EnvType.DEFAULT]: false,
QA: true
},
owner: adminMasterAccountId,
Expand Down
Loading