config generate + openBalena v3: Fix "Cannot read property '__id' of undefined"

Change-type: patch
This commit is contained in:
Paulo Castro 2020-11-05 13:15:55 +00:00
parent 849fc24158
commit 6b4c28a026

View File

@ -19,7 +19,7 @@ import { flags } from '@oclif/command';
import Command from '../../command'; import Command from '../../command';
import * as cf from '../../utils/common-flags'; import * as cf from '../../utils/common-flags';
import { getBalenaSdk, getCliForm, stripIndent } from '../../utils/lazy'; import { getBalenaSdk, getCliForm, stripIndent } from '../../utils/lazy';
import type { Application, PineDeferred } from 'balena-sdk'; import type { PineDeferred } from 'balena-sdk';
interface FlagsDef { interface FlagsDef {
version: string; // OS version version: string; // OS version
@ -131,23 +131,33 @@ export default class ConfigGenerateCmd extends Command {
await this.validateOptions(options); await this.validateOptions(options);
let resourceDeviceType: string; let resourceDeviceType: string;
// Get device | application let application: ApplicationWithDeviceType | null = null;
let resource; let device:
| (DeviceWithDeviceType & { belongs_to__application: PineDeferred })
| null = null;
if (options.device != null) { if (options.device != null) {
const { tryAsInteger } = await import('../../utils/validation'); const { tryAsInteger } = await import('../../utils/validation');
resource = (await balena.models.device.get(tryAsInteger(options.device), { const rawDevice = await balena.models.device.get(
$expand: { tryAsInteger(options.device),
is_of__device_type: { $select: 'slug' }, { $expand: { is_of__device_type: { $select: 'slug' } } },
}, );
})) as DeviceWithDeviceType & { belongs_to__application: PineDeferred }; if (!rawDevice.belongs_to__application) {
resourceDeviceType = resource.is_of__device_type[0].slug; const { ExpectedError } = await import('../../errors');
throw new ExpectedError(stripIndent`
Device ${options.device} does not appear to belong to an accessible application.
Try with a different device, or use '--application' instead of '--device'.`);
}
device = rawDevice as DeviceWithDeviceType & {
belongs_to__application: PineDeferred;
};
resourceDeviceType = device.is_of__device_type[0].slug;
} else { } else {
resource = (await balena.models.application.get(options.application!, { application = (await balena.models.application.get(options.application!, {
$expand: { $expand: {
is_for__device_type: { $select: 'slug' }, is_for__device_type: { $select: 'slug' },
}, },
})) as ApplicationWithDeviceType; })) as ApplicationWithDeviceType;
resourceDeviceType = resource.is_for__device_type[0].slug; resourceDeviceType = application.is_for__device_type[0].slug;
} }
const deviceType = options.deviceType || resourceDeviceType; const deviceType = options.deviceType || resourceDeviceType;
@ -186,18 +196,15 @@ export default class ConfigGenerateCmd extends Command {
); );
let config; let config;
if ('uuid' in resource && resource.uuid != null) { if (device) {
config = await generateDeviceConfig( config = await generateDeviceConfig(
resource, device,
options.deviceApiKey || options['generate-device-api-key'] || undefined, options.deviceApiKey || options['generate-device-api-key'] || undefined,
answers, answers,
); );
} else { } else if (application) {
answers.deviceType = deviceType; answers.deviceType = deviceType;
config = await generateApplicationConfig( config = await generateApplicationConfig(application, answers);
resource as Application,
answers,
);
} }
// Output // Output