From a931375c8feeb72fbb121fe762429d5dca6385f3 Mon Sep 17 00:00:00 2001 From: Jannik Schönartz Date: Tue, 31 Jul 2018 09:35:27 +0000 Subject: [server/external-backends] Added iDoIT Api call for getting the locationtree of an object. --- .../external-backends/backends/idoit-backend.js | 94 +++++++++++++++++----- 1 file changed, 72 insertions(+), 22 deletions(-) (limited to 'server/lib/external-backends/backends/idoit-backend.js') 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 { -- cgit v1.2.3-55-g7522