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

View File

@ -83,7 +83,7 @@ export async function loadTargetFromFile(
preloadState.config = { ...formattedConf, ...deviceConf };
const localState = {
local: { name: '', ...preloadState },
dependent: { apps: [], devices: [] },
dependent: { apps: {}, devices: {} },
};
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.
export interface DatabaseApp {
name: string;
releaseId: number;
commit: string;
releaseId?: number;
commit?: string;
appId: number;
services: string;
networks: string;

View File

@ -243,6 +243,16 @@ export function isValidDependentAppsObject(apps: unknown): boolean {
}
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) => {
if (!isValidShortText(n)) {
log.debug(
@ -283,16 +293,6 @@ export function isValidDependentAppsObject(apps: unknown): boolean {
}
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: {
[appId: string]: {
name: string;
commit: string;
releaseId: number;
commit?: string;
releaseId?: number;
services: {
[serviceId: string]: {
labels: LabelObject;
@ -65,6 +65,7 @@ export interface TargetState {
image: string;
running?: boolean;
environment: Dictionary<string>;
contract?: Dictionary<any>;
} & ServiceComposeConfig;
};
volumes: Dictionary<Partial<ComposeVolumeConfig>>;
@ -72,23 +73,29 @@ export interface TargetState {
};
};
};
// TODO: Correctly type this once dependent devices are
// actually properly supported
dependent: {
apps: Array<{
name?: string;
image?: string;
commit?: string;
config?: EnvVarObject;
environment?: EnvVarObject;
}>;
devices: Array<{
name?: string;
apps?: Dictionary<{
config?: EnvVarObject;
environment?: EnvVarObject;
}>;
}>;
apps: {
[appId: string]: {
name: string;
parentApp: number;
config: EnvVarObject;
releaseId?: number;
imageId?: number;
commit?: string;
image?: string;
};
};
devices: {
[uuid: string]: {
name: string;
apps: {
[id: string]: {
config: EnvVarObject;
environment: EnvVarObject;
};
};
};
};
};
}
@ -98,6 +105,10 @@ export type TargetApplication = LocalTargetState['apps'][0];
export type TargetApplicationService = TargetApplication['services'][0];
export type AppsJsonFormat = Omit<TargetState['local'], 'name'> & {
pinDevice?: boolean;
apps: {
// The releaseId/commit are required for preloading
[id: string]: Required<TargetState['local']['apps'][string]>;
};
};
export type InstancedAppState = { [appId: number]: App };

View File

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

View File

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

View File

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