2014-11-04 12:34:49 +01:00
|
|
|
/**
|
2015-02-03 23:02:26 +01:00
|
|
|
* Copyright 2014, 2015 IBM Corp.
|
2014-11-04 12:34:49 +01:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
2015-06-17 22:01:57 +02:00
|
|
|
var when = require("when");
|
2015-11-18 11:35:22 +01:00
|
|
|
var comms = require("./comms");
|
2015-11-22 00:12:39 +01:00
|
|
|
var locales = require("./locales");
|
2015-11-11 23:11:02 +01:00
|
|
|
var redNodes;
|
|
|
|
var log;
|
|
|
|
var i18n;
|
|
|
|
var settings;
|
2014-11-04 12:34:49 +01:00
|
|
|
|
|
|
|
module.exports = {
|
2015-11-11 23:11:02 +01:00
|
|
|
init: function(runtime) {
|
2015-11-24 23:38:42 +01:00
|
|
|
redNodes = runtime.nodes;
|
2015-11-11 23:11:02 +01:00
|
|
|
log = runtime.log;
|
|
|
|
i18n = runtime.i18n;
|
|
|
|
settings = runtime.settings;
|
|
|
|
},
|
2014-11-04 12:34:49 +01:00
|
|
|
getAll: function(req,res) {
|
|
|
|
if (req.get("accept") == "application/json") {
|
2015-05-14 15:22:28 +02:00
|
|
|
log.audit({event: "nodes.list.get"},req);
|
2015-03-30 22:49:20 +02:00
|
|
|
res.json(redNodes.getNodeList());
|
2014-11-04 12:34:49 +01:00
|
|
|
} else {
|
2015-11-22 00:12:39 +01:00
|
|
|
var lang = locales.determineLangFromHeaders(req.acceptsLanguages());
|
2015-05-14 15:22:28 +02:00
|
|
|
log.audit({event: "nodes.configs.get"},req);
|
2015-06-03 22:00:23 +02:00
|
|
|
res.send(redNodes.getNodeConfigs(lang));
|
2014-11-04 12:34:49 +01:00
|
|
|
}
|
|
|
|
},
|
2014-11-21 16:15:24 +01:00
|
|
|
|
2014-11-04 12:34:49 +01:00
|
|
|
post: function(req,res) {
|
|
|
|
if (!settings.available()) {
|
2015-05-14 15:22:28 +02:00
|
|
|
log.audit({event: "nodes.install",error:"settings_unavailable"},req);
|
2015-07-15 23:43:24 +02:00
|
|
|
res.status(400).json({error:"settings_unavailable", message:"Settings unavailable"});
|
2014-11-04 12:34:49 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
var node = req.body;
|
|
|
|
var promise;
|
2015-03-26 12:39:35 +01:00
|
|
|
if (node.module) {
|
2015-03-30 22:49:20 +02:00
|
|
|
var module = redNodes.getModuleInfo(node.module);
|
2014-11-04 12:34:49 +01:00
|
|
|
if (module) {
|
2015-05-14 15:22:28 +02:00
|
|
|
log.audit({event: "nodes.install",module:node.module,error:"module_already_loaded"},req);
|
2015-07-15 23:43:24 +02:00
|
|
|
res.status(400).json({error:"module_already_loaded", message:"Module already loaded"});
|
2014-11-04 12:34:49 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-11-08 15:06:36 +01:00
|
|
|
promise = redNodes.installModule(node.module);
|
2014-11-04 12:34:49 +01:00
|
|
|
} else {
|
2015-05-14 15:22:28 +02:00
|
|
|
log.audit({event: "nodes.install",module:node.module,error:"invalid_request"},req);
|
2015-07-15 23:43:24 +02:00
|
|
|
res.status(400).json({error:"invalid_request", message:"Invalid request"});
|
2014-11-04 12:34:49 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
promise.then(function(info) {
|
2015-11-08 15:06:36 +01:00
|
|
|
comms.publish("node/added",info.nodes,false);
|
2015-05-27 15:11:11 +02:00
|
|
|
if (node.module) {
|
|
|
|
log.audit({event: "nodes.install",module:node.module},req);
|
2015-11-09 17:52:14 +01:00
|
|
|
res.json(info);
|
2015-05-27 15:11:11 +02:00
|
|
|
}
|
2014-11-04 12:34:49 +01:00
|
|
|
}).otherwise(function(err) {
|
|
|
|
if (err.code === 404) {
|
2015-11-09 17:52:14 +01:00
|
|
|
log.audit({event: "nodes.install",module:node.module,error:"not_found"},req);
|
2015-07-15 23:43:24 +02:00
|
|
|
res.status(404).end();
|
2015-05-27 15:11:11 +02:00
|
|
|
} else if (err.code) {
|
|
|
|
log.audit({event: "nodes.install",module:node.module,error:err.code},req);
|
2015-07-15 23:43:24 +02:00
|
|
|
res.status(400).json({error:err.code, message:err.message});
|
|
|
|
} else {
|
2015-11-09 17:52:14 +01:00
|
|
|
log.audit({event: "nodes.install",module:node.module,error:err.code||"unexpected_error",message:err.toString()},req);
|
2015-07-15 23:43:24 +02:00
|
|
|
res.status(400).json({error:err.code||"unexpected_error", message:err.toString()});
|
2014-11-04 12:34:49 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2014-11-20 16:17:13 +01:00
|
|
|
|
2014-11-04 12:34:49 +01:00
|
|
|
delete: function(req,res) {
|
|
|
|
if (!settings.available()) {
|
2015-05-14 15:22:28 +02:00
|
|
|
log.audit({event: "nodes.remove",error:"settings_unavailable"},req);
|
2015-07-15 23:43:24 +02:00
|
|
|
res.status(400).json({error:"settings_unavailable", message:"Settings unavailable"});
|
2014-11-04 12:34:49 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-05-17 22:56:03 +02:00
|
|
|
var mod = req.params[0];
|
2014-11-04 12:34:49 +01:00
|
|
|
try {
|
|
|
|
var promise = null;
|
2015-03-30 22:49:20 +02:00
|
|
|
var module = redNodes.getModuleInfo(mod);
|
2014-11-21 12:25:51 +01:00
|
|
|
if (!module) {
|
2015-05-14 15:22:28 +02:00
|
|
|
log.audit({event: "nodes.remove",module:mod,error:"not_found"},req);
|
2015-07-15 23:43:24 +02:00
|
|
|
res.status(404).end();
|
2014-11-21 12:25:51 +01:00
|
|
|
return;
|
2014-11-04 12:34:49 +01:00
|
|
|
} else {
|
2015-11-08 15:06:36 +01:00
|
|
|
promise = redNodes.uninstallModule(mod);
|
2014-11-04 12:34:49 +01:00
|
|
|
}
|
2014-11-20 16:17:13 +01:00
|
|
|
|
2015-11-08 15:06:36 +01:00
|
|
|
promise.then(function(list) {
|
|
|
|
comms.publish("node/removed",list,false);
|
2015-05-14 15:22:28 +02:00
|
|
|
log.audit({event: "nodes.remove",module:mod},req);
|
2015-07-15 23:43:24 +02:00
|
|
|
res.status(204).end();
|
2014-11-04 12:34:49 +01:00
|
|
|
}).otherwise(function(err) {
|
2015-05-14 15:22:28 +02:00
|
|
|
log.audit({event: "nodes.remove",module:mod,error:err.code||"unexpected_error",message:err.toString()},req);
|
2015-07-15 23:43:24 +02:00
|
|
|
res.status(400).json({error:err.code||"unexpected_error", message:err.toString()});
|
2014-11-04 12:34:49 +01:00
|
|
|
});
|
|
|
|
} catch(err) {
|
2015-05-14 15:22:28 +02:00
|
|
|
log.audit({event: "nodes.remove",module:mod,error:err.code||"unexpected_error",message:err.toString()},req);
|
2015-07-15 23:43:24 +02:00
|
|
|
res.status(400).json({error:err.code||"unexpected_error", message:err.toString()});
|
2014-11-04 12:34:49 +01:00
|
|
|
}
|
|
|
|
},
|
2014-11-20 16:17:13 +01:00
|
|
|
|
|
|
|
getSet: function(req,res) {
|
2016-05-17 22:56:03 +02:00
|
|
|
var id = req.params[0] + "/" + req.params[2];
|
2014-11-04 12:34:49 +01:00
|
|
|
var result = null;
|
2014-11-20 16:17:13 +01:00
|
|
|
if (req.get("accept") === "application/json") {
|
2014-11-04 12:34:49 +01:00
|
|
|
result = redNodes.getNodeInfo(id);
|
2014-12-08 17:26:54 +01:00
|
|
|
if (result) {
|
2015-05-14 15:22:28 +02:00
|
|
|
log.audit({event: "nodes.info.get",id:id},req);
|
2015-03-30 22:49:20 +02:00
|
|
|
delete result.loaded;
|
2015-05-14 15:22:28 +02:00
|
|
|
res.send(result);
|
|
|
|
} else {
|
|
|
|
log.audit({event: "nodes.info.get",id:id,error:"not_found"},req);
|
2015-07-15 23:43:24 +02:00
|
|
|
res.status(404).end();
|
2014-12-08 17:26:54 +01:00
|
|
|
}
|
2014-11-04 12:34:49 +01:00
|
|
|
} else {
|
2015-11-22 00:12:39 +01:00
|
|
|
var lang = locales.determineLangFromHeaders(req.acceptsLanguages());
|
2015-06-03 22:00:23 +02:00
|
|
|
result = redNodes.getNodeConfig(id,lang);
|
2015-05-14 15:22:28 +02:00
|
|
|
if (result) {
|
|
|
|
log.audit({event: "nodes.config.get",id:id},req);
|
|
|
|
res.send(result);
|
|
|
|
} else {
|
|
|
|
log.audit({event: "nodes.config.get",id:id,error:"not_found"},req);
|
2015-07-15 23:43:24 +02:00
|
|
|
res.status(404).end();
|
2015-05-14 15:22:28 +02:00
|
|
|
}
|
2014-11-04 12:34:49 +01:00
|
|
|
}
|
|
|
|
},
|
2014-11-20 16:17:13 +01:00
|
|
|
|
|
|
|
getModule: function(req,res) {
|
2016-05-17 22:56:03 +02:00
|
|
|
var module = req.params[0];
|
2014-11-20 16:17:13 +01:00
|
|
|
var result = redNodes.getModuleInfo(module);
|
|
|
|
if (result) {
|
2015-05-14 15:22:28 +02:00
|
|
|
log.audit({event: "nodes.module.get",module:module},req);
|
2015-03-30 22:49:20 +02:00
|
|
|
res.json(result);
|
2014-11-20 16:17:13 +01:00
|
|
|
} else {
|
2015-05-14 15:22:28 +02:00
|
|
|
log.audit({event: "nodes.module.get",module:module,error:"not_found"},req);
|
2015-07-15 23:43:24 +02:00
|
|
|
res.status(404).end();
|
2014-11-20 16:17:13 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-11-21 11:36:32 +01:00
|
|
|
putSet: function(req,res) {
|
2014-11-04 12:34:49 +01:00
|
|
|
if (!settings.available()) {
|
2015-05-14 15:22:28 +02:00
|
|
|
log.audit({event: "nodes.info.set",error:"settings_unavailable"},req);
|
2015-07-15 23:43:24 +02:00
|
|
|
res.status(400).json({error:"settings_unavailable", message:"Settings unavailable"});
|
2014-11-04 12:34:49 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
var body = req.body;
|
|
|
|
if (!body.hasOwnProperty("enabled")) {
|
2015-05-14 15:22:28 +02:00
|
|
|
log.audit({event: "nodes.info.set",error:"invalid_request"},req);
|
2015-07-15 23:43:24 +02:00
|
|
|
res.status(400).json({error:"invalid_request", message:"Invalid request"});
|
2014-11-04 12:34:49 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-05-17 22:56:03 +02:00
|
|
|
var id = req.params[0] + "/" + req.params[2];
|
2014-11-04 12:34:49 +01:00
|
|
|
try {
|
|
|
|
var node = redNodes.getNodeInfo(id);
|
2014-11-21 11:36:32 +01:00
|
|
|
var info;
|
2014-11-04 12:34:49 +01:00
|
|
|
if (!node) {
|
2015-05-14 15:22:28 +02:00
|
|
|
log.audit({event: "nodes.info.set",id:id,error:"not_found"},req);
|
2015-07-15 23:43:24 +02:00
|
|
|
res.status(404).end();
|
2014-11-04 12:34:49 +01:00
|
|
|
} else {
|
2015-03-30 22:49:20 +02:00
|
|
|
delete node.loaded;
|
2015-05-27 15:11:11 +02:00
|
|
|
putNode(node, body.enabled).then(function(result) {
|
|
|
|
log.audit({event: "nodes.info.set",id:id,enabled:body.enabled},req);
|
|
|
|
res.json(result);
|
|
|
|
});
|
2014-11-04 12:34:49 +01:00
|
|
|
}
|
|
|
|
} catch(err) {
|
2015-05-14 15:22:28 +02:00
|
|
|
log.audit({event: "nodes.info.set",id:id,enabled:body.enabled,error:err.code||"unexpected_error",message:err.toString()},req);
|
2015-07-15 23:43:24 +02:00
|
|
|
res.status(400).json({error:err.code||"unexpected_error", message:err.toString()});
|
2014-11-20 16:17:13 +01:00
|
|
|
}
|
2014-11-21 11:36:32 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
putModule: function(req,res) {
|
|
|
|
if (!settings.available()) {
|
2015-05-14 15:22:28 +02:00
|
|
|
log.audit({event: "nodes.module.set",error:"settings_unavailable"},req);
|
2015-07-15 23:43:24 +02:00
|
|
|
res.status(400).json({error:"settings_unavailable", message:"Settings unavailable"});
|
2014-11-21 11:36:32 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
var body = req.body;
|
|
|
|
if (!body.hasOwnProperty("enabled")) {
|
2015-05-14 15:22:28 +02:00
|
|
|
log.audit({event: "nodes.module.set",error:"invalid_request"},req);
|
2015-07-15 23:43:24 +02:00
|
|
|
res.status(400).json({error:"invalid_request", message:"Invalid request"});
|
2014-11-21 11:36:32 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-05-17 22:56:03 +02:00
|
|
|
var mod = req.params[0];
|
2014-11-21 11:36:32 +01:00
|
|
|
try {
|
|
|
|
var module = redNodes.getModuleInfo(mod);
|
|
|
|
if (!module) {
|
2015-05-14 15:22:28 +02:00
|
|
|
log.audit({event: "nodes.module.set",module:mod,error:"not_found"},req);
|
2015-07-15 23:43:24 +02:00
|
|
|
return res.status(404).end();
|
2014-11-21 11:36:32 +01:00
|
|
|
}
|
2014-12-08 17:26:54 +01:00
|
|
|
|
2014-11-21 11:36:32 +01:00
|
|
|
var nodes = module.nodes;
|
2015-05-27 15:11:11 +02:00
|
|
|
var promises = [];
|
2014-11-21 11:36:32 +01:00
|
|
|
for (var i = 0; i < nodes.length; ++i) {
|
2015-05-27 15:11:11 +02:00
|
|
|
promises.push(putNode(nodes[i],body.enabled));
|
2014-11-21 11:36:32 +01:00
|
|
|
}
|
2015-05-27 15:11:11 +02:00
|
|
|
when.settle(promises).then(function() {
|
|
|
|
res.json(redNodes.getModuleInfo(mod));
|
|
|
|
});
|
2014-11-21 11:36:32 +01:00
|
|
|
} catch(err) {
|
2015-05-14 15:22:28 +02:00
|
|
|
log.audit({event: "nodes.module.set",module:mod,enabled:body.enabled,error:err.code||"unexpected_error",message:err.toString()},req);
|
2015-07-15 23:43:24 +02:00
|
|
|
res.status(400).json({error:err.code||"unexpected_error", message:err.toString()});
|
2014-11-21 11:36:32 +01:00
|
|
|
}
|
2014-11-04 12:34:49 +01:00
|
|
|
}
|
2014-11-20 16:17:13 +01:00
|
|
|
};
|
2014-12-08 17:26:54 +01:00
|
|
|
|
2014-12-17 14:35:57 +01:00
|
|
|
function putNode(node, enabled) {
|
2014-12-08 17:26:54 +01:00
|
|
|
var info;
|
2015-05-27 15:11:11 +02:00
|
|
|
var promise;
|
2014-12-17 14:35:57 +01:00
|
|
|
if (!node.err && node.enabled === enabled) {
|
2015-05-27 15:11:11 +02:00
|
|
|
promise = when.resolve(node);
|
2014-12-08 17:26:54 +01:00
|
|
|
} else {
|
2014-12-17 14:35:57 +01:00
|
|
|
if (enabled) {
|
2015-05-27 15:11:11 +02:00
|
|
|
promise = redNodes.enableNode(node.id);
|
2014-12-17 14:35:57 +01:00
|
|
|
} else {
|
2015-05-27 15:11:11 +02:00
|
|
|
promise = redNodes.disableNode(node.id);
|
2014-12-17 14:35:57 +01:00
|
|
|
}
|
2015-07-15 23:43:24 +02:00
|
|
|
|
2015-05-27 15:11:11 +02:00
|
|
|
return promise.then(function(info) {
|
|
|
|
if (info.enabled === enabled && !info.err) {
|
|
|
|
comms.publish("node/"+(enabled?"enabled":"disabled"),info,false);
|
2015-05-08 15:21:01 +02:00
|
|
|
log.info(" "+log._("api.nodes."+(enabled?"enabled":"disabled")));
|
2015-05-27 15:11:11 +02:00
|
|
|
for (var i=0;i<info.types.length;i++) {
|
|
|
|
log.info(" - "+info.types[i]);
|
|
|
|
}
|
|
|
|
} else if (enabled && info.err) {
|
2015-05-08 15:21:01 +02:00
|
|
|
log.warn(log._("api.nodes.error-enable"));
|
2015-05-27 15:11:11 +02:00
|
|
|
log.warn(" - "+info.name+" : "+info.err);
|
2014-12-17 14:35:57 +01:00
|
|
|
}
|
2015-05-27 15:11:11 +02:00
|
|
|
return info;
|
|
|
|
});
|
2014-12-08 17:26:54 +01:00
|
|
|
}
|
|
|
|
|
2015-05-27 15:11:11 +02:00
|
|
|
return promise;
|
2014-12-08 17:26:54 +01:00
|
|
|
}
|