Skip to content

Commit

Permalink
Merge pull request #28 from electricimp/develop
Browse files Browse the repository at this point in the history
v1.5.0
  • Loading branch information
Pavel Petroshenko authored Mar 5, 2019
2 parents 84d9aad + e7ee185 commit 9229678
Show file tree
Hide file tree
Showing 16 changed files with 293 additions and 94 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright 2017 Electric Imp
Copyright 2017-2019 Electric Imp

SPDX-License-Identifier: MIT

Expand Down
7 changes: 4 additions & 3 deletions lib/Deployments.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// MIT License
//
// Copyright 2017 Electric Imp
// Copyright 2017-2019 Electric Imp
//
// SPDX-License-Identifier: MIT
//
Expand Down Expand Up @@ -128,8 +128,9 @@ class Deployments extends Entities {
// deviceGroupId : String The Device Group's ID
// deviceGroupType : String The Device Group's 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 Deployment to be created.
// DeviceGroups.TYPE_PRE_FACTORY_FIXTURE, DeviceGroups.TYPE_FACTORY_FIXTURE,
// DeviceGroups.TYPE_PRE_DUT, DeviceGroups.TYPE_DUT.
// attributes : Object Key/Value attributes of the Deployment to be created.
// The valid keys are:
// 'device_code' (String, required) - The Squirrel device code
// for this Deployment
Expand Down
75 changes: 57 additions & 18 deletions lib/DeviceGroups.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// MIT License
//
// Copyright 2017 Electric Imp
// Copyright 2017-2019 Electric Imp
//
// SPDX-License-Identifier: MIT
//
Expand Down Expand Up @@ -74,13 +74,23 @@ class DeviceGroups extends Entities {
return 'factoryfixture_devicegroup';
}

static get TYPE_PRE_DUT() {
return 'pre_dut_devicegroup';
}

static get TYPE_DUT() {
return 'dut_devicegroup';
}

static get _validTypes() {
return [
DeviceGroups.TYPE_DEVELOPMENT,
DeviceGroups.TYPE_PRE_PRODUCTION,
DeviceGroups.TYPE_PRODUCTION,
DeviceGroups.TYPE_PRE_FACTORY_FIXTURE,
DeviceGroups.TYPE_FACTORY_FIXTURE
DeviceGroups.TYPE_FACTORY_FIXTURE,
DeviceGroups.TYPE_PRE_DUT,
DeviceGroups.TYPE_DUT
];
}

Expand Down Expand Up @@ -152,13 +162,21 @@ class DeviceGroups extends Entities {
// One of DeviceGroups.TYPE_PRE_PRODUCTION,
// DeviceGroups.TYPE_PRODUCTION.
// 'id' (String, required) - the target Device Group's ID.
// dutTarget : Optional dut_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_DUT,
// DeviceGroups.TYPE_DUT.
// 'id' (String, required) - the DUT 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) {
create(productId, type, attributes, productionTarget = null, dutTarget = null) {
let error = ParamsChecker.validateNonEmpty(productId) ||
DeviceGroups.validateType(type, 'type') ||
this._validateProductionTarget(type, productionTarget, true);
this._validateTarget(type, productionTarget, false, true) ||
this._validateTarget(type, dutTarget, true, true);
if (!error && 'region' in attributes &&
!(type == DeviceGroups.TYPE_PRE_PRODUCTION || type == DeviceGroups.TYPE_PRODUCTION)) {
error = new Errors.InvalidDataError(
Expand All @@ -180,6 +198,9 @@ class DeviceGroups extends Entities {
if (productionTarget) {
relationships['production_target'] = productionTarget;
}
if (dutTarget) {
relationships['dut_target'] = dutTarget;
}

const body = {
data : {
Expand Down Expand Up @@ -233,12 +254,20 @@ class DeviceGroups extends Entities {
// One of DeviceGroups.TYPE_PRE_PRODUCTION,
// DeviceGroups.TYPE_PRODUCTION.
// 'id' (String, required) - the target Device Group's ID.
// dutTarget : Optional dut_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_DUT, DeviceGroups.TYPE_DUT.
// 'id' (String, required) - the DUT 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) {
update(deviceGroupId, type, attributes, productionTarget = null, dutTarget = null) {
let error = DeviceGroups.validateType(type, 'type') ||
this._validateProductionTarget(type, productionTarget, false);
this._validateTarget(type, productionTarget, false, false) ||
this._validateTarget(type, dutTarget, true, false);
if (!error && 'load_code_after_blessing' in attributes &&
!(type == DeviceGroups.TYPE_PRE_PRODUCTION || type == DeviceGroups.TYPE_PRODUCTION)) {
error = new Errors.InvalidDataError(
Expand All @@ -255,10 +284,15 @@ class DeviceGroups extends Entities {
id : deviceGroupId,
attributes : attributes
};
if (productionTarget) {
data['relationships'] = {
production_target : productionTarget
};
if (productionTarget || dutTarget) {
const relationships = {};
if (productionTarget) {
relationships['production_target'] = productionTarget;
}
if (dutTarget) {
relationships['dut_target'] = dutTarget;
}
data['relationships'] = relationships;
}
return super.update(deviceGroupId, attributes, { data: data });
}
Expand Down Expand Up @@ -392,28 +426,33 @@ class DeviceGroups extends Entities {
});
}

_validateProductionTarget(deviceGroupType, productionTarget, isCreate) {
_validateTarget(deviceGroupType, target, isDut, isCreate) {
let isFactoryFixture =
(deviceGroupType == DeviceGroups.TYPE_PRE_FACTORY_FIXTURE ||
deviceGroupType == DeviceGroups.TYPE_FACTORY_FIXTURE);
if (productionTarget) {
let targetName = isDut ? 'dutTarget' : 'productionTarget';
if (target) {
if (!isFactoryFixture) {
return new Errors.InvalidDataError(
Util.format(
'productionTarget can be specified for "%s" and "%s" Device Groups only',
'%s can be specified for "%s" and "%s" Device Groups only',
targetName,
DeviceGroups.TYPE_PRE_FACTORY_FIXTURE,
DeviceGroups.TYPE_FACTORY_FIXTURE));
}
return ParamsChecker.validateOptions(productionTarget, { type : true, id : true }, 'productionTarget', true) ||
return ParamsChecker.validateOptions(target, { type : true, id : true }, targetName, true) ||
ParamsChecker.validateEnum(
productionTarget.type,
[DeviceGroups.TYPE_PRE_PRODUCTION, DeviceGroups.TYPE_PRODUCTION],
'productionTarget.type');
target.type,
isDut ?
[DeviceGroups.TYPE_PRE_DUT, DeviceGroups.TYPE_DUT] :
[DeviceGroups.TYPE_PRE_PRODUCTION, DeviceGroups.TYPE_PRODUCTION],
targetName + '.type');
}
else if (isFactoryFixture && isCreate) {
return new Errors.InvalidDataError(
Util.format(
'productionTarget must be specified for "%s" and "%s" Device Groups',
'%s must be specified for "%s" and "%s" Device Groups',
targetName,
DeviceGroups.TYPE_PRE_FACTORY_FIXTURE,
DeviceGroups.TYPE_FACTORY_FIXTURE));
}
Expand Down
7 changes: 4 additions & 3 deletions lib/Webhooks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// MIT License
//
// Copyright 2017 Electric Imp
// Copyright 2017-2019 Electric Imp
//
// SPDX-License-Identifier: MIT
//
Expand Down Expand Up @@ -106,8 +106,9 @@ class Webhooks extends Entities {
// deviceGroupId : String ID of the Device Group for which the Webhook is created.
// deviceGroupType : String The Device Group's 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 Webhook to be created.
// DeviceGroups.TYPE_PRE_FACTORY_FIXTURE, DeviceGroups.TYPE_FACTORY_FIXTURE,
// DeviceGroups.TYPE_PRE_DUT, DeviceGroups.TYPE_DUT.
// attributes : Object Key/Value attributes of the Webhook to be created.
// The valid keys are:
// 'url' (String, required) - the Webhook's target URL.
// 'event' (String, required) - the event that triggers the webhook.
Expand Down
5 changes: 3 additions & 2 deletions lib/util/HttpHelper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// MIT License
//
// Copyright 2017 Electric Imp
// Copyright 2017-2019 Electric Imp
//
// SPDX-License-Identifier: MIT
//
Expand Down Expand Up @@ -133,7 +133,8 @@ class HttpHelper {
json : true,
qs : query,
body : body,
qsStringifyOptions : { arrayFormat: 'repeat' }
qsStringifyOptions : { arrayFormat: 'repeat' },
forever : true
};

if (Logger.debug) {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "imp-central-api",
"version": "1.4.2",
"version": "1.5.0",
"description": "Electric Imp impCentral API v5 Client",
"main": "index.js",
"scripts": {
Expand Down
6 changes: 3 additions & 3 deletions spec/deployments.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// MIT License
//
// Copyright 2017 Electric Imp
// Copyright 2017-2019 Electric Imp
//
// SPDX-License-Identifier: MIT
//
Expand Down Expand Up @@ -44,7 +44,7 @@ describe('impCentralAPI.deployments test suite', () => {
beforeAll(util.init, util.TIMEOUT);

it('should create a product', (done) => {
productName = util.PRODUCT_NAME;
productName = util.getProductName();
impCentralApi.products.create({name : productName}).
then((res) => {
productId = res.data.id;
Expand All @@ -56,7 +56,7 @@ describe('impCentralAPI.deployments test suite', () => {
});

it('should create a device group', (done) => {
deviceGroupName = util.DEVICE_GROUP_NAME;
deviceGroupName = util.getDeviceGroupName();
impCentralApi.deviceGroups.create(productId, DeviceGroups.TYPE_DEVELOPMENT, { name : deviceGroupName }).
then((res) => {
expect(res.data.type).toBe(DeviceGroups.TYPE_DEVELOPMENT);
Expand Down
14 changes: 7 additions & 7 deletions spec/device_groups.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// MIT License
//
// Copyright 2017 Electric Imp
// Copyright 2017-2019 Electric Imp
//
// SPDX-License-Identifier: MIT
//
Expand Down Expand Up @@ -46,7 +46,7 @@ describe('impCentralAPI.device_groups test suite', () => {
beforeAll(util.init, util.TIMEOUT);

it('should create a product', (done) => {
productName = util.PRODUCT_NAME;
productName = util.getProductName();
impCentralApi.products.create({name : productName}).
then((res) => {
productId = res.data.id;
Expand All @@ -58,7 +58,7 @@ describe('impCentralAPI.device_groups test suite', () => {
}, util.TIMEOUT);

it('should create a device group', (done) => {
deviceGroupName = util.DEVICE_GROUP_NAME;
deviceGroupName = util.getDeviceGroupName();
impCentralApi.deviceGroups.create(productId, DeviceGroups.TYPE_DEVELOPMENT, { name : deviceGroupName }).
then((res) => {
expect(res.data.type).toBe(DeviceGroups.TYPE_DEVELOPMENT);
Expand Down Expand Up @@ -86,7 +86,7 @@ describe('impCentralAPI.device_groups test suite', () => {
});

it('should not create a device group with wrong type', (done) => {
let deviceGroupName = util.DEVICE_GROUP_NAME_2;
let deviceGroupName = util.getDeviceGroupName(1);
impCentralApi.deviceGroups.create(productId, 'wrong_type', { name : deviceGroupName }).
then((res) => {
done.fail('device group with wrong type created successfully');
Expand All @@ -100,7 +100,7 @@ describe('impCentralAPI.device_groups test suite', () => {
});

it('should not create a device group with wrong attributes', (done) => {
let deviceGroupName = util.DEVICE_GROUP_NAME_3;
let deviceGroupName = util.getDeviceGroupName(2);
impCentralApi.deviceGroups.create(productId, DeviceGroups.TYPE_DEVELOPMENT, { name_ : deviceGroupName }).
then((res) => {
done.fail('device group with wrong attributes created successfully');
Expand Down Expand Up @@ -202,7 +202,7 @@ describe('impCentralAPI.device_groups test suite', () => {

it('should update a specific device group', (done) => {
let descr = 'test description';
deviceGroupName = util.DEVICE_GROUP_NAME_4;
deviceGroupName = util.getDeviceGroupName(3);
impCentralApi.deviceGroups.update(deviceGroupId, DeviceGroups.TYPE_DEVELOPMENT, { description : descr, name: deviceGroupName }).
then((res) => {
expect(res.data.id).toBe(deviceGroupId);
Expand Down Expand Up @@ -345,7 +345,7 @@ describe('impCentralAPI.device_groups test suite', () => {
it('should add devices by Agent ID to a specific device group', (done) => {
// impossible to address unassigned devices by Agent ID. Need to assign them to a different DG first.
let devGroupId;
impCentralApi.deviceGroups.create(productId, DeviceGroups.TYPE_DEVELOPMENT, { name : util.DEVICE_GROUP_NAME_5 }).
impCentralApi.deviceGroups.create(productId, DeviceGroups.TYPE_DEVELOPMENT, { name : util.getDeviceGroupName(4) }).
then((res) => {
devGroupId = res.data.id;
return impCentralApi.deviceGroups.addDevices(devGroupId, ...Object.keys(devices));
Expand Down
6 changes: 3 additions & 3 deletions spec/devices.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// MIT License
//
// Copyright 2017 Electric Imp
// Copyright 2017-2019 Electric Imp
//
// SPDX-License-Identifier: MIT
//
Expand Down Expand Up @@ -47,7 +47,7 @@ describe('impCentralAPI.devices test suite', () => {
beforeAll(util.init, util.TIMEOUT);

it('should create a product', (done) => {
productName = util.PRODUCT_NAME;
productName = util.getProductName();
impCentralApi.products.create({name : productName}).
then((res) => {
expect(res.data.type).toBe('product');
Expand All @@ -62,7 +62,7 @@ describe('impCentralAPI.devices test suite', () => {
});

it('should create a device group', (done) => {
deviceGroupName = util.DEVICE_GROUP_NAME;
deviceGroupName = util.getDeviceGroupName();
impCentralApi.deviceGroups.create(productId, DeviceGroups.TYPE_DEVELOPMENT, { name : deviceGroupName }).
then((res) => {
deviceGroupId = res.data.id;
Expand Down
6 changes: 3 additions & 3 deletions spec/logstreams.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// MIT License
//
// Copyright 2017 Electric Imp
// Copyright 2017-2019 Electric Imp
//
// SPDX-License-Identifier: MIT
//
Expand Down Expand Up @@ -54,7 +54,7 @@ describe('impCentralAPI.logStreams test suite', () => {
beforeAll(util.init, util.TIMEOUT);

it('should create a product', (done) => {
productName = util.PRODUCT_NAME;
productName = util.getProductName();
impCentralApi.products.create({name : productName}).
then((res) => {
productId = res.data.id;
Expand All @@ -66,7 +66,7 @@ describe('impCentralAPI.logStreams test suite', () => {
});

it('should create a device group', (done) => {
deviceGroupName = util.DEVICE_GROUP_NAME;
deviceGroupName = util.getDeviceGroupName();
impCentralApi.deviceGroups.create(productId, DeviceGroups.TYPE_DEVELOPMENT, { name : deviceGroupName }).
then((res) => {
expect(res.data.type).toBe(DeviceGroups.TYPE_DEVELOPMENT);
Expand Down
Loading

0 comments on commit 9229678

Please sign in to comment.