Merge pull request #1720 from balena-os/device-state-typings

Improve target state typings
This commit is contained in:
bulldozer-balena[bot] 2021-06-08 20:00:29 +00:00 committed by GitHub
commit f5382be077
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 61 additions and 50 deletions

View File

@ -35,6 +35,7 @@ import {
TargetApplications, TargetApplications,
DeviceStatus, DeviceStatus,
DeviceReportFields, DeviceReportFields,
TargetState,
} from '../types/state'; } from '../types/state';
import { checkTruthy, checkInt } from '../lib/validation'; import { checkTruthy, checkInt } from '../lib/validation';
import { Proxyvisor } from '../proxyvisor'; import { Proxyvisor } from '../proxyvisor';
@ -446,7 +447,7 @@ export async function executeStep(
// FIXME: This shouldn't be in this module // FIXME: This shouldn't be in this module
export async function setTarget( export async function setTarget(
apps: TargetApplications, apps: TargetApplications,
dependent: any, dependent: TargetState['dependent'],
source: string, source: string,
maybeTrx?: Transaction, maybeTrx?: Transaction,
) { ) {

View File

@ -83,7 +83,7 @@ export async function loadTargetFromFile(
preloadState.config = { ...formattedConf, ...deviceConf }; preloadState.config = { ...formattedConf, ...deviceConf };
const localState = { const localState = {
local: { name: '', ...preloadState }, local: { name: '', ...preloadState },
dependent: { apps: [], devices: [] }, dependent: { apps: {}, devices: {} },
}; };
await deviceState.setTarget(localState); await deviceState.setTarget(localState);

View File

@ -7,8 +7,8 @@ import * as db from '../db';
// at all, and we can use the below type for both insertion and retrieval. // at all, and we can use the below type for both insertion and retrieval.
export interface DatabaseApp { export interface DatabaseApp {
name: string; name: string;
releaseId: number; releaseId?: number;
commit: string; commit?: string;
appId: number; appId: number;
services: string; services: string;
networks: string; networks: string;

View File

@ -243,6 +243,16 @@ export function isValidDependentAppsObject(apps: unknown): boolean {
} }
return _.conformsTo(val, { return _.conformsTo(val, {
parentApp: (n: any) => {
if (!checkInt(n)) {
log.debug(
'Invalid parentApp passed to validation.isValidDependentAppsObject\nName:',
inspect(n),
);
return false;
}
return true;
},
name: (n: any) => { name: (n: any) => {
if (!isValidShortText(n)) { if (!isValidShortText(n)) {
log.debug( log.debug(
@ -283,16 +293,6 @@ export function isValidDependentAppsObject(apps: unknown): boolean {
} }
return true; return true;
}, },
environment: (e: any) => {
if (!undefinedOrValidEnv(e)) {
log.debug(
'Invalid environment passed to validation.isValidDependentAppsObject\nEnvironment:',
inspect(e),
);
return false;
}
return true;
},
}); });
}); });
} }

View File

@ -55,8 +55,8 @@ export interface TargetState {
apps: { apps: {
[appId: string]: { [appId: string]: {
name: string; name: string;
commit: string; commit?: string;
releaseId: number; releaseId?: number;
services: { services: {
[serviceId: string]: { [serviceId: string]: {
labels: LabelObject; labels: LabelObject;
@ -65,6 +65,7 @@ export interface TargetState {
image: string; image: string;
running?: boolean; running?: boolean;
environment: Dictionary<string>; environment: Dictionary<string>;
contract?: Dictionary<any>;
} & ServiceComposeConfig; } & ServiceComposeConfig;
}; };
volumes: Dictionary<Partial<ComposeVolumeConfig>>; volumes: Dictionary<Partial<ComposeVolumeConfig>>;
@ -72,23 +73,29 @@ export interface TargetState {
}; };
}; };
}; };
// TODO: Correctly type this once dependent devices are
// actually properly supported
dependent: { dependent: {
apps: Array<{ apps: {
name?: string; [appId: string]: {
image?: string; name: string;
parentApp: number;
config: EnvVarObject;
releaseId?: number;
imageId?: number;
commit?: string; commit?: string;
config?: EnvVarObject; image?: string;
environment?: EnvVarObject; };
}>; };
devices: Array<{ devices: {
name?: string; [uuid: string]: {
apps?: Dictionary<{ name: string;
config?: EnvVarObject; apps: {
environment?: EnvVarObject; [id: string]: {
}>; config: EnvVarObject;
}>; environment: EnvVarObject;
};
};
};
};
}; };
} }
@ -98,6 +105,10 @@ export type TargetApplication = LocalTargetState['apps'][0];
export type TargetApplicationService = TargetApplication['services'][0]; export type TargetApplicationService = TargetApplication['services'][0];
export type AppsJsonFormat = Omit<TargetState['local'], 'name'> & { export type AppsJsonFormat = Omit<TargetState['local'], 'name'> & {
pinDevice?: boolean; pinDevice?: boolean;
apps: {
// The releaseId/commit are required for preloading
[id: string]: Required<TargetState['local']['apps'][string]>;
};
}; };
export type InstancedAppState = { [appId: number]: App }; export type InstancedAppState = { [appId: number]: App };

View File

@ -66,7 +66,7 @@ const testTarget2 = {
}, },
}, },
}, },
dependent: { apps: [], devices: [] }, dependent: { apps: {}, devices: {} },
}; };
const testTargetWithDefaults2 = { const testTargetWithDefaults2 = {
@ -112,7 +112,6 @@ const testTargetWithDefaults2 = {
}, },
}, },
}, },
dependent: { apps: [], devices: [] },
}; };
const testTargetInvalid = { const testTargetInvalid = {
@ -155,7 +154,7 @@ const testTargetInvalid = {
}, },
}, },
}, },
dependent: { apps: [], devices: [] }, dependent: { apps: {}, devices: {} },
}; };
describe('deviceState', () => { describe('deviceState', () => {

View File

@ -205,7 +205,7 @@ describe('compose/application-manager', () => {
}, },
}, },
}, },
dependent: { apps: [], devices: [] }, dependent: { apps: {}, devices: {} },
}; };
before(async () => { before(async () => {

View File

@ -54,7 +54,7 @@ targetState[0] = {
}, },
}, },
}, },
dependent: { apps: [], devices: [] }, dependent: { apps: {}, devices: {} },
}; };
targetState[1] = { targetState[1] = {
@ -92,7 +92,7 @@ targetState[1] = {
}, },
}, },
}, },
dependent: { apps: [], devices: [] }, dependent: { apps: {}, devices: {} },
}; };
targetState[2] = { targetState[2] = {
@ -146,7 +146,7 @@ targetState[2] = {
}, },
}, },
}, },
dependent: { apps: [], devices: [] }, dependent: { apps: {}, devices: {} },
}; };
targetState[3] = { targetState[3] = {
@ -201,7 +201,7 @@ targetState[3] = {
}, },
}, },
}, },
dependent: { apps: [], devices: [] }, dependent: { apps: {}, devices: {} },
}; };
targetState[4] = { targetState[4] = {
@ -255,7 +255,7 @@ targetState[4] = {
}, },
}, },
}, },
dependent: { apps: [], devices: [] }, dependent: { apps: {}, devices: {} },
}; };
targetState[5] = { targetState[5] = {
@ -308,7 +308,7 @@ targetState[5] = {
}, },
}, },
}, },
dependent: { apps: [], devices: [] }, dependent: { apps: {}, devices: {} },
}; };
targetState[6] = { targetState[6] = {
@ -327,7 +327,7 @@ targetState[6] = {
}, },
}, },
}, },
dependent: { apps: [], devices: [] }, dependent: { apps: {}, devices: {} },
}; };
currentState = []; currentState = [];
@ -415,7 +415,7 @@ currentState[0] = {
}, },
}, },
}, },
dependent: { apps: [], devices: [] }, dependent: { apps: {}, devices: {} },
}; };
currentState[1] = { currentState[1] = {
@ -437,7 +437,7 @@ currentState[1] = {
}, },
}, },
}, },
dependent: { apps: [], devices: [] }, dependent: { apps: {}, devices: {} },
}; };
currentState[2] = { currentState[2] = {
@ -494,7 +494,7 @@ currentState[2] = {
}, },
}, },
}, },
dependent: { apps: [], devices: [] }, dependent: { apps: {}, devices: {} },
}; };
currentState[3] = { currentState[3] = {
@ -585,7 +585,7 @@ currentState[3] = {
}, },
}, },
}, },
dependent: { apps: [], devices: [] }, dependent: { apps: {}, devices: {} },
}; };
currentState[4] = { currentState[4] = {
@ -640,7 +640,7 @@ currentState[4] = {
}, },
}, },
}, },
dependent: { apps: [], devices: [] }, dependent: { apps: {}, devices: {} },
}; };
currentState[5] = { currentState[5] = {
@ -670,7 +670,7 @@ currentState[5] = {
}, },
}, },
}, },
dependent: { apps: [], devices: [] }, dependent: { apps: {}, devices: {} },
}; };
currentState[6] = { currentState[6] = {
@ -757,7 +757,7 @@ currentState[6] = {
}, },
}, },
}, },
dependent: { apps: [], devices: [] }, dependent: { apps: {}, devices: {} },
}; };
availableImages = []; availableImages = [];