summaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorJannik Schönartz2018-08-05 03:24:49 +0200
committerJannik Schönartz2018-08-05 03:24:49 +0200
commitfb7727d1c0ee0a93376773943436779e27f60ba8 (patch)
tree08192340d691ed63a62c6dcf6044a95afe349061 /server
parentfixed eslint (diff)
downloadbas-fb7727d1c0ee0a93376773943436779e27f60ba8.tar.gz
bas-fb7727d1c0ee0a93376773943436779e27f60ba8.tar.xz
bas-fb7727d1c0ee0a93376773943436779e27f60ba8.zip
[external-backends] Added comments for the API functions and in the external-backend prototype file. Added iDoIT method to get a list of all objects.
Diffstat (limited to 'server')
-rw-r--r--server/api/backends.js105
-rw-r--r--server/lib/external-backends/backends/another-backend.js4
-rw-r--r--server/lib/external-backends/backends/dummy-backend.js2
-rw-r--r--server/lib/external-backends/backends/idoit-backend.js25
-rw-r--r--server/lib/external-backends/external-backends.js54
5 files changed, 172 insertions, 18 deletions
diff --git a/server/api/backends.js b/server/api/backends.js
index ff165a4..5e98a9c 100644
--- a/server/api/backends.js
+++ b/server/api/backends.js
@@ -5,6 +5,12 @@ var db = require(path.join(__appdir, 'lib', 'sequelize'))
// GET requests
module.exports.get = {
+
+ /*
+ * ?id=<BACKEND_ID>
+ *
+ * @return: Returns the credentials structure and fields of a backend type.
+ */
getCredentialsByType: function (req, res) {
const backendType = req.query.type
const b = new ExternalBackends()
@@ -15,6 +21,11 @@ module.exports.get = {
res.status(200).send(instance.getCredentials())
},
+ /*
+ * ?id=<BACKEND_ID>
+ *
+ * @return: Returns the information of a backend.
+ */
getInfoById: function (req, res) {
const backendId = req.query.id
db.backend.findOne({ where: { id: backendId } }).then(backend => {
@@ -28,6 +39,9 @@ module.exports.get = {
})
},
+ /*
+ * @return: Returns a list of all available backend types.
+ */
getTypes: function (req, res) {
const backends = new ExternalBackends()
var files = backends.getBackends()
@@ -40,6 +54,9 @@ module.exports.get = {
res.status(200).send(files)
},
+ /*
+ * @return: Returns a list of all backends saved in the db.
+ */
getList: function (req, res) {
db.backend.findAll({
attributes: ['id', 'name', 'type']
@@ -48,18 +65,49 @@ module.exports.get = {
})
},
+ /*
+ * ?id=<Backend_ID>
+ *
+ * @return: Returns a list with all objects of the backend.
+ */
+ getObjects: function (req, res) {
+ const id = req.query.id
+ db.backend.findOne({ where: { id: id } }).then(backend => {
+ if (backend) {
+ const ba = new ExternalBackends()
+ const instance = ba.getInstance(backend.type)
+ instance.getObjects(backend.credentials).then(result => {
+ res.status(200).send(result)
+ })
+ } else res.status(500).send({ status: 'INVALID_BACKEND_ID', error: 'The provided backend id is invalid.' })
+ })
+ },
+
+ /*
+ * ?id=<Backend_ID>
+ * ?oid=<OBJECT_ID>
+ *
+ * @return: Returns information about a given object and all childs.
+ */
getObject: function (req, res) {
const id = req.query.id
const oid = req.query.oid
db.backend.findOne({ where: { id: id } }).then(backend => {
- const ba = new ExternalBackends()
- const instance = ba.getInstance(backend.type)
- instance.getRoomdata(backend.credentials, oid).then(result => {
- res.status(200).send(result)
- })
+ if (backend) {
+ const ba = new ExternalBackends()
+ const instance = ba.getInstance(backend.type)
+ instance.getObject(backend.credentials, oid).then(result => {
+ res.status(200).send(result)
+ })
+ } else res.status(500).send({ status: 'INVALID_BACKEND_ID', error: 'The provided backend id is invalid.' })
})
},
+ /*
+ * ?id=<Backend_ID>
+ *
+ * @return: Returns a list of all the object types of the given backend. [{id: <id>, title: <title>}, ...]
+ */
getObjectTypes: function (req, res) {
const id = req.query.id
db.backend.findOne({ where: { id: id } }).then(backend => {
@@ -71,6 +119,11 @@ module.exports.get = {
})
},
+ /*
+ * ?id=<Backend_ID>
+ *
+ * @return: Returns the sync settings saved in the db.
+ */
getSyncSettings: function (req, res) {
const id = req.query.id
var types = {}
@@ -82,6 +135,11 @@ module.exports.get = {
})
},
+ /*
+ * ?id=<Backend_ID>
+ *
+ * @return: Returns a list of sync types the backend supports.
+ */
getSyncTypes: function (req, res) {
const id = req.query.id
db.backend.findOne({ where: { id: id } }).then(backend => {
@@ -94,6 +152,15 @@ module.exports.get = {
// POST requests
module.exports.post = {
+
+ /*
+ * id: <BACKEND_ID>
+ * name: <BACKEND_NAME>
+ * type: <BACKEND_TYPE>
+ * credentials: <BACKEND_CREDENTIALS>
+ *
+ * Creates or updates the backend.
+ */
save: function (req, res) {
// Save credentials in the db.
const backend = req.body
@@ -111,6 +178,11 @@ module.exports.post = {
res.status(200).send('success')
},
+ /*
+ * id: <BACKEND_ID>
+ *
+ * Deletes the backend to the given id.
+ */
delete: function (req, res) {
const backendIds = req.body.id
@@ -119,8 +191,16 @@ module.exports.post = {
})
},
- // If id is set the backend connection in the db is testest.
- // Else backendinfo has to be set manually, to test unsaved connections.
+ /*
+ * id: <BACKEND_ID>
+ * <OR>
+ * name: <BACKEND_NAME>
+ * type: <BACKEND_TYPE>
+ * credentials: <BACKEND_CREDENTIALS>
+ *
+ * If the id is set, the backend in the db ist testet.
+ * Else the backend is postet in the request.
+ */
checkConnection: function (req, res) {
const id = req.body.id
@@ -133,12 +213,19 @@ module.exports.post = {
const b = new ExternalBackends()
const instance = b.getInstance(backend.type)
instance.checkConnection(backend.credentials).then(connection => {
- res.status(200).send({ success: connection.success, msg: connection.msg })
+ res.status(200).send({ success: connection.success, error: connection.error })
})
})
},
- // Saves the group / clients assigned object types in the database and sync type.
+ /*
+ * id: <BACKEND_ID>
+ * groups: <JSON_OF_ASSIGNED_GROUPS> (list of backend types)
+ * clients: <JSON_OF_ASSIGNED_CLIENTS> (list of backend types)
+ * sync: <SYNC_OPTION>
+ *
+ * Saves the group / clients assigned object types in the database and sync type.
+ */
saveSyncSettings: function (req, res) {
const id = req.body.id
const groups = JSON.stringify(req.body.groups)
diff --git a/server/lib/external-backends/backends/another-backend.js b/server/lib/external-backends/backends/another-backend.js
index 8f6b717..fd867a2 100644
--- a/server/lib/external-backends/backends/another-backend.js
+++ b/server/lib/external-backends/backends/another-backend.js
@@ -5,10 +5,6 @@ class AnotherBackend extends ExternalBackends {
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 (backend) {
- return { success: true }
- }
-
getSyncTypes () {
return ['THERE', 'ARE', 'NO', 'SYNC', 'TYPES']
}
diff --git a/server/lib/external-backends/backends/dummy-backend.js b/server/lib/external-backends/backends/dummy-backend.js
index a1704e3..f445a9e 100644
--- a/server/lib/external-backends/backends/dummy-backend.js
+++ b/server/lib/external-backends/backends/dummy-backend.js
@@ -6,7 +6,7 @@ class DummyBackend extends ExternalBackends {
}
async checkConnection (backend) {
- return { success: false, msg: 'This is a test where success was intentionally set to false!' }
+ return { success: false, error: 'This is a test where success was intentionally set to false!' }
}
}
diff --git a/server/lib/external-backends/backends/idoit-backend.js b/server/lib/external-backends/backends/idoit-backend.js
index b282c6b..3645710 100644
--- a/server/lib/external-backends/backends/idoit-backend.js
+++ b/server/lib/external-backends/backends/idoit-backend.js
@@ -78,7 +78,30 @@ class IdoitBackend extends ExternalBackends {
return mapped
}
- async getRoomdata (credentials, oid) {
+ async getObjects (credentials) {
+ var c = this.mapCredentials(credentials)
+ var login = await this.getSession(c)
+ var sid = login.data.result['session-id']
+
+ // Headers
+ var headers = {
+ 'X-RPC-Auth-Session': sid
+ }
+
+ // Params
+ var params = {
+ 'apikey': c.apikey,
+ 'language': 'en'
+ }
+
+ var result = {}
+ result.objects = await this.axiosRequest(c.url, 'cmdb.objects', params, headers)
+ result.objects = result.objects.data.result
+
+ return result
+ }
+
+ async getObject (credentials, oid) {
var c = this.mapCredentials(credentials)
var login = await this.getSession(c)
var sid = login.data.result['session-id']
diff --git a/server/lib/external-backends/external-backends.js b/server/lib/external-backends/external-backends.js
index be32c13..e01a15c 100644
--- a/server/lib/external-backends/external-backends.js
+++ b/server/lib/external-backends/external-backends.js
@@ -3,16 +3,34 @@ const fs = require('fs')
const path = require('path')
class ExternalBackends {
+ /*
+ * Returns a list of all backends which have a .js file.
+ */
getBackends () {
var files = fs.readdirSync(path.join(__appdir, 'lib', 'external-backends', 'backends'))
return files
}
+ /*
+ * Returns the credential structure / fields, defined in the backends.
+ *
+ * If type === switch there can be child elements in the elements attribute.
+ * Those elements are only shown, if the switch is set to true.
+ * [{ type: 'text', id: 1, name: '<NAME>', icon: '<ICON_NAME>' },
+ * { type: 'password', id: 2, name: '<NAME>', icon: '<ICON_NAME>' },
+ * { type: 'switch', id: 3, name: '<NAME>', icon: '<ICON_NAME>' , elements: [ { type: ... } ] },
+ * { type: 'select', id: 4, name: '<NAME>', icon: '<ICON_NAME>' }, ...]
+ */
getCredentials () {
console.log('If this method gets called the backend class has NOT IMPLEMENTED the getCredentials method!')
return null
}
+ /*
+ * type: <BACKEND_TYPE>
+ *
+ * Returns the instance of a given backend type.
+ */
getInstance (type) {
const bList = this.getBackends()
const bType = type + '-backend.js'
@@ -27,16 +45,46 @@ class ExternalBackends {
return backend
}
+ /* Returns an empty array [] if the backends doesn't have the function implemented.
+ *
+ * return: ['<SYNC_TYPE>', ...]
+ */
getSyncTypes () {
return []
}
+ /*
+ * credendtials: <BACKEND_CREDENTIALS>
+ *
+ * Returns a list of all objects in the backend.
+ */
+ async getObjects (credendtials) {
+ return { status: 'NOT_IMPLEMENTED_EXCEPTION', error: 'The provided backend does not have a getObjects method' }
+ }
+
+ /*
+ * credendtials: <BACKEND_CREDENTIALS>
+ * oid: <OBJECT_ID>
+ *
+ * Call the API of the backend and returns the information to the object including a list of childs.
+ */
+ async getObject (credentials, oid) {
+ return { status: 'NOT_IMPLEMENTED_EXCEPTION', error: 'The provided backend does not have a getObject method' }
+ }
+
+ /*
+ * Checks the connection of a given backend.
+ *
+ * return: { success: <boolean>, status: '<STATUS_CODE_IF_ERROR>', error: '<ERROR_MESSAGE>' }
+ */
async checkConnection (backend) {
- console.log('If this method gets called the backend class has NOT IMPLEMENTED the checkConnection method!')
- return null
+ return { success: false, status: 'NOT_IMPLEMENTED_EXCEPTION', error: 'The provided backend does not have a checkConnection method' }
}
- // Return an empty array [] if the backends doesn't have such a function.
+ /* Returns an empty array [] if the backends doesn't have such a function.
+ *
+ * return: [{id: <id>, title: <title>}, ...]
+ */
async getObjectTypes (credentials) {
return []
}