Skip to content

Commit

Permalink
Merge b717cb5 into 0dab5e1
Browse files Browse the repository at this point in the history
  • Loading branch information
bbcyyb authored Nov 6, 2017
2 parents 0dab5e1 + b717cb5 commit 98c8ad2
Show file tree
Hide file tree
Showing 7 changed files with 248 additions and 44 deletions.
2 changes: 1 addition & 1 deletion lib/jobs/base-job.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ function baseJobFactory(
self.subscriptions.push(subscription);
});
};

BaseJob.prototype._subscribeNodeNotification = function _subscribeNodeNotification(NodeId, callback) {
var self = this;
var deferred = eventsProtocol.subscribeNodeNotification(NodeId, callback.bind(self));
Expand Down
78 changes: 78 additions & 0 deletions lib/jobs/ucs-base-job.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2017 Dell Inc. or its subsidiaries. All Rights Reserved.

'use strict';

var di = require('di');

module.exports = ucsBaseJobFactory;
di.annotate(ucsBaseJobFactory, new di.Provide('Job.Ucs.Base'));
di.annotate(ucsBaseJobFactory,
new di.Inject(
'Job.Base',
'Logger',
'Util',
'Promise',
'JobUtils.UcsTool',
'_',
'Assert'));

function ucsBaseJobFactory(
BaseJob,
Logger,
util,
Promise,
UcsTool,
_,
assert
) {
function UcsBaseJob(logger, options, context, taskId) {
UcsBaseJob.super_.call(this, logger, options, context, taskId);

var _ucstoolInstance = null;
this._getUcsToolInstance = function() {
if (!_ucstoolInstance) {
_ucstoolInstance = new UcsTool();
}
return _ucstoolInstance;
};
}
util.inherits(UcsBaseJob, BaseJob);

UcsBaseJob.prototype._run = function() {};

UcsBaseJob.prototype._ucsRequest = function(url, settings) {
var self = this;
var ucsTool = self._getUcsToolInstance();
ucsTool.settings = settings;
return ucsTool.clientRequest(url);
};

UcsBaseJob.prototype._ucsRequestAsync = function(url, settings, callbackId) {
var self = this;
assert.ok(callbackId);
return Promise.try(function() {
return self._ucsRequest(url, settings);
})
.then(function(res) {
if (res && res.body && res.body.toUpperCase() !== "ACCEPTED") {
throw new Error(
"Request was not ACCEPTED. Please check input parameters.");
}
return self._subscribeHttpResponseUuidByPromisify(callbackId);
});
};

UcsBaseJob.prototype._subscribeHttpResponseUuidByPromisify = function(id) {
var self = this;
var nodeCallback = function(id, callback) {
self._subscribeHttpResponseUuid(function(data) {
return callback.call(self, null, data);
}, id);
};

var promisify = Promise.promisify(nodeCallback);
return promisify(id);
};

return UcsBaseJob;
}
8 changes: 4 additions & 4 deletions lib/jobs/ucs-catalog.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

// Copyright 2017, EMC, Inc.
// Copyright 2016-2017 Dell Inc. or its subsidiaries. All Rights Reserved.

'use strict';

Expand Down Expand Up @@ -172,12 +171,13 @@ function UcsCatalogJobFactory(
})
.then(function(node){
nodeName = node.name;
var isChassis = _.startsWith(_.last(nodeName.split('/')), 'chassis');
return Promise.all([
self._getDataByDN(nodeName, true),
self._getDataByDN(nodeName + '/board')
isChassis ? new Promise.resolve() : self._getDataByDN(nodeName + '/board')
])
.spread(function(nodeResult, boardResult) {
if(nodeResult.board) {
if(boardResult && nodeResult.board) {
// Get more details under rn="board" as hardware info like memory,
// disk, cpu are under rn="board"
nodeResult.board.children = boardResult;
Expand Down
41 changes: 19 additions & 22 deletions lib/jobs/ucs-job.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017, Dell EMC, Inc.
// Copyright 2017 Dell Inc. or its subsidiaries. All Rights Reserved.

'use strict';

Expand All @@ -7,29 +7,29 @@ var di = require('di');
module.exports = ucsJobFactory;
di.annotate(ucsJobFactory, new di.Provide('Job.Ucs'));
di.annotate(ucsJobFactory, new di.Inject(
'Job.Base',
'Job.Ucs.Base',
'Logger',
'Util',
'Promise',
'Services.Waterline',
'JobUtils.UcsTool',
'Services.Configuration',
'Services.Encryption',
'_',
'Assert'
'Assert',
'uuid'
));

function ucsJobFactory(
BaseJob,
UcsBaseJob,
Logger,
util,
Promise,
waterline,
UcsTool,
configuration,
encryption,
_,
assert
assert,
uuid
) {
var logger = Logger.initialize(ucsJobFactory);

Expand All @@ -46,7 +46,7 @@ function ucsJobFactory(
assert.uuid(this.routingKey);

this.concurrent = {};
this.maxConcurrent = 1;
this.maxConcurrent = 10;
this.ucsComandClassIds = {
"ucs.powerthermal": "memoryUnitEnvStats,processorEnvStats," +
"computeMbPowerStats,computeMbTempStats,equipmentChassisStats",
Expand All @@ -58,15 +58,7 @@ function ucsJobFactory(
};
}

util.inherits(UcsJob, BaseJob);

/**
* @function _initUcsTool
* @description generate a new UcsTool object.
**/
UcsJob.prototype._initUcsTool = function() {
return new UcsTool();
};
util.inherits(UcsJob, UcsBaseJob);

/**
* @function _run
Expand Down Expand Up @@ -143,6 +135,10 @@ function ucsJobFactory(
});
};

/**
* @function _collectUcsPollerData
* @description collect ucs poller data
*/
UcsJob.prototype._collectUcsPollerData = function(entity) {
var self = this;
return Promise.try(function() {
Expand All @@ -152,11 +148,12 @@ function ucsJobFactory(
new Error('No ucs classIds found for command %s'.format(entity.command))
);
}
var url = "/pollers?identifier=" + entity.obmSetting.config.dn + "&classIds=" + classIds;
var ucs = self._initUcsTool();
ucs.settings = _.cloneDeep(entity.obmSetting.config);
ucs.settings.ucsPassword = encryption.decrypt(ucs.settings.ucsPassword);
return ucs.clientRequest(url)
var callbackId = uuid.v4();
var url = "/pollers/async?identifier=%s&classIds=%s&callbackId=%s"
.format(entity.obmSetting.config.dn, classIds, callbackId);
var settings = _.cloneDeep(entity.obmSetting.config);
settings.ucsPassword = encryption.decrypt(settings.ucsPassword);
return self._ucsRequestAsync(url, settings, callbackId)
.then(function(res) {
return res.body;
});
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/job-utils/ucs-tool.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017, EMC, Inc.
// Copyright 2016-2017 Dell Inc. or its subsidiaries. All Rights Reserved.

'use strict';

Expand Down
103 changes: 103 additions & 0 deletions spec/lib/jobs/ucs-base-job-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright 2017 Dell Inc. or its subsidiaries. All Rights Reserved.

'use strict';

var uuid = require('node-uuid'),
sandbox = sinon.sandbox.create(),
taskId = uuid.v4(),
UcsTool,
ucsJobBase,
UcsTool = function() {
return {
clientRequest: sandbox.stub().resolves({
"body": "ACCEPTED"
})
};
};

describe('Job.Ucs.Base', function() {
var base = require('./base-spec');

base.before(function(context) {
helper.setupInjector([
helper.require('/spec/mocks/logger.js'),
helper.requireGlob('/lib/services/*.js'),
helper.require('/lib/jobs/base-job.js'),
helper.require('/lib/jobs/ucs-base-job.js'),
helper.di.simpleWrapper(UcsTool, 'JobUtils.UcsTool')
]);
context.Jobclass = helper.injector.get('Job.Ucs.Base');
UcsTool = helper.injector.get('JobUtils.UcsTool');
});

describe('Base', function() {
base.examples();
});

describe('ucs-job-base', function() {
beforeEach(function() {

var graphId = uuid.v4();
ucsJobBase = new this.Jobclass({}, {}, {
graphId: graphId
}, taskId);
});

afterEach(function() {
sandbox.restore();
});



it("should run ucs request", function() {

return ucsJobBase._ucsRequest("http://localhost:12345", {})
.then(function() {
expect(ucsJobBase._getUcsToolInstance().clientRequest).to.be.calledOnce;
});
});

it("should run ucs request asynchronously", function() {

ucsJobBase._subscribeHttpResponseUuidByPromisify = sandbox.stub().resolves("123");
sandbox.spy(ucsJobBase, '_ucsRequest');

return ucsJobBase._ucsRequestAsync("http://localhost:12345", {}, taskId)
.then(function() {
expect(ucsJobBase._ucsRequest).to.be.calledOnce;
expect(ucsJobBase._ucsRequest).to.be.calledWith("http://localhost:12345", {});
expect(ucsJobBase._subscribeHttpResponseUuidByPromisify).to.be.calledOnce;
expect(ucsJobBase._subscribeHttpResponseUuidByPromisify)
.to.be.calledWith(taskId);
});
});

it("should get bad request error to run ucs request asynchronously", function() {
ucsJobBase._subscribeHttpResponseUuidByPromisify = sandbox.stub();
ucsJobBase._ucsRequest = sandbox.stub().resolves({
"body": "ERROR"
});
return ucsJobBase._ucsRequestAsync("http://localhost:12345", {}, taskId)
.then(function() {
throw new Error("Test should fail");
}, function(err) {
expect(ucsJobBase._ucsRequest).to.be.calledOnce;
expect(ucsJobBase._ucsRequest).to.be.calledWith("http://localhost:12345", {});
expect(ucsJobBase._subscribeHttpResponseUuidByPromisify).not.to.be.calledOnce;
expect(err.message).to.deep.equal(
"Request was not ACCEPTED. Please check input parameters.");
});
});

it("should promisify subscribeHttpResponseUuid", function() {
var mock = function(mockSpy) {
mockSpy("abc");
};
ucsJobBase._subscribeHttpResponseUuid = mock;
return ucsJobBase._subscribeHttpResponseUuidByPromisify(taskId)
.then(function(data) {
expect(data).to.equal("abc");
});
});
});
});
Loading

0 comments on commit 98c8ad2

Please sign in to comment.