Skip to content

Commit

Permalink
Merge pull request #1060 from balena-io/add-device-type-model
Browse files Browse the repository at this point in the history
Add device type model
  • Loading branch information
bulldozer-balena[bot] authored Mar 5, 2021
2 parents 2a6d102 + 0dbf5a9 commit 192f180
Show file tree
Hide file tree
Showing 7 changed files with 638 additions and 21 deletions.
250 changes: 230 additions & 20 deletions DOCUMENTATION.md

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions lib/models/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface Config {
mixpanelToken?: string;
intercomAppId?: string;
recurlyPublicKey?: string;
/** @deprecated */
deviceTypes: DeviceTypeJson.DeviceType[];
DEVICE_ONLINE_ICON: string;
DEVICE_OFFLINE_ICON: string;
Expand Down Expand Up @@ -127,6 +128,7 @@ const getConfigModel = function (
* @function
* @memberof balena.models.config
*
* @deprecated use balena.models.deviceType.getAll
* @fulfil {Object[]} - device types
* @returns {Promise}
*
Expand Down
288 changes: 288 additions & 0 deletions lib/models/device-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,288 @@
/*
Copyright 2016 Balena
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import type { InjectedDependenciesParam, PineOptions } from '..';
import { DeviceType } from '../types/models';
import { mergePineOptions } from '../util';
import * as errors from 'balena-errors';

const getDeviceTypeModel = function (deps: InjectedDependenciesParam) {
const { pine } = deps;

const exports = {
/**
* @summary Get a single deviceType
* @name get
* @public
* @function
* @memberof balena.models.deviceType
*
* @param {String|Number} idOrSlug - device type slug (string) or id
* @param {Object} [options={}] - extra pine options to use
* @fulfil {Object[]} - device types
* @returns {Promise}
*
* @description
* This method returns a single device type.
*
* @example
* balena.models.deviceType.get('raspberry-pi').then(function(deviceType) {
* console.log(deviceType);
* });
*
* @example
* balena.models.deviceType.get('raspberry-pi', function(error, deviceType) {
* if (error) throw error;
* console.log(deviceType);
* });
*/
async get(
idOrSlug: number | string,
options?: PineOptions<DeviceType>,
): Promise<DeviceType> {
if (options == null) {
options = {};
}

if (idOrSlug == null) {
throw new errors.BalenaInvalidDeviceType(idOrSlug);
}

const deviceType = await pine.get({
resource: 'device_type',
id: typeof idOrSlug === 'string' ? { slug: idOrSlug } : idOrSlug,
options,
});

if (deviceType == null) {
throw new errors.BalenaInvalidDeviceType(idOrSlug.toString());
}

return deviceType;
},
/**
* @summary Get all deviceTypes
* @name getAll
* @public
* @function
* @memberof balena.models.deviceType
*
* @param {Object} [options={}] - extra pine options to use
* @fulfil {Object[]} - device types
* @returns {Promise}
*
* @description
* This method returns all device types.
*
* @example
* balena.models.deviceType.getAll().then(function(deviceTypes) {
* console.log(deviceTypes);
* });
*
* @example
* balena.models.deviceType.getAll({ $select: ['name', 'slug'] }).then(function(deviceTypes) {
* console.log(deviceTypes);
* })
*
* @example
* balena.models.deviceType.getAll(function(error, deviceTypes) {
* if (error) throw error;
* console.log(deviceTypes);
* });
*/
async getAll(options?: PineOptions<DeviceType>): Promise<DeviceType[]> {
if (options == null) {
options = {};
}

const deviceTypes = await pine.get({
resource: 'device_type',
options: mergePineOptions({ $orderby: 'name asc' }, options),
});

return deviceTypes;
},

/**
* @summary Get all supported deviceTypes
* @name getAllSupported
* @public
* @function
* @memberof balena.models.deviceType
*
* @param {Object} [options={}] - extra pine options to use
* @fulfil {Object[]} - device types
* @returns {Promise}
*
* @description
* This method returns all supported device types.
*
* @example
* balena.models.deviceType.getAllSupported().then(function(deviceTypes) {
* console.log(deviceTypes);
* });
*
* @example
* balena.models.deviceType.getAllSupported({ $select: ['name', 'slug'] }).then(function(deviceTypes) {
* console.log(deviceTypes);
* })
*
* @example
* balena.models.deviceType.getAllSupported(function(error, deviceTypes) {
* if (error) throw error;
* console.log(deviceTypes);
* });
*/
async getAllSupported(
options?: PineOptions<DeviceType>,
): Promise<DeviceType[]> {
if (options == null) {
options = {};
}

const deviceTypes = await exports.getAll(
mergePineOptions(
{
$filter: {
is_default_for__application: {
$any: {
$alias: 'idfa',
$expr: {
idfa: {
is_host: true,
is_archived: false,
},
},
},
},
},
},
options,
),
);

return deviceTypes;
},

/**
* @summary Get a deviceType by slug or name
* @name getBySlugOrName
* @public
* @function
* @memberof balena.models.deviceType
*
* @param {String} slugOrName - deviceType slug
* @fulfil {Object} - device manifest
* @returns {Promise}
*
* @example
* balena.models.deviceType.getBySlugOrName('raspberry-pi').then(function(manifest) {
* console.log(manifest);
* });
*
* @example
* balena.models.deviceType.getBySlugOrName('raspberry-pi', function(error, manifest) {
* if (error) throw error;
* console.log(manifest);
* });
*/
getBySlugOrName: async (
slugOrName: string,
options?: PineOptions<DeviceType>,
): Promise<DeviceType> => {
if (options == null) {
options = {};
}
const [deviceType] = await exports.getAll(
mergePineOptions(
{
$top: 1,
$filter: { $or: { name: slugOrName, slug: slugOrName } },
},
options,
),
);
if (deviceType == null) {
throw new errors.BalenaInvalidDeviceType(slugOrName);
}
return deviceType;
},

/**
* @summary Get display name for a device
* @name getName
* @public
* @function
* @memberof balena.models.deviceType
*
* @param {String} deviceTypeSlug - device type slug
* @fulfil {String} - device display name
* @returns {Promise}
*
* @example
* balena.models.deviceType.getName('raspberry-pi').then(function(deviceTypeName) {
* console.log(deviceTypeName);
* // Raspberry Pi
* });
*
* @example
* balena.models.deviceType.getName('raspberry-pi', function(error, deviceTypeName) {
* if (error) throw error;
* console.log(deviceTypeName);
* // Raspberry Pi
* });
*/
getName: async (deviceTypeSlug: string): Promise<string> => {
return (
await exports.getBySlugOrName(deviceTypeSlug, { $select: 'name' })
).name;
},

/**
* @summary Get device slug
* @name getSlugByName
* @public
* @function
* @memberof balena.models.deviceType
*
* @param {String} deviceTypeName - device type name
* @fulfil {String} - device slug name
* @returns {Promise}
*
* @example
* balena.models.deviceType.getSlugByName('Raspberry Pi').then(function(deviceTypeSlug) {
* console.log(deviceTypeSlug);
* // raspberry-pi
* });
*
* @example
* balena.models.deviceType.getSlugByName('Raspberry Pi', function(error, deviceTypeSlug) {
* if (error) throw error;
* console.log(deviceTypeSlug);
* // raspberry-pi
* });
*/
getSlugByName: async (deviceTypeName: string): Promise<string> => {
return (
await exports.getBySlugOrName(deviceTypeName, { $select: 'slug' })
).slug;
},
};

return exports;
};

export { getDeviceTypeModel as default };
4 changes: 4 additions & 0 deletions lib/models/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1939,6 +1939,7 @@ const getDeviceModel = function (
* @function
* @memberof balena.models.device
*
* @deprecated use balena.models.deviceType.getName
* @see {@link balena.models.device.getSupportedDeviceTypes} for a list of supported devices
*
* @param {String} deviceTypeSlug - device type slug
Expand Down Expand Up @@ -1980,6 +1981,7 @@ const getDeviceModel = function (
* @function
* @memberof balena.models.device
*
* @deprecated use balena.models.deviceType.getSlugByName
* @see {@link balena.models.device.getSupportedDeviceTypes} for a list of supported devices
*
* @param {String} deviceTypeName - device type name
Expand Down Expand Up @@ -2021,6 +2023,7 @@ const getDeviceModel = function (
* @function
* @memberof balena.models.device
*
* @deprecated use balena.models.deviceType.getAll
* @fulfil {String[]} - supported device types
* @returns {Promise}
*
Expand Down Expand Up @@ -2052,6 +2055,7 @@ const getDeviceModel = function (
* @function
* @memberof balena.models.device
*
* @deprecated use balena.models.deviceType.getBySlugOrName
* @param {String} slugOrName - device slug
* @fulfil {Object} - device manifest
* @returns {Promise}
Expand Down
7 changes: 7 additions & 0 deletions lib/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ const modelsTemplate = {
*/
device: () => (require('./device') as typeof import('./device')).default,

/**
* @namespace deviceType
* @memberof balena.models
*/
deviceType: () =>
(require('./device-type') as typeof import('./device-type')).default,

/**
* @namespace apiKey
* @memberof balena.models
Expand Down
20 changes: 19 additions & 1 deletion lib/types/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,12 +380,30 @@ export interface DeviceType {
name: string;
is_private: boolean;
logo: string | null;

belongs_to__device_family: OptionalNavigationResource<DeviceFamily>;
is_default_for__application: ReverseNavigationResource<Application>;
is_of__cpu_architecture: NavigationResource<CpuArchitecture>;
is_accessible_privately_by__organization: ReverseNavigationResource<Organization>;
describes_device: ReverseNavigationResource<Device>;
}

export interface DeviceFamily {
created_at: string;
modified_at: string;
id: number;
slug: string;
name: string;
is_manufactured_by__device_manufacturer: OptionalNavigationResource<DeviceManufacturer>;
}

export interface DeviceManufacturer {
created_at: string;
modified_at: string;
id: number;
slug: string;
name: string;
}

export interface OrganizationPrivateDeviceTypeAccess {
id: number;
organization: NavigationResource<Organization>;
Expand Down
Loading

0 comments on commit 192f180

Please sign in to comment.