From dfbc9e183af3fbd28dc72f386b97f8df7b3de7c1 Mon Sep 17 00:00:00 2001 From: Jannik Schönartz Date: Thu, 26 Jul 2018 13:09:54 +0000 Subject: [server/backends] Added iDoII Backend. Implemented a checkConnection method for each individual backend. Switches in backends can now have recursive elements. They are only shown if the switch is set to true. --- server/api/backends.js | 40 +++++++++++++++------- .../external-backends/backends/another-backend.js | 4 +++ .../external-backends/backends/dummy-backend.js | 4 +++ .../external-backends/backends/idoit-backend.js | 27 +++++++++++++++ .../external-backends/backends/template-backend.js | 14 ++++++++ server/lib/external-backends/external-backends.js | 8 ++++- server/router.js | 3 +- 7 files changed, 85 insertions(+), 15 deletions(-) create mode 100644 server/lib/external-backends/backends/idoit-backend.js (limited to 'server') diff --git a/server/api/backends.js b/server/api/backends.js index b744bd2..a5d1233 100644 --- a/server/api/backends.js +++ b/server/api/backends.js @@ -47,19 +47,21 @@ module.exports = { }) }, - checkConnection: function (req, res) { - var x = Math.floor(Math.random() * Math.floor(2)) - var y = '' - if (x === 0) { - y = 'success' - } else { - y = 'error' - } - var w = 'Oh noes! An error occurred.' - var z = Math.floor(Math.random() * Math.floor(5000)) - - - setTimeout(function() { res.status(200).send({ status: y, msg: w }) }, 0) + checkConnectionById: function (req, res) { + const backendId = req.query.id + db.backend.findOne({ where: { id: backendId } }).then(backend => { + const bCredentials = { + backendId: backendId, + backendName: backend.name, + backendType: backend.type, + backendCredentials: backend.credentials + } + const b = new ExternalBackends() + const instance = b.getInstance(bCredentials.backendType) + instance.checkConnection().then(connection => { + res.status(200).send({ success: connection.success, msg: connection.msg }) + }) + }) }, // POST REQUESTS @@ -86,5 +88,17 @@ module.exports = { db.backend.destroy({ where: { id: backendIds } }).then(function () { res.status(200).send('success') }) + }, + + checkConnection: function (req, res) { + const type = req.body.backendType + // const credentials = req.body.backendCredentials + + 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 } } diff --git a/server/lib/external-backends/backends/another-backend.js b/server/lib/external-backends/backends/another-backend.js index eb329ae..90b8f8b 100644 --- a/server/lib/external-backends/backends/another-backend.js +++ b/server/lib/external-backends/backends/another-backend.js @@ -4,6 +4,10 @@ class AnotherBackend extends ExternalBackends { getCredentials () { return [{ type: 'text', id: 1, name: 'text 1' }, { type: 'text', id: 2, name: 'text 2' }, { type: 'password', id: 3, name: 'password 1', show: false }, { type: 'password', id: 4, name: 'password 2', show: true }, { type: 'password', id: 5, name: 'password 3', show: false }] } + + async checkConnection () { + return { success: true } + } } module.exports = AnotherBackend diff --git a/server/lib/external-backends/backends/dummy-backend.js b/server/lib/external-backends/backends/dummy-backend.js index 6052953..99d27c1 100644 --- a/server/lib/external-backends/backends/dummy-backend.js +++ b/server/lib/external-backends/backends/dummy-backend.js @@ -4,6 +4,10 @@ class DummyBackend extends ExternalBackends { getCredentials () { return [{ type: 'switch', id: 1, name: 'switch 1', value: false }, { type: 'switch', id: 2, name: 'switch 2', value: false }, { type: 'switch', id: 3, name: 'switch 3', value: true }, { type: 'select', id: 4, name: 'selection 1', items: ['wasd', 'asdf', 'qwertz'] }, { type: 'select', id: 5, name: 'selection 2', items: ['1', '2', '3'] }] } + + async checkConnection () { + return { success: false, msg: 'This is a test where success was intentionally set to false!' } + } } module.exports = DummyBackend diff --git a/server/lib/external-backends/backends/idoit-backend.js b/server/lib/external-backends/backends/idoit-backend.js new file mode 100644 index 0000000..2520b07 --- /dev/null +++ b/server/lib/external-backends/backends/idoit-backend.js @@ -0,0 +1,27 @@ +var ExternalBackends = require('../external-backends.js') + +class IdoitBackend extends ExternalBackends { + getCredentials () { + // I do it only needs the API-key. + return [ + { type: 'text', id: 1, name: 'API url', icon: 'link' }, + { type: 'password', id: 2, name: 'API token', icon: 'vpn_key', show: false }, + { type: 'switch', + id: 3, + name: 'Login', + icon: 'lock_open', + elements: [ + { type: 'text', id: 4, name: 'username', icon: 'person_outline' }, + { type: 'password', id: 5, name: 'password', icon: 'lock', show: false } + ] + } + ] + } + + async checkConnection () { + // Make a simple login and check if the connection works. + return { success: false, msg: 'TODO: IMPLEMENT' } + } +} + +module.exports = IdoitBackend diff --git a/server/lib/external-backends/backends/template-backend.js b/server/lib/external-backends/backends/template-backend.js index 19f1d76..ceab46a 100644 --- a/server/lib/external-backends/backends/template-backend.js +++ b/server/lib/external-backends/backends/template-backend.js @@ -4,6 +4,20 @@ class TemplateBackend extends ExternalBackends { getCredentials () { return [{ type: 'text', id: 1, name: 'text test', icon: 'bug_report' }, { type: 'password', id: 2, name: 'password test', show: true }, { type: 'password', id: 3, name: 'password test nr2', show: false }, { type: 'switch', id: 4, name: 'bool test', value: false }, { type: 'select', id: 5, name: 'selection test', items: ['wasd', 'asdf', 'qwertz'] }] } + + async checkConnection () { + var result = await x() + console.log(result) + return result + } +} + +function x () { + return new Promise(resolve => { + setTimeout(() => { + resolve({ success: true }) + }, 5000) + }) } module.exports = TemplateBackend diff --git a/server/lib/external-backends/external-backends.js b/server/lib/external-backends/external-backends.js index 0bf891e..a1264a0 100644 --- a/server/lib/external-backends/external-backends.js +++ b/server/lib/external-backends/external-backends.js @@ -9,7 +9,8 @@ class ExternalBackends { } getCredentials () { - return 'If this method gets called the backend class has NOT IMPLEMENTED the getCredentials method!' + console.log('If this method gets called the backend class has NOT IMPLEMENTED the getCredentials method!') + return null } getInstance (type) { @@ -25,6 +26,11 @@ class ExternalBackends { const backend = new (require(path.join(__appdir, 'lib', 'external-backends', 'backends', bType)))() return backend } + + async checkConnection () { + console.log('If this method gets called the backend class has NOT IMPLEMENTED the checkConnection method!') + return null + } } module.exports = ExternalBackends diff --git a/server/router.js b/server/router.js index bde35a8..ab67f19 100644 --- a/server/router.js +++ b/server/router.js @@ -43,9 +43,10 @@ router.get('/backends/getCredentialsByType', auth.verifyToken, backends.getCrede 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.checkConnection) +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')) -- cgit v1.2.3-55-g7522