Skip to content

Commit

Permalink
Add getLatestOsVersions in hostapp model
Browse files Browse the repository at this point in the history
Add getLatestOsVersions method in hostapp model

Change-type: minor
Signed-off-by: Andrea Rosci <andrear@balena.io>
  • Loading branch information
JSReds committed Mar 8, 2021
1 parent c9a6ad2 commit b6885eb
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
22 changes: 22 additions & 0 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ const sdk = fromSharedOptions();
* [.isArchitectureCompatibleWith(osArchitecture, applicationArchitecture)](#balena.models.os.isArchitectureCompatibleWith) ⇒ <code>Boolean</code>
* [.hostapp](#balena.models.hostapp) : <code>object</code>
* [.getAllOsVersions(deviceTypes)](#balena.models.hostapp.getAllOsVersions) ⇒ <code>Promise</code>
* [.getLatestOsVersions(deviceTypes)](#balena.models.hostapp.getLatestOsVersions) ⇒ <code>Promise</code>
* [.config](#balena.models.config) : <code>object</code>
* [.getAll()](#balena.models.config.getAll) ⇒ <code>Promise</code>
* ~~[.getDeviceTypes()](#balena.models.config.getDeviceTypes) ⇒ <code>Promise</code>~~
Expand Down Expand Up @@ -671,6 +672,7 @@ balena.models.device.get(123).catch(function (error) {
* [.isArchitectureCompatibleWith(osArchitecture, applicationArchitecture)](#balena.models.os.isArchitectureCompatibleWith) ⇒ <code>Boolean</code>
* [.hostapp](#balena.models.hostapp) : <code>object</code>
* [.getAllOsVersions(deviceTypes)](#balena.models.hostapp.getAllOsVersions) ⇒ <code>Promise</code>
* [.getLatestOsVersions(deviceTypes)](#balena.models.hostapp.getLatestOsVersions) ⇒ <code>Promise</code>
* [.config](#balena.models.config) : <code>object</code>
* [.getAll()](#balena.models.config.getAll) ⇒ <code>Promise</code>
* ~~[.getDeviceTypes()](#balena.models.config.getDeviceTypes) ⇒ <code>Promise</code>~~
Expand Down Expand Up @@ -6273,6 +6275,11 @@ console.log(result2);
**Beta** The hostapp methods are still in beta and might change in a non-major release.

**Kind**: static namespace of [<code>models</code>](#balena.models)

* [.hostapp](#balena.models.hostapp) : <code>object</code>
* [.getAllOsVersions(deviceTypes)](#balena.models.hostapp.getAllOsVersions) ⇒ <code>Promise</code>
* [.getLatestOsVersions(deviceTypes)](#balena.models.hostapp.getLatestOsVersions) ⇒ <code>Promise</code>

<a name="balena.models.hostapp.getAllOsVersions"></a>

##### hostapp.getAllOsVersions(deviceTypes) ⇒ <code>Promise</code>
Expand All @@ -6288,6 +6295,21 @@ console.log(result2);
```js
balena.models.hostapp.getAllOsVersions(['fincm3', 'raspberrypi3']);
```
<a name="balena.models.hostapp.getLatestOsVersions"></a>

##### hostapp.getLatestOsVersions(deviceTypes) ⇒ <code>Promise</code>
**Kind**: static method of [<code>hostapp</code>](#balena.models.hostapp)
**Summary**: Get latest OS versions for the passed device types
**Access**: public

| Param | Type | Description |
| --- | --- | --- |
| deviceTypes | <code>Array.&lt;String&gt;</code> | device type slugs |

**Example**
```js
balena.models.hostapp.getLatestOsVersions(['fincm3', 'raspberrypi3']);
```
<a name="balena.models.config"></a>

#### models.config : <code>object</code>
Expand Down
55 changes: 55 additions & 0 deletions lib/models/hostapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
} from '../types/models';
import { Dictionary } from '../../typings/utils';
import { getAuthDependentMemoize } from '../util/cache';
import { maxSatisfying } from 'semver';

const RELEASE_POLICY_TAG_NAME = 'release-policy';
const ESR_NEXT_TAG_NAME = 'esr-next';
Expand Down Expand Up @@ -161,6 +162,12 @@ const getHostappModel = function (deps: InjectedDependenciesParam) {
return res;
};

const mapVersions = (versions: OsVersion[], type: OsTypes) => {
return versions
.filter((version) => version.osType === type)
.map((v) => v.strippedVersion);
};

// This mutates the passed object.
const transformVersionSets = (
osVersionsByDeviceType: OsVersionsByDeviceType,
Expand Down Expand Up @@ -227,6 +234,14 @@ const getHostappModel = function (deps: InjectedDependenciesParam) {
is_invalidated: false,
},
},
should_be_running__release: {
$select: ['release_version', 'release_tag'],
$expand: {
release_tag: {
$select: ['tag_key', 'value'],
},
},
},
},
},
});
Expand Down Expand Up @@ -261,9 +276,49 @@ const getHostappModel = function (deps: InjectedDependenciesParam) {
return memoizedGetTransformedOsVersions(sortedDeviceTypes);
};

/**
* @summary Get latest OS versions for the passed device types
* @name getLatestOsVersions
* @public
* @function
* @memberof balena.models.hostapp
*
* @param {String[]} deviceTypes - device type slugs
* @returns {Promise}
*
* @example
* balena.models.hostapp.getLatestOsVersions(['fincm3', 'raspberrypi3']);
*/
const getLatestOsVersions = async (
deviceTypes: string[],
): Promise<OsVersionsByDeviceType> => {
const allOsVersions = await getAllOsVersions(deviceTypes);
return Object.entries(allOsVersions).reduce(
(osVersionsByDeviceType: OsVersionsByDeviceType, [key, versions]) => {
const latestOSVersion = maxSatisfying(
mapVersions(versions, OsTypes.DEFAULT),
'>0.0.0',
);
const latestESRVersion = maxSatisfying(
mapVersions(versions, OsTypes.ESR),
'>0.0.0',
);
const filteredVersions = versions.filter(
(v) =>
v.strippedVersion === latestOSVersion ||
v.strippedVersion === latestESRVersion,
)!;
osVersionsByDeviceType[key] = filteredVersions;
return osVersionsByDeviceType;
},
{},
);
};

return {
OsTypes,
getAllOsVersions,
getLatestOsVersions,
};
};

Expand Down
18 changes: 18 additions & 0 deletions tests/integration/models/hostapp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,22 @@ describe('Hostapp model', function () {
expect(firstRes).to.equal(secondRes);
});
});

parallel('balena.models.hostapp.getAllOsVersions()', function () {
it('should contain latest balenaOS and balenaOS ESR OS types', async () => {
const res = await balena.models.hostapp.getLatestOsVersions([
'fincm3',
'raspberrypi3',
]);

['fincm3', 'raspberrypi3'].forEach((key) => {
expect(res[key]).to.be.an('Array');
expect(res[key]).to.have.lengthOf(4);
expect(res[key].filter((r) => r.osType === 'esr')).to.have.lengthOf(2);
expect(res[key].filter((r) => r.osType === 'default')).to.have.lengthOf(
2,
);
});
});
});
});

0 comments on commit b6885eb

Please sign in to comment.