diff --git a/lib/models/application-invite.ts b/lib/models/application-invite.ts index 4f04048d7..ab63d2afd 100644 --- a/lib/models/application-invite.ts +++ b/lib/models/application-invite.ts @@ -38,7 +38,7 @@ const getApplicationInviteModel = function ( deps: InjectedDependenciesParam, opts: InjectedOptionsParam, getApplication: ( - nameOrSlugOrId: string | number, + nameOrSlugOrUuidOrId: string | number, options?: PineOptions, ) => Promise, ) { @@ -89,7 +89,7 @@ const getApplicationInviteModel = function ( * @description * This method returns all invites for a specific application. * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @param {Object} [options={}] - extra pine options to use * @fulfil {Object[]} - invites * @returns {Promise} @@ -110,10 +110,10 @@ const getApplicationInviteModel = function ( * }); */ async getAllByApplication( - nameOrSlugOrId: number | string, + nameOrSlugOrUuidOrId: number | string, options: PineOptions = {}, ): Promise { - const { id } = await getApplication(nameOrSlugOrId, { + const { id } = await getApplication(nameOrSlugOrUuidOrId, { $select: 'id', }); return await exports.getAll( @@ -133,7 +133,7 @@ const getApplicationInviteModel = function ( * * @description This method invites a user by their email to an application. * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @param {Object} options - invite creation parameters * @param {String} options.invitee - the email/balena_username of the invitee * @param {String} [options.roleName="developer"] - the role name to be granted to the invitee @@ -153,11 +153,11 @@ const getApplicationInviteModel = function ( * }); */ async create( - nameOrSlugOrId: string | number, + nameOrSlugOrUuidOrId: string | number, { invitee, roleName, message }: ApplicationInviteOptions, ): Promise { const [{ id }, roles] = await Promise.all([ - getApplication(nameOrSlugOrId, { $select: 'id' }), + getApplication(nameOrSlugOrUuidOrId, { $select: 'id' }), roleName ? pine.get({ resource: 'application_membership_role', diff --git a/lib/models/application-membership.ts b/lib/models/application-membership.ts index 6ab31a96e..927db95dc 100644 --- a/lib/models/application-membership.ts +++ b/lib/models/application-membership.ts @@ -42,7 +42,7 @@ export interface ApplicationMembershipCreationOptions { const getApplicationMembershipModel = function ( deps: InjectedDependenciesParam, getApplication: ( - nameOrSlugOrId: string | number, + nameOrSlugOrUuidOrId: string | number, options?: PineOptions, ) => Promise, ) { @@ -161,7 +161,7 @@ const getApplicationMembershipModel = function ( * @description * This method returns all application memberships for a specific application. * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @param {Object} [options={}] - extra pine options to use * @fulfil {Object[]} - application memberships * @returns {Promise} @@ -182,10 +182,10 @@ const getApplicationMembershipModel = function ( * }); */ async getAllByApplication( - nameOrSlugOrId: number | string, + nameOrSlugOrUuidOrId: number | string, options: PineOptions = {}, ): Promise { - const { id } = await getApplication(nameOrSlugOrId, { + const { id } = await getApplication(nameOrSlugOrUuidOrId, { $select: 'id', }); return await exports.getAll( diff --git a/lib/models/application.ts b/lib/models/application.ts index 71d5858f1..fb620a9da 100644 --- a/lib/models/application.ts +++ b/lib/models/application.ts @@ -91,8 +91,12 @@ const getApplicationModel = function ( resourceName: 'application_tag', resourceKeyField: 'tag_key', parentResourceName: 'application', - async getResourceId(nameOrSlugOrId: string | number): Promise { - const { id } = await exports.get(nameOrSlugOrId, { $select: 'id' }); + async getResourceId( + nameOrSlugOrUuidOrId: string | number, + ): Promise { + const { id } = await exports.get(nameOrSlugOrUuidOrId, { + $select: 'id', + }); return id; }, }, @@ -104,8 +108,12 @@ const getApplicationModel = function ( resourceName: 'application_config_variable', resourceKeyField: 'name', parentResourceName: 'application', - async getResourceId(nameOrSlugOrId: string | number): Promise { - const { id } = await exports.get(nameOrSlugOrId, { $select: 'id' }); + async getResourceId( + nameOrSlugOrUuidOrId: string | number, + ): Promise { + const { id } = await exports.get(nameOrSlugOrUuidOrId, { + $select: 'id', + }); return id; }, }, @@ -116,8 +124,12 @@ const getApplicationModel = function ( resourceName: 'application_environment_variable', resourceKeyField: 'name', parentResourceName: 'application', - async getResourceId(nameOrSlugOrId: string | number): Promise { - const { id } = await exports.get(nameOrSlugOrId, { $select: 'id' }); + async getResourceId( + nameOrSlugOrUuidOrId: string | number, + ): Promise { + const { id } = await exports.get(nameOrSlugOrUuidOrId, { + $select: 'id', + }); return id; }, }, @@ -128,8 +140,12 @@ const getApplicationModel = function ( resourceName: 'build_environment_variable', resourceKeyField: 'name', parentResourceName: 'application', - async getResourceId(nameOrSlugOrId: string | number): Promise { - const { id } = await exports.get(nameOrSlugOrId, { $select: 'id' }); + async getResourceId( + nameOrSlugOrUuidOrId: string | number, + ): Promise { + const { id } = await exports.get(nameOrSlugOrUuidOrId, { + $select: 'id', + }); return id; }, }, @@ -140,11 +156,11 @@ const getApplicationModel = function ( // Internal method for name/id disambiguation // Note that this throws an exception for missing names, but not missing ids - const getId = async (nameOrSlugOrId: string | number) => { - if (isId(nameOrSlugOrId)) { - return nameOrSlugOrId; + const getId = async (nameOrSlugOrUuidOrId: string | number) => { + if (isId(nameOrSlugOrUuidOrId)) { + return nameOrSlugOrUuidOrId; } else { - const { id } = await exports.get(nameOrSlugOrId, { $select: 'id' }); + const { id } = await exports.get(nameOrSlugOrUuidOrId, { $select: 'id' }); return id; } }; @@ -309,7 +325,7 @@ const getApplicationModel = function ( * @function * @memberof balena.models.application * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @param {Object} [options={}] - extra pine options to use * @fulfil {Object} - application * @returns {Promise} @@ -331,26 +347,26 @@ const getApplicationModel = function ( * }); */ async get( - nameOrSlugOrId: string | number, + nameOrSlugOrUuidOrId: string | number, options?: PineOptions, ): Promise { if (options == null) { options = {}; } - if (nameOrSlugOrId == null) { - throw new errors.BalenaApplicationNotFound(nameOrSlugOrId); + if (nameOrSlugOrUuidOrId == null) { + throw new errors.BalenaApplicationNotFound(nameOrSlugOrUuidOrId); } let application; - if (isId(nameOrSlugOrId)) { + if (isId(nameOrSlugOrUuidOrId)) { application = await pine.get({ resource: 'application', - id: nameOrSlugOrId, + id: nameOrSlugOrUuidOrId, options: mergePineOptions({}, options), }); if (application == null) { - throw new errors.BalenaApplicationNotFound(nameOrSlugOrId); + throw new errors.BalenaApplicationNotFound(nameOrSlugOrUuidOrId); } } else { const applications = await pine.get({ @@ -359,8 +375,9 @@ const getApplicationModel = function ( { $filter: { $or: { - app_name: nameOrSlugOrId, - slug: nameOrSlugOrId.toLowerCase(), + app_name: nameOrSlugOrUuidOrId, + slug: nameOrSlugOrUuidOrId.toLowerCase(), + uuid: nameOrSlugOrUuidOrId.toLowerCase(), }, }, }, @@ -368,11 +385,11 @@ const getApplicationModel = function ( ), }); if (applications.length === 0) { - throw new errors.BalenaApplicationNotFound(nameOrSlugOrId); + throw new errors.BalenaApplicationNotFound(nameOrSlugOrUuidOrId); } if (applications.length > 1) { - throw new errors.BalenaAmbiguousApplication(nameOrSlugOrId); + throw new errors.BalenaAmbiguousApplication(nameOrSlugOrUuidOrId); } application = applications[0]; } @@ -393,7 +410,7 @@ const getApplicationModel = function ( * understand format. If you want more control, or to see the raw model * directly, use `application.get(uuidOrId, options)` instead. * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @param {Object} [options={}] - extra pine options to use * @fulfil {Object} - application * @returns {Promise} @@ -415,7 +432,7 @@ const getApplicationModel = function ( * }); */ async getWithDeviceServiceDetails( - nameOrSlugOrId: string | number, + nameOrSlugOrUuidOrId: string | number, options?: PineOptions, ): Promise< Application & { @@ -440,7 +457,7 @@ const getApplicationModel = function ( ); const app = (await exports.get( - nameOrSlugOrId, + nameOrSlugOrUuidOrId, serviceOptions, )) as Application & { owns__device: Array>; @@ -503,7 +520,7 @@ const getApplicationModel = function ( * @function * @memberof balena.models.application * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @fulfil {Boolean} - has application * @returns {Promise} * @@ -523,9 +540,9 @@ const getApplicationModel = function ( * console.log(hasApp); * }); */ - has: async (nameOrSlugOrId: string | number): Promise => { + has: async (nameOrSlugOrUuidOrId: string | number): Promise => { try { - await exports.get(nameOrSlugOrId, { $select: ['id'] }); + await exports.get(nameOrSlugOrUuidOrId, { $select: ['id'] }); return true; } catch (err) { if (err instanceof errors.BalenaApplicationNotFound) { @@ -722,7 +739,7 @@ const getApplicationModel = function ( * @function * @memberof balena.models.application * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @returns {Promise} * * @example @@ -736,16 +753,16 @@ const getApplicationModel = function ( * if (error) throw error; * }); */ - remove: async (nameOrSlugOrId: string | number): Promise => { + remove: async (nameOrSlugOrUuidOrId: string | number): Promise => { try { - const applicationId = await getId(nameOrSlugOrId); + const applicationId = await getId(nameOrSlugOrUuidOrId); await pine.delete({ resource: 'application', id: applicationId, }); } catch (err) { if (isNotFoundResponse(err)) { - treatAsMissingApplication(nameOrSlugOrId, err); + treatAsMissingApplication(nameOrSlugOrUuidOrId, err); } throw err; } @@ -758,7 +775,7 @@ const getApplicationModel = function ( * @function * @memberof balena.models.application * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @param {String} newName - new application name (string) * @returns {Promise} * @@ -774,11 +791,11 @@ const getApplicationModel = function ( * }); */ rename: async ( - nameOrSlugOrId: string | number, + nameOrSlugOrUuidOrId: string | number, newAppName: string, ): Promise => { try { - const applicationId = await getId(nameOrSlugOrId); + const applicationId = await getId(nameOrSlugOrUuidOrId); await pine.patch({ resource: 'application', id: applicationId, @@ -788,7 +805,7 @@ const getApplicationModel = function ( }); } catch (err) { if (isNotFoundResponse(err)) { - treatAsMissingApplication(nameOrSlugOrId, err); + treatAsMissingApplication(nameOrSlugOrUuidOrId, err); } throw err; } @@ -801,7 +818,7 @@ const getApplicationModel = function ( * @function * @memberof balena.models.application * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @returns {Promise} * * @example @@ -815,10 +832,10 @@ const getApplicationModel = function ( * if (error) throw error; * }); */ - restart: (nameOrSlugOrId: string | number): Promise => + restart: (nameOrSlugOrUuidOrId: string | number): Promise => withSupervisorLockedError(async () => { try { - const applicationId = await getId(nameOrSlugOrId); + const applicationId = await getId(nameOrSlugOrUuidOrId); await request.send({ method: 'POST', @@ -827,7 +844,7 @@ const getApplicationModel = function ( }); } catch (err) { if (isNotFoundResponse(err)) { - treatAsMissingApplication(nameOrSlugOrId, err); + treatAsMissingApplication(nameOrSlugOrUuidOrId, err); } throw err; } @@ -845,7 +862,7 @@ const getApplicationModel = function ( * version (2.4.0+) then generateProvisioningKey should work just as well, but * be more secure. * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @fulfil {String} - api key * @returns {Promise} * @@ -866,11 +883,11 @@ const getApplicationModel = function ( * }); */ generateApiKey: async ( - nameOrSlugOrId: string | number, + nameOrSlugOrUuidOrId: string | number, ): Promise => { // Do a full get, not just getId, because the actual api endpoint doesn't fail if the id // doesn't exist. TODO: Can use getId once https://github.com/balena-io/balena-api/issues/110 is resolved - const { id } = await exports.get(nameOrSlugOrId, { $select: 'id' }); + const { id } = await exports.get(nameOrSlugOrUuidOrId, { $select: 'id' }); const { body } = await request.send({ method: 'POST', url: `/application/${id}/generate-api-key`, @@ -886,7 +903,7 @@ const getApplicationModel = function ( * @function * @memberof balena.models.application * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @fulfil {String} - device provisioning key * @returns {Promise} * @@ -907,10 +924,10 @@ const getApplicationModel = function ( * }); */ generateProvisioningKey: async ( - nameOrSlugOrId: string | number, + nameOrSlugOrUuidOrId: string | number, ): Promise => { try { - const applicationId = await getId(nameOrSlugOrId); + const applicationId = await getId(nameOrSlugOrUuidOrId); const { body } = await request.send({ method: 'POST', url: `/api-key/application/${applicationId}/provisioning`, @@ -919,7 +936,7 @@ const getApplicationModel = function ( return body; } catch (err) { if (isNoApplicationForKeyResponse(err)) { - treatAsMissingApplication(nameOrSlugOrId, err); + treatAsMissingApplication(nameOrSlugOrUuidOrId, err); } throw err; } @@ -1043,7 +1060,7 @@ const getApplicationModel = function ( * @function * @memberof balena.models.application * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @fulfil {Boolean} - is tracking the latest release * @returns {Promise} * @@ -1063,11 +1080,13 @@ const getApplicationModel = function ( * }); */ willTrackNewReleases: async ( - nameOrSlugOrId: string | number, + nameOrSlugOrUuidOrId: string | number, ): Promise => { const { should_track_latest_release } = await exports.get( - nameOrSlugOrId, - { $select: 'should_track_latest_release' }, + nameOrSlugOrUuidOrId, + { + $select: 'should_track_latest_release', + }, ); return should_track_latest_release; }, @@ -1079,7 +1098,7 @@ const getApplicationModel = function ( * @function * @memberof balena.models.application * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @fulfil {Boolean} - is tracking the latest release * @returns {Promise} * @@ -1099,7 +1118,7 @@ const getApplicationModel = function ( * }); */ isTrackingLatestRelease: async ( - nameOrSlugOrId: string | number, + nameOrSlugOrUuidOrId: string | number, ): Promise => { const appOptions = { $select: 'should_track_latest_release', @@ -1117,7 +1136,7 @@ const getApplicationModel = function ( } as const; const application = (await exports.get( - nameOrSlugOrId, + nameOrSlugOrUuidOrId, appOptions, )) as PineTypedResult; const trackedRelease = application.should_be_running__release[0]; @@ -1138,7 +1157,7 @@ const getApplicationModel = function ( * @description Configures the application to run a particular release * and not get updated when the latest release changes. * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @param {String} fullReleaseHash - the hash of a successful release (string) * @returns {Promise} * @@ -1159,10 +1178,10 @@ const getApplicationModel = function ( * }); */ pinToRelease: async ( - nameOrSlugOrId: string | number, + nameOrSlugOrUuidOrId: string | number, fullReleaseHash: string, ): Promise => { - const applicationId = await getId(nameOrSlugOrId); + const applicationId = await getId(nameOrSlugOrUuidOrId); const release = await releaseModel().get(fullReleaseHash, { $select: 'id', $top: 1, @@ -1188,7 +1207,7 @@ const getApplicationModel = function ( * @function * @memberof balena.models.application * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @fulfil {String|undefined} - The release hash of the current release * @returns {Promise} * @@ -1208,7 +1227,7 @@ const getApplicationModel = function ( * }); */ getTargetReleaseHash: async ( - nameOrSlugOrId: string | number, + nameOrSlugOrUuidOrId: string | number, ): Promise => { const appOptions = { $select: 'id', @@ -1216,7 +1235,7 @@ const getApplicationModel = function ( } as const; const application = (await exports.get( - nameOrSlugOrId, + nameOrSlugOrUuidOrId, appOptions, )) as PineTypedResult; return application.should_be_running__release[0]?.commit; @@ -1231,7 +1250,7 @@ const getApplicationModel = function ( * * @description The application's current release will be updated with each new successfully built release. * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @returns {Promise} * * @example @@ -1251,7 +1270,7 @@ const getApplicationModel = function ( * }); */ trackLatestRelease: async ( - nameOrSlugOrId: string | number, + nameOrSlugOrUuidOrId: string | number, ): Promise => { const appOptions = { $select: 'id', @@ -1268,7 +1287,7 @@ const getApplicationModel = function ( } as const; const application = (await exports.get( - nameOrSlugOrId, + nameOrSlugOrUuidOrId, appOptions, )) as PineTypedResult; const body: SubmitBody = { @@ -1292,7 +1311,7 @@ const getApplicationModel = function ( * @function * @memberof balena.models.application * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @returns {Promise} * * @example @@ -1307,9 +1326,9 @@ const getApplicationModel = function ( * }); */ enableDeviceUrls: async ( - nameOrSlugOrId: string | number, + nameOrSlugOrUuidOrId: string | number, ): Promise => { - const { id } = await exports.get(nameOrSlugOrId, { $select: 'id' }); + const { id } = await exports.get(nameOrSlugOrUuidOrId, { $select: 'id' }); await pine.patch({ resource: 'device', body: { @@ -1330,7 +1349,7 @@ const getApplicationModel = function ( * @function * @memberof balena.models.application * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @returns {Promise} * * @example @@ -1345,9 +1364,9 @@ const getApplicationModel = function ( * }); */ disableDeviceUrls: async ( - nameOrSlugOrId: string | number, + nameOrSlugOrUuidOrId: string | number, ): Promise => { - const { id } = await exports.get(nameOrSlugOrId, { $select: 'id' }); + const { id } = await exports.get(nameOrSlugOrUuidOrId, { $select: 'id' }); await pine.patch({ resource: 'device', body: { @@ -1368,7 +1387,7 @@ const getApplicationModel = function ( * @function * @memberof balena.models.application * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @param {Number} expiryTimestamp - a timestamp in ms for when the support access will expire * @returns {Promise} * @@ -1384,7 +1403,7 @@ const getApplicationModel = function ( * }); */ async grantSupportAccess( - nameOrSlugOrId: string | number, + nameOrSlugOrUuidOrId: string | number, expiryTimestamp: number, ): Promise { if (expiryTimestamp == null || expiryTimestamp <= Date.now()) { @@ -1395,7 +1414,7 @@ const getApplicationModel = function ( } try { - const applicationId = await getId(nameOrSlugOrId); + const applicationId = await getId(nameOrSlugOrUuidOrId); await pine.patch({ resource: 'application', id: applicationId, @@ -1403,7 +1422,7 @@ const getApplicationModel = function ( }); } catch (err) { if (isNotFoundResponse(err)) { - treatAsMissingApplication(nameOrSlugOrId, err); + treatAsMissingApplication(nameOrSlugOrUuidOrId, err); } throw err; } @@ -1416,7 +1435,7 @@ const getApplicationModel = function ( * @function * @memberof balena.models.application * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @returns {Promise} * * @example @@ -1431,10 +1450,10 @@ const getApplicationModel = function ( * }); */ revokeSupportAccess: async ( - nameOrSlugOrId: string | number, + nameOrSlugOrUuidOrId: string | number, ): Promise => { try { - const applicationId = await getId(nameOrSlugOrId); + const applicationId = await getId(nameOrSlugOrUuidOrId); await pine.patch({ resource: 'application', id: applicationId, @@ -1442,7 +1461,7 @@ const getApplicationModel = function ( }); } catch (err) { if (isNotFoundResponse(err)) { - treatAsMissingApplication(nameOrSlugOrId, err); + treatAsMissingApplication(nameOrSlugOrUuidOrId, err); } throw err; } @@ -1460,7 +1479,7 @@ const getApplicationModel = function ( * @function * @memberof balena.models.application.tags * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @param {Object} [options={}] - extra pine options to use * @fulfil {Object[]} - application tags * @returns {Promise} @@ -1514,7 +1533,7 @@ const getApplicationModel = function ( * @function * @memberof balena.models.application.tags * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @param {String} tagKey - tag key * @param {String|undefined} value - tag value * @@ -1540,7 +1559,7 @@ const getApplicationModel = function ( * @function * @memberof balena.models.application.tags * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @param {String} tagKey - tag key * @returns {Promise} * @@ -1567,7 +1586,7 @@ const getApplicationModel = function ( * @function * @memberof balena.models.application.configVar * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @param {Object} [options={}] - extra pine options to use * @fulfil {Object[]} - application config variables * @returns {Promise} @@ -1597,7 +1616,7 @@ const getApplicationModel = function ( * @function * @memberof balena.models.application.configVar * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @param {String} key - config variable name * @fulfil {String|undefined} - the config variable value (or undefined) * @returns {Promise} @@ -1627,7 +1646,7 @@ const getApplicationModel = function ( * @function * @memberof balena.models.application.configVar * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @param {String} key - config variable name * @param {String} value - config variable value * @returns {Promise} @@ -1657,7 +1676,7 @@ const getApplicationModel = function ( * @function * @memberof balena.models.application.configVar * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @param {String} key - config variable name * @returns {Promise} * @@ -1692,7 +1711,7 @@ const getApplicationModel = function ( * @function * @memberof balena.models.application.envVar * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @param {Object} [options={}] - extra pine options to use * @fulfil {Object[]} - application environment variables * @returns {Promise} @@ -1722,7 +1741,7 @@ const getApplicationModel = function ( * @function * @memberof balena.models.application.envVar * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @param {String} key - environment variable name * @fulfil {String|undefined} - the environment variable value (or undefined) * @returns {Promise} @@ -1752,7 +1771,7 @@ const getApplicationModel = function ( * @function * @memberof balena.models.application.envVar * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @param {String} key - environment variable name * @param {String} value - environment variable value * @returns {Promise} @@ -1782,7 +1801,7 @@ const getApplicationModel = function ( * @function * @memberof balena.models.application.envVar * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @param {String} key - environment variable name * @returns {Promise} * @@ -1817,7 +1836,7 @@ const getApplicationModel = function ( * @function * @memberof balena.models.application.buildVar * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @param {Object} [options={}] - extra pine options to use * @fulfil {Object[]} - application build environment variables * @returns {Promise} @@ -1847,7 +1866,7 @@ const getApplicationModel = function ( * @function * @memberof balena.models.application.buildVar * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @param {String} key - build environment variable name * @fulfil {String|undefined} - the build environment variable value (or undefined) * @returns {Promise} @@ -1877,7 +1896,7 @@ const getApplicationModel = function ( * @function * @memberof balena.models.application.buildVar * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @param {String} key - build environment variable name * @param {String} value - build environment variable value * @returns {Promise} @@ -1907,7 +1926,7 @@ const getApplicationModel = function ( * @function * @memberof balena.models.application.buildVar * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @param {String} key - build environment variable name * @returns {Promise} * diff --git a/lib/models/device.ts b/lib/models/device.ts index 9adb33474..a5e447448 100644 --- a/lib/models/device.ts +++ b/lib/models/device.ts @@ -362,7 +362,7 @@ const getDeviceModel = function ( * * `overall_status` * * `overall_progress` * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @param {Object} [options={}] - extra pine options to use * @fulfil {Object[]} - devices * @returns {Promise} @@ -389,14 +389,14 @@ const getDeviceModel = function ( * }); */ async getAllByApplication( - nameOrSlugOrId: string | number, + nameOrSlugOrUuidOrId: string | number, options?: PineOptions, ): Promise { if (options == null) { options = {}; } - const { id } = await applicationModel().get(nameOrSlugOrId, { + const { id } = await applicationModel().get(nameOrSlugOrUuidOrId, { $select: 'id', }); return await exports.getAll( @@ -1096,7 +1096,7 @@ const getDeviceModel = function ( * @memberof balena.models.device * * @param {String|Number} uuidOrId - device uuid (string) or id (number) - * @param {String|Number} applicationNameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} applicationNameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * * @returns {Promise} * @@ -1116,7 +1116,7 @@ const getDeviceModel = function ( */ move: async ( uuidOrId: string | number, - applicationNameOrSlugOrId: string | number, + applicationNameOrSlugOrUuidOrId: string | number, ): Promise => { const deviceOptions = { $select: 'uuid', @@ -1134,7 +1134,7 @@ const getDeviceModel = function ( >, configModel().getDeviceTypes(), applicationModel().get( - applicationNameOrSlugOrId, + applicationNameOrSlugOrUuidOrId, applicationOptions, ) as Promise>, ]); @@ -1152,7 +1152,7 @@ const getDeviceModel = function ( ); if (!isCompatibleMove) { throw new errors.BalenaInvalidDeviceType( - `Incompatible application: ${applicationNameOrSlugOrId}`, + `Incompatible application: ${applicationNameOrSlugOrUuidOrId}`, ); } @@ -1414,7 +1414,7 @@ const getDeviceModel = function ( * @function * @memberof balena.models.device * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @fulfil {Object} - device manifest * @returns {Promise} * @@ -1435,7 +1435,7 @@ const getDeviceModel = function ( * }); */ getManifestByApplication: async ( - nameOrSlugOrId: string | number, + nameOrSlugOrUuidOrId: string | number, ): Promise => { const applicationOptions = { $select: 'id', @@ -1443,7 +1443,7 @@ const getDeviceModel = function ( } as const; const app = (await applicationModel().get( - nameOrSlugOrId, + nameOrSlugOrUuidOrId, applicationOptions, )) as PineTypedResult; return await exports.getManifestBySlug(app.is_for__device_type[0].slug); @@ -1475,7 +1475,7 @@ const getDeviceModel = function ( * @function * @memberof balena.models.device * - * @param {String|Number} applicationNameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} applicationNameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @param {String} uuid - device uuid * * @fulfil {Object} Device registration info ({ id: "...", uuid: "...", api_key: "..." }) @@ -1501,7 +1501,7 @@ const getDeviceModel = function ( * }); */ async register( - applicationNameOrSlugOrId: string | number, + applicationNameOrSlugOrUuidOrId: string | number, uuid: string, ): Promise<{ id: number; @@ -1515,9 +1515,11 @@ const getDeviceModel = function ( const [userId, apiKey, application] = await Promise.all([ sdkInstance.auth.getUserId(), - applicationModel().generateProvisioningKey(applicationNameOrSlugOrId), + applicationModel().generateProvisioningKey( + applicationNameOrSlugOrUuidOrId, + ), applicationModel().get( - applicationNameOrSlugOrId, + applicationNameOrSlugOrUuidOrId, applicationOptions, ) as Promise>, ]); @@ -2591,7 +2593,7 @@ const getDeviceModel = function ( * @function * @memberof balena.models.device.tags * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @param {Object} [options={}] - extra pine options to use * @fulfil {Object[]} - device tags * @returns {Promise} @@ -2613,13 +2615,13 @@ const getDeviceModel = function ( * }); */ async getAllByApplication( - nameOrSlugOrId: string | number, + nameOrSlugOrUuidOrId: string | number, options?: PineOptions, ): Promise { if (options == null) { options = {}; } - const { id } = await applicationModel().get(nameOrSlugOrId, { + const { id } = await applicationModel().get(nameOrSlugOrUuidOrId, { $select: 'id', }); return await tagsModel.getAll( @@ -2783,7 +2785,7 @@ const getDeviceModel = function ( * @function * @memberof balena.models.device.configVar * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @param {Object} [options={}] - extra pine options to use * @fulfil {Object[]} - device config variables * @returns {Promise} @@ -2805,14 +2807,14 @@ const getDeviceModel = function ( * }); */ async getAllByApplication( - nameOrSlugOrId: string | number, + nameOrSlugOrUuidOrId: string | number, options?: PineOptions, ): Promise { if (options == null) { options = {}; } - const { id } = await applicationModel().get(nameOrSlugOrId, { + const { id } = await applicationModel().get(nameOrSlugOrUuidOrId, { $select: 'id', }); return await configVarModel.getAll( @@ -2969,7 +2971,7 @@ const getDeviceModel = function ( * @function * @memberof balena.models.device.envVar * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @param {Object} [options={}] - extra pine options to use * @fulfil {Object[]} - device environment variables * @returns {Promise} @@ -2991,14 +2993,14 @@ const getDeviceModel = function ( * }); */ async getAllByApplication( - nameOrSlugOrId: string | number, + nameOrSlugOrUuidOrId: string | number, options?: PineOptions, ): Promise { if (options == null) { options = {}; } - const { id } = await applicationModel().get(nameOrSlugOrId, { + const { id } = await applicationModel().get(nameOrSlugOrUuidOrId, { $select: 'id', }); return await envVarModel.getAll( @@ -3180,7 +3182,7 @@ const getDeviceModel = function ( * @function * @memberof balena.models.device.serviceVar * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @param {Object} [options={}] - extra pine options to use * @fulfil {Object[]} - service variables * @returns {Promise} @@ -3202,14 +3204,14 @@ const getDeviceModel = function ( * }); */ async getAllByApplication( - nameOrSlugOrId: string | number, + nameOrSlugOrUuidOrId: string | number, options?: PineOptions, ): Promise { if (options == null) { options = {}; } - const { id } = await applicationModel().get(nameOrSlugOrId, { + const { id } = await applicationModel().get(nameOrSlugOrUuidOrId, { $select: 'id', }); return await pine.get({ diff --git a/lib/models/os.ts b/lib/models/os.ts index ec8878224..eeb5ecce5 100644 --- a/lib/models/os.ts +++ b/lib/models/os.ts @@ -463,7 +463,7 @@ const getOsModel = function ( * Note that an OS version is required. For versions < 2.7.8, config * generation is only supported when using a session token, not an API key. * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number). + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number). * @param {Object} options - OS configuration options to use. * @param {String} options.version - Required: the OS version of the image. * @param {String} [options.network='ethernet'] - The network type that @@ -495,7 +495,7 @@ const getOsModel = function ( * }); */ const getConfig = async function ( - nameOrSlugOrId: string | number, + nameOrSlugOrUuidOrId: string | number, options: ImgConfigOptions, ): Promise { if (!options?.version) { @@ -505,7 +505,9 @@ const getOsModel = function ( options.network = options.network ?? 'ethernet'; try { - const applicationId = await applicationModel()._getId(nameOrSlugOrId); + const applicationId = await applicationModel()._getId( + nameOrSlugOrUuidOrId, + ); const { body } = await request.send({ method: 'POST', @@ -516,7 +518,7 @@ const getOsModel = function ( return body; } catch (err) { if (isNotFoundResponse(err)) { - treatAsMissingApplication(nameOrSlugOrId, err); + treatAsMissingApplication(nameOrSlugOrUuidOrId, err); } throw err; } diff --git a/lib/models/release.ts b/lib/models/release.ts index 306f0a66c..ad5cb2627 100644 --- a/lib/models/release.ts +++ b/lib/models/release.ts @@ -249,7 +249,7 @@ const getReleaseModel = function ( * @function * @memberof balena.models.release * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @param {Object} [options={}] - extra pine options to use * @fulfil {Object[]} - releases * @returns {Promise} @@ -271,10 +271,10 @@ const getReleaseModel = function ( * }); */ async function getAllByApplication( - nameOrSlugOrId: string | number, + nameOrSlugOrUuidOrId: string | number, options: BalenaSdk.PineOptions = {}, ): Promise { - const { id } = await applicationModel().get(nameOrSlugOrId, { + const { id } = await applicationModel().get(nameOrSlugOrUuidOrId, { $select: 'id', }); return await pine.get({ @@ -298,7 +298,7 @@ const getReleaseModel = function ( * @function * @memberof balena.models.release * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @param {Object} [options={}] - extra pine options to use * @fulfil {Object|undefined} - release * @returns {Promise} @@ -320,11 +320,11 @@ const getReleaseModel = function ( * }); */ async function getLatestByApplication( - nameOrSlugOrId: string | number, + nameOrSlugOrUuidOrId: string | number, options: BalenaSdk.PineOptions = {}, ): Promise { const [release] = await getAllByApplication( - nameOrSlugOrId, + nameOrSlugOrUuidOrId, mergePineOptions( { $top: 1, @@ -345,7 +345,7 @@ const getReleaseModel = function ( * @function * @memberof balena.models.release * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @param {Object} urlDeployOptions - builder options * @param {String} urlDeployOptions.url - a url with a tarball of the project to build * @param {Boolean} [urlDeployOptions.shouldFlatten=true] - Should be true when the tarball includes an extra root folder with all the content @@ -369,7 +369,7 @@ const getReleaseModel = function ( * }); */ async function createFromUrl( - nameOrSlugOrId: string | number, + nameOrSlugOrUuidOrId: string | number, urlDeployOptions: BuilderUrlDeployOptions, ): Promise { const appOptions = { @@ -382,7 +382,7 @@ const getReleaseModel = function ( } as const; const { app_name, organization } = (await applicationModel().get( - nameOrSlugOrId, + nameOrSlugOrUuidOrId, appOptions, )) as PineTypedResult; return await builderHelper().buildFromUrl( @@ -404,7 +404,7 @@ const getReleaseModel = function ( * @function * @memberof balena.models.release.tags * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @param {Object} [options={}] - extra pine options to use * @fulfil {Object[]} - release tags * @returns {Promise} @@ -426,10 +426,10 @@ const getReleaseModel = function ( * }); */ async getAllByApplication( - nameOrSlugOrId: string | number, + nameOrSlugOrUuidOrId: string | number, options: BalenaSdk.PineOptions = {}, ): Promise { - const { id } = await applicationModel().get(nameOrSlugOrId, { + const { id } = await applicationModel().get(nameOrSlugOrUuidOrId, { $select: 'id', }); return await tagsModel.getAll( diff --git a/lib/models/service.ts b/lib/models/service.ts index 721cfc524..ceba0132c 100644 --- a/lib/models/service.ts +++ b/lib/models/service.ts @@ -77,7 +77,7 @@ const getServiceModel = ( * @function * @memberof balena.models.service * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @param {Object} [options={}] - extra pine options to use * @fulfil {Object[]} - services * @returns {Promise} @@ -99,10 +99,10 @@ const getServiceModel = ( * }); */ async getAllByApplication( - nameOrSlugOrId: string | number, + nameOrSlugOrUuidOrId: string | number, options: PineOptions = {}, ): Promise { - const { id } = await applicationModel().get(nameOrSlugOrId, { + const { id } = await applicationModel().get(nameOrSlugOrUuidOrId, { $select: 'id', }); return pine.get({ @@ -148,7 +148,7 @@ const getServiceModel = ( * @function * @memberof balena.models.service.var * - * @param {String|Number} nameOrSlugOrId - application name (string), slug (string) or id (number) + * @param {String|Number} nameOrSlugOrUuidOrId - application name (string), slug (string), uuid (string) or id (number) * @param {Object} [options={}] - extra pine options to use * @fulfil {Object[]} - service variables * @returns {Promise} @@ -170,10 +170,10 @@ const getServiceModel = ( * }); */ async getAllByApplication( - nameOrSlugOrId: string | number, + nameOrSlugOrUuidOrId: string | number, options: PineOptions = {}, ): Promise { - const { id } = await applicationModel().get(nameOrSlugOrId, { + const { id } = await applicationModel().get(nameOrSlugOrUuidOrId, { $select: 'id', }); return varModel.getAll( diff --git a/tests/integration/models/application.spec.js b/tests/integration/models/application.spec.js index 4fd136130..bdb5500df 100644 --- a/tests/integration/models/application.spec.js +++ b/tests/integration/models/application.spec.js @@ -1474,7 +1474,7 @@ describe('Application Model', function () { resource: 'application', options: { $top: 1, - $select: ['id', 'app_name', 'slug', 'is_public'], + $select: ['id', 'app_name', 'slug', 'uuid', 'is_public'], $filter: { is_public: true }, }, }); @@ -1503,7 +1503,7 @@ describe('Application Model', function () { .get({ resource: 'application', options: { - $select: ['id', 'app_name', 'slug', 'is_public'], + $select: ['id', 'app_name', 'slug', 'uuid', 'is_public'], }, }) .then(function (apps) { @@ -1516,6 +1516,7 @@ describe('Application Model', function () { expect(app).to.have.property('id').that.is.a('number'); expect(app).to.have.property('app_name').that.is.a('string'); expect(app).to.have.property('slug').that.is.a('string'); + expect(app).to.have.property('uuid').that.is.a('string'); return expect(app).to.have.property('is_public', true); }); }); @@ -1533,6 +1534,7 @@ describe('Application Model', function () { expect(app).to.have.property('id').that.is.a('number'); expect(app).to.have.property('app_name').that.is.a('string'); expect(app).to.have.property('slug').that.is.a('string'); + expect(app).to.have.property('uuid').that.is.a('string'); return expect(app).to.have.property('is_public', true); }); }, diff --git a/tests/integration/setup.ts b/tests/integration/setup.ts index c19a3c28d..b844ed3c6 100644 --- a/tests/integration/setup.ts +++ b/tests/integration/setup.ts @@ -621,5 +621,6 @@ export const applicationRetrievalFields = toWritable([ 'id', 'app_name', 'slug', + 'uuid', ] as const); export const deviceUniqueFields = toWritable(['id', 'uuid'] as const);