Skip to content

Commit

Permalink
Drop the /device-types/v1/:deviceType/images route in favor of hostApps
Browse files Browse the repository at this point in the history
  • Loading branch information
thgreasi committed Sep 6, 2024
1 parent be3b9c7 commit 935ed56
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 170 deletions.
45 changes: 3 additions & 42 deletions src/features/device-types/device-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import _ from 'lodash';
import type { DeviceTypeJson } from './device-type-json.js';
import type { sbvrUtils } from '@balena/pinejs';
import { errors } from '@balena/pinejs';
const { InternalRequestError } = errors;

import { captureException } from '../../infra/error-handling/index.js';

Expand Down Expand Up @@ -123,11 +122,11 @@ export const getImageSize = async (
buildId: string,
): Promise<number> => {
const deviceTypeInfo = await findDeviceTypeInfoBySlug(resinApi, slug);
const deviceType = deviceTypeInfo.latest;
const normalizedSlug = deviceType.slug;
const deviceTypeJson = deviceTypeInfo.latest;
const normalizedSlug = deviceTypeJson.slug;

if (buildId === 'latest') {
buildId = deviceType.buildId;
buildId = deviceTypeJson.buildId;
}

if (!deviceTypeInfo.versions.includes(buildId)) {
Expand All @@ -150,41 +149,3 @@ export const getImageSize = async (
throw err;
}
};

export interface ImageVersions {
versions: string[];
latest: string;
}

export const getImageVersions = async (
resinApi: typeof sbvrUtils.api.resin,
slug: string,
): Promise<ImageVersions> => {
const deviceTypeInfo = await findDeviceTypeInfoBySlug(resinApi, slug);
const deviceType = deviceTypeInfo.latest;
const normalizedSlug = deviceType.slug;

const buildIds = (
await Promise.all(
deviceTypeInfo.versions.map(async (buildId) => {
try {
if ((await getDeviceTypeJson(normalizedSlug, buildId)) != null) {
return buildId;
}
} catch {
return;
}
}),
)
).filter((buildId) => buildId != null);
if (_.isEmpty(buildIds) && !_.isEmpty(deviceTypeInfo.versions)) {
throw new InternalRequestError(
`Could not retrieve any image version for device type ${slug}`,
);
}

return {
versions: buildIds,
latest: buildIds[0],
};
};
12 changes: 1 addition & 11 deletions src/features/device-types/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import type { Application } from 'express';
import { middleware } from '../../infra/auth/index.js';
import {
downloadImageSize,
getDeviceType,
getDeviceTypes,
listAvailableImageVersions,
} from './routes.js';
import { downloadImageSize, getDeviceType, getDeviceTypes } from './routes.js';

export const setup = (app: Application) => {
app.get(
Expand All @@ -18,11 +13,6 @@ export const setup = (app: Application) => {
middleware.resolveCredentialsAndUser,
getDeviceType,
);
app.get(
'/device-types/v1/:deviceType/images',
middleware.resolveCredentialsAndUser,
listAvailableImageVersions,
);
app.get(
'/device-types/v1/:deviceType/images/:version/download-size',
middleware.resolveCredentialsAndUser,
Expand Down
15 changes: 0 additions & 15 deletions src/features/device-types/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,6 @@ export const getDeviceType: RequestHandler = async (req, res) => {
}
};

export const listAvailableImageVersions: RequestHandler = async (req, res) => {
try {
const resinApi = api.resin.clone({ passthrough: { req } });
const slug = deviceTypesLib.validateSlug(req.params.deviceType);
const data = await deviceTypesLib.getImageVersions(resinApi, slug);
res.json(data);
} catch (err) {
captureException(err, 'Error getting image versions', { req });
if (handleHttpErrors(req, res, err)) {
return;
}
res.status(500).send(translateError(err));
}
};

const DOWNLOAD_TIMEOUT = 30000; // we must respond within this time

export const downloadImageSize: RequestHandler = async (req, res) => {
Expand Down
102 changes: 0 additions & 102 deletions test/02_device-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,107 +277,5 @@ export default () => {
expect(rpiConfig).to.not.have.property('logoUrl');
});
});

describe('/device-types/v1/:deviceType/images', () => {
it('should return a proper result', async () => {
const res = await supertest()
.get('/device-types/v1/raspberrypi3-64/images')
.expect(200);
expect(res.body).to.deep.equal({
versions: [
'2.0.2+rev2',
'2.0.2+rev2.dev',
'2.0.2+rev1',
'2.0.2+rev1.dev',
],
latest: '2.0.2+rev2',
});
});

it('should return a proper result for an alias', async () => {
const res = await supertest()
.get('/device-types/v1/raspberrypi364/images')
.expect(200);
expect(res.body).to.deep.equal({
versions: [
'2.0.2+rev2',
'2.0.2+rev2.dev',
'2.0.2+rev1',
'2.0.2+rev1.dev',
],
latest: '2.0.2+rev2',
});
});

it('should omit releases that have an IGNORE file', async () => {
const res = await supertest()
.get('/device-types/v1/dt-with-ignored-release/images')
.expect(200);
expect(res.body).to.deep.equal({
versions: ['2.0.1+rev1.prod', '2.0.0+rev1.prod'],
latest: '2.0.1+rev1.prod',
});
});

it('should omit releases that have an IGNORE file that gives an unauthorized error', async () => {
const res = await supertest()
.get('/device-types/v1/dt-with-403-ignore-file-release/images')
.expect(200);
expect(res.body).to.deep.equal({
versions: ['2.0.1+rev1.prod', '2.0.0+rev1.prod'],
latest: '2.0.1+rev1.prod',
});
});

it('should omit releases that have an empty device-type.json', async () => {
const res = await supertest()
.get('/device-types/v1/dt-with-empty-device-type-json-release/images')
.expect(200);
expect(res.body).to.deep.equal({
versions: ['2.0.1+rev1.prod', '2.0.0+rev1.prod'],
latest: '2.0.1+rev1.prod',
});
});

it('should omit releases that do not have a device-type.json', async () => {
const res = await supertest()
.get('/device-types/v1/dt-with-404-device-type-json-release/images')
.expect(200);
expect(res.body).to.deep.equal({
versions: ['2.0.1+rev1.prod', '2.0.0+rev1.prod'],
latest: '2.0.1+rev1.prod',
});
});

it('should succeed and omit releases that the retrieval of the IGNORE file fails', async () => {
const res = await supertest()
.get('/device-types/v1/dt-with-500-ignore-file-release/images')
.expect(200);
expect(res.body).to.deep.equal({
versions: ['2.0.1+rev1.prod', '2.0.0+rev1.prod'],
latest: '2.0.1+rev1.prod',
});
});

it('should succeed and omit releases that the retrieval of device-type.json fails', async () => {
const res = await supertest()
.get('/device-types/v1/dt-with-500-device-type-json-release/images')
.expect(200);
expect(res.body).to.deep.equal({
versions: ['2.0.1+rev1.prod', '2.0.0+rev1.prod'],
latest: '2.0.1+rev1.prod',
});
});

it('should succeed and omit device types whose details fail to be retrieved', async () => {
const res = await supertest()
.get('/device-types/v1/dt-with-500-device-type-json-release/images')
.expect(200);
expect(res.body).to.deep.equal({
versions: ['2.0.1+rev1.prod', '2.0.0+rev1.prod'],
latest: '2.0.1+rev1.prod',
});
});
});
});
};

0 comments on commit 935ed56

Please sign in to comment.