Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add device service fks #1839

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
39 changes: 39 additions & 0 deletions src/balena-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,21 @@ export interface Service {
service_install?: Array<ServiceInstall['Read']>;
is_installed_on__device?: Array<ServiceInstall['Read']>;
is_built_by__image?: Array<Image['Read']>;
is_of__device__installs__application__has__service_name__has__name?: Array<
DeviceServiceEnvironmentVariable['Read']
>;
is_of__device__installs__service__has__name?: Array<
DeviceServiceEnvironmentVariable['Read']
>;
is_of__device__installs__service_environment_variable?: Array<
DeviceServiceEnvironmentVariable['Read']
>;
is_of__service_install__has__name?: Array<
DeviceServiceEnvironmentVariable['Read']
>;
is_of__device_service_environment_variable?: Array<
DeviceServiceEnvironmentVariable['Read']
>;
};
Write: {
created_at: Types['Date Time']['Write'];
Expand Down Expand Up @@ -780,6 +795,21 @@ export interface Device {
installs__image?: Array<ImageInstall['Read']>;
installs__application__has__service_name?: Array<ServiceInstall['Read']>;
installs__service?: Array<ServiceInstall['Read']>;
is_of__device__installs__application__has__service_name__has__name?: Array<
DeviceServiceEnvironmentVariable['Read']
>;
is_of__device__installs__service__has__name?: Array<
DeviceServiceEnvironmentVariable['Read']
>;
is_of__device__installs__service_environment_variable?: Array<
DeviceServiceEnvironmentVariable['Read']
>;
is_of__service_install__has__name?: Array<
DeviceServiceEnvironmentVariable['Read']
>;
is_of__device_service_environment_variable?: Array<
DeviceServiceEnvironmentVariable['Read']
>;
};
Write: {
created_at: Types['Date Time']['Write'];
Expand Down Expand Up @@ -954,12 +984,19 @@ export interface DeviceServiceEnvironmentVariable {
name: Types['Short Text']['Read'];
id: Types['Serial']['Read'];
value: Types['Text']['Read'];
service: { __id: Service['Read']['id'] } | [Service['Read']] | [] | null;
device: { __id: Device['Read']['id'] } | [Device['Read']] | [] | null;
device__installs__application__has__service_name:
| { __id: ServiceInstall['Read']['id'] }
| [ServiceInstall['Read']];
device__installs__service:
| { __id: ServiceInstall['Read']['id'] }
| [ServiceInstall['Read']];
application__has__service_name:
| { __id: Service['Read']['id'] }
| [Service['Read']]
| []
| null;
};
Write: {
created_at: Types['Date Time']['Write'];
Expand All @@ -968,6 +1005,8 @@ export interface DeviceServiceEnvironmentVariable {
name: Types['Short Text']['Write'];
id: Types['Serial']['Write'];
value: Types['Text']['Write'];
service: Service['Write']['id'] | null;
device: Device['Write']['id'] | null;
};
}

Expand Down
4 changes: 4 additions & 0 deletions src/balena.sbvr
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,10 @@ Fact type: image environment variable has value

Fact type: device service environment variable has value
Necessity: each device service environment variable has exactly one value.
Fact type: device service environment variable has service
Necessity: each device service environment variable has at most one service.
Fact type: device service environment variable has device
Necessity: each device service environment variable has at most one device.


-- application tag
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { hooks, errors, type sbvrUtils } from '@balena/pinejs';

async function backfillDeviceAndService({
request,
api,
}: sbvrUtils.HookArgs<'resin'>) {
const { service_install: siId } = request.values;

if (siId == null) {
return;
}

const si = await api.get({
resource: 'service_install',
id: siId,
options: {
$select: ['device', 'service'],
},
});

if (si == null) {
throw new errors.UnauthorizedError();
}

request.values.device = si.device.__id;
request.values.service = si.service.__id;
}

hooks.addPureHook('POST', 'resin', 'device_service_environment_variable', {
POSTPARSE: backfillDeviceAndService,
});

hooks.addPureHook('PATCH', 'resin', 'device_service_environment_variable', {
POSTPARSE: backfillDeviceAndService,
});
1 change: 1 addition & 0 deletions src/features/service-install/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './backfill-device-service-environment-variable.js';
1 change: 1 addition & 0 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import './features/device-urls/hooks.js';
import './features/hostapp/hooks/index.js';
import './features/organizations/hooks/index.js';
import './features/releases/hooks/index.js';
import './features/service-install/hooks/index.js';
import './features/supervisor-app/hooks/index.js';
import './features/tags/hooks.js';
import './features/vars-schema/hooks/index.js';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
ALTER TABLE "device service environment variable"
ADD COLUMN IF NOT EXISTS "device" INTEGER NULL;

ALTER TABLE "device service environment variable"
ADD COLUMN IF NOT EXISTS "service" INTEGER NULL;

CREATE UNIQUE INDEX IF NOT EXISTS "device service environment variable_device_service_name_key"
ON "device service environment variable" ("device", "service", "name");

DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM information_schema.table_constraints AS tc
JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name
WHERE tc.constraint_type = 'FOREIGN KEY'
AND tc.table_schema = CURRENT_SCHEMA()
AND tc.table_name = 'device service environment variable'
AND kcu.column_name = 'device'
) THEN
ALTER TABLE "device service environment variable"
ADD CONSTRAINT "device service environment variable_device_fkey"
FOREIGN KEY ("device") REFERENCES "device" ("id");
END IF;

IF NOT EXISTS (
SELECT 1
FROM information_schema.table_constraints AS tc
JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name
WHERE tc.constraint_type = 'FOREIGN KEY'
AND tc.table_schema = CURRENT_SCHEMA()
AND tc.table_name = 'device service environment variable'
AND kcu.column_name = 'service'
) THEN
ALTER TABLE "device service environment variable"
ADD CONSTRAINT "device service environment variable_service_fkey"
FOREIGN KEY ("service") REFERENCES "service" ("id");
END IF;
END;
$$;
105 changes: 105 additions & 0 deletions test/25_service-installs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as fixtures from './test-lib/fixtures.js';

import { assertExists, expectToEventually } from './test-lib/common.js';
import * as config from '../src/lib/config.js';
import { supertest } from './test-lib/supertest.js';

export default () => {
versions.test((version, pineTest) => {
Expand Down Expand Up @@ -40,6 +41,7 @@ export default () => {
ctx.app1Service2 = fx.services.app1Service2;
ctx.app1Service3 = fx.services.app1Service3;
ctx.app2Service1 = fx.services.app2Service1;
ctx.app2Service2 = fx.services.app2Service2;

config.TEST_MOCK_ONLY.ASYNC_TASK_CREATE_SERVICE_INSTALLS_ENABLED =
isServiceInstallEnabled;
Expand Down Expand Up @@ -169,6 +171,109 @@ export default () => {
);
});
});

it('should be able to use service_install to create a device_service_environment_variable', async () => {
const { body: serviceInstall } = await pineUser
.get({
resource: 'service_install',
id: {
device: ctx.device.id,
installs__service: ctx.app2Service1.id,
},
})
.expect(200);
assertExists(serviceInstall);

const { body: deviceServiceEnvVar } = await pineUser
.post({
resource: 'device_service_environment_variable',
body: {
service_install: serviceInstall.id,
name: 'test',
value: '123',
},
})
.expect(201);
assertExists(deviceServiceEnvVar);

const {
body: {
d: [dbDeviceServiceEnvVar],
},
} = await supertest(ctx.admin)
.get(
`/resin/device_service_environment_variable(${deviceServiceEnvVar.id})?$select=device,service`,
)
.expect(200);

expect(dbDeviceServiceEnvVar.device.__id).to.equal(ctx.device.id);
expect(dbDeviceServiceEnvVar.service.__id).to.equal(
ctx.app2Service1.id,
);
});

it('should be able to update device_service_environment_variable service_install', async () => {
const { body: serviceInstallService1 } = await pineUser
.get({
resource: 'service_install',
id: {
device: ctx.device.id,
installs__service: ctx.app2Service1.id,
},
})
.expect(200);
assertExists(serviceInstallService1);

const { body: serviceInstallService2 } = await pineUser
.get({
resource: 'service_install',
id: {
device: ctx.device.id,
installs__service: ctx.app2Service2.id,
},
})
.expect(200);
assertExists(serviceInstallService2);

const {
body: [deviceServiceEnvVar],
} = await pineUser
.get({
resource: 'device_service_environment_variable',
options: {
$filter: {
service_install: serviceInstallService1.id,
},
},
})
.expect(200);
assertExists(deviceServiceEnvVar);

await pineUser
.patch({
resource: 'device_service_environment_variable',
id: deviceServiceEnvVar.id,
body: {
service_install: serviceInstallService2.id,
},
})
.expect(200);

const {
body: {
d: [dbDeviceServiceEnvVar],
},
} = await supertest(ctx.admin)
.get(
`/resin/device_service_environment_variable(${deviceServiceEnvVar.id})?$select=device,service`,
)
.expect(200);

expect(dbDeviceServiceEnvVar.device.__id).to.equal(ctx.device.id);
expect(dbDeviceServiceEnvVar.service.__id).to.equal(
ctx.app2Service2.id,
);
});
});
});
});
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/25-service-installs/images.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,14 @@
"project_type": "Dockerfile.template",
"build_log": "This is also the build log",
"status": "success"
},
"image5": {
"user": "admin",
"service": "app2Service2",
"releases": [ "app2release1" ],
"image_size": 2048,
"project_type": "Dockerfile.template",
"build_log": "This is also the build log",
"status": "success"
}
}
5 changes: 5 additions & 0 deletions test/fixtures/25-service-installs/services.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,10 @@
"user": "admin",
"application": "app2",
"service_name": "app2Service1"
},
"app2Service2": {
"user": "admin",
"application": "app2",
"service_name": "app2Service2"
}
}
Loading