2020-11-04 15:42:31 +00:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright 2020 Balena Ltd.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2020-12-11 11:48:44 +00:00
|
|
|
import type {
|
|
|
|
Application,
|
|
|
|
BalenaSDK,
|
|
|
|
Organization,
|
|
|
|
PineOptions,
|
2023-05-19 17:59:24 +00:00
|
|
|
PineTypedResult,
|
2020-12-11 11:48:44 +00:00
|
|
|
} from 'balena-sdk';
|
2020-11-04 15:42:31 +00:00
|
|
|
|
2023-05-19 17:59:24 +00:00
|
|
|
export async function getApplication(
|
|
|
|
sdk: BalenaSDK,
|
|
|
|
nameOrSlugOrId: string | number,
|
|
|
|
): Promise<Application>;
|
|
|
|
export async function getApplication<TP extends PineOptions<Application>>(
|
|
|
|
sdk: BalenaSDK,
|
|
|
|
nameOrSlugOrId: string | number,
|
|
|
|
options?: TP,
|
|
|
|
): Promise<PineTypedResult<Application, TP>>;
|
2020-11-04 15:42:31 +00:00
|
|
|
/**
|
2021-12-21 00:02:45 +00:00
|
|
|
* Get a fleet object, disambiguating the fleet identifier which may be a
|
2022-07-15 15:01:37 +00:00
|
|
|
* a fleet slug or name.
|
2021-12-21 00:02:45 +00:00
|
|
|
* TODO: add support for fleet UUIDs.
|
2020-11-04 15:42:31 +00:00
|
|
|
*/
|
|
|
|
export async function getApplication(
|
|
|
|
sdk: BalenaSDK,
|
2023-05-19 17:59:24 +00:00
|
|
|
nameOrSlugOrId: string | number,
|
2020-11-04 15:42:31 +00:00
|
|
|
options?: PineOptions<Application>,
|
|
|
|
): Promise<Application> {
|
2022-07-15 15:01:37 +00:00
|
|
|
const { looksLikeFleetSlug } = await import('./validation');
|
2023-08-22 14:28:53 +00:00
|
|
|
const whoamiResult = await sdk.auth.whoami();
|
|
|
|
const isDeviceActor = whoamiResult?.actorType === 'device';
|
|
|
|
|
|
|
|
if (isDeviceActor) {
|
|
|
|
const $filterByActor = {
|
|
|
|
$filter: {
|
|
|
|
owns__device: {
|
|
|
|
$any: {
|
|
|
|
$alias: 'd',
|
|
|
|
$expr: {
|
|
|
|
d: {
|
|
|
|
actor: whoamiResult.id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
options = options
|
|
|
|
? sdk.utils.mergePineOptions(options, $filterByActor)
|
|
|
|
: $filterByActor;
|
|
|
|
}
|
|
|
|
|
2023-05-19 17:59:24 +00:00
|
|
|
if (
|
|
|
|
typeof nameOrSlugOrId === 'string' &&
|
|
|
|
!looksLikeFleetSlug(nameOrSlugOrId)
|
|
|
|
) {
|
2022-07-15 15:01:37 +00:00
|
|
|
// Not a slug: must be an app name.
|
|
|
|
// TODO: revisit this logic when we add support for fleet UUIDs.
|
|
|
|
return await sdk.models.application.getAppByName(
|
2023-05-19 17:59:24 +00:00
|
|
|
nameOrSlugOrId,
|
2021-12-21 00:02:45 +00:00
|
|
|
options,
|
2023-08-22 14:28:53 +00:00
|
|
|
isDeviceActor ? undefined : 'directly_accessible',
|
2021-12-21 00:02:45 +00:00
|
|
|
);
|
|
|
|
}
|
2023-08-22 14:28:53 +00:00
|
|
|
|
|
|
|
const getFunction = isDeviceActor
|
|
|
|
? sdk.models.application.get
|
|
|
|
: sdk.models.application.getDirectlyAccessible;
|
|
|
|
|
|
|
|
return getFunction(nameOrSlugOrId, options);
|
2020-11-04 15:42:31 +00:00
|
|
|
}
|
2020-12-11 11:48:44 +00:00
|
|
|
|
2021-12-19 20:50:03 +00:00
|
|
|
/**
|
2022-07-15 15:01:37 +00:00
|
|
|
* Given a fleet name or slug, return its slug.
|
2021-12-19 20:50:03 +00:00
|
|
|
* This function conditionally makes an async SDK/API call to retrieve the
|
2021-12-21 00:02:45 +00:00
|
|
|
* application object, which can be wasteful if the application object is
|
2021-12-19 20:50:03 +00:00
|
|
|
* required before or after the call to this function. If this is the case,
|
|
|
|
* consider calling `getApplication()` and reusing the application object.
|
|
|
|
*/
|
|
|
|
export async function getFleetSlug(
|
|
|
|
sdk: BalenaSDK,
|
2022-07-15 15:01:37 +00:00
|
|
|
nameOrSlug: string,
|
2021-12-19 20:50:03 +00:00
|
|
|
): Promise<string> {
|
2021-12-21 00:02:45 +00:00
|
|
|
const { looksLikeFleetSlug } = await import('./validation');
|
2022-07-15 15:01:37 +00:00
|
|
|
if (!looksLikeFleetSlug(nameOrSlug)) {
|
|
|
|
// Not a slug: must be an app name.
|
|
|
|
// TODO: revisit this logic when we add support for fleet UUIDs.
|
2023-05-19 22:04:22 +00:00
|
|
|
return (await getApplication(sdk, nameOrSlug, { $select: 'slug' })).slug;
|
2021-12-19 20:50:03 +00:00
|
|
|
}
|
2022-07-15 15:01:37 +00:00
|
|
|
return nameOrSlug.toLowerCase();
|
2021-12-19 20:50:03 +00:00
|
|
|
}
|
|
|
|
|
2023-05-19 21:19:10 +00:00
|
|
|
export async function getOwnOrganizations(
|
|
|
|
sdk: BalenaSDK,
|
|
|
|
): Promise<Organization[]>;
|
|
|
|
export async function getOwnOrganizations<TP extends PineOptions<Organization>>(
|
|
|
|
sdk: BalenaSDK,
|
|
|
|
options: TP,
|
|
|
|
): Promise<Array<PineTypedResult<Organization, TP>>>;
|
2020-12-11 11:48:44 +00:00
|
|
|
/**
|
|
|
|
* Wraps the sdk organization.getAll method,
|
|
|
|
* restricting to those orgs user is a member of
|
|
|
|
*/
|
|
|
|
export async function getOwnOrganizations(
|
|
|
|
sdk: BalenaSDK,
|
2023-05-19 21:19:10 +00:00
|
|
|
options?: PineOptions<Organization>,
|
2020-12-11 11:48:44 +00:00
|
|
|
): Promise<Organization[]> {
|
2023-05-19 21:19:10 +00:00
|
|
|
return await sdk.models.organization.getAll(
|
|
|
|
sdk.utils.mergePineOptions(
|
|
|
|
{
|
|
|
|
$filter: {
|
|
|
|
organization_membership: {
|
|
|
|
$any: {
|
|
|
|
$alias: 'orm',
|
|
|
|
$expr: {
|
|
|
|
orm: {
|
2023-08-15 18:30:22 +00:00
|
|
|
user: (await sdk.auth.getUserInfo()).id,
|
2023-05-19 21:19:10 +00:00
|
|
|
},
|
|
|
|
},
|
2020-12-11 11:48:44 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-05-19 21:19:10 +00:00
|
|
|
$orderby: 'name asc',
|
2020-12-11 11:48:44 +00:00
|
|
|
},
|
2023-05-19 21:19:10 +00:00
|
|
|
options,
|
|
|
|
),
|
|
|
|
);
|
2020-12-11 11:48:44 +00:00
|
|
|
}
|