From 4cfaf6e66619ae5145b8dfe7a8765b0bb8186271 Mon Sep 17 00:00:00 2001 From: Cameron Diver Date: Mon, 14 Jan 2019 11:58:10 +0000 Subject: [PATCH] Add device status endpoint api function Change-type: minor Signed-off-by: Cameron Diver --- lib/utils/device/api.ts | 42 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/lib/utils/device/api.ts b/lib/utils/device/api.ts index d3877913..e597e4d9 100644 --- a/lib/utils/device/api.ts +++ b/lib/utils/device/api.ts @@ -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 { + 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 { const url = this.getUrlForAction('logs');