1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00
node-red/red/api/admin/nodes.js

244 lines
9.6 KiB
JavaScript
Raw Permalink Normal View History

2014-11-04 12:34:49 +01:00
/**
* Copyright JS Foundation and other contributors, http://js.foundation
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.
**/
var when = require("when");
var apiUtils = require("../util");
var redNodes;
var log;
var settings;
2014-11-04 12:34:49 +01:00
module.exports = {
init: function(runtime) {
2015-11-24 23:38:42 +01:00
redNodes = runtime.nodes;
log = runtime.log;
settings = runtime.settings;
},
2014-11-04 12:34:49 +01:00
getAll: function(req,res) {
if (req.get("accept") == "application/json") {
log.audit({event: "nodes.list.get"},req);
res.json(redNodes.getNodeList());
2014-11-04 12:34:49 +01:00
} else {
var lang = apiUtils.determineLangFromHeaders(req.acceptsLanguages());
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-04 12:34:49 +01:00
post: function(req,res) {
if (!settings.available()) {
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;
var isUpgrade = false;
if (node.module) {
var module = redNodes.getModuleInfo(node.module);
2014-11-04 12:34:49 +01:00
if (module) {
if (!node.version || module.version === node.version) {
log.audit({event: "nodes.install",module:node.module, version:node.version, error:"module_already_loaded"},req);
res.status(400).json({error:"module_already_loaded", message:"Module already loaded"});
return;
}
if (!module.local) {
log.audit({event: "nodes.install",module:node.module, version:node.version, error:"module_not_local"},req);
res.status(400).json({error:"module_not_local", message:"Module not locally installed"});
return;
}
isUpgrade = true;
2014-11-04 12:34:49 +01:00
}
promise = redNodes.installModule(node.module,node.version);
2014-11-04 12:34:49 +01:00
} else {
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) {
if (node.module) {
log.audit({event: "nodes.install",module:node.module,version:node.version},req);
res.json(info);
}
}).catch(function(err) {
2014-11-04 12:34:49 +01:00
if (err.code === 404) {
log.audit({event: "nodes.install",module:node.module,version:node.version,error:"not_found"},req);
2015-07-15 23:43:24 +02:00
res.status(404).end();
} else if (err.code) {
log.audit({event: "nodes.install",module:node.module,version:node.version,error:err.code},req);
2015-07-15 23:43:24 +02:00
res.status(400).json({error:err.code, message:err.message});
} else {
log.audit({event: "nodes.install",module:node.module,version:node.version,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-04 12:34:49 +01:00
delete: function(req,res) {
if (!settings.available()) {
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;
var module = redNodes.getModuleInfo(mod);
2014-11-21 12:25:51 +01:00
if (!module) {
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 {
promise = redNodes.uninstallModule(mod);
2014-11-04 12:34:49 +01:00
}
promise.then(function(list) {
log.audit({event: "nodes.remove",module:mod},req);
2015-07-15 23:43:24 +02:00
res.status(204).end();
}).catch(function(err) {
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) {
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
}
},
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;
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) {
log.audit({event: "nodes.info.get",id:id},req);
delete result.loaded;
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 {
var lang = apiUtils.determineLangFromHeaders(req.acceptsLanguages());
2015-06-03 22:00:23 +02:00
result = redNodes.getNodeConfig(id,lang);
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();
}
2014-11-04 12:34:49 +01:00
}
},
getModule: function(req,res) {
2016-05-17 22:56:03 +02:00
var module = req.params[0];
var result = redNodes.getModuleInfo(module);
if (result) {
log.audit({event: "nodes.module.get",module:module},req);
res.json(result);
} else {
log.audit({event: "nodes.module.get",module:module,error:"not_found"},req);
2015-07-15 23:43:24 +02:00
res.status(404).end();
}
},
putSet: function(req,res) {
2014-11-04 12:34:49 +01:00
if (!settings.available()) {
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")) {
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);
var info;
2014-11-04 12:34:49 +01:00
if (!node) {
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 {
delete node.loaded;
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) {
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()});
}
},
putModule: function(req,res) {
if (!settings.available()) {
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"});
return;
}
var body = req.body;
if (!body.hasOwnProperty("enabled")) {
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"});
return;
}
2016-05-17 22:56:03 +02:00
var mod = req.params[0];
try {
var module = redNodes.getModuleInfo(mod);
if (!module) {
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-12-08 17:26:54 +01:00
var nodes = module.nodes;
var promises = [];
for (var i = 0; i < nodes.length; ++i) {
promises.push(putNode(nodes[i],body.enabled));
}
when.settle(promises).then(function() {
res.json(redNodes.getModuleInfo(mod));
});
} catch(err) {
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()});
}
},
getIcons: function(req,res) {
log.audit({event: "nodes.icons.get"},req);
res.json(redNodes.getNodeIcons());
2014-11-04 12:34:49 +01:00
}
};
2014-12-08 17:26:54 +01:00
function putNode(node, enabled) {
2014-12-08 17:26:54 +01:00
var info;
var promise;
if (!node.err && node.enabled === enabled) {
promise = when.resolve(node);
2014-12-08 17:26:54 +01:00
} else {
if (enabled) {
promise = redNodes.enableNode(node.id);
} else {
promise = redNodes.disableNode(node.id);
}
2014-12-08 17:26:54 +01:00
}
return promise;
2014-12-08 17:26:54 +01:00
}