From 67d1402c75e562af34058d0021cf6a14b5588d22 Mon Sep 17 00:00:00 2001 From: Jannik Schönartz Date: Wed, 20 Mar 2019 04:22:50 +0000 Subject: [server/registration/backends] Rework addClient and updateClient to receive json Add idoit workaround for saving floats again Rework the grepSystemInfo bash script to match the new api --- server/lib/external-backends/backendhelper.js | 204 +++++++++++---------- .../external-backends/backends/idoit-backend.js | 132 +++++++------ server/lib/external-backends/index.js | 13 +- 3 files changed, 191 insertions(+), 158 deletions(-) (limited to 'server/lib/external-backends') diff --git a/server/lib/external-backends/backendhelper.js b/server/lib/external-backends/backendhelper.js index 4bd9969..6892f7c 100644 --- a/server/lib/external-backends/backendhelper.js +++ b/server/lib/external-backends/backendhelper.js @@ -4,107 +4,115 @@ const ExternalBackends = require(path.join(__appdir, 'lib', 'external-backends') const db = require(path.join(__appdir, 'lib', 'sequelize')) const log = require(path.join(__appdir, 'lib', 'log')) -module.exports = { - addClient: async function (client) { - // Get all backends and call addClient for each instance. - var backends = await db.backend.findAll({ include: ['mappedGroups', 'mappedClients'] }) - var result = [] - - for (var b in backends) { - var backend = backends[b] - const ba = new ExternalBackends() - const instance = ba.getInstance(backend.type) - var tmpClient = JSON.parse(JSON.stringify(client)) - - // Add client has no support for update anymore.. use update method.. maybe call conflict here - // Convert the parent group id to the external backend parentId. - // if (client.parentId) { - if (client.parents) { - const elements = backend.mappedGroups.filter(x => client.parents.includes(x.id)) - if (elements.length > 1) { - // Conflict occured! - const conflict = await db.conflict.create({ description: 'Multiple parents found' }) - - // Add backend to the conflict. - conflict.createObject({ objectType: 'BACKEND', objectId: backend.id }) - - // Add the groups to the conflict. - for (let element of elements) { - conflict.createObject({ objectType: 'GROUP', objectId: element.id }) - } - } else if (elements.length === 1) tmpClient['parentId'] = elements[0].backend_x_group.externalId - } - - let addClient = await instance.addClient(backend.credentials, tmpClient) - addClient.backendId = backend.id - if (addClient.succes) { - // If the object was created we need to make the objectid / external id mapping. - const clientDb = await db.client.findOne({ where: { id: client.id } }) - backend.addMappedClients(clientDb, { through: { externalId: addClient.id, externalType: addClient.type } }) - } - if (addClient.error && addClient.error !== 'NOT_IMPLEMENTED_EXCEPTION') log({ category: 'BACKEND_ERROR', description: `[${addClient.backendId}] ${addClient.error}: ${addClient.message}`, clientId: client.id }) - result.push(addClient) +module.exports = { addClient, updateClient, deleteClients, uploadFiles } + +async function addClient (client) { + // Get all backends and call addClient for each instance. + var backends = await db.backend.findAll({ include: ['mappedGroups', 'mappedClients'] }) + var result = [] + + for (var b in backends) { + var backend = backends[b] + const ba = new ExternalBackends() + const instance = ba.getInstance(backend.type) + var tmpClient = JSON.parse(JSON.stringify(client)) + + // Convert the parent group ids to the external backend parentIds. If multiple -> conflict + if (client.parents) { + const elements = backend.mappedGroups.filter(x => client.parents.includes(x.id)) + if (elements.length > 1) { + // Conflict occured! + const conflict = await db.conflict.create({ description: 'Multiple parents found' }) + + // Add backend to the conflict. + conflict.createObject({ objectType: 'BACKEND', objectId: backend.id }) + + // Add the groups to the conflict. + for (let element of elements) { + conflict.createObject({ objectType: 'GROUP', objectId: element.id }) + } + } else if (elements.length === 1) tmpClient['parentId'] = elements[0].backend_x_group.externalId } - return result - }, - - updateClient: async function (client) { - // Get all backends and call addClient for each instance. - const backends = await db.backend.findAll({ include: ['mappedGroups', 'mappedClients'] }) - let result = [] - - for (let b in backends) { - const backend = backends[b] - const ba = new ExternalBackends() - const instance = ba.getInstance(backend.type) - var tmpClient = JSON.parse(JSON.stringify(client)) - - // Get the external clientid. - var exid = backend.mappedClients.find(y => y.id === parseInt(client.id)) - if (exid) tmpClient.id = exid.backend_x_client.externalId - - // Convert the parent group id to the external backend parentId. - if (client.parentId) { - var element = backend.mappedGroups.find(x => x.id === parseInt(client.parentId)) - if (element) tmpClient['parentId'] = element.backend_x_group.externalId - } - - let updateClient = await instance.updateClient(backend.credentials, tmpClient) - updateClient.backendId = backend.id - result.push(updateClient) + + let addClient = await instance.addClient(backend.credentials, tmpClient) + addClient.backendId = backend.id + if (addClient.succes) { + // If the object was created we need to make the objectid / external id mapping. + const clientDb = await db.client.findOne({ where: { id: client.id } }) + backend.addMappedClients(clientDb, { through: { externalId: addClient.id, externalType: addClient.type } }) } - return result - }, - - deleteClients: async function (clientids) { - // Get all backends and call deleteClient for each instance. - const backends = await db.backend.findAll({ where: { '$clientMappings.clientid$': clientids }, include: ['clientMappings'] }) - - backends.forEach(backend => { - const ba = new ExternalBackends() - const instance = ba.getInstance(backend.type) - var objectsToDelete = [] - backend.clientMappings.forEach(mapping => { - objectsToDelete.push(mapping.externalId) - }) - // If there are objects to delete -> delete them. - if (objectsToDelete.length > 0) instance.deleteObjects(backend.credentials, objectsToDelete) - }) - }, - - uploadFiles: async function (clientId, files) { - const backends = await db.backend.findAll({ include: ['mappedClients'] }) - let results = [] - for (let b in backends) { - const backend = backends[b] - const ba = new ExternalBackends() - const instance = ba.getInstance(backend.type) - - let exid = backend.mappedClients.find(y => y.id === parseInt(clientId)) - if (exid) exid = exid.backend_x_client.externalId - results.push(await instance.uploadFiles(backend.credentials, exid, files)) + if (addClient.error && addClient.error !== 'NOT_IMPLEMENTED_EXCEPTION') log({ category: 'BACKEND_ERROR', description: `[${addClient.backendId}] ${addClient.error}: ${addClient.message}`, clientId: client.id }) + result.push(addClient) + } + return result +} + +async function updateClient (client) { + // Get all backends and call addClient for each instance. + const backends = await db.backend.findAll({ include: ['mappedGroups', 'mappedClients'] }) + let result = [] + + for (let b in backends) { + const backend = backends[b] + const ba = new ExternalBackends() + const instance = ba.getInstance(backend.type) + var tmpClient = JSON.parse(JSON.stringify(client)) + + // Get the external clientid. + var exid = backend.mappedClients.find(y => y.id === parseInt(client.id)) + if (exid) tmpClient.id = exid.backend_x_client.externalId + + // Convert the parent group ids to the external backend parentIds. If multiple -> conflict + if (client.parents) { + const elements = backend.mappedGroups.filter(x => client.parents.includes(x.id)) + if (elements.length > 1) { + // Conflict occured! + const conflict = await db.conflict.create({ description: 'Multiple parents found' }) + + // Add backend to the conflict. + conflict.createObject({ objectType: 'BACKEND', objectId: backend.id }) + + // Add the groups to the conflict. + for (let element of elements) { + conflict.createObject({ objectType: 'GROUP', objectId: element.id }) + } + } else if (elements.length === 1) tmpClient['parentId'] = elements[0].backend_x_group.externalId } - return results + + let updateClient = await instance.updateClient(backend.credentials, tmpClient) + updateClient.backendId = backend.id + result.push(updateClient) } + return result +} + +async function deleteClients (clientids) { + // Get all backends and call deleteClient for each instance. + const backends = await db.backend.findAll({ where: { '$clientMappings.clientid$': clientids }, include: ['clientMappings'] }) + + backends.forEach(backend => { + const ba = new ExternalBackends() + const instance = ba.getInstance(backend.type) + var objectsToDelete = [] + backend.clientMappings.forEach(mapping => { + objectsToDelete.push(mapping.externalId) + }) + // If there are objects to delete -> delete them. + if (objectsToDelete.length > 0) instance.deleteObjects(backend.credentials, objectsToDelete) + }) +} +async function uploadFiles (clientId, files) { + const backends = await db.backend.findAll({ include: ['mappedClients'] }) + let results = [] + for (let b in backends) { + const backend = backends[b] + const ba = new ExternalBackends() + const instance = ba.getInstance(backend.type) + + let exid = backend.mappedClients.find(y => y.id === parseInt(clientId)) + if (exid) exid = exid.backend_x_client.externalId + results.push(await instance.uploadFiles(backend.credentials, exid, files)) + } + return results } diff --git a/server/lib/external-backends/backends/idoit-backend.js b/server/lib/external-backends/backends/idoit-backend.js index 8c1a4c3..ce7680a 100644 --- a/server/lib/external-backends/backends/idoit-backend.js +++ b/server/lib/external-backends/backends/idoit-backend.js @@ -170,7 +170,7 @@ class IdoitBackend extends ExternalBackends { if (headers.error) return headers let params = { 'apikey': c.apikey, - 'title': client.title, + 'title': client.name, 'purpose': client.purpose === 'Pool PC' ? 7 : undefined, 'categories': {} } @@ -218,7 +218,7 @@ class IdoitBackend extends ExternalBackends { 'apikey': c.apikey } const bodyMac = this.getBody('cmdb.category.save', paramsMac, 'add_mac_address') - const requestMac = await this.axiosRequest(c.url, [bodyMac], headers) + await this.axiosRequest(c.url, [bodyMac], headers) } // Purpose for Clients: @@ -247,9 +247,11 @@ class IdoitBackend extends ExternalBackends { if (headers.error) return headers let bodies = [] + // workaround for the fucking idoit shit. -.- + let requestResults = [] // Update title of the object - if (client.title) bodies.push(this.getBody('cmdb.object.update', { 'id': client.id, 'title': client.title, 'apikey': c.apikey }, 'update_title')) + if (client.name) bodies.push(this.getBody('cmdb.object.update', { 'id': client.id, 'title': client.name, 'apikey': c.apikey }, 'update_title')) // 'object' should be 'objID' but there is a fucking bug in the idoit api. Soo let's add both parameters because else it will break when they fix it. // Update the productid to the uuid. @@ -298,95 +300,123 @@ class IdoitBackend extends ExternalBackends { // Update the object. CPU data. // TODO: Delete cpu if exists? - if (client.cpu) { - let params = { - 'object': client.id, - 'objID': client.id, - 'category': 'C__CATG__CPU', - 'data': { - 'category_id': 1, - 'manufacturer': client.cpu.manufacturer, - 'title': client.cpu.model, - 'type': client.cpu.type, - 'frequency': client.cpu.frequency, - 'frequency_unit': 3, - 'cores': client.cpu.cores - }, - 'apikey': c.apikey + if (client.cpus) { + let counter = 1 + for (let cpu of client.cpus) { + // Add KB and TB + if (cpu.unit === 'MB') cpu.unit = 2 + else if (cpu.unit === 'GB') cpu.unit = 3 + + let params = { + 'object': client.id, + 'objID': client.id, + 'category': 'C__CATG__CPU', + 'data': { + 'manufacturer': cpu.manufacturer, + 'title': cpu.model, + 'type': cpu.type, + 'frequency': parseFloat(cpu.frequency), + 'frequency_unit': 3, + 'cores': parseInt(cpu.cores) + }, + 'apikey': c.apikey + } + + counter++ + // WORKAROUND + // bodies.push(this.getBody('cmdb.category.save', params, 'create_cpu_' + counter)) + const a = await this.axiosRequest(c.url, [this.getBody('cmdb.category.save', params, 'create_cpu_' + counter)], headers) + requestResults.push(a) } - bodies.push(this.getBody('cmdb.category.save', params, 'update_cpu')) } // Update the object. Ram data. if (client.ram) { let counter = 1 - for (var memory in client.ram) { - var mem = client.ram[memory] - var ramId = 'create_memory_' + counter - if (mem.unit === 'MB') mem.capacity = mem.capacity / 1024 + for (let module of client.ram.modules) { + // Add KB and TB + if (module.unit === 'MB') module.unit = 2 + else if (module.unit === 'GB') module.unit = 3 - // 2 = MB - // 3 = GB let params = { 'object': client.id, 'objID': client.id, 'category': 'C__CATG__MEMORY', 'data': { - 'title': mem.title, - 'manufacturer': mem.manufacturer, - 'type': mem.type, - 'capacity': mem.capacity, - 'unit': 3 + 'title': module.type, + 'manufacturer': module.manufacturer, + 'type': module.type, + 'capacity': parseFloat(module.capacity), + 'unit': module.unit }, 'apikey': c.apikey } counter++ - bodies.push(this.getBody('cmdb.category.save', params, ramId)) + // WORKAROUND + // bodies.push(this.getBody('cmdb.category.save', params, 'create_memory_' + counter)) + const a = await this.axiosRequest(c.url, [this.getBody('cmdb.category.save', params, 'create_memory_' + counter)], headers) + requestResults.push(a) } } // Update the object. Drive data. if (client.drives) { let counter = 1 - for (var drive in client.drives) { - var d = client.drives[drive] - var driveId = 'create_drive_' + counter - + for (let drive of client.drives) { // UNIT - var unit = 0 - if (d.unit === 'GB') unit = 3 - else if (d.unit === 'TB') unit = 4 - else if (d.unit === 'MB') unit = 2 - else if (d.unit === 'KB') unit = 1 - else if (d.unit === 'B') unit = 0 + if (drive.unit === 'GB') drive.unit = 3 + else if (drive.unit === 'MB') drive.unit = 2 + else if (drive.unit === 'TB') drive.unit = 4 + else if (drive.unit === 'KB') drive.unit = 1 + else if (drive.unit === 'B') drive.unit = 0 + + if (drive.type === 'Solid State Device') drive.type = 'SSD' + else if (drive.type === '5400 rpm') drive.type = 'Hard disk' + else if (drive.type === '7200 rpm') drive.type = 'Hard disk' + else if (drive.type === '') drive.type = 'Hard disk' let params = { 'object': client.id, 'objID': client.id, 'category': 'C__CATG__STORAGE_DEVICE', 'data': { - 'title': d.model, - 'type': d.type, + 'category_id': counter, + 'title': drive.model, + 'type': drive.type, // 'manufacturer': , // 'model': , - 'capacity': d.capacity, - 'unit': unit, - 'serial': d.serial, - 'connected': d.connection + 'capacity': parseFloat(drive.capacity), + 'unit': drive.unit, + 'serial': drive.serial, + 'connected': drive.connection }, 'apikey': c.apikey } - bodies.push(this.getBody('cmdb.category.save', params, driveId)) + + counter++ + // WORKAROUND + // bodies.push(this.getBody('cmdb.category.save', params, 'create_drive_' + counter)) + const a = await this.axiosRequest(c.url, [this.getBody('cmdb.category.save', params, 'create_drive_' + counter)], headers) + requestResults.push(a) } } const requestUpdate = await this.axiosRequest(c.url, bodies, headers) + requestResults.push(requestUpdate) + + if (requestUpdate.error) return requestUpdate // 10 is the idoit object id for clients. - var result = { + // 5 is the idoit object id for servers. + let type = 0 + if (client.type === 'CLIENT') type = 10 + else if (client.type === 'SERVER') type = 5 + + const result = { success: true, id: client.id, - type: 10, - response: requestUpdate + type: type, + // response: requestUpdate + response: requestResults } return result diff --git a/server/lib/external-backends/index.js b/server/lib/external-backends/index.js index 41d1c31..7acaa85 100644 --- a/server/lib/external-backends/index.js +++ b/server/lib/external-backends/index.js @@ -128,7 +128,10 @@ class ExternalBackends { * The client parameters are all optional. If the client has an id the object is not created but the categories of the object. * client: { * id: , title: , parentId: , uuid: , - * network: { mac: , ip: } + * network: { mac: , ip: }, + * location: { assembly: , insertion: , position: }, + * system: { model: , manufacturer: , serialnumber: }, + * formfactor: { formfactor: , rackunits: } * } */ async addClient (credentials, client) { @@ -148,14 +151,6 @@ class ExternalBackends { * storage: {}, * network: { mac: , ip: } * } - * - * Servers are clients, that can additionally have special values: - * { - * ..., rackid: - * location: { assembly: , insertion: , position: } - * system: { model: , manufacturer: , serialnumber: }, - * formfactor: { formfactor: , rackunits: } - * } */ async updateClient (credentials, client) { return { error: 'NOT_IMPLEMENTED_EXCEPTION', message: 'The provided backend does not have an updateClient method' } -- cgit v1.2.3-55-g7522