Skip to content

Commit

Permalink
application.get: Add support for retrieving applications by uuid
Browse files Browse the repository at this point in the history
Resolves: #1016
Change-type: minor
Signed-off-by: Thodoris Greasidis <thodoris@balena.io>
  • Loading branch information
thgreasi committed Dec 30, 2021
1 parent 4acbde6 commit 3374abe
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
41 changes: 20 additions & 21 deletions lib/models/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ const getApplicationModel = function (
* @function
* @memberof balena.models.application
*
* @param {String|Number} slugOrId - application slug (string) or id (number)
* @param {String|Number} slugOrUuidOrId - application slug (string), uuid (string) or id (number)
* @param {Object} [options={}] - extra pine options to use
* @param {String} [context] - extra access filters, undefined or 'directly_accessible'
* @fulfil {Object} - application
Expand All @@ -283,6 +283,11 @@ const getApplicationModel = function (
* });
*
* @example
* balena.models.application.get('1bf99a68cf9e4266986e6dec7a6e8f46').then(function(application) {
* console.log(application);
* });
*
* @example
* balena.models.application.get(123).then(function(application) {
* console.log(application);
* });
Expand All @@ -294,58 +299,52 @@ const getApplicationModel = function (
* });
*/
async get(
slugOrId: string | number,
slugOrUuidOrId: string | number,
options?: PineOptions<Application>,
context?: 'directly_accessible',
): Promise<Application> {
if (options == null) {
options = {};
}

if (slugOrId == null) {
throw new errors.BalenaApplicationNotFound(slugOrId);
}
options ??= {};

const accessFilter =
context === 'directly_accessible'
? isDirectlyAccessibleByUserFilter
: null;

let application;
if (isId(slugOrId)) {
if (isId(slugOrUuidOrId)) {
application = await pine.get({
resource: 'application',
id: slugOrId,
id: slugOrUuidOrId,
options: mergePineOptions(
accessFilter != null ? { $filter: accessFilter } : {},
options,
),
});
if (application == null) {
throw new errors.BalenaApplicationNotFound(slugOrId);
}
} else {
} else if (typeof slugOrUuidOrId === 'string') {
const lowerCaseSlugOrUuid = slugOrUuidOrId.toLowerCase();
const applications = await pine.get({
resource: 'application',
options: mergePineOptions(
{
$filter: {
...accessFilter,
slug: slugOrId.toLowerCase(),
$or: {
slug: lowerCaseSlugOrUuid,
uuid: lowerCaseSlugOrUuid,
},
},
},
options,
),
});
if (applications.length === 0) {
throw new errors.BalenaApplicationNotFound(slugOrId);
}

if (applications.length > 1) {
throw new errors.BalenaAmbiguousApplication(slugOrId);
throw new errors.BalenaAmbiguousApplication(slugOrUuidOrId);
}
application = applications[0];
}
if (application == null) {
throw new errors.BalenaApplicationNotFound(slugOrUuidOrId);
}
return normalizeApplication(application);
},

Expand Down
6 changes: 4 additions & 2 deletions tests/integration/models/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1764,7 +1764,7 @@ describe('Application Model', function () {
before(async function () {
const [app] = await balena.models.application.getAll({
$top: 1,
$select: ['id', 'app_name', 'slug', 'is_public'],
$select: ['id', 'app_name', 'slug', 'uuid', 'is_public'],
$filter: { is_public: true },
});
expect(app).to.have.property('is_public', true);
Expand Down Expand Up @@ -1901,7 +1901,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) {
Expand All @@ -1914,6 +1914,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');
expect(app).to.have.property('is_public', true);
});
});
Expand All @@ -1931,6 +1932,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');
expect(app).to.have.property('is_public', true);
});
},
Expand Down
6 changes: 5 additions & 1 deletion tests/integration/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -623,5 +623,9 @@ export function givenASupervisorRelease(
});
}

export const applicationRetrievalFields = toWritable(['id', 'slug'] as const);
export const applicationRetrievalFields = toWritable([
'id',
'slug',
'uuid',
] as const);
export const deviceUniqueFields = toWritable(['id', 'uuid'] as const);

0 comments on commit 3374abe

Please sign in to comment.