Skip to content

Commit

Permalink
wip config vars
Browse files Browse the repository at this point in the history
  • Loading branch information
Page- committed Dec 23, 2021
1 parent 42c50c2 commit a96ceff
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 123 deletions.
6 changes: 6 additions & 0 deletions src/abstract-sql-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ const $renameEnvVarName = (abstractSql: AbstractSqlModel, resource: string) => {
export const renameEnvVarName = (abstractSql: AbstractSqlModel) => {
$renameEnvVarName(abstractSql, 'device');
$renameEnvVarName(abstractSql, 'application');
$renameEnvVarName(
abstractSql,
'device-installs-application-has-service name',
);
$renameEnvVarName(abstractSql, 'application-has-service name');
$renameEnvVarName(abstractSql, 'image-is part of-release');
};

const sqlConcatFactory = (
Expand Down
18 changes: 15 additions & 3 deletions src/balena.sbvr
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,14 @@ Term: application
Term Form: service label
Database Table Name: service label

Fact type: service has name (Auth)
Fact type: service has env var name
Term Form: service environment variable
Database Table Name: service environment variable

Fact type: service has name (Auth)
Term Form: service config variable
Database Table Name: service config variable

Fact type: application has tag key
Term Form: application tag
Database Table Name: application tag
Expand Down Expand Up @@ -364,10 +368,14 @@ Term: device
Term Form: service install
Database Table Name: service install

Fact type: service install has name (Auth)
Fact type: service install has env var name
Term Form: device service environment variable
Database Table Name: device service environment variable

Fact type: service install has name (Auth)
Term Form: device service config variable
Database Table Name: device service config variable

Fact type: device has tag key
Term Form: device tag
Database Table Name: device tag
Expand All @@ -393,10 +401,14 @@ Fact type: image is part of release
Term Form: image label
Database Table Name: image label

Fact type: release image has name (Auth)
Fact type: release image has env var name
Term Form: image environment variable
Database Table Name: image environment variable

Fact type: release image has name (Auth)
Term Form: image config variable
Database Table Name: image config variable

Fact type: user (Auth) is member of organization
Synonymous Form: organization includes user (Auth)
Database Table Name: organization membership
Expand Down
3 changes: 3 additions & 0 deletions src/features/vars-schema/hooks/config-var-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const configVarHook: hooks.Hooks = {
for (const resource of [
'application_config_variable',
'device_config_variable',
'service_config_variable',
'device_services_config_variable',
'image_config_variable',
]) {
for (const method of ['POST', 'PATCH', 'PUT'] as const) {
hooks.addPureHook(method, 'resin', resource, configVarHook);
Expand Down
247 changes: 127 additions & 120 deletions src/features/vars-schema/hooks/vars-update-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ const addAppEnvHooks = (resource: string) =>
};
},
);

addAppEnvHooks('application_config_variable');
addAppEnvHooks('application_environment_variable');

Expand Down Expand Up @@ -153,18 +152,41 @@ const addDeviceEnvHooks = (resource: string) =>
};
},
);

addDeviceEnvHooks('device_config_variable');
addDeviceEnvHooks('device_environment_variable');

addEnvHooks(
'service_environment_variable',
async (
args: hooks.HookArgs & {
tx: Tx;
},
) => {
if (args.req.body.service != null) {
const addServiceEnvHooks = (resource: string) =>
addEnvHooks(
resource,
async (
args: hooks.HookArgs & {
tx: Tx;
},
) => {
if (args.req.body.service != null) {
return {
service_install: {
$any: {
$alias: 'si',
$expr: {
si: {
service: {
$any: {
$alias: 's',
$expr: { s: { id: args.req.body.service } },
},
},
},
},
},
},
};
}

const envVarIds = await sbvrUtils.getAffectedIds(args);
if (envVarIds.length === 0) {
return;
}
return {
service_install: {
$any: {
Expand All @@ -174,36 +196,14 @@ addEnvHooks(
service: {
$any: {
$alias: 's',
$expr: { s: { id: args.req.body.service } },
},
},
},
},
},
},
};
}

const envVarIds = await sbvrUtils.getAffectedIds(args);
if (envVarIds.length === 0) {
return;
}
return {
service_install: {
$any: {
$alias: 'si',
$expr: {
si: {
service: {
$any: {
$alias: 's',
$expr: {
s: {
service_environment_variable: {
$any: {
$alias: 'e',
$expr: {
e: { id: { $in: envVarIds } },
$expr: {
s: {
[resource]: {
$any: {
$alias: 'e',
$expr: {
e: { id: { $in: envVarIds } },
},
},
},
},
Expand All @@ -214,61 +214,96 @@ addEnvHooks(
},
},
},
};
},
);
addServiceEnvHooks('service_environment_variable');
addServiceEnvHooks('service_config_variable');

const addDeviceServiceEnvHooks = (resource: string) =>
addEnvHooks(
resource,
async (
args: hooks.HookArgs & {
tx: Tx;
},
};
},
);
) => {
if (args.req.body.service_install != null) {
return {
service_install: {
$any: {
$alias: 's',
$expr: { s: { id: args.req.body.service_install } },
},
},
};
}

addEnvHooks(
'device_service_environment_variable',
async (
args: hooks.HookArgs & {
tx: Tx;
},
) => {
if (args.req.body.service_install != null) {
const envVarIds = await sbvrUtils.getAffectedIds(args);
if (envVarIds.length === 0) {
return;
}
return {
service_install: {
$any: {
$alias: 's',
$expr: { s: { id: args.req.body.service_install } },
$expr: {
s: {
[resource]: {
$any: {
$alias: 'e',
$expr: { e: { id: { $in: envVarIds } } },
},
},
},
},
},
},
};
}
},
);
addDeviceServiceEnvHooks('device_service_environment_variable');
addDeviceServiceEnvHooks('device_service_config_variable');

const envVarIds = await sbvrUtils.getAffectedIds(args);
if (envVarIds.length === 0) {
return;
}
return {
service_install: {
$any: {
$alias: 's',
$expr: {
s: {
device_service_environment_variable: {
$any: {
$alias: 'e',
$expr: { e: { id: { $in: envVarIds } } },
const addImageEnvHooks = (resource: string) =>
addEnvHooks(
resource,
async (
args: hooks.HookArgs & {
tx: Tx;
},
) => {
if (args.req.body.release_image != null) {
return {
image_install: {
$any: {
$alias: 'ii',
$expr: {
installs__image: {
$any: {
$alias: 'i',
$expr: {
i: {
release_image: {
$any: {
$alias: 'ri',
$expr: { ri: { id: args.req.body.release_image } },
},
},
},
},
},
},
},
},
},
},
},
};
},
);
};
}

addEnvHooks(
'image_environment_variable',
async (
args: hooks.HookArgs & {
tx: Tx;
},
) => {
if (args.req.body.release_image != null) {
const envVarIds = await sbvrUtils.getAffectedIds(args);
if (envVarIds.length === 0) {
return;
}
return {
image_install: {
$any: {
Expand All @@ -282,42 +317,13 @@ addEnvHooks(
release_image: {
$any: {
$alias: 'ri',
$expr: { ri: { id: args.req.body.release_image } },
},
},
},
},
},
},
},
},
},
};
}

const envVarIds = await sbvrUtils.getAffectedIds(args);
if (envVarIds.length === 0) {
return;
}
return {
image_install: {
$any: {
$alias: 'ii',
$expr: {
installs__image: {
$any: {
$alias: 'i',
$expr: {
i: {
release_image: {
$any: {
$alias: 'ri',
$expr: {
ri: {
image_environment_variable: {
$any: {
$alias: 'e',
$expr: { e: { id: { $in: envVarIds } } },
$expr: {
ri: {
[resource]: {
$any: {
$alias: 'e',
$expr: { e: { id: { $in: envVarIds } } },
},
},
},
},
Expand All @@ -330,7 +336,8 @@ addEnvHooks(
},
},
},
},
};
},
);
};
},
);
addImageEnvHooks('image_environment_variable');
addImageEnvHooks('image_config_variable');

0 comments on commit a96ceff

Please sign in to comment.