forked from electricimp/imp-central-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeviceGroups.js
424 lines (391 loc) · 18.8 KB
/
DeviceGroups.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// MIT License
//
// Copyright 2017 Electric Imp
//
// SPDX-License-Identifier: MIT
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
'use strict';
const Products = require('./Products');
const Entities = require('./util/Entities');
const ParamsChecker = require('./util/ParamsChecker');
const HttpHelper = require('./util/HttpHelper');
const Util = require('util');
const Errors = require('./Errors');
// This class provides access to Device Groups impCentral API methods.
class DeviceGroups extends Entities {
constructor() {
super();
this._validFilters = {
[DeviceGroups.FILTER_OWNER_ID] : false,
[DeviceGroups.FILTER_PRODUCT_ID] : false,
[DeviceGroups.FILTER_TYPE] : false
};
this._validCreateAttributes = {
name : true,
description : false
};
this._validUpdateAttributes = {
name : true,
description : false,
load_code_after_blessing : false
};
}
// Device Groups types
static get TYPE_DEVELOPMENT() {
return 'development_devicegroup';
}
static get TYPE_PRE_PRODUCTION() {
return 'pre_production_devicegroup';
}
static get TYPE_PRODUCTION() {
return 'production_devicegroup';
}
static get TYPE_PRE_FACTORY_FIXTURE() {
return 'pre_factoryfixture_devicegroup';
}
static get TYPE_FACTORY_FIXTURE() {
return 'factoryfixture_devicegroup';
}
static get _validTypes() {
return [
DeviceGroups.TYPE_DEVELOPMENT,
DeviceGroups.TYPE_PRE_PRODUCTION,
DeviceGroups.TYPE_PRODUCTION,
DeviceGroups.TYPE_PRE_FACTORY_FIXTURE,
DeviceGroups.TYPE_FACTORY_FIXTURE
];
}
static validateType(param, paramName) {
return ParamsChecker.validateEnum(param, DeviceGroups._validTypes, paramName);
}
// Retrieves a list of Device Groups associated with the logged-in account.
//
// Parameters:
// filters : Object Optional Key/Value filters that will be applied to the result list
// The valid keys are:
// 'filter[owner.id]' - filter by the Device Group owner
// 'filter[product.id]' - filter by the Product that holds the Device Group
// 'filter[type]' - filter by the Device Group type
// pageNumber : Number Optional pagination page number (starts at 1).
// If not specified, the default value is 1.
// pageSize : Number Optional pagination size - maximum number of items to return.
// If not specified, the default value is 20.
//
// Returns: Promise that resolves when the Device Groups list is successfully
// obtained, or rejects with an error
list(filters = null, pageNumber = null, pageSize = null) {
if (filters && DeviceGroups.FILTER_TYPE in filters) {
const error = DeviceGroups.validateType(filters[DeviceGroups.FILTER_TYPE], DeviceGroups.FILTER_TYPE);
if (error) {
return Promise.reject(error);
}
}
return super.list(filters, pageNumber, pageSize);
}
// Possible filter keys for the list() filters:
static get FILTER_OWNER_ID() {
return 'filter[owner.id]';
}
static get FILTER_PRODUCT_ID() {
return 'filter[product.id]';
}
static get FILTER_TYPE() {
return 'filter[type]';
}
// Creates a Device Group of specified type and associates it with an existing Product
// specified by productId.
//
// Parameters:
// productId : String The Product's ID
// type : String The Device Group type.
// One of DeviceGroups.TYPE_DEVELOPMENT,
// DeviceGroups.TYPE_PRE_PRODUCTION, DeviceGroups.TYPE_PRODUCTION,
// DeviceGroups.TYPE_PRE_FACTORY_FIXTURE, DeviceGroups.TYPE_FACTORY_FIXTURE.
// attributes : Object Key/Value attributes of the Device Group to be created.
// The valid keys are:
// 'name' (String, required) - the Device Group's name,
// this must be unique for all DeviceGroups in this Product
// 'description' (String, optional) - an optional free-form
// description of the Device Group
// productionTarget : Optional production_target relationship of the Device Group to be
// Object created. Must be specified for pre_factoryfixture and factoryfixture device groups.
// The valid keys are:
// 'type' (String, required) - the target Device Group's type.
// One of DeviceGroups.TYPE_PRE_PRODUCTION,
// DeviceGroups.TYPE_PRODUCTION.
// 'id' (String, required) - the target Device Group's ID.
//
// Returns: Promise that resolves when the Device Group is successfully created,
// or rejects with an error
create(productId, type, attributes, productionTarget = null) {
const error = ParamsChecker.validateNonEmpty(productId) ||
DeviceGroups.validateType(type, 'type') ||
this._validateProductionTarget(type, productionTarget, true);
if (error) {
return Promise.reject(error);
}
const relationships = {
[Products._TYPE] : {
type : Products._TYPE,
id : productId
}
};
if (productionTarget) {
relationships['production_target'] = productionTarget;
}
const body = {
data : {
type : type,
attributes : attributes,
relationships : relationships
}
};
return super.create(attributes, body);
}
// Retrieves a specific Device Group.
//
// Parameters:
// deviceGroupId : String ID of the Device Group to be retrieved
//
// Returns: Promise that resolves when the Device Group is successfully obtained,
// or rejects with an error
get(deviceGroupId) {
return super.get(deviceGroupId);
}
// Updates a specific Device Group.
//
// Parameters:
// deviceGroupId : String ID of the Device Group to be updated
// type : String The Device Group type.
// One of DeviceGroups.TYPE_DEVELOPMENT,
// DeviceGroups.TYPE_PRE_PRODUCTION, DeviceGroups.TYPE_PRODUCTION,
// DeviceGroups.TYPE_PRE_FACTORY_FIXTURE, DeviceGroups.TYPE_FACTORY_FIXTURE.
// attributes : Object Key/Value attributes of the Device Group that will be updated.
// The valid keys are:
// 'name' (String, optional) - the Device Group's name,
// this must be unique for all Device Groups in this Product
// 'description' (String, optional) - an optional free-form
// description of the Device Group
// 'load_code_after_blessing' (boolean, optional) -
// indicates whether production code will be loaded by the
// device while in your factory.
// If true, code is immediately loaded by the device after
// blessing, and will run when the device is powered on.
// If false, code will be loaded the next time the device
// connects as part of BlinkUp, whether successful or not.
// Valid for production and pre_production Device Groups only.
// Default value is true.
// productionTarget : Optional production_target relationship of the Device Group to be
// Object updated. Can be specified for pre_factoryfixture and factoryfixture
// device groups only.
// The valid keys are:
// 'type' (String, required) - the target Device Group's type.
// One of DeviceGroups.TYPE_PRE_PRODUCTION,
// DeviceGroups.TYPE_PRODUCTION.
// 'id' (String, required) - the target Device Group's ID.
//
// Returns: Promise that resolves when the Device Group is successfully updated,
// or rejects with an error
update(deviceGroupId, type, attributes, productionTarget = null) {
let error = DeviceGroups.validateType(type, 'type') ||
this._validateProductionTarget(type, productionTarget, false);
if (!error && 'load_code_after_blessing' in attributes &&
!(type == DeviceGroups.TYPE_PRE_PRODUCTION || type == DeviceGroups.TYPE_PRODUCTION)) {
error = new Errors.InvalidDataError(
Util.format(
'load_code_after_blessing can be specified for "%s" and "%s" device groups only',
DeviceGroups.TYPE_PRE_PRODUCTION,
DeviceGroups.TYPE_PRODUCTION));
}
if (error) {
return Promise.reject(error);
}
const data = {
type : type,
id : deviceGroupId,
attributes : attributes
};
if (productionTarget) {
data['relationships'] = {
production_target : productionTarget
};
}
return super.update(deviceGroupId, attributes, { data: data });
}
// Deletes a specific Device Group.
//
// Parameters:
// deviceGroupId : String ID of the Device Group to be deleted
//
// Returns: Promise that resolves when the Device Group is successfully deleted,
// or rejects with an error
delete(deviceGroupId) {
return super.delete(deviceGroupId);
}
// Restarts all the devices in a Device Group immediately.
//
// Parameters:
// deviceGroupId : String ID of the Device Group
//
// Returns: Promise that resolves when the Device Group's devices were restarted,
// or rejects with an error
restartDevices(deviceGroupId) {
const error = ParamsChecker.validateNonEmpty(deviceGroupId);
if (error) {
return Promise.reject(error);
}
return HttpHelper.post(this._getPath(deviceGroupId) + '/restart');
}
// Conditionally restarts all the devices in a Device Group.
// Sends a SHUTDOWN_NEWSQUIRREL message to a all devices in the Device Group running a Deployment
// newer-than or equal-to the Device Group's min_supported_deployment,
// or restarts all devices running an older Deployment.
//
// Parameters:
// deviceGroupId : String ID of the Device Group
//
// Returns: Promise that resolves when the Device Group's devices were restarted,
// or rejects with an error
conditionalRestartDevices(deviceGroupId) {
const error = ParamsChecker.validateNonEmpty(deviceGroupId);
if (error) {
return Promise.reject(error);
}
return HttpHelper.post(this._getPath(deviceGroupId) + '/conditional_restart');
}
// Adds one or more devices to the specified Device Group.
// It is not an atomic operation — it is possible for some Devices to be added and other Devices
// to fail to be added.
//
// Parameters:
// deviceGroupId : String ID of the Device Group
// deviceIds : String One or more Devices identifiers to be added to the Device Group.
// Device identifier can be a MAC address, an Agent ID, or the device ID.
//
// Returns: Promise that resolves when all of the devices were successfully added
// to the Device Group or rejects with an error when one or more devices
// could not be assigned
addDevices(deviceGroupId, ...deviceIds) {
const error = ParamsChecker.validateNonEmpty(deviceGroupId);
if (error) {
return Promise.reject(error);
}
return HttpHelper.post(this._getPath(deviceGroupId) + '/relationships/devices', { data : this._convertDeviceIds(deviceIds) });
}
// Removes one or more devices from the specified Device Group.
// It is not an atomic operation — it's possible for some Devices to be unassigned and other Devices
// to fail to be unassigned.
//
// Parameters:
// deviceGroupId : String ID of the Device Group
// unbondKey : String Optional unbond key that must be specified
// when removing devices from a production device group. Removing devices
// from a production group is a rate-limited operation.
// deviceIds : String One or more Devices identifiers to be removed from the Device Group.
// Device identifier can be a MAC address, an Agent ID, or the device ID.
//
// Returns: Promise that resolves when all of the devices were successfully removed
// from the Device Group or rejects with an error when one or more devices
// could not be unassigned
removeDevices(deviceGroupId, unbondKey = null, ...deviceIds) {
let error = ParamsChecker.validateNonEmpty(deviceGroupId);
if (error) {
return Promise.reject(error);
}
let additionalHeaders = null;
if (unbondKey) {
additionalHeaders = {
'X-Electricimp-Key' : unbondKey
};
}
return HttpHelper.delete(
this._getPath(deviceGroupId) + '/relationships/devices',
{ data : this._convertDeviceIds(deviceIds) },
additionalHeaders);
}
// Updates the min_supported_deployment relationship for the specified Devicegroup.
//
// Parameters:
// deviceGroupId : String ID of the Device Group
// deploymentId : String ID of the Deployment that will be set as min_supported_deployment
// for the Device Group. Must be newer than the current Device Group's
// min_supported_deployment
//
// Returns: Promise that resolves when the Device Group min_supported_deployment
// relationships is updated successfully, or rejects with an error
updateMinSupportedDeployment(deviceGroupId, deploymentId) {
const error = ParamsChecker.validateNonEmpty(deviceGroupId) ||
ParamsChecker.validateNonEmpty(deploymentId, 'deploymentId');
if (error) {
return Promise.reject(error);
}
const Deployments = require('./Deployments');
return HttpHelper.put(this._getPath(deviceGroupId) + '/relationships/min_supported_deployment',
{ data : { type : Deployments._TYPE, id : deploymentId } });
}
// converts [id1, id2, ...] array to array of objects [{ type : 'device', id : id1 }, { type : 'device', id : id2 }, ...]
_convertDeviceIds(deviceIds) {
const Devices = require('./Devices');
return deviceIds.map((deviceId) => {
return {
type : Devices._TYPE,
id : deviceId
};
});
}
_validateProductionTarget(deviceGroupType, productionTarget, isCreate) {
let isFactoryFixture =
(deviceGroupType == DeviceGroups.TYPE_PRE_FACTORY_FIXTURE ||
deviceGroupType == DeviceGroups.TYPE_FACTORY_FIXTURE);
if (productionTarget) {
if (!isFactoryFixture) {
return new Errors.InvalidDataError(
Util.format(
'productionTarget can be specified for "%s" and "%s" Device Groups only',
DeviceGroups.TYPE_PRE_FACTORY_FIXTURE,
DeviceGroups.TYPE_FACTORY_FIXTURE));
}
return ParamsChecker.validateOptions(productionTarget, { type : true, id : true }, 'productionTarget', true) ||
ParamsChecker.validateEnum(
productionTarget.type,
[DeviceGroups.TYPE_PRE_PRODUCTION, DeviceGroups.TYPE_PRODUCTION],
'productionTarget.type');
}
else if (isFactoryFixture && isCreate) {
return new Errors.InvalidDataError(
Util.format(
'productionTarget must be specified for "%s" and "%s" Device Groups',
DeviceGroups.TYPE_PRE_FACTORY_FIXTURE,
DeviceGroups.TYPE_FACTORY_FIXTURE));
}
return null;
}
_getPath(deviceGroupId = null) {
if (deviceGroupId) {
return '/devicegroups/' + deviceGroupId;
}
return '/devicegroups';
}
// Returns DeviceGroup resource type
static get _TYPE() {
return 'devicegroup';
}
}
module.exports = DeviceGroups;