summaryrefslogtreecommitdiffstats
path: root/server/lib/external-backends
diff options
context:
space:
mode:
authorJannik Schönartz2018-08-06 03:04:54 +0200
committerJannik Schönartz2018-08-06 03:04:54 +0200
commit311a686f3672a00c8def9190c874d6f83a006e35 (patch)
tree29d05513d8c1e96dca50aae4445845177bbf5d3f /server/lib/external-backends
parent[webapp/searchtable] bugfix (diff)
downloadbas-311a686f3672a00c8def9190c874d6f83a006e35.tar.gz
bas-311a686f3672a00c8def9190c874d6f83a006e35.tar.xz
bas-311a686f3672a00c8def9190c874d6f83a006e35.zip
[server/external-backends] Implemented import Objects from iDoIT
renamed external-backends.js in index.js so its return when requireing the folder Added new tables for the external id mapping for clients / groups iDoIT method for importing Objects and adding them in the db with all neccessary constraints
Diffstat (limited to 'server/lib/external-backends')
-rw-r--r--server/lib/external-backends/backends/another-backend.js4
-rw-r--r--server/lib/external-backends/backends/dummy-backend.js4
-rw-r--r--server/lib/external-backends/backends/idoit-backend.js120
-rw-r--r--server/lib/external-backends/backends/template-backend.js4
-rw-r--r--server/lib/external-backends/index.js (renamed from server/lib/external-backends/external-backends.js)11
5 files changed, 114 insertions, 29 deletions
diff --git a/server/lib/external-backends/backends/another-backend.js b/server/lib/external-backends/backends/another-backend.js
index fd867a2..0d60259 100644
--- a/server/lib/external-backends/backends/another-backend.js
+++ b/server/lib/external-backends/backends/another-backend.js
@@ -1,4 +1,6 @@
-var ExternalBackends = require('../external-backends.js')
+/* global __appdir */
+const path = require('path')
+const ExternalBackends = require(path.join(__appdir, 'lib', 'external-backends'))
class AnotherBackend extends ExternalBackends {
getCredentials () {
diff --git a/server/lib/external-backends/backends/dummy-backend.js b/server/lib/external-backends/backends/dummy-backend.js
index f445a9e..c157e0a 100644
--- a/server/lib/external-backends/backends/dummy-backend.js
+++ b/server/lib/external-backends/backends/dummy-backend.js
@@ -1,4 +1,6 @@
-var ExternalBackends = require('../external-backends.js')
+/* global __appdir */
+const path = require('path')
+const ExternalBackends = require(path.join(__appdir, 'lib', 'external-backends'))
class DummyBackend extends ExternalBackends {
getCredentials () {
diff --git a/server/lib/external-backends/backends/idoit-backend.js b/server/lib/external-backends/backends/idoit-backend.js
index 3645710..5d94e41 100644
--- a/server/lib/external-backends/backends/idoit-backend.js
+++ b/server/lib/external-backends/backends/idoit-backend.js
@@ -1,10 +1,16 @@
-var ExternalBackends = require('../external-backends.js')
+/* global __appdir */
+const path = require('path')
+const ExternalBackends = require(path.join(__appdir, 'lib', 'external-backends'))
var axios = require('axios')
class IdoitBackend extends ExternalBackends {
- // Needed functions
+ // ############################################################################
+ // ######################## needed functions #################################
+
+ /*
+ * Returns the credential structure / fields, defined in the backends.
+ */
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 },
@@ -21,6 +27,12 @@ class IdoitBackend extends ExternalBackends {
]
}
+ /*
+ * Checks the connection of a given backend.
+ * idoIT: Checks if we can get a valid session id. If so the connection must be successfull.
+ *
+ * return: { success: <boolean>, status: '<STATUS_CODE_IF_ERROR>', error: '<ERROR_MESSAGE>' }
+ */
async checkConnection (credentials) {
var c = this.mapCredentials(credentials)
return this.getSession(c)
@@ -59,25 +71,6 @@ class IdoitBackend extends ExternalBackends {
return ['None', 'Two-Way', 'Upload Only', 'Upload Then Delete', 'Upload Mirror', 'Download Only', 'Download Then Delete', 'Download Mirror']
}
- // Optional functions e.g. helperfunctions or testing stuff.
-
- // Helper function, to map the array of credential objects into a single js object.
- mapCredentials (credentials) {
- const c = JSON.parse(credentials)
- const login = c.find(x => x.id === 3)
- var mapped = {
- url: c.find(x => x.id === 1).value,
- apikey: c.find(x => x.id === 2).value,
- login: login.value
- }
-
- if (mapped.login) {
- mapped.username = login.elements.find(x => x.id === 4).value
- mapped.password = login.elements.find(x => x.id === 5).value
- }
- return mapped
- }
-
async getObjects (credentials) {
var c = this.mapCredentials(credentials)
var login = await this.getSession(c)
@@ -123,10 +116,86 @@ class IdoitBackend extends ExternalBackends {
result.childs = await this.axiosRequest(c.url, 'cmdb.location_tree', params, headers)
result.object = result.object.data.result
result.childs = result.childs.data.result
-
return result
}
+ // Function to use the same session for multiple requests
+ getDataTree (credentials, array) {
+ var c = this.mapCredentials(credentials)
+
+ // LOGIN
+ // Open and get a session
+ const body = {
+ 'version': '2.0',
+ 'method': 'idoit.login',
+ 'params': {
+ 'apikey': c.apikey,
+ 'language': 'en'
+ },
+ 'id': 1
+ }
+
+ // Headers
+ var headers = {}
+ // Optional credentials
+ if (c.login) {
+ headers['X-RPC-Auth-Username'] = c.username
+ headers['X-RPC-Auth-Password'] = c.password
+ }
+
+ var config = {
+ timeout: 90000,
+ headers: headers
+ }
+ // Make a login request and see if we are authenticated.
+ return axios.post(c.url, body, config).then(log => {
+ var sid = log.data.result['session-id']
+ // Headers
+ config.headers = { 'X-RPC-Auth-Session': sid, 'Content-Type': 'application/json' }
+ body.method = 'cmdb.location_tree'
+ var promises = []
+
+ array.forEach(element => {
+ const bod = {
+ 'version': '2.0',
+ 'method': 'cmdb.location_tree',
+ 'params': {
+ 'id': element.eid,
+ 'apikey': c.apikey,
+ 'language': 'en'
+ },
+ 'id': 1
+ }
+ promises.push(axios.post(c.url, bod, config).then(result => {
+ return { gid: element.gid, childs: result.data.result }
+ }))
+ })
+ return Promise.all(promises).then(ret => {
+ return ret
+ })
+ })
+ }
+
+ // ############################################################################
+ // ####################### helper/optional functions #########################
+
+ // Helper function, to map the array of credential objects into a single js object.
+ mapCredentials (credentials) {
+ const c = JSON.parse(credentials)
+ const login = c.find(x => x.id === 3)
+ var mapped = {
+ url: c.find(x => x.id === 1).value,
+ apikey: c.find(x => x.id === 2).value,
+ login: login.value
+ }
+
+ if (mapped.login) {
+ mapped.username = login.elements.find(x => x.id === 4).value
+ mapped.password = login.elements.find(x => x.id === 5).value
+ }
+ return mapped
+ }
+
// Username and password are optional.
async getSession (credentials) {
// Headers
@@ -157,11 +226,10 @@ class IdoitBackend extends ExternalBackends {
}
var config = {
- timeout: 30000,
+ timeout: 90000,
headers: headers
}
config.headers['Content-Type'] = 'application/json'
-
var response = await axios.post(url, body, config)
.then(response => {
return response
@@ -174,11 +242,11 @@ class IdoitBackend extends ExternalBackends {
if (response.status !== 200) {
return { success: false, error: response.status, msg: response.statusText }
}
-
// iDoIT error handling.
if (response.data.result) {
return { success: true, data: response.data }
} else if (response.data.error) {
+ console.log(response.data.error)
return { success: false, error: response.data.error.message, msg: response.data.error.data.error }
} else {
return { success: false, msg: 'UNNOWN ERROR' }
diff --git a/server/lib/external-backends/backends/template-backend.js b/server/lib/external-backends/backends/template-backend.js
index 4e5098d..8bee5e1 100644
--- a/server/lib/external-backends/backends/template-backend.js
+++ b/server/lib/external-backends/backends/template-backend.js
@@ -1,4 +1,6 @@
-var ExternalBackends = require('../external-backends.js')
+/* global __appdir */
+const path = require('path')
+const ExternalBackends = require(path.join(__appdir, 'lib', 'external-backends'))
class TemplateBackend extends ExternalBackends {
getCredentials () {
diff --git a/server/lib/external-backends/external-backends.js b/server/lib/external-backends/index.js
index e01a15c..c882522 100644
--- a/server/lib/external-backends/external-backends.js
+++ b/server/lib/external-backends/index.js
@@ -73,6 +73,17 @@ class ExternalBackends {
}
/*
+ * credentials: <BACKEND_CREDENTIALS>
+ * objects: [{ eid: <EXTERNAL_ID>, gid: <GROUP_ID> }, ...]
+ * EXTERNAL_ID is the id of the objects in the external backend requestet.
+ *
+ * return: [{ gid: <GROUP_ID>, childs: [{ id: <EXTERNAL_ID>, }, ...]}, ...]
+ */
+ getDataTree (credendtials, objects) {
+ return { status: 'NOT_IMPLEMENTED_EXCEPTION', error: 'The provided backend does not have a getDataTree method' }
+ }
+
+ /*
* Checks the connection of a given backend.
*
* return: { success: <boolean>, status: '<STATUS_CODE_IF_ERROR>', error: '<ERROR_MESSAGE>' }