Skip to content

Commit

Permalink
feat(dao): add ReSTv2 support to event and node DAOs
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Reed committed Jul 31, 2017
1 parent 5806aa6 commit c524493
Show file tree
Hide file tree
Showing 10 changed files with 617 additions and 8 deletions.
12 changes: 10 additions & 2 deletions src/dao/EventDAO.ts
Expand Up @@ -30,15 +30,15 @@ export class EventDAO extends AbstractDAO<number, OnmsEvent> {
/** Get an event, given the event's ID. */
public async get(id: number): Promise<OnmsEvent> {
const opts = this.getOptions();
return this.http.get('rest/events/' + id, opts).then((result) => {
return this.http.get(this.pathToEventsEndpoint() + '/' + id, opts).then((result) => {
return this.fromData(result.data);
});
}

/** Get an event, given a filter. */
public async find(filter?: Filter): Promise<OnmsEvent[]> {
const opts = this.getOptions(filter);
return this.http.get('rest/events', opts).then((result) => {
return this.http.get(this.pathToEventsEndpoint(), opts).then((result) => {
let data = result.data;

if (this.getCount(data) > 0 && data.event) {
Expand Down Expand Up @@ -110,4 +110,12 @@ export class EventDAO extends AbstractDAO<number, OnmsEvent> {
return event;
}

/**
* Get the path to the events endpoint for the appropriate API version.
* @hidden
*/
private pathToEventsEndpoint() {
return this.getApiVersion() === 2 ? 'api/v2/events' : 'rest/events';
}

}
17 changes: 12 additions & 5 deletions src/dao/NodeDAO.ts
Expand Up @@ -46,7 +46,7 @@ export class NodeDAO extends AbstractDAO<number, OnmsNode> {
public async get(id: number, recurse = false): Promise<OnmsNode> {
const opts = this.getOptions();

return this.http.get('rest/nodes/' + id, opts).then((result) => {
return this.http.get(this.pathToNodesEndpoint() + '/' + id, opts).then((result) => {
const node = this.fromData(result.data);

if (recurse) {
Expand All @@ -60,7 +60,7 @@ export class NodeDAO extends AbstractDAO<number, OnmsNode> {
/** Search for nodes, given an optional filter. */
public async find(filter?: Filter): Promise<OnmsNode[]> {
const opts = this.getOptions(filter);
return this.http.get('rest/nodes', opts).then((result) => {
return this.http.get(this.pathToNodesEndpoint(), opts).then((result) => {
let data = result.data;

if (this.getCount(data) > 0 && data.node) {
Expand Down Expand Up @@ -115,7 +115,7 @@ export class NodeDAO extends AbstractDAO<number, OnmsNode> {
if (node instanceof OnmsNode) {
node = node.id;
}
return this.http.get('rest/nodes/' + node + '/ipinterfaces', opts).then((result) => {
return this.http.get(this.pathToNodesEndpoint() + '/' + node + '/ipinterfaces', opts).then((result) => {
let data = result.data;

if (this.getCount(data) > 0 && data.ipInterface) {
Expand Down Expand Up @@ -143,7 +143,7 @@ export class NodeDAO extends AbstractDAO<number, OnmsNode> {
if (node instanceof OnmsNode) {
node = node.id;
}
return this.http.get('rest/nodes/' + node + '/snmpinterfaces', opts).then((result) => {
return this.http.get(this.pathToNodesEndpoint() + '/' + node + '/snmpinterfaces', opts).then((result) => {
let data = result.data;

if (this.getCount(data) > 0 && data.snmpInterface) {
Expand Down Expand Up @@ -179,7 +179,7 @@ export class NodeDAO extends AbstractDAO<number, OnmsNode> {
if (ipInterface instanceof OnmsIpInterface && ipInterface.ipAddress) {
ipInterface = ipInterface.ipAddress.address;
}
const url = 'rest/nodes/' + node + '/ipinterfaces/' + ipInterface + '/services';
const url = this.pathToNodesEndpoint() + '/' + node + '/ipinterfaces/' + ipInterface + '/services';
return this.http.get(url, opts).then((result) => {
let data = result.data;

Expand Down Expand Up @@ -318,4 +318,11 @@ export class NodeDAO extends AbstractDAO<number, OnmsNode> {
return service;
}

/**
* Get the path to the nodes endpoint for the appropriate API version.
* @hidden
*/
private pathToNodesEndpoint() {
return this.getApiVersion() === 2 ? 'api/v2/nodes' : 'rest/nodes';
}
}
64 changes: 63 additions & 1 deletion test/dao/NodeDAO.spec.ts
Expand Up @@ -19,6 +19,7 @@ import {SnmpStatusTypes} from '../../src/model/OnmsSnmpStatusType';
import {NodeDAO} from '../../src/dao/NodeDAO';

import {MockHTTP19} from '../rest/MockHTTP19';
import {MockHTTP21} from '../rest/MockHTTP21';

/** @hidden */
// tslint:disable-next-line
Expand All @@ -31,7 +32,7 @@ const SERVER_PASSWORD='demo';

let opennms : Client, server, auth, mockHTTP, dao : NodeDAO;

describe('NodeDAO', () => {
describe('NodeDAO with v1 API', () => {
beforeEach((done) => {
auth = new OnmsAuthConfig(SERVER_USER, SERVER_PASSWORD);
server = new OnmsServer(SERVER_NAME, SERVER_URL, auth);
Expand Down Expand Up @@ -91,3 +92,64 @@ describe('NodeDAO', () => {
});
});
});

describe('NodeDAO with v2 API', () => {
beforeEach((done) => {
auth = new OnmsAuthConfig(SERVER_USER, SERVER_PASSWORD);
server = new OnmsServer(SERVER_NAME, SERVER_URL, auth);
mockHTTP = new MockHTTP21(server);
opennms = new Client(mockHTTP);
dao = new NodeDAO(mockHTTP);
Client.getMetadata(server, mockHTTP).then((metadata) => {
server.metadata = metadata;
done();
});
});
it('NodeDAO.get(81, [recurse=false])', () => {
return dao.get(81).then((node) => {
expect(node.id).toEqual(81);
expect(node.categories.length).toEqual(1);
expect(node.categories[0]).toBeInstanceOf(OnmsCategory);
expect(node.foreignSource).toEqual('test');
expect(node.createTime).toBeInstanceOf(moment);
expect(node.type).toBeDefined();
expect(node.type).toBeInstanceOf(OnmsNodeType);
});
});
it('NodeDAO.get(81, recurse=true)', () => {
return dao.get(81, true).then((node) => {
expect(node.id).toEqual(81);
expect(node.categories.length).toEqual(1);
expect(node.categories[0]).toBeInstanceOf(OnmsCategory);
expect(node.foreignSource).toEqual('test');
expect(node.createTime).toBeInstanceOf(moment);
expect(node.type).toBeDefined();
expect(node.type).toBeInstanceOf(OnmsNodeType);

expect(node.snmpInterfaces.length).toEqual(10);

const snmp = node.snmpInterfaces[2];
expect(snmp.ifIndex).toEqual(3);
expect(snmp.ifSpeed).toEqual(0);
expect(snmp.ifAdminStatus).toEqual(SnmpStatusTypes['2']);
expect(snmp.ifOperStatus).toEqual(SnmpStatusTypes['2']);
expect(snmp.ifName).toEqual('stf0');

expect(node.ipInterfaces.length).toEqual(2);
const ip = node.ipInterfaces[1];
expect(ip.hostname).toEqual('172.20.1.110');
expect(ip.services.length).toEqual(3);

expect(ip.snmpInterface).toBeUndefined();
});
});
/** find is currently broken in v2
it('NodeDAO.find(id=81)', () => {
const filter = new Filter();
filter.withOrRestriction(new Restriction('id', Comparators.EQ, 81));
return dao.find(filter).then((nodes) => {
expect(nodes.length).toEqual(1);
});
});
*/
});
Empty file.
@@ -0,0 +1,82 @@
{
"count": 5,
"offset": null,
"service": [
{
"applications": [],
"down": false,
"lastFail": null,
"lastGood": null,
"notify": null,
"qualifier": null,
"serviceType": {
"id": 2,
"name": "SNMP"
},
"source": null,
"status": "A",
"statusLong": "Managed"
},
{
"applications": [],
"down": false,
"lastFail": null,
"lastGood": null,
"notify": null,
"qualifier": null,
"serviceType": {
"id": 1,
"name": "ICMP"
},
"source": null,
"status": "A",
"statusLong": "Managed"
},
{
"applications": [],
"down": false,
"lastFail": null,
"lastGood": null,
"notify": null,
"qualifier": null,
"serviceType": {
"id": 5,
"name": "StrafePing"
},
"source": null,
"status": "A",
"statusLong": "Managed"
},
{
"applications": [],
"down": false,
"lastFail": null,
"lastGood": null,
"notify": null,
"qualifier": null,
"serviceType": {
"id": 3,
"name": "OpenNMS-JVM"
},
"source": null,
"status": "A",
"statusLong": "Managed"
},
{
"applications": [],
"down": false,
"lastFail": null,
"lastGood": null,
"notify": null,
"qualifier": null,
"serviceType": {
"id": 4,
"name": "SSH"
},
"source": null,
"status": "A",
"statusLong": "Managed"
}
],
"totalCount": 5
}
@@ -0,0 +1,52 @@
{
"count": 3,
"offset": null,
"service": [
{
"applications": [],
"down": false,
"lastFail": null,
"lastGood": null,
"notify": null,
"qualifier": null,
"serviceType": {
"id": 1,
"name": "ICMP"
},
"source": null,
"status": "A",
"statusLong": "Managed"
},
{
"applications": [],
"down": false,
"lastFail": null,
"lastGood": null,
"notify": null,
"qualifier": null,
"serviceType": {
"id": 5,
"name": "StrafePing"
},
"source": null,
"status": "A",
"statusLong": "Managed"
},
{
"applications": [],
"down": false,
"lastFail": null,
"lastGood": null,
"notify": null,
"qualifier": null,
"serviceType": {
"id": 4,
"name": "SSH"
},
"source": null,
"status": "A",
"statusLong": "Managed"
}
],
"totalCount": 3
}
71 changes: 71 additions & 0 deletions test/rest/21.0.0/get/api/v2/nodes/81.ipinterfaces.json
@@ -0,0 +1,71 @@
{
"count": 2,
"ipInterface": [
{
"hostName": "127.0.0.1",
"id": "1957",
"ifIndex": 1,
"ipAddress": "127.0.0.1",
"isDown": false,
"isManaged": "M",
"lastCapsdPoll": 1501532685099,
"monitoredServiceCount": 5,
"nodeId": 81,
"snmpInterface": {
"collect": false,
"collectFlag": "N",
"collectionUserSpecified": false,
"id": 1959,
"ifAdminStatus": 1,
"ifAlias": "",
"ifDescr": "lo0",
"ifIndex": 1,
"ifName": "lo0",
"ifOperStatus": 1,
"ifSpeed": 0,
"ifType": 24,
"lastCapsdPoll": 1501532685099,
"lastSnmpPoll": null,
"netMask": "255.0.0.0",
"physAddr": null,
"poll": false,
"pollFlag": "N"
},
"snmpPrimary": "P"
},
{
"hostName": "172.20.1.110",
"id": "2139",
"ifIndex": 6,
"ipAddress": "172.20.1.110",
"isDown": false,
"isManaged": "M",
"lastCapsdPoll": 1501532685099,
"monitoredServiceCount": 3,
"nodeId": 81,
"snmpInterface": {
"collect": true,
"collectFlag": "C",
"collectionUserSpecified": false,
"id": 2018,
"ifAdminStatus": 1,
"ifAlias": "",
"ifDescr": "en0",
"ifIndex": 6,
"ifName": "en0",
"ifOperStatus": 1,
"ifSpeed": 304200000,
"ifType": 6,
"lastCapsdPoll": 1501532685099,
"lastSnmpPoll": null,
"netMask": "255.255.255.0",
"physAddr": "000f241fa144",
"poll": false,
"pollFlag": "N"
},
"snmpPrimary": "N"
}
],
"offset": null,
"totalCount": 2
}

0 comments on commit c524493

Please sign in to comment.