summaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorJannik Schönartz2019-04-16 13:01:43 +0200
committerJannik Schönartz2019-04-16 13:01:43 +0200
commit67887b78305d91d67bf1a174b9ddae6e9a262df6 (patch)
treec28a3d488f0e35ff8592efa9354b87244ba48863 /server
parent[server/registration] Add ipxe overview menu (diff)
downloadbas-67887b78305d91d67bf1a174b9ddae6e9a262df6.tar.gz
bas-67887b78305d91d67bf1a174b9ddae6e9a262df6.tar.xz
bas-67887b78305d91d67bf1a174b9ddae6e9a262df6.zip
[server/external-backends] Add dhcp update method & object mapping from existing entries
Diffstat (limited to 'server')
-rw-r--r--server/api/backends.js4
-rw-r--r--server/lib/external-backends/backendhelper.js28
-rw-r--r--server/lib/external-backends/backends/idoit-backend.js1
-rw-r--r--server/lib/external-backends/backends/infoblox-backend.js66
4 files changed, 89 insertions, 10 deletions
diff --git a/server/api/backends.js b/server/api/backends.js
index f3de01f..32c7cc7 100644
--- a/server/api/backends.js
+++ b/server/api/backends.js
@@ -316,7 +316,9 @@ router.getAsync('/:id/mapping', async (req, res) => {
for (let index in objects) {
const object = objects[index]
const client = await db.client.findOne({ where: { mac: object.mac } })
- await backend.addMappedClients(client, { through: { externalId: object.id } })
+ // TODO: external Type?
+ let data = { externalId: object.id }
+ if (client) await backend.addMappedClients(client, { through: data })
}
return res.status(200).send()
} else return res.status(500).send()
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
}
}