Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions data/views/poller.2.0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<%
var Constants = injector.get('Constants');
var _ = injector.get('_');
var type = _.findKey(Constants.WorkItems.Pollers, function(value) {
return value === name;
});
%>
{
"id": "<%=id%>",
"type": "<%=type.toLowerCase()%>",
"pollInterval": <%=pollInterval%>,
<% if (node !== null) { %>
"node": "<%=basepath%>/nodes/<%=node%>",
<% } else { %>
"node": null,
<% } %>
"config": <%-JSON.stringify(config)%>,
<% if (lastStarted !== null) { %>
"lastStarted": "<%=lastStarted%>",
<% } else { %>
"lastStarted": null,
<% } %>
<% if (node !== null) { %>
"lastFinished": "<%=lastFinished%>",
<% } else { %>
"lastFinished": null,
<% } %>
"paused": <%=paused%>,
"failureCount": <%=failureCount%>
}
2 changes: 1 addition & 1 deletion lib/services/files/file-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ function factory(Promise, Errors, waterline, uuid, assert, _, fs) {

return this.findEntries(query)
.then(function(fileArray) {

//TODO: remove toJSON when 1.1 API is deprecated.
var files = _.map(fileArray, function(file) {
return file.toJSON();
});
Expand Down
14 changes: 4 additions & 10 deletions lib/services/pollers-api-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ di.annotate(pollerApiServiceFactory,
'Services.Waterline',
'Protocol.Task',
'Errors',
'_'
'_',
'Promise'
)
);

function pollerApiServiceFactory(
waterline,
taskProtocol,
Errors,
_
_,
Promise

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'Promise' is defined but never used.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'Promise' is defined but never used.

) {

function PollerApiService() {
Expand Down Expand Up @@ -91,17 +93,11 @@ function pollerApiServiceFactory(
]
}
];
/**

*/
PollerApiService.prototype.getPollerLib = function() {
return pollerLibrary;
};

/**

*/

PollerApiService.prototype.getPollerLibById = function(id) {
return _.detect(pollerLibrary, { name: id });
};
Expand All @@ -114,7 +110,6 @@ function pollerApiServiceFactory(
return waterline.workitems.needByIdentifier(id);
};


PollerApiService.prototype.postPollers = function(poller) {
return waterline.workitems.create(poller);
};
Expand All @@ -134,7 +129,6 @@ function pollerApiServiceFactory(
return waterline.workitems.updateByIdentifier(id, { paused: false });
};


PollerApiService.prototype.deletePollersById = function(id){
return waterline.workitems.destroyByIdentifier(id);
};
Expand Down
25 changes: 18 additions & 7 deletions lib/services/swagger-api-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,18 @@ function swaggerFactory(
}
return function(req, res, next) {
req.swagger.options = options;
return Promise.resolve().then(function() {
return Promise.try(function() {
_parseQuery(req);
return callback(req, res);
}).then(function(result) {
if (!res.headersSent) {
res.body = result;
if (_.isArray(result)) {
res.body = result.map(function(element) {
return element.toJSON ? element.toJSON() : element;
});
} else {
res.body = result.toJSON ? result.toJSON() : result;
}
next();
}
}).catch(function(err) {
Expand Down Expand Up @@ -144,6 +150,7 @@ function swaggerFactory(
})
.then(function() {
var render;
var options;

if (!viewName || _.isEmpty(res.body)) {
if (typeof(res.body) === 'string') {
Expand All @@ -154,17 +161,21 @@ function swaggerFactory(
// Need to set content-type here
// because render will resolve to a string.
res.set('Content-Type', 'application/json');

options = {
basepath: req.swagger.operation.api.basePath,
injector: injector
};

if (_.isArray(res.body)) {
render = Promise.map(res.body, function(element) {
element.basepath = req.swagger.operation.api.basePath;
return views.render(viewName, element);
return views.render(viewName, _.merge(options, element));
})
.then(function(collection) {
return views.render('collection.2.0.json', { collection: collection });
});
} else {
res.body.basepath = req.swagger.operation.api.basePath;
render = views.render(viewName, res.body);
render = views.render(viewName, _.merge(options, res.body));
}

return render.then(function(data) {
Expand Down Expand Up @@ -198,7 +209,7 @@ function swaggerFactory(
return schemaApiService.validate(data, schemaName)
.then(function (validationResults) {
if (validationResults.error) {
throw new Error(validationResults.error);
throw new Errors.ValidationError(validationResults.error.message);
}
next();
}).catch(function (err) {
Expand Down
36 changes: 26 additions & 10 deletions spec/lib/api/2.0/pollers-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ describe('Http.Api.Pollers', function () {
});

beforeEach('reset test DB', function () {
return helper.reset();
return helper.reset().then(function() {
return helper.injector.get('Views').load();
});
});

after('stop HTTP server', function () {
Expand Down Expand Up @@ -52,10 +54,7 @@ describe('Http.Api.Pollers', function () {
expect(res.body).to.have.deep.property('config.command', 'sdr');
expect(res.body).to.have.deep.property('config.host', '0.0.0.0');
expect(res.body).to.have.deep.property('config.user', 'myuser');
expect(res.body).to.have.deep.property('config.password', 'mypass');
expect(res.body).to.have.property('id').to.be.a('string');
expect(res.body).to.have.property('createdAt').to.be.a('string');
expect(res.body).to.have.property('updatedAt').to.be.a('string');
expect(res.body).to.have.property('lastStarted', null);
expect(res.body).to.have.property('lastFinished', null);
});
Expand Down Expand Up @@ -86,7 +85,6 @@ describe('Http.Api.Pollers', function () {
expect(res.body).to.have.property('pollInterval', 6000);
expect(res.body).to.have.property('config').to.be.an('object');
expect(res.body).to.have.deep.property('config.host', '0.0.0.0');
expect(res.body).to.have.deep.property('config.communityString', 'public');

expect(res.body).to.have.deep.property('config.extensionMibs')
.to.be.an.instanceOf(Array);
Expand All @@ -99,13 +97,26 @@ describe('Http.Api.Pollers', function () {
.and.to.include('PowerNet-MIB::rPDUIdentDeviceLinetoLineVoltage');

expect(res.body).to.have.property('id').to.be.a('string');
expect(res.body).to.have.property('createdAt').to.be.a('string');
expect(res.body).to.have.property('updatedAt').to.be.a('string');
expect(res.body).to.have.property('lastStarted', null);
expect(res.body).to.have.property('lastFinished', null);
});
});

it('should 400 when required field is missing from POST /pollers', function () {
return helper.request().post('/api/2.0/pollers')
.send({
pollInterval: 5000,
config: {
command: 'sdr',
host: '0.0.0.0',
user: 'myuser',
password: 'mypass'
}
})
.expect('Content-Type', /^application\/json/)
.expect(400);
});

// TODO: probably change this later to not just be a static check
var library = [
{
Expand Down Expand Up @@ -259,15 +270,20 @@ describe('Http.Api.Pollers', function () {
expect(res.body).to.have.property('failureCount', poller.failureCount);
expect(res.body).to.have.property('config').to.be.an('object');
expect(res.body).to.have.property('id', poller.id);
expect(res.body).to.have.property('createdAt', poller.createdAt);
expect(res.body).to.have.property('updatedAt')
.to.not.equal(poller.updatedAt);
expect(res.body).to.have.property('lastStarted', poller.lastStarted);
expect(res.body).to.have.property('lastFinished', poller.lastFinished);
expect(res.body).to.have.property('paused', false);
});
});

it('should 400 on invalid PATCH /pollers', function () {
poller.pollInterval = 'some string';
return helper.request().patch('/api/2.0/pollers/' + poller.id)
.send(poller)
.expect('Content-Type', /^application\/json/)
.expect(400);
});

it('should create pollers that are not paused by default', function () {
return helper.request().get('/api/2.0/pollers/' + poller.id)
.expect('Content-Type', /^application\/json/)
Expand Down
12 changes: 7 additions & 5 deletions static/monorail-2.0.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ paths:
/pollers:
x-swagger-router-controller: pollers
get:
x-swagger-serializer: pollers
operationId: pollersGet
x-privileges: [ 'Read' ]
x-authentication-type: [ 'jwt' ]
x-view: poller.2.0.json
summary: |
get list of all pollers
description: |
Expand All @@ -98,10 +98,11 @@ paths:
schema:
$ref: '#/definitions/Error'
post:
x-swagger-deserializer: pollers
operationId: pollersPost
x-privileges: [ 'Write' ]
x-authentication-type: [ 'jwt' ]
x-swagger-schema: poller.2.0.json#/definitions/Poller
x-view: poller.2.0.json
summary: |
create a poller
description: |
Expand All @@ -127,10 +128,10 @@ paths:
/pollers/{identifier}:
x-swagger-router-controller: pollers
get:
x-swagger-serializer: pollers
operationId: pollersIdGet
x-privileges: [ 'Read' ]
x-authentication-type: [ 'jwt' ]
x-view: poller.2.0.json
summary: |
Get specifics of the specified poller
description: |
Expand Down Expand Up @@ -159,10 +160,11 @@ paths:
schema:
$ref: '#/definitions/Error'
patch:
x-swagger-deserializer: pollers
operationId: pollersPatch
x-privileges: [ 'Write' ]
x-authentication-type: [ 'jwt' ]
x-swagger-schema: poller.2.0.json#/definitions/PartialPoller
x-view: poller.2.0.json
summary: |
patch specifics of the specified poller
description: |
Expand Down Expand Up @@ -2088,7 +2090,7 @@ paths:
$ref: '#/definitions/Error'
post:
x-swagger-deserializer: nodes
x-swagger-schema: Node.2.0.json#/definitions/Node
x-swagger-schema: node.2.0.json#/definitions/Node
operationId: nodesPost
x-privileges: [ 'Write' ]
x-authentication-type: [ 'jwt' ]
Expand Down
File renamed without changes.
34 changes: 34 additions & 0 deletions static/schemas/2.0/poller.2.0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"title": "Poller.2.0",
"definitions": {
"PartialPoller": {
"description": "A poller for periodic collection of telemetry data",
"type": "object",
"properties": {
"type": {
"description": "Type of poller",
"type": "string",
"enum": ["ipmi", "snmp", "redfish"]
},
"pollInterval": {
"description": "Interval at which poller will run",
"type": "number"
},
"paused": {
"description": "Asserted if poller is paused",
"type": "boolean"
},
"config": {
"description": "Poller configuration object",
"type": "object"
}
}
},
"Poller": {
"$ref": "#/definitions/PartialPoller",
"required": [
"type", "pollInterval", "config"
]
}
}
}