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 [ { type: 'text', id: 1, name: 'API url', icon: 'link' }, { type: 'password', id: 2, name: 'API token', icon: 'vpn_key', show: false }, { type: 'switch', id: 3, name: 'Login', icon: 'lock_open', elements: [ { type: 'text', id: 4, name: 'username', icon: 'person_outline' }, { type: 'password', id: 5, name: 'password', icon: 'lock', show: false } ] } ] } async checkConnection (credentials) { var c = this.mapCredentials(credentials) 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. 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' } var result = {} result.object = await this.axiosRequest(c.url, 'cmdb.object.read', params, headers) 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 } // 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': method, 'params': params, 'id': 1 } var config = { timeout: 30000, headers: headers } config.headers['Content-Type'] = 'application/json' var response = await axios.post(url, body, config) .then(response => { return response }) .catch(error => { 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 } } // iDoIT error handling. if (response.data.result) { 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 { return { success: false, msg: 'UNNOWN ERROR' } } } } module.exports = IdoitBackend