Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add application settings #1097

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ const sdk = fromSharedOptions();
* [.disableDeviceUrls(nameOrSlugOrId)](#balena.models.application.disableDeviceUrls) ⇒ <code>Promise</code>
* [.grantSupportAccess(nameOrSlugOrId, expiryTimestamp)](#balena.models.application.grantSupportAccess) ⇒ <code>Promise</code>
* [.revokeSupportAccess(nameOrSlugOrId)](#balena.models.application.revokeSupportAccess) ⇒ <code>Promise</code>
* [.setRepositoryUrl(nameOrSlugOrId, repositoryUrl)](#balena.models.application.setRepositoryUrl) ⇒ <code>Promise</code>
* [.setIsPublic(nameOrSlugOrId, isPublic)](#balena.models.application.setIsPublic) ⇒ <code>Promise</code>
* [.setClass(nameOrSlugOrId, applicationClass)](#balena.models.application.setClass) ⇒ <code>Promise</code>
* [.device](#balena.models.device) : <code>object</code>
* [.tags](#balena.models.device.tags) : <code>object</code>
* [.getAllByApplication(nameOrSlugOrId, [options])](#balena.models.device.tags.getAllByApplication) ⇒ <code>Promise</code>
Expand Down Expand Up @@ -535,6 +538,9 @@ balena.models.device.get(123).catch(function (error) {
* [.disableDeviceUrls(nameOrSlugOrId)](#balena.models.application.disableDeviceUrls) ⇒ <code>Promise</code>
* [.grantSupportAccess(nameOrSlugOrId, expiryTimestamp)](#balena.models.application.grantSupportAccess) ⇒ <code>Promise</code>
* [.revokeSupportAccess(nameOrSlugOrId)](#balena.models.application.revokeSupportAccess) ⇒ <code>Promise</code>
* [.setRepositoryUrl(nameOrSlugOrId, repositoryUrl)](#balena.models.application.setRepositoryUrl) ⇒ <code>Promise</code>
* [.setIsPublic(nameOrSlugOrId, isPublic)](#balena.models.application.setIsPublic) ⇒ <code>Promise</code>
* [.setClass(nameOrSlugOrId, applicationClass)](#balena.models.application.setClass) ⇒ <code>Promise</code>
* [.device](#balena.models.device) : <code>object</code>
* [.tags](#balena.models.device.tags) : <code>object</code>
* [.getAllByApplication(nameOrSlugOrId, [options])](#balena.models.device.tags.getAllByApplication) ⇒ <code>Promise</code>
Expand Down Expand Up @@ -777,6 +783,9 @@ balena.models.device.get(123).catch(function (error) {
* [.disableDeviceUrls(nameOrSlugOrId)](#balena.models.application.disableDeviceUrls) ⇒ <code>Promise</code>
* [.grantSupportAccess(nameOrSlugOrId, expiryTimestamp)](#balena.models.application.grantSupportAccess) ⇒ <code>Promise</code>
* [.revokeSupportAccess(nameOrSlugOrId)](#balena.models.application.revokeSupportAccess) ⇒ <code>Promise</code>
* [.setRepositoryUrl(nameOrSlugOrId, repositoryUrl)](#balena.models.application.setRepositoryUrl) ⇒ <code>Promise</code>
* [.setIsPublic(nameOrSlugOrId, isPublic)](#balena.models.application.setIsPublic) ⇒ <code>Promise</code>
* [.setClass(nameOrSlugOrId, applicationClass)](#balena.models.application.setClass) ⇒ <code>Promise</code>

<a name="balena.models.application.tags"></a>

Expand Down Expand Up @@ -2401,6 +2410,84 @@ balena.models.application.revokeSupportAccess('MyApp', function(error) {
if (error) throw error;
});
```
<a name="balena.models.application.setRepositoryUrl"></a>

##### application.setRepositoryUrl(nameOrSlugOrId, repositoryUrl) ⇒ <code>Promise</code>
**Kind**: static method of [<code>application</code>](#balena.models.application)
**Summary**: Set application repository URL
**Access**: public

| Param | Type | Description |
| --- | --- | --- |
| nameOrSlugOrId | <code>String</code> \| <code>Number</code> | application name (string) (deprecated), slug (string) or id (number) |
| repositoryUrl | <code>String</code> | The application repository url |

**Example**
```js
balena.models.application.setRepositoryUrl('MyApp', 'https://github.com/balenalabs/balena-sound');
```
**Example**
```js
balena.models.application.setRepositoryUrl(123, 'https://github.com/balenalabs/balena-sound');
```
**Example**
```js
balena.models.application.setRepositoryUrl('MyApp', 'https://github.com/balenalabs/balena-sound', function(error) {
if (error) throw error;
});
```
<a name="balena.models.application.setIsPublic"></a>

##### application.setIsPublic(nameOrSlugOrId, isPublic) ⇒ <code>Promise</code>
**Kind**: static method of [<code>application</code>](#balena.models.application)
**Summary**: Set an application as public or not public
**Access**: public

| Param | Type | Description |
| --- | --- | --- |
| nameOrSlugOrId | <code>String</code> \| <code>Number</code> | application name (string) (deprecated), slug (string) or id (number) |
| isPublic | <code>Boolean</code> | If true set the application as public |

**Example**
```js
balena.models.application.setIsPublic('MyApp', true);
```
**Example**
```js
balena.models.application.setIsPublic(123, true);
```
**Example**
```js
balena.models.application.setIsPublic('MyApp', true, function(error) {
if (error) throw error;
});
```
<a name="balena.models.application.setClass"></a>

##### application.setClass(nameOrSlugOrId, applicationClass) ⇒ <code>Promise</code>
**Kind**: static method of [<code>application</code>](#balena.models.application)
**Summary**: Set an application class
**Access**: public

| Param | Type | Description |
| --- | --- | --- |
| nameOrSlugOrId | <code>String</code> \| <code>Number</code> | application name (string) (deprecated), slug (string) or id (number) |
| applicationClass | <code>String</code> | Possible values are |

**Example**
```js
balena.models.application.setIsPublic('MyApp', true);
```
**Example**
```js
balena.models.application.setIsPublic(123, true);
```
**Example**
```js
balena.models.application.setIsPublic('MyApp', true, function(error) {
if (error) throw error;
});
```
<a name="balena.models.device"></a>

#### models.device : <code>object</code>
Expand Down
142 changes: 142 additions & 0 deletions lib/models/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {
getCurrentServiceDetailsPineExpand,
generateCurrentServiceDetails,
} from '../util/device-service-details';
import { ApplicationClassType } from '../types/models';

const getApplicationModel = function (
deps: InjectedDependenciesParam,
Expand Down Expand Up @@ -1501,6 +1502,147 @@ const getApplicationModel = function (
}
},

/**
* @summary Set application repository URL
* @name setRepositoryUrl
* @public
* @function
* @memberof balena.models.application
*
* @param {String|Number} nameOrSlugOrId - application name (string) (deprecated), slug (string) or id (number)
* @param {String} repositoryUrl - The application repository url
* @returns {Promise}
*
* @example
* balena.models.application.setRepositoryUrl('MyApp', 'https://github.com/balenalabs/balena-sound');
*
* @example
* balena.models.application.setRepositoryUrl(123, 'https://github.com/balenalabs/balena-sound');
*
* @example
* balena.models.application.setRepositoryUrl('MyApp', 'https://github.com/balenalabs/balena-sound', function(error) {
* if (error) throw error;
* });
*/
setRepositoryUrl: async (
nameOrSlugOrId: string | number,
repositoryUrl: string,
): Promise<void> => {
if (!repositoryUrl) {
throw new errors.BalenaInvalidParameterError(
'repositoryUrl',
repositoryUrl,
);
}

try {
const applicationId = await getId(nameOrSlugOrId);
await pine.patch({
resource: 'application',
id: applicationId,
body: { is_stored_at__repository_url: repositoryUrl },
});
} catch (err) {
if (isNotFoundResponse(err)) {
treatAsMissingApplication(nameOrSlugOrId, err);
}
throw err;
}
},

/**
* @summary Set an application as public or not public
* @name setIsPublic
* @public
* @function
* @memberof balena.models.application
*
* @param {String|Number} nameOrSlugOrId - application name (string) (deprecated), slug (string) or id (number)
* @param {Boolean} isPublic - If true set the application as public
* @returns {Promise}
*
* @example
* balena.models.application.setIsPublic('MyApp', true);
*
* @example
* balena.models.application.setIsPublic(123, true);
*
* @example
* balena.models.application.setIsPublic('MyApp', true, function(error) {
* if (error) throw error;
* });
*/
setIsPublic: async (
nameOrSlugOrId: string | number,
isPublic: boolean,
): Promise<void> => {
if (isPublic == null) {
throw new errors.BalenaInvalidParameterError('isPublic', isPublic);
}

try {
const applicationId = await getId(nameOrSlugOrId);
await pine.patch({
resource: 'application',
id: applicationId,
body: { is_public: isPublic },
});
} catch (err) {
if (isNotFoundResponse(err)) {
treatAsMissingApplication(nameOrSlugOrId, err);
}
throw err;
}
},

/**
* @summary Set an application class
* @name setClass
* @public
* @function
* @memberof balena.models.application
*
* @param {String|Number} nameOrSlugOrId - application name (string) (deprecated), slug (string) or id (number)
* @param {String} applicationClass - Possible values are
* @returns {Promise}
*
* @example
* balena.models.application.setIsPublic('MyApp', true);
*
* @example
* balena.models.application.setIsPublic(123, true);
*
* @example
* balena.models.application.setIsPublic('MyApp', true, function(error) {
* if (error) throw error;
* });
Comment on lines +1610 to +1618
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* balena.models.application.setIsPublic('MyApp', true);
*
* @example
* balena.models.application.setIsPublic(123, true);
*
* @example
* balena.models.application.setIsPublic('MyApp', true, function(error) {
* if (error) throw error;
* });
* balena.models.application.setClass('MyApp', 'fleet');
*
* @example
* balena.models.application.setClass(123, 'fleet');
*
* @example
* balena.models.application.setClass('MyApp', 'fleet', function(error) {
* if (error) throw error;
* });

*/
setClass: async (
nameOrSlugOrId: string | number,
applicationClass: ApplicationClassType,
): Promise<void> => {
if (applicationClass == null) {
throw new errors.BalenaInvalidParameterError(
'applicationClass',
applicationClass,
);
}

try {
const applicationId = await getId(nameOrSlugOrId);
await pine.patch({
resource: 'application',
id: applicationId,
body: { is_of__class: applicationClass },
});
} catch (err) {
if (isNotFoundResponse(err)) {
treatAsMissingApplication(nameOrSlugOrId, err);
}
throw err;
}
},

/**
* @namespace balena.models.application.tags
* @memberof balena.models.application
Expand Down
4 changes: 3 additions & 1 deletion lib/types/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ export interface ApiKey {
is_of__actor: PineDeferred;
}

export type ApplicationClassType = 'fleet' | 'block' | 'app';

export interface Application {
id: number;
created_at: string;
Expand All @@ -167,7 +169,7 @@ export interface Application {
is_host: boolean;
should_track_latest_release: boolean;
is_public: boolean;
is_of__class: 'fleet' | 'block' | 'app';
is_of__class: ApplicationClassType;
is_archived: boolean;
is_discoverable: boolean;
is_stored_at__repository_url: string | null;
Expand Down
65 changes: 65 additions & 0 deletions tests/integration/models/application.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,71 @@ describe('Application Model', function () {
});
});

describe('balena.models.application.setRepositoryUrl()', () => {
it('...should set a repository url', async function () {
const balenaSoundRepoUrl =
'https://github.com/balenalabs/balena-sound';
const { is_stored_at__repository_url: originalRepositoryUrl } =
await balena.models.application.get(this.application.id, {
$select: 'is_stored_at__repository_url',
});
expect(originalRepositoryUrl).to.equal(null);

await balena.models.application.setRepositoryUrl(
this.application.id,
balenaSoundRepoUrl,
);
const { is_stored_at__repository_url: repositoryUrl } =
await balena.models.application.get(this.application.id, {
$select: 'is_stored_at__repository_url',
});
expect(repositoryUrl).to.equal(balenaSoundRepoUrl);
});
});

describe('balena.models.application.setIsPublic()', () => {
it('...should set the application as public', async function () {
const { is_public: originalIsPublic } =
await balena.models.application.get(this.application.id, {
$select: 'is_public',
});
expect(originalIsPublic).to.not.be.true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expect(originalIsPublic).to.not.be.true;
expect(originalIsPublic).to.be.false;

?


await balena.models.application.setIsPublic(
this.application.id,
true,
);
const { is_public: isPublic } = await balena.models.application.get(
this.application.id,
{
$select: 'is_public',
},
);
expect(isPublic).to.be.true;
});
});

describe('balena.models.application.setClass()', () => {
it('...should set the application class', async function () {
const classParam = 'block';
const { is_of__class: originalClass } =
await balena.models.application.get(this.application.id, {
$select: 'is_of__class',
});
expect(originalClass).to.not.equal(null);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about?

Suggested change
expect(originalClass).to.not.equal(null);
expect(originalClass).to.equal('fleet');


await balena.models.application.setClass(
this.application.id,
classParam,
);
const { is_of__class: applicationClass } =
await balena.models.application.get(this.application.id, {
$select: 'is_of__class',
});
expect(applicationClass).to.equal(classParam);
});
});

describe('balena.models.application.tags', function () {
const tagTestOptions = {
// prettier-ignore
Expand Down