2014-05-12 20:08:19 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
angular.module('ffffng').factory('NodeResource', function (
|
|
|
|
Constraints,
|
|
|
|
Validator,
|
2016-06-26 19:09:12 +02:00
|
|
|
Logger,
|
|
|
|
MonitoringService,
|
2014-05-12 20:08:19 +02:00
|
|
|
NodeService,
|
|
|
|
_,
|
2016-06-26 19:09:12 +02:00
|
|
|
deepExtend,
|
2014-05-12 20:08:19 +02:00
|
|
|
Strings,
|
2016-05-18 22:50:06 +02:00
|
|
|
Resources,
|
2014-05-12 20:08:19 +02:00
|
|
|
ErrorTypes
|
|
|
|
) {
|
2016-05-18 19:32:19 +02:00
|
|
|
var nodeFields = ['hostname', 'key', 'email', 'nickname', 'mac', 'coords', 'monitoring'];
|
2014-05-12 20:08:19 +02:00
|
|
|
|
2016-06-07 10:50:15 +02:00
|
|
|
function getNormalizedNodeData(reqData) {
|
2014-05-12 20:08:19 +02:00
|
|
|
var node = {};
|
|
|
|
_.each(nodeFields, function (field) {
|
|
|
|
var value = Strings.normalizeString(reqData[field]);
|
|
|
|
if (field === 'mac') {
|
|
|
|
value = Strings.normalizeMac(value);
|
|
|
|
}
|
|
|
|
node[field] = value;
|
|
|
|
});
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
var isValidNode = Validator.forConstraints(Constraints.node);
|
|
|
|
var isValidToken = Validator.forConstraint(Constraints.token);
|
|
|
|
|
|
|
|
return {
|
|
|
|
create: function (req, res) {
|
2016-05-18 22:50:06 +02:00
|
|
|
var data = Resources.getData(req);
|
2014-05-12 20:08:19 +02:00
|
|
|
|
2016-06-07 10:50:15 +02:00
|
|
|
var node = getNormalizedNodeData(data);
|
2014-05-12 20:08:19 +02:00
|
|
|
if (!isValidNode(node)) {
|
2016-05-18 22:50:06 +02:00
|
|
|
return Resources.error(res, {data: 'Invalid node data.', type: ErrorTypes.badRequest});
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return NodeService.createNode(node, function (err, token, node) {
|
|
|
|
if (err) {
|
2016-05-18 22:50:06 +02:00
|
|
|
return Resources.error(res, err);
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
2016-05-18 22:50:06 +02:00
|
|
|
return Resources.success(res, {token: token, node: node});
|
2014-05-12 20:08:19 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
update: function (req, res) {
|
2016-05-18 22:50:06 +02:00
|
|
|
var data = Resources.getData(req);
|
2014-05-12 20:08:19 +02:00
|
|
|
|
|
|
|
var token = Strings.normalizeString(data.token);
|
|
|
|
if (!isValidToken(token)) {
|
2016-05-18 22:50:06 +02:00
|
|
|
return Resources.error(res, {data: 'Invalid token.', type: ErrorTypes.badRequest});
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
|
|
|
|
2016-06-07 10:50:15 +02:00
|
|
|
var node = getNormalizedNodeData(data);
|
2014-05-12 20:08:19 +02:00
|
|
|
if (!isValidNode(node)) {
|
2016-05-18 22:50:06 +02:00
|
|
|
return Resources.error(res, {data: 'Invalid node data.', type: ErrorTypes.badRequest});
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return NodeService.updateNode(token, node, function (err, token, node) {
|
|
|
|
if (err) {
|
2016-05-18 22:50:06 +02:00
|
|
|
return Resources.error(res, err);
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
2016-05-18 22:50:06 +02:00
|
|
|
return Resources.success(res, {token: token, node: node});
|
2014-05-12 20:08:19 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-05-16 18:27:03 +02:00
|
|
|
delete: function (req, res) {
|
2016-05-18 22:50:06 +02:00
|
|
|
var data = Resources.getData(req);
|
2016-05-16 18:27:03 +02:00
|
|
|
|
|
|
|
var token = Strings.normalizeString(data.token);
|
|
|
|
if (!isValidToken(token)) {
|
2016-05-18 22:50:06 +02:00
|
|
|
return Resources.error(res, {data: 'Invalid token.', type: ErrorTypes.badRequest});
|
2016-05-16 18:27:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return NodeService.deleteNode(token, function (err) {
|
|
|
|
if (err) {
|
2016-05-18 22:50:06 +02:00
|
|
|
return Resources.error(res, err);
|
2016-05-16 18:27:03 +02:00
|
|
|
}
|
2016-05-18 22:50:06 +02:00
|
|
|
return Resources.success(res, {});
|
2016-05-16 18:27:03 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2014-05-12 20:08:19 +02:00
|
|
|
get: function (req, res) {
|
2016-05-18 22:50:06 +02:00
|
|
|
var token = Strings.normalizeString(Resources.getData(req).token);
|
2014-05-12 20:08:19 +02:00
|
|
|
if (!isValidToken(token)) {
|
2016-05-18 22:50:06 +02:00
|
|
|
return Resources.error(res, {data: 'Invalid token.', type: ErrorTypes.badRequest});
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
|
|
|
|
2016-05-18 22:50:06 +02:00
|
|
|
return NodeService.getNodeDataByToken(token, function (err, node) {
|
2014-05-12 20:08:19 +02:00
|
|
|
if (err) {
|
2016-05-18 22:50:06 +02:00
|
|
|
return Resources.error(res, err);
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
2016-05-18 22:50:06 +02:00
|
|
|
return Resources.success(res, node);
|
2014-05-12 20:08:19 +02:00
|
|
|
});
|
2016-06-07 00:21:26 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
getAll: function (req, res) {
|
2016-07-23 11:30:41 +02:00
|
|
|
Resources.getValidRestParams('list', 'node', req, function (err, restParams) {
|
2016-06-07 00:21:26 +02:00
|
|
|
if (err) {
|
|
|
|
return Resources.error(res, err);
|
|
|
|
}
|
|
|
|
|
2016-06-07 12:39:52 +02:00
|
|
|
return NodeService.getAllNodes(function (err, nodes) {
|
2016-06-07 10:50:15 +02:00
|
|
|
if (err) {
|
|
|
|
return Resources.error(res, err);
|
|
|
|
}
|
|
|
|
|
2016-06-22 00:06:32 +02:00
|
|
|
var realNodes = _.filter(nodes, function (node) {
|
|
|
|
// We ignore nodes without tokens as those are only manually added ones like gateways.
|
|
|
|
return node.token;
|
|
|
|
});
|
|
|
|
|
2016-06-26 19:09:12 +02:00
|
|
|
var macs = _.map(realNodes, function (node) {
|
|
|
|
return node.mac;
|
|
|
|
});
|
|
|
|
|
|
|
|
MonitoringService.getByMacs(macs, function (err, nodeStateByMac) {
|
|
|
|
if (err) {
|
|
|
|
Logger.tag('nodes', 'admin').error('Error getting nodes by MACs:', err);
|
|
|
|
return Resources.error(res, {data: 'Internal error.', type: ErrorTypes.internalError});
|
|
|
|
}
|
|
|
|
|
|
|
|
var enhancedNodes = _.map(realNodes, function (node) {
|
|
|
|
var nodeState = nodeStateByMac[node.mac];
|
|
|
|
if (nodeState) {
|
|
|
|
return deepExtend({}, node, {
|
2017-08-13 17:22:34 +02:00
|
|
|
site: nodeState.site,
|
2016-06-26 19:09:12 +02:00
|
|
|
onlineState: nodeState.state
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
});
|
|
|
|
|
|
|
|
var filteredNodes = Resources.filter(
|
|
|
|
enhancedNodes,
|
|
|
|
[
|
|
|
|
'hostname',
|
|
|
|
'nickname',
|
|
|
|
'email',
|
|
|
|
'token',
|
|
|
|
'mac',
|
2017-08-13 18:18:40 +02:00
|
|
|
'site',
|
2016-06-26 19:09:12 +02:00
|
|
|
'key',
|
|
|
|
'onlineState'
|
|
|
|
],
|
|
|
|
restParams
|
|
|
|
);
|
|
|
|
var total = filteredNodes.length;
|
|
|
|
|
|
|
|
var sortedNodes = Resources.sort(
|
|
|
|
filteredNodes,
|
|
|
|
[
|
|
|
|
'hostname',
|
|
|
|
'nickname',
|
|
|
|
'email',
|
|
|
|
'token',
|
|
|
|
'mac',
|
|
|
|
'key',
|
2017-08-13 18:18:40 +02:00
|
|
|
'site',
|
2016-06-26 19:09:12 +02:00
|
|
|
'coords',
|
|
|
|
'onlineState',
|
|
|
|
'monitoringState'
|
|
|
|
],
|
|
|
|
restParams
|
|
|
|
);
|
|
|
|
var pageNodes = Resources.getPageEntities(sortedNodes, restParams);
|
|
|
|
|
|
|
|
res.set('X-Total-Count', total);
|
|
|
|
return Resources.success(res, pageNodes);
|
|
|
|
});
|
2016-06-07 10:50:15 +02:00
|
|
|
});
|
2016-06-07 00:21:26 +02:00
|
|
|
});
|
2014-05-12 20:08:19 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|