From cee3b5539762d12ad8b22e90404b76219886af44 Mon Sep 17 00:00:00 2001 From: Jannik Schönartz Date: Thu, 2 Aug 2018 21:17:50 +0000 Subject: [server/external-backends] Added sync settings for the backends. Method for getting the backend oject types and the client / group mapping saved in the db. --- server/api/backends.js | 34 ++++++++++++++++++++++ .../external-backends/backends/idoit-backend.js | 32 ++++++++++++++++++-- server/lib/external-backends/external-backends.js | 5 ++++ server/migrations/20180715193710-create-backend.js | 17 ++++++++++- server/models/backend.js | 19 +++++++++++- 5 files changed, 102 insertions(+), 5 deletions(-) (limited to 'server') diff --git a/server/api/backends.js b/server/api/backends.js index 3d7c36e..2b82cab 100644 --- a/server/api/backends.js +++ b/server/api/backends.js @@ -58,6 +58,28 @@ module.exports.get = { res.status(200).send(result) }) }) + }, + + getObjectTypes: function (req, res) { + const id = req.query.id + db.backend.findOne({ where: { id: id } }).then(backend => { + const ba = new ExternalBackends() + const instance = ba.getInstance(backend.type) + console.log(backend.type) + instance.getObjectTypes(backend.credentials).then(result => { + res.status(200).send(result) + }) + }) + }, + + getObjectTypesById: function (req, res) { + const id = req.query.id + var types = {} + db.backend.findOne({ where: { id: id } }).then(backend => { + types.groups = JSON.parse(backend.groups) + types.clients = JSON.parse(backend.clients) + res.status(200).send(types) + }) } } @@ -105,5 +127,17 @@ module.exports.post = { res.status(200).send({ success: connection.success, msg: connection.msg }) }) }) + }, + + // Saves the group / clients assigned object types in the database. + saveObjectTypes: function (req, res) { + const id = req.body.id + const groups = JSON.stringify(req.body.groups) + const clients = JSON.stringify(req.body.clients) + db.backend.findOne({ where: { id: id } }).then(backend => { + db.backend.update({ groups: groups, clients: clients }, { where: { id: id } }).then(() => { + res.status(200).send() + }) + }) } } diff --git a/server/lib/external-backends/backends/idoit-backend.js b/server/lib/external-backends/backends/idoit-backend.js index cdd4dde..de41481 100644 --- a/server/lib/external-backends/backends/idoit-backend.js +++ b/server/lib/external-backends/backends/idoit-backend.js @@ -26,6 +26,35 @@ class IdoitBackend extends ExternalBackends { return this.getSession(c) } + // Return the list of object types created in iDoIT. + async getObjectTypes (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.types = await this.axiosRequest(c.url, 'cmdb.object_types', params, headers) + result.types = result.types.data.result + + var types = [] + result.types.forEach(type => { + types.push({ id: type.id, title: type.title }) + }) + + return types + } + // Optional functions e.g. helperfunctions or testing stuff. // Helper function, to map the array of credential objects into a single js object. @@ -68,9 +97,6 @@ class IdoitBackend extends ExternalBackends { result.object = result.object.data.result result.childs = result.childs.data.result - console.log(result) - // Make an request to read the object - //return this.axiosRequest(c.url, 'cmdb.location_tree', params, headers) return result } diff --git a/server/lib/external-backends/external-backends.js b/server/lib/external-backends/external-backends.js index 7d306b3..20f8ce6 100644 --- a/server/lib/external-backends/external-backends.js +++ b/server/lib/external-backends/external-backends.js @@ -31,6 +31,11 @@ class ExternalBackends { console.log('If this method gets called the backend class has NOT IMPLEMENTED the checkConnection method!') return null } + + // Return an empty array [] if the backends doesn't have such a function. + async getObjectTypes (credentials) { + return [] + } } module.exports = ExternalBackends diff --git a/server/migrations/20180715193710-create-backend.js b/server/migrations/20180715193710-create-backend.js index e89362e..98f5476 100644 --- a/server/migrations/20180715193710-create-backend.js +++ b/server/migrations/20180715193710-create-backend.js @@ -15,7 +15,22 @@ module.exports = { type: Sequelize.STRING }, credentials: { - type: Sequelize.STRING(2048) + allowNull: false, + type: Sequelize.STRING(2048), + defaultValue: '[]' + }, + groups: { + allowNull: false, + type: Sequelize.STRING(4096), + defaultValue: '[]' + }, + clients: { + allowNull: false, + type: Sequelize.STRING(4096), + defaultValue: '[]' + }, + sync: { + type: Sequelize.STRING(1024) } }) }, diff --git a/server/models/backend.js b/server/models/backend.js index 4ed5fce..62b936a 100644 --- a/server/models/backend.js +++ b/server/models/backend.js @@ -9,7 +9,24 @@ module.exports = (sequelize, DataTypes) => { }, name: DataTypes.STRING, type: DataTypes.STRING, - credentials: DataTypes.STRING(2048) + credentials: { + allowNull: false, + type: DataTypes.STRING(2048), + defaultValue: '[]' + }, + groups: { + allowNull: false, + type: DataTypes.STRING(4096), + defaultValue: '[]' + }, + clients: { + allowNull: false, + type: DataTypes.STRING(4096), + defaultValue: '[]' + }, + sync: { + type: DataTypes.STRING(1024) + } }, { timestamps: false }) -- cgit v1.2.3-55-g7522