balena-cli 16.7.4 → 16.7.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,58 @@
1
+ import { ExpectedError } from '../errors';
2
+ import { getBalenaSdk } from './lazy';
3
+
4
+ export interface FlagsDef {
5
+ organization?: string;
6
+ type?: string; // application device type
7
+ help: void;
8
+ }
9
+
10
+ export interface ArgsDef {
11
+ name: string;
12
+ }
13
+
14
+ export async function applicationCreateBase(
15
+ resource: 'fleet' | 'app' | 'block',
16
+ options: FlagsDef,
17
+ params: ArgsDef,
18
+ ) {
19
+ // Ascertain device type
20
+ const deviceType =
21
+ options.type || (await (await import('./patterns')).selectDeviceType());
22
+
23
+ // Ascertain organization
24
+ const organization =
25
+ options.organization?.toLowerCase() ||
26
+ (await (await import('./patterns')).getAndSelectOrganization());
27
+
28
+ // Create application
29
+ try {
30
+ const application = await getBalenaSdk().models.application.create({
31
+ name: params.name,
32
+ deviceType,
33
+ organization,
34
+ });
35
+
36
+ // Output
37
+ const { capitalize } = await import('lodash');
38
+ console.log(
39
+ `${capitalize(resource)} created: slug "${
40
+ application.slug
41
+ }", device type "${deviceType}"`,
42
+ );
43
+ } catch (err) {
44
+ if ((err.message || '').toLowerCase().includes('unique')) {
45
+ // BalenaRequestError: Request error: "organization" and "app_name" must be unique.
46
+ throw new ExpectedError(
47
+ `Error: An app or block or fleet with the name "${params.name}" already exists in organization "${organization}".`,
48
+ );
49
+ } else if ((err.message || '').toLowerCase().includes('unauthorized')) {
50
+ // BalenaRequestError: Request error: Unauthorized
51
+ throw new ExpectedError(
52
+ `Error: You are not authorized to create ${resource}s in organization "${organization}".`,
53
+ );
54
+ }
55
+
56
+ throw err;
57
+ }
58
+ }
@@ -228,6 +228,24 @@ export async function selectOrganization(
228
228
  });
229
229
  }
230
230
 
231
+ export async function getAndSelectOrganization() {
232
+ const { getOwnOrganizations } = await import('./sdk');
233
+ const organizations = await getOwnOrganizations(getBalenaSdk(), {
234
+ $select: ['name', 'handle'],
235
+ });
236
+
237
+ if (organizations.length === 0) {
238
+ // User is not a member of any organizations (should not happen).
239
+ throw new Error('This account is not a member of any organizations');
240
+ } else if (organizations.length === 1) {
241
+ // User is a member of only one organization - use this.
242
+ return organizations[0].handle;
243
+ } else {
244
+ // User is a member of multiple organizations -
245
+ return selectOrganization(organizations);
246
+ }
247
+ }
248
+
231
249
  export async function awaitDeviceOsUpdate(
232
250
  uuid: string,
233
251
  targetOsVersion: string,
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "balena-cli",
3
- "version": "16.7.4",
3
+ "version": "16.7.5",
4
4
  "lockfileVersion": 2,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "balena-cli",
9
- "version": "16.7.4",
9
+ "version": "16.7.5",
10
10
  "hasInstallScript": true,
11
11
  "license": "Apache-2.0",
12
12
  "dependencies": {