From e3b59f1edf821c1970657197f38b99cec63d7493 Mon Sep 17 00:00:00 2001 From: Jannik Schönartz Date: Mon, 22 Jul 2019 14:32:43 +0000 Subject: [external-backends/idoit] Add more information in idoit Add contact assignment (Search by name in the idoit persons and link them) Add OS + version information Add network device speed/type/name Add dhcp active check for adding a client Small bugfixes Add console logs for the idoit add server requests --- server/lib/external-backends/backendhelper.js | 6 +- .../external-backends/backends/idoit-backend.js | 117 +++++++++++++++++++-- 2 files changed, 114 insertions(+), 9 deletions(-) (limited to 'server') diff --git a/server/lib/external-backends/backendhelper.js b/server/lib/external-backends/backendhelper.js index 87b2696..7e682d1 100644 --- a/server/lib/external-backends/backendhelper.js +++ b/server/lib/external-backends/backendhelper.js @@ -158,10 +158,14 @@ async function getDhcp () { const isDHCP = await instance.isDhcp(backend.credentials) if (isDHCP) { + // Check weather the backend is active + const checkConnection = await instance.checkConnection(backend.credentials) + if (checkConnection.error) continue + if (!dhcp) dhcp = { instance: instance, backend: backend } else { // Conflict occured! - const conflict = await db.conflict.create({ description: 'Multiple dhcp backends found' }) + const conflict = await db.conflict.create({ description: 'Multiple active dhcp backends found' }) // Add both backends to the conflict. conflict.createObject({ objectType: 'BACKEND', objectId: backend.id }) diff --git a/server/lib/external-backends/backends/idoit-backend.js b/server/lib/external-backends/backends/idoit-backend.js index f0d8ae1..207a448 100644 --- a/server/lib/external-backends/backends/idoit-backend.js +++ b/server/lib/external-backends/backends/idoit-backend.js @@ -182,8 +182,8 @@ class IdoitBackend extends ExternalBackends { } else if (client.type === 'SERVER') { params['type'] = 5 - if (client.location && !client.location.bay) params.categories.C__CATG__LOCATION = { 'data': { 'parent': client.parentId, 'option': client.location.assembly, 'insertion': client.location.insertion, 'pos': client.location.slot } } - if (client.formfactor) params.categories.C__CATG__FORMFACTOR = { 'data': { 'formfactor': client.formfactor, 'rackunits': client.formfactor.rackunits } } + if (client.location && client.location.bay === null) params.categories.C__CATG__LOCATION = { 'data': { 'parent': client.parentId, 'option': client.location.assembly, 'insertion': client.location.insertion, 'pos': client.location.slot } } + if (client.formfactor) params.categories.C__CATG__FORMFACTOR = { 'data': { 'formfactor': client.formfactor.formfactor, 'rackunits': client.formfactor.rackunits } } // Rack segmentation if (client.location.bay !== undefined && client.location.bay !== null) { @@ -192,6 +192,8 @@ class IdoitBackend extends ExternalBackends { const rackobjects = await this.axiosRequest(c.url, [rackobjectsBody], headers) // Get the name of the rack + console.log('') + console.log('Get Rack Name:') const rackBody = this.getBody('cmdb.category.read', { 'apikey': c.apikey, 'object': client.parentId, 'objID': client.parentId, 'category': 'C__CATG__GLOBAL' }, 'get_rack') const rack = await this.axiosRequest(c.url, [rackBody], headers) @@ -202,6 +204,8 @@ class IdoitBackend extends ExternalBackends { let objectPositionBodies = [] // For each segmentation object in the rack get the slot number + console.log('') + console.log('Get Slot IDs Request:') for (let obj in rackobjects[0].result) { const object = rackobjects[0].result[obj] if (object.assigned_object.type !== 'C__OBJTYPE__RACK_SEGMENT') continue @@ -257,6 +261,8 @@ class IdoitBackend extends ExternalBackends { } } + console.log('') + console.log('Create Segment Request:') const createSegmentParam = this.getBody('cmdb.object.create', createSegmentParamObject, 'create_segment') const createSegment = await this.axiosRequest(c.url, [createSegmentParam], headers) chassisId = createSegment[0].result.id @@ -285,11 +291,85 @@ class IdoitBackend extends ExternalBackends { if (network.hostname) networkparams.hostname = network.hostname if (network.domain) networkparams.domain = network.domain if (network.net) networkparams.net = network.net + if (network.primary) networkparams.primary = network.primary ? 1 : 0 params.categories.C__CATG__IP.push(networkparams) } } + // Add contact assignment to the object. + if (client.contacts) { + // Get the persons ids. + let readPersonBodies = [] + for (let index in client.contacts) { + readPersonBodies.push(this.getBody('cmdb.objects.read', { + 'apikey': c.apikey, + 'filter': { + 'type': 'C__OBJTYPE__PERSON', + 'first_name': client.contacts[index].first_name, + 'last_name': client.contacts[index].last_name + } + }, 'read_persons_' + index)) + } + console.log('') + console.log('Read Person Request:') + const requestReadPersons = await this.axiosRequest(c.url, readPersonBodies, headers) + if (requestReadPersons.error) return requestReadPersons + const error = requestReadPersons.filter(x => x.error) + + if (error.length === 0) { + const personIds = requestReadPersons.map(x => { + if (x.result.length === 1) return x.result[0].id + }).filter(Boolean) + params.categories.C__CATG__CONTACT = [] + for (let index in personIds) { + params.categories.C__CATG__CONTACT.push({ + 'contact': personIds[index] + }) + } + } else console.log(error) + } + + // Add operating system information. + if (client.runtime && client.runtime.operating_system) { + // Get the operating system ids. + console.log('') + console.log('Get OS Request:') + const getOSParam = { + 'apikey': c.apikey, + 'filter': { + 'type': 35, // 35 = Operating System + 'title': client.runtime.operating_system.name + } + } + const getOSBody = this.getBody('cmdb.objects.read', getOSParam, 'get_os') + const requestGetOS = await this.axiosRequest(c.url, [getOSBody], headers) + + // Extra request for getting the id of the version number + console.log('') + console.log('Get OS-Version Request:') + const getOSVersionParam = { + 'apikey': c.apikey, + 'objID': requestGetOS[0].result[0].id, + 'catgID': 'C__CATG__VERSION' + } + const getOSVersionBody = this.getBody('cmdb.category.read', getOSVersionParam, 'get_os_version') + const requestGetOSVersion = await this.axiosRequest(c.url, [getOSVersionBody], headers) + const osVersion = requestGetOSVersion[0].result.filter(x => x.title === client.runtime.operating_system.version) + + // Add the result of the OS request (ids) to the create request. + if (requestGetOS[0].result) { + params.categories.C__CATG__OPERATING_SYSTEM = { + 'data': { + 'application': requestGetOS[0].result[0].id, + 'assigned_version': osVersion[0].id + } + } + } + } + // Send the create request. + console.log('') + console.log('Create Client Request:') const body = this.getBody('cmdb.object.create', params, 'client_create') const requestCreate = await this.axiosRequest(c.url, [body], headers) @@ -302,15 +382,16 @@ class IdoitBackend extends ExternalBackends { const hostnameIds = requestCreate[0].result.categories.C__CATG__IP if (client.networks) { + let macBodies = [] for (let index in client.networks) { const network = client.networks[index] // For the ip adresses // network.id = requestCreate[0].result.categories.C__CATG__IP[index] let addresses = [] // Push the ids as string - if (hostnameIds.length >= index + 1) addresses.push('' + hostnameIds[index]) + if (hostnameIds.length > index) addresses.push('' + hostnameIds[index]) - const paramsMac = { + let paramsMac = { 'object': requestCreate[0].result.id, 'objID': requestCreate[0].result.id, 'category': 'C__CATG__NETWORK_PORT', @@ -320,10 +401,25 @@ class IdoitBackend extends ExternalBackends { }, 'apikey': c.apikey } - const bodyMac = this.getBody('cmdb.category.save', paramsMac, 'add_mac_address') - const response = await this.axiosRequest(c.url, [bodyMac], headers) - macRequests.push(response) + + if (network.device) { + if (network.device.speed) { + paramsMac.data.speed = parseFloat(network.device.speed) + // MB/s GB/s ... etc not supported?! Only Mbit/s Gbit/s ... + paramsMac.data.speed_type = null + } + + if (network.device.name) paramsMac.data.title = network.device.name + if (network.device.type) paramsMac.data.port_type = network.device.type + } + + macBodies.push(this.getBody('cmdb.category.save', paramsMac, 'add_mac_address_' + index)) } + + console.log('') + console.log('Add MAC Request:') + const response = await this.axiosRequest(c.url, macBodies, headers) + macRequests.push(response) } // If chassis id is set, assign the object to the chassis bay @@ -335,10 +431,15 @@ class IdoitBackend extends ExternalBackends { 'category': 'C__CATS__CHASSIS_SLOT', 'apikey': c.apikey } + + console.log('') + console.log('Read Rack Slots:') const readSlotsParam = this.getBody('cmdb.category.read', paramsSlots, 'read_slots') const readSlots = await this.axiosRequest(c.url, [readSlotsParam], headers) const bays = readSlots[0].result + console.log('') + console.log('Assign to Rack Slot:') const assignToSlotBody = this.getBody('cmdb.category.save', { 'apikey': c.apikey, 'objID': chassisId, 'object': chassisId, @@ -705,7 +806,7 @@ class IdoitBackend extends ExternalBackends { for (let i = 0; i < batchRequests.length; i++) { // Axios error handling try { - console.log(requestCounter + '/' + batchRequests.length + ' requests send') + console.log(requestCounter + '/' + batchRequests.length + ' requests sent') requestCounter++ const responses = await axios.post(url, batchRequests[i], config) if (Array.isArray(responses.data)) results = [...results, ...responses.data] -- cgit v1.2.3-55-g7522