summaryrefslogtreecommitdiffstats
path: root/server/lib/external-backends/backends/idoit-backend.js
diff options
context:
space:
mode:
authorJannik Schönartz2018-07-31 11:35:27 +0200
committerJannik Schönartz2018-07-31 11:35:27 +0200
commita931375c8feeb72fbb121fe762429d5dca6385f3 (patch)
tree1138f0c260603bb7d2f4a3c5412c1b1dafd40903 /server/lib/external-backends/backends/idoit-backend.js
parent[webapp/groups] small bugfix (diff)
downloadbas-a931375c8feeb72fbb121fe762429d5dca6385f3.tar.gz
bas-a931375c8feeb72fbb121fe762429d5dca6385f3.tar.xz
bas-a931375c8feeb72fbb121fe762429d5dca6385f3.zip
[server/external-backends] Added iDoIT Api call for getting the locationtree of an object.
Diffstat (limited to 'server/lib/external-backends/backends/idoit-backend.js')
-rw-r--r--server/lib/external-backends/backends/idoit-backend.js94
1 files changed, 72 insertions, 22 deletions
diff --git a/server/lib/external-backends/backends/idoit-backend.js b/server/lib/external-backends/backends/idoit-backend.js
index d95618d..d188ecd 100644
--- a/server/lib/external-backends/backends/idoit-backend.js
+++ b/server/lib/external-backends/backends/idoit-backend.js
@@ -2,6 +2,7 @@ var ExternalBackends = require('../external-backends.js')
var axios = require('axios')
class IdoitBackend extends ExternalBackends {
+ // Needed functions
getCredentials () {
// I do it only needs the API-key.
return [
@@ -20,36 +21,86 @@ class IdoitBackend extends ExternalBackends {
]
}
- async checkConnection (backend) {
- const credentials = JSON.parse(backend.credentials)
- const url = credentials.find(x => x.id === 1).value
- const apiKey = credentials.find(x => x.id === 2).value
+ async checkConnection (credentials) {
+ var c = this.mapCredentials(credentials)
+ return this.getSession(c)
+ }
+
+ // 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 getRoomdata (credentials, oid) {
+ 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 = {
+ 'id': oid,
+ 'apikey': c.apikey,
+ 'language': 'en'
+ }
+
+ // Make an request to read the object
+ return this.axiosRequest(c.url, 'cmdb.location_tree', params, headers)
+ }
+ // Username and password are optional.
+ async getSession (credentials) {
+ // Headers
+ var headers = {}
+ // Optional credentials
+ if (credentials.login) {
+ headers['X-RPC-Auth-Username'] = credentials.username
+ headers['X-RPC-Auth-Password'] = credentials.password
+ }
+
+ // Params
+ var params = {
+ 'apikey': credentials.apikey,
+ 'language': 'en'
+ }
+
+ // Make a login request and see if we are authenticated.
+ return this.axiosRequest(credentials.url, 'idoit.login', params, headers)
+ }
+
+ // Helper function to make the axios http/https requests to reduced copy pasta code in this backend. Including the error handling.
+ async axiosRequest (url, method, params, headers) {
const body = {
'version': '2.0',
- 'method': 'idoit.login',
- 'params': {
- 'apikey': apiKey,
- 'language': 'en'
- },
+ 'method': method,
+ 'params': params,
'id': 1
}
var config = {
timeout: 30000,
- headers: {
- 'Content-Type': 'application/json'
- }
+ headers: headers
}
+ config.headers['Content-Type'] = 'application/json'
- // Optional credentials
- const login = credentials.find(x => x.id === 3)
- if (login.value) {
- config.headers['X-RPC-Auth-Username'] = login.elements.find(x => x.id === 4).value
- config.headers['X-RPC-Auth-Password'] = login.elements.find(x => x.id === 5).value
- }
-
- // Make a login request and see if we are authenticated.
var response = await axios.post(url, body, config)
.then(response => {
return response
@@ -58,7 +109,6 @@ class IdoitBackend extends ExternalBackends {
console.log(error)
return error.response ? error.response : { status: 900, statusText: 'Request failed timeout' }
})
-
// Axios error handling
if (response.status !== 200) {
return { success: false, error: response.status, msg: response.statusText }
@@ -66,7 +116,7 @@ class IdoitBackend extends ExternalBackends {
// iDoIT error handling.
if (response.data.result) {
- return { success: true }
+ return { success: true, data: response.data }
} else if (response.data.error) {
return { success: false, error: response.data.error.message, msg: response.data.error.data.error }
} else {