Improve id disambiguation for tag commands

Change-type: patch
Signed-off-by: Scott Lowe <scott@balena.io>
This commit is contained in:
Scott Lowe 2020-12-18 12:06:55 +01:00
parent 194d12cb3d
commit 584aa745f7
4 changed files with 46 additions and 4 deletions

View File

@ -102,8 +102,9 @@ export default class TagRmCmd extends Command {
const { tryAsInteger } = await import('../../utils/validation');
if (options.application) {
const { getTypedApplicationIdentifier } = await import('../../utils/sdk');
return balena.models.application.tags.remove(
tryAsInteger(options.application),
await getTypedApplicationIdentifier(balena, options.application),
params.tagKey,
);
}

View File

@ -117,8 +117,9 @@ export default class TagSetCmd extends Command {
const { tryAsInteger } = await import('../../utils/validation');
if (options.application) {
const { getTypedApplicationIdentifier } = await import('../../utils/sdk');
return balena.models.application.tags.set(
tryAsInteger(options.application),
await getTypedApplicationIdentifier(balena, options.application),
params.tagKey,
params.value,
);

View File

@ -91,8 +91,9 @@ export default class TagsCmd extends Command {
let tags;
if (options.application) {
const { getTypedApplicationIdentifier } = await import('../utils/sdk');
tags = await balena.models.application.tags.getAllByApplication(
tryAsInteger(options.application),
await getTypedApplicationIdentifier(balena, options.application),
);
}
if (options.device) {

View File

@ -26,7 +26,6 @@ import type {
* Wraps the sdk application.get method,
* adding disambiguation in cases where the provided
* identifier could be interpreted in multiple valid ways.
* // TODO: Remove this once support for numeric App IDs is removed.
*/
export async function getApplication(
sdk: BalenaSDK,
@ -50,6 +49,46 @@ export async function getApplication(
return sdk.models.application.get(nameOrSlugOrId, options);
}
/**
* Given an string representation of an application identifier,
* which could be one of:
* - name (including numeric names)
* - slug
* - numerical id
* disambiguate and return a properly typed identifier.
*
* Attempts to minimise the number of API calls required.
* TODO: Remove this once support for numeric App IDs is removed.
*/
export async function getTypedApplicationIdentifier(
sdk: BalenaSDK,
nameOrSlugOrId: string,
) {
const { looksLikeInteger } = await import('./validation');
// If there's no possible ambiguity,
// return the passed identifier unchanged
if (!looksLikeInteger(nameOrSlugOrId)) {
return nameOrSlugOrId;
}
// Resolve ambiguity
try {
// Test for existence of app with this numerical ID,
// and return typed id if found
return (await sdk.models.application.get(Number(nameOrSlugOrId))).id;
} catch (e) {
const { instanceOf } = await import('../errors');
const { BalenaApplicationNotFound } = await import('balena-errors');
if (!instanceOf(e, BalenaApplicationNotFound)) {
throw e;
}
}
// App with this numerical id not found
// return the passed identifier unchanged
return nameOrSlugOrId;
}
/**
* Wraps the sdk organization.getAll method,
* restricting to those orgs user is a member of