summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--server/api/backends.js48
-rw-r--r--server/api/ipxe.js (renamed from server/api/ipxe-loader.js)9
-rw-r--r--server/lib/shell.js2
-rw-r--r--server/router.js24
4 files changed, 41 insertions, 42 deletions
diff --git a/server/api/backends.js b/server/api/backends.js
index 3b9551e..e4d3ff6 100644
--- a/server/api/backends.js
+++ b/server/api/backends.js
@@ -3,7 +3,8 @@ const path = require('path')
const ExternalBackends = require(path.join(__appdir, 'lib', 'external-backends', 'external-backends.js'))
var db = require(path.join(__appdir, 'lib', 'sequelize'))
-module.exports = {
+// GET requests
+module.exports.get = {
getCredentialsByType: function (req, res) {
const backendType = req.query.type
const b = new ExternalBackends()
@@ -14,7 +15,7 @@ module.exports = {
res.status(200).send(instance.getCredentials())
},
- getBackendInfoById: function (req, res) {
+ getInfoById: function (req, res) {
const backendId = req.query.id
db.backend.findOne({ where: { id: backendId } }).then(backend => {
const b = {
@@ -27,7 +28,7 @@ module.exports = {
})
},
- getBackendTypes: function (req, res) {
+ getTypes: function (req, res) {
const backends = new ExternalBackends()
var files = backends.getBackends()
@@ -39,7 +40,7 @@ module.exports = {
res.status(200).send(files)
},
- getBackendList: function (req, res) {
+ getList: function (req, res) {
db.backend.findAll({
attributes: ['id', 'name', 'type']
}).then(function (backends) {
@@ -62,10 +63,12 @@ module.exports = {
res.status(200).send({ success: connection.success, msg: connection.msg })
})
})
- },
+ }
+}
- // POST REQUESTS
- saveBackend: function (req, res) {
+// POST requests
+module.exports.post = {
+ save: function (req, res) {
// Save credentials in the db.
const formData = req.body
const credentialString = JSON.stringify(formData.backendCredentials)
@@ -82,7 +85,7 @@ module.exports = {
res.status(200).send('success')
},
- deleteBackends: function (req, res) {
+ delete: function (req, res) {
const backendIds = req.body.id
db.backend.destroy({ where: { id: backendIds } }).then(function () {
@@ -90,15 +93,26 @@ module.exports = {
})
},
+ // If id is set the backend connection in the db is testest.
+ // Else backendinfo has to be set manually, to test unsaved connections.
checkConnection: function (req, res) {
- const type = req.body.backendType
- // const credentials = req.body.backendCredentials
+ const id = req.body.id
- const b = new ExternalBackends()
- const instance = b.getInstance(type)
- instance.checkConnection().then(connection => {
- res.status(200).send({ success: connection.success, msg: connection.msg })
- })
- // TODO: Set credentials
+ var getBackend = null
+ if (id) getBackend = db.backend.findOne({ where: { id: id } })
+ else getBackend = new Promise(resolve => resolve(req.body))
+
+ getBackend.then(backend => {
+ var bCredentials = {
+ type: backend.type,
+ credentials: backend.credentials
+ }
+ // Creating the backend instance and calling the specific checkConnection.
+ const b = new ExternalBackends()
+ const instance = b.getInstance(bCredentials.type)
+ instance.checkConnection(bCredentials).then(connection => {
+ res.status(200).send({ success: connection.success, msg: connection.msg })
+ })
+ })
}
-}
+} \ No newline at end of file
diff --git a/server/api/ipxe-loader.js b/server/api/ipxe.js
index c484578..79c0ad8 100644
--- a/server/api/ipxe-loader.js
+++ b/server/api/ipxe.js
@@ -1,4 +1,11 @@
-module.exports = {
+/* global __appdir */
+var path = require('path')
+var shell = require(path.join(__appdir, 'lib', 'shell'))
+
+module.exports.get = {
+ build: function(req, res) {
+ shell.buildIpxe(req, res)
+ },
loadScript: function (req, res) {
res.setHeader('content-type', 'text/plain')
res.status(200).send(`#!ipxe
diff --git a/server/lib/shell.js b/server/lib/shell.js
index 311af1d..e0782f1 100644
--- a/server/lib/shell.js
+++ b/server/lib/shell.js
@@ -4,7 +4,7 @@ var shell = require('shelljs')
var ipxeGIT = 'git://git.ipxe.org/ipxe.git'
module.exports = {
- buildIPXE: function (req, res) {
+ buildIpxe: function (req, res) {
if (!shell.which('git')) {
return res.status(500).send({ status: 'GIT_MISSING', error_message: 'Please install git on the server.' })
}
diff --git a/server/router.js b/server/router.js
index 828f8b7..7d5613a 100644
--- a/server/router.js
+++ b/server/router.js
@@ -10,7 +10,7 @@ router.post('/signup', auth.signup)
router.post('/logout', auth.logout)
router.post('/changepassword', auth.changePassword)
-// ############ Legacy Code: TODO(Jannik): Rework to api and get/post or delete! ############
+// ############ Legacy Code: TODO(Chris): Rework to api and get/post or delete! ############
// User API
var user = require(path.join(__dirname, 'api', 'user'))
router.get('/user/info', auth.verifyToken, user.info)
@@ -20,28 +20,6 @@ var permissions = require(path.join(__dirname, 'api', 'permissions'))
router.get('/getRolesByUserid', permissions.getRolesByUserid)
router.post('/getRoleById', auth.verifyToken, permissions.getRoleById)
-// Shell API
-var shell = require(path.join(__dirname, 'lib', 'shell'))
-router.get('/shell/buildipxe', shell.buildIPXE)
-
-// Nodemailer API
-var nodemailer = require(path.join(__dirname, 'lib', 'nodemailer'))
-router.get('/mail/send', nodemailer.sendMail)
-
-// External backends API
-var backends = require(path.join(__dirname, 'api', 'backends'))
-router.get('/backends/getCredentialsByType', auth.verifyToken, backends.getCredentialsByType)
-router.get('/backends/getBackendInfoById', auth.verifyToken, backends.getBackendInfoById)
-router.get('/backends/getBackendList', auth.verifyToken, backends.getBackendList)
-router.get('/backends/getBackendTypes', backends.getBackendTypes)
-router.get('/backends/checkConnection', auth.verifyToken, backends.checkConnectionById)
-router.post('/backends/saveBackend', auth.verifyToken, backends.saveBackend)
-router.post('/backends/deleteBackends', auth.verifyToken, backends.deleteBackends)
-router.post('/backends/checkConnection', auth.verifyToken, backends.checkConnection)
-
-// Load ipxe scipts API
-var ipxeloader = require(path.join(__dirname, 'api', 'ipxe-loader'))
-router.get('/ipxe-loader/load-script', ipxeloader.loadScript)
// ############################################################################
// Dynamic API routes