Add device status endpoint api function

Change-type: minor
Signed-off-by: Cameron Diver <cameron@balena.io>
This commit is contained in:
Cameron Diver 2019-01-14 11:58:10 +00:00
parent bc563ea963
commit 4cfaf6e666
No known key found for this signature in database
GPG Key ID: 49690ED87032539F

View File

@ -15,6 +15,7 @@
* limitations under the License.
*/
import * as Bluebird from 'bluebird';
import * as _ from 'lodash';
import * as request from 'request';
import * as Stream from 'stream';
@ -33,6 +34,29 @@ export interface DeviceInfo {
arch: string;
}
export interface Status {
appState: 'applied' | 'applying';
overallDownloadProgress: null | number;
containers: Array<{
status: string;
serviceName: string;
appId: number;
imageId: number;
serviceId: number;
containerId: string;
createdAt: string;
}>;
images: Array<{
name: string;
appId: number;
serviceName: string;
imageId: number;
dockerImageId: string;
status: string;
downloadProgress: null | number;
}>;
}
const deviceEndpoints = {
setTargetState: 'v2/local/target-state',
getTargetState: 'v2/local/target-state',
@ -40,6 +64,7 @@ const deviceEndpoints = {
logs: 'v2/local/logs',
ping: 'ping',
version: 'v2/version',
status: 'v2/state/status',
};
export class DeviceAPI {
@ -126,6 +151,23 @@ export class DeviceAPI {
});
}
public getStatus(): Promise<Status> {
const url = this.getUrlForAction('status');
return DeviceAPI.promisifiedRequest(request.get, {
url,
json: true,
}).then(body => {
if (body.status !== 'success') {
throw new ApiErrors.DeviceAPIError(
'Non-successful response from supervisor status endpoint',
);
}
return _.omit(body, 'status') as Status;
});
}
public getLogStream(): Bluebird<Stream.Readable> {
const url = this.getUrlForAction('logs');