2014-11-04 12:34:49 +01:00
|
|
|
/**
|
2017-01-11 16:24:33 +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.
|
|
|
|
**/
|
|
|
|
|
2017-08-22 23:26:29 +02:00
|
|
|
var apiUtils = require("../util");
|
2018-04-15 12:18:10 +02:00
|
|
|
|
|
|
|
var runtimeAPI;
|
2014-11-04 12:34:49 +01:00
|
|
|
|
|
|
|
module.exports = {
|
2018-04-15 12:18:10 +02:00
|
|
|
init: function(_runtimeAPI) {
|
|
|
|
runtimeAPI = _runtimeAPI;
|
2015-11-11 23:11:02 +01:00
|
|
|
},
|
2014-11-04 12:34:49 +01:00
|
|
|
getAll: function(req,res) {
|
2018-04-15 12:18:10 +02:00
|
|
|
var opts = {
|
|
|
|
user: req.user
|
|
|
|
}
|
2014-11-04 12:34:49 +01:00
|
|
|
if (req.get("accept") == "application/json") {
|
2018-04-15 12:18:10 +02:00
|
|
|
runtimeAPI.nodes.getNodeList(opts).then(function(list) {
|
|
|
|
res.json(list);
|
|
|
|
})
|
2014-11-04 12:34:49 +01:00
|
|
|
} else {
|
2018-04-15 12:18:10 +02:00
|
|
|
opts.lang = apiUtils.determineLangFromHeaders(req.acceptsLanguages());
|
|
|
|
runtimeAPI.nodes.getNodeConfigs(opts).then(function(configs) {
|
|
|
|
res.send(configs);
|
|
|
|
})
|
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) {
|
2018-04-15 12:18:10 +02:00
|
|
|
var opts = {
|
|
|
|
user: req.user,
|
|
|
|
module: req.body.module,
|
|
|
|
version: req.body.version
|
2014-11-04 12:34:49 +01:00
|
|
|
}
|
2018-04-15 12:18:10 +02:00
|
|
|
runtimeAPI.nodes.addModule(opts).then(function(info) {
|
|
|
|
res.json(info);
|
2018-01-29 10:50:41 +01:00
|
|
|
}).catch(function(err) {
|
2018-04-15 12:18:10 +02:00
|
|
|
apiUtils.rejectHandler(req,res,err);
|
|
|
|
})
|
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) {
|
2018-04-15 12:18:10 +02:00
|
|
|
var opts = {
|
|
|
|
user: req.user,
|
|
|
|
module: req.params[0]
|
2014-11-04 12:34:49 +01:00
|
|
|
}
|
2018-04-24 16:01:49 +02:00
|
|
|
runtimeAPI.nodes.removeModule(opts).then(function() {
|
2018-04-15 12:18:10 +02:00
|
|
|
res.status(204).end();
|
|
|
|
}).catch(function(err) {
|
|
|
|
apiUtils.rejectHandler(req,res,err);
|
|
|
|
})
|
2014-11-04 12:34:49 +01:00
|
|
|
},
|
2014-11-20 16:17:13 +01:00
|
|
|
|
|
|
|
getSet: function(req,res) {
|
2018-04-15 12:18:10 +02:00
|
|
|
var opts = {
|
|
|
|
user: req.user,
|
|
|
|
id: req.params[0] + "/" + req.params[2]
|
|
|
|
}
|
2014-11-20 16:17:13 +01:00
|
|
|
if (req.get("accept") === "application/json") {
|
2018-04-15 12:18:10 +02:00
|
|
|
runtimeAPI.nodes.getNodeInfo(opts).then(function(result) {
|
2015-05-14 15:22:28 +02:00
|
|
|
res.send(result);
|
2018-04-15 12:18:10 +02:00
|
|
|
}).catch(function(err) {
|
|
|
|
apiUtils.rejectHandler(req,res,err);
|
|
|
|
})
|
2014-11-04 12:34:49 +01:00
|
|
|
} else {
|
2018-04-15 12:18:10 +02:00
|
|
|
opts.lang = apiUtils.determineLangFromHeaders(req.acceptsLanguages());
|
|
|
|
runtimeAPI.nodes.getNodeConfig(opts).then(function(result) {
|
2018-04-24 16:01:49 +02:00
|
|
|
return res.send(result);
|
2018-04-15 12:18:10 +02:00
|
|
|
}).catch(function(err) {
|
|
|
|
apiUtils.rejectHandler(req,res,err);
|
|
|
|
})
|
2014-11-04 12:34:49 +01:00
|
|
|
}
|
|
|
|
},
|
2014-11-20 16:17:13 +01:00
|
|
|
|
|
|
|
getModule: function(req,res) {
|
2018-04-15 12:18:10 +02:00
|
|
|
var opts = {
|
|
|
|
user: req.user,
|
|
|
|
module: req.params[0]
|
2014-11-20 16:17:13 +01:00
|
|
|
}
|
2018-04-15 12:18:10 +02:00
|
|
|
runtimeAPI.nodes.getModuleInfo(opts).then(function(result) {
|
|
|
|
res.send(result);
|
|
|
|
}).catch(function(err) {
|
|
|
|
apiUtils.rejectHandler(req,res,err);
|
|
|
|
})
|
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
|
|
|
var body = req.body;
|
|
|
|
if (!body.hasOwnProperty("enabled")) {
|
2018-04-15 12:18:10 +02:00
|
|
|
// log.audit({event: "nodes.module.set",error:"invalid_request"},req);
|
2018-04-24 16:01:49 +02:00
|
|
|
res.status(400).json({code:"invalid_request", message:"Invalid request"});
|
2014-11-04 12:34:49 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-04-15 12:18:10 +02:00
|
|
|
var opts = {
|
|
|
|
user: req.user,
|
|
|
|
id: req.params[0] + "/" + req.params[2],
|
|
|
|
enabled: body.enabled
|
2014-11-20 16:17:13 +01:00
|
|
|
}
|
2018-04-15 12:18:10 +02:00
|
|
|
runtimeAPI.nodes.setNodeSetState(opts).then(function(result) {
|
|
|
|
res.send(result);
|
|
|
|
}).catch(function(err) {
|
|
|
|
apiUtils.rejectHandler(req,res,err);
|
|
|
|
})
|
2014-11-21 11:36:32 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
putModule: function(req,res) {
|
|
|
|
var body = req.body;
|
|
|
|
if (!body.hasOwnProperty("enabled")) {
|
2018-04-15 12:18:10 +02:00
|
|
|
// log.audit({event: "nodes.module.set",error:"invalid_request"},req);
|
2018-04-24 16:01:49 +02:00
|
|
|
res.status(400).json({code:"invalid_request", message:"Invalid request"});
|
2014-11-21 11:36:32 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-04-15 12:18:10 +02:00
|
|
|
var opts = {
|
|
|
|
user: req.user,
|
|
|
|
module: req.params[0],
|
|
|
|
enabled: body.enabled
|
2014-11-21 11:36:32 +01:00
|
|
|
}
|
2018-04-15 12:18:10 +02:00
|
|
|
runtimeAPI.nodes.setModuleState(opts).then(function(result) {
|
|
|
|
res.send(result);
|
|
|
|
}).catch(function(err) {
|
|
|
|
apiUtils.rejectHandler(req,res,err);
|
|
|
|
})
|
|
|
|
|
2017-11-30 14:13:35 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
getIcons: function(req,res) {
|
2018-04-15 12:18:10 +02:00
|
|
|
var opts = {
|
|
|
|
user: req.user
|
2014-12-17 14:35:57 +01:00
|
|
|
}
|
2018-04-15 12:18:10 +02:00
|
|
|
runtimeAPI.nodes.getIconList(opts).then(function(list) {
|
|
|
|
res.json(list);
|
|
|
|
});
|
2014-12-08 17:26:54 +01:00
|
|
|
}
|
2018-04-15 12:18:10 +02:00
|
|
|
};
|