From 67887b78305d91d67bf1a174b9ddae6e9a262df6 Mon Sep 17 00:00:00 2001 From: Jannik Schönartz Date: Tue, 16 Apr 2019 11:01:43 +0000 Subject: [server/external-backends] Add dhcp update method & object mapping from existing entries --- server/lib/external-backends/backendhelper.js | 28 ++++++++- .../external-backends/backends/idoit-backend.js | 1 - .../external-backends/backends/infoblox-backend.js | 66 ++++++++++++++++++++-- 3 files changed, 86 insertions(+), 9 deletions(-) (limited to 'server/lib/external-backends') diff --git a/server/lib/external-backends/backendhelper.js b/server/lib/external-backends/backendhelper.js index b7a8a9b..5bbde55 100644 --- a/server/lib/external-backends/backendhelper.js +++ b/server/lib/external-backends/backendhelper.js @@ -67,7 +67,9 @@ async function updateClient (client) { // Get the external clientid. const exid = backend.mappedClients[0] + // If there is no external id, there is no client in the backend to update. if (exid) tmpClient.id = exid.backend_x_client.externalId + else continue // Convert the parent group ids to the external backend parentIds. If multiple -> conflict if (client.parents) { @@ -85,10 +87,30 @@ async function updateClient (client) { } } else if (elements.length === 1) tmpClient['parentId'] = elements[0].backend_x_group.externalId } + try { + let updateClient = await instance.updateClient(backend.credentials, tmpClient) + updateClient.backendId = backend.id + + // If mappings is set the backend id to external id has to be updated. Mappings is a list of oldId, newId object pairs. + if (updateClient.mappings) { + for (let index in updateClient.mappings) { + const mapping = updateClient.mappings[index] + if (mapping.newId.Error) console.log(mapping.newId) + else { + const clientDb = await db.client.findOne({ where: { id: client.id } }) + // Update the mapping. + await backend.removeMappedClients(clientDb) + let data = { externalId: mapping.newId } + if (mapping.newType) data.externalType = mapping.newType + await backend.addMappedClients(clientDb, { through: data }) + } + } + } - let updateClient = await instance.updateClient(backend.credentials, tmpClient) - updateClient.backendId = backend.id - result.push(updateClient) + result.push(updateClient) + } catch (error) { + console.log(error) + } } return result } diff --git a/server/lib/external-backends/backends/idoit-backend.js b/server/lib/external-backends/backends/idoit-backend.js index 833c9b3..aab6838 100644 --- a/server/lib/external-backends/backends/idoit-backend.js +++ b/server/lib/external-backends/backends/idoit-backend.js @@ -383,7 +383,6 @@ class IdoitBackend extends ExternalBackends { }, 'apikey': c.apikey } - console.log(paramsLocation) bodies.push(this.getBody('cmdb.category.save', paramsLocation, 'update_parent')) // Update the object. Model data. diff --git a/server/lib/external-backends/backends/infoblox-backend.js b/server/lib/external-backends/backends/infoblox-backend.js index 74f0265..d32936b 100644 --- a/server/lib/external-backends/backends/infoblox-backend.js +++ b/server/lib/external-backends/backends/infoblox-backend.js @@ -143,6 +143,41 @@ class InfobloxBackend extends ExternalBackends { return true } + async updateClient (credentials, client) { + const c = this.mapCredentials(credentials) + + const ipam = new Infoblox({ + ip: c.url, + apiVersion: c.version + }) + const login = await ipam.login(c.username, c.password) + if (!login) return { error: 'LOGIN_FAILED' } + + let data = {} + let result = [] + + for (let index in client.networks) { + const network = client.networks[index] + network.domain = 'public.ads.uni-freiburg.de' + if (client.name && network.domain) { + data.name = client.name + '.' + network.domain + } + + let ipv4addr = {} + if (network.ip) ipv4addr.ipv4addr = network.ip + if (network.mac) ipv4addr.mac = network.mac + if (network.ip || network.mac) { + data.ipv4addrs = [ipv4addr] + } + + const oldId = client.id + const newId = await ipam.update(oldId, data) + // If the id changed (domain / name / ip) the mapping has to be updated + if (oldId !== newId) result.push({ oldId: oldId, newId: newId }) + } + return { mappings: result } + } + async deleteObjects (credentials, objectIds) { const c = this.mapCredentials(credentials) @@ -170,10 +205,25 @@ class InfobloxBackend extends ExternalBackends { if (!login) return { error: 'LOGIN_FAILED' } let result = [] - const records = JSON.parse(await ipam.list('record:host?zone=lp.privat')) - for (let index in records) { - const record = records[index] - result.push({ id: record._ref, mac: record.ipv4addrs[0].mac, ip: record.ipv4addrs[0].ipv4addr }) + + const domains = await ipam.getDomain() + for (let index in domains) { + const domain = domains[index] + + // Max result of infoblox is 1000, so we need to use the paging system + const response = JSON.parse(await ipam.list('record:host?zone=' + domain + '&_paging=1&_return_as_object=1&_max_results=1000')) + let records = response.result + let nextPageId = response.next_page_id + while (nextPageId) { + let nextpage = JSON.parse(await ipam.list('record:host?zone=' + domain + '&_paging=1&_return_as_object=1&_max_results=1000&_page_id=' + nextPageId)) + records = records.concat(nextpage.result) + nextPageId = nextpage.next_page_id + } + + for (let i in records) { + const record = records[i] + result.push({ id: record._ref, mac: record.ipv4addrs[0].mac, ip: record.ipv4addrs[0].ipv4addr, domain: domain }) + } } return result } @@ -211,6 +261,7 @@ class InfobloxBackend extends ExternalBackends { result['getHost.78'] = JSON.parse(await ipam.getHost('10.21.9.78')) // result["getHost.219"] = JSON.parse(await ipam.getHost('10.21.9.219')) result['getHost.12'] = JSON.parse(await ipam.getHost('10.21.9.12')) + result['getHost.43'] = JSON.parse(await ipam.getHost('10.21.11.43')) // Get ips from subnet // result["getIpsFromSubnet"] = JSON.parse(await ipam.getIpsFromSubnet('10.21.9.0/24')) @@ -248,7 +299,12 @@ class InfobloxBackend extends ExternalBackends { // result["deleteHost.173"] = JSON.parse(await ipam.delete(result["getHost.173"][0]._ref)) // result["deleteHost.206"] = JSON.parse(await ipam.delete('record:host/ZG5zLmhvc3QkLl9kZWZhdWx0LnByaXZhdC5scC50ZXN0:test.lp.privat/default')) - result['allrecords'] = JSON.parse(await ipam.list('record:host?zone=lp.privat')) + // result['allrecords'] = JSON.parse(await ipam.list('record:host?zone=lp.privat')) + /* + result['updateDomain'] = await ipam.update('record:host/ZG5zLmhvc3QkLl9kZWZhdWx0LmRlLnVuaS1mcmVpYnVyZy5hZHMucHVibGljLnplZnQ5MDQz', { + 'name': 'zeft9043.public.ads.uni-freiburg.de' + }) + */ return result } } -- cgit v1.2.3-55-g7522