summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--server/api/clients.js17
-rw-r--r--server/api/groups.js9
-rw-r--r--server/api/registration.js3
-rw-r--r--server/lib/external-backends/backendhelper.js2
-rw-r--r--server/lib/external-backends/backends/idoit-backend.js69
5 files changed, 86 insertions, 14 deletions
diff --git a/server/api/clients.js b/server/api/clients.js
index 32b845d..3152e10 100644
--- a/server/api/clients.js
+++ b/server/api/clients.js
@@ -35,14 +35,31 @@ router.postAsync(['', '/:id'], async (req, res) => {
} else {
let client
let action = 'updated'
+
+ let backendClient = {
+ id: req.params.id,
+ name: req.body.data.name,
+ type: 'CLIENT',
+ uuid: req.body.data.uuid,
+ parents: req.body.groupIds,
+ networks: [
+ { ip: req.body.data.ip, mac: req.body.data.mac }
+ ]
+ }
+
if (req.params.id === undefined) {
client = await db.client.create(req.body.data)
action = 'created'
io.in('broadcast newClient').emit('notifications newAlert', { type: 'info', text: 'New client!' })
+
+ // Add client to the backends
+ backendClient.id = client.id
+ backendHelper.addClient(backendClient)
} else if (req.params.id > 0) {
client = await db.client.findOne({ where: { id: req.params.id } })
if (!client) return HttpResponse.notFound(req.params.id).send(res)
else await client.update(req.body.data)
+ backendHelper.updateClient(backendClient)
} else {
return HttpResponse.invalidId().send(res)
}
diff --git a/server/api/groups.js b/server/api/groups.js
index e3683bf..7cab3d8 100644
--- a/server/api/groups.js
+++ b/server/api/groups.js
@@ -7,6 +7,7 @@ const express = require('express')
const { decorateApp } = require('@awaitjs/express')
const router = decorateApp(express.Router())
const HttpResponse = require(path.join(__appdir, 'lib', 'httpresponse'))
+const backendHelper = require(path.join(__appdir, 'lib', 'external-backends', 'backendhelper'))
// ############################################################################
// ########################### GET requests #################################
@@ -120,12 +121,20 @@ router.postAsync('/:id/clients', async (req, res) => {
if (!(req.params.id > 0)) return HttpResponse.invalidId().send(res)
const group = await db.group.findOne({ where: { id: req.params.id } })
if (group) {
+ let groupid = null
if (req.query.delete !== undefined && req.query.delete !== 'false') {
const count = await group.removeClients(req.body.ids)
HttpResponse.successBatch('removed', 'client', count).send(res)
} else {
const count = await group.addClients(req.body.ids)
HttpResponse.successBatch('added', 'client', count).send(res)
+ groupid = group.id
+ }
+
+ // Update each client backend
+ for (let index in req.body.ids) {
+ const id = req.body.ids[index]
+ backendHelper.updateClient({ id: id, parents: [groupid] })
}
} else {
HttpResponse.notFound(req.params.id).send(res)
diff --git a/server/api/registration.js b/server/api/registration.js
index 0a9a4cb..e3ca350 100644
--- a/server/api/registration.js
+++ b/server/api/registration.js
@@ -129,7 +129,6 @@ noAuthRouter.postAsync('/clients', async (req, res) => {
// Client does not exist.
if (!client.parents) client.parents = []
// TODO: Save all IPs? Maybe only primary ip?
- // const createClient = { name: client.name, description: client.type, ip: client.network.ip, mac: client.network.mac, uuid: client.uuid }
const createClient = { name: client.name, description: client.type, ip: client.networks[0].ip, mac: client.networks[0].mac, uuid: client.uuid }
if (client.type === 'CLIENT') createClient.registrationState = await getNextHookScript(client.parents)
const newClient = await db.client.create(createClient)
@@ -138,7 +137,7 @@ noAuthRouter.postAsync('/clients', async (req, res) => {
// Add groups to the client.
if (client.parents.length === 0) client.parents = await ipHelper.getGroups(client.networks[0].ip)
client.parents.forEach(pid => { newClient.addGroup(pid) })
- log({ category: 'CLIENT_REGISTRATION', description: 'Client added successfully.', clientId: newClient.id })
+ log({ category: 'REGISTRATION', description: 'Client added successfully.', clientId: newClient.id })
// Add the client to the backends.
const result = await backendHelper.addClient(client)
diff --git a/server/lib/external-backends/backendhelper.js b/server/lib/external-backends/backendhelper.js
index a088074..d8d4b24 100644
--- a/server/lib/external-backends/backendhelper.js
+++ b/server/lib/external-backends/backendhelper.js
@@ -43,7 +43,7 @@ async function addClient (client) {
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 })
+ if (addClient.error && addClient.error !== 'NOT_IMPLEMENTED_EXCEPTION') log({ category: 'ERROR_BACKEND', description: `[${addClient.backendId}] ${addClient.error}: ${addClient.message}`, clientId: client.id })
result.push(addClient)
}
return result
diff --git a/server/lib/external-backends/backends/idoit-backend.js b/server/lib/external-backends/backends/idoit-backend.js
index 601c9f9..833c9b3 100644
--- a/server/lib/external-backends/backends/idoit-backend.js
+++ b/server/lib/external-backends/backends/idoit-backend.js
@@ -374,18 +374,17 @@ class IdoitBackend extends ExternalBackends {
}
// Update the object. Location
- if (client.parentId) {
- let params = {
- 'object': client.id,
- 'objID': client.id,
- 'category': 'C__CATG__LOCATION',
- 'data': {
- 'parent': client.parentId
- },
- 'apikey': c.apikey
- }
- bodies.push(this.getBody('cmdb.category.save', params, 'update_parent'))
+ let paramsLocation = {
+ 'object': client.id,
+ 'objID': client.id,
+ 'category': 'C__CATG__LOCATION',
+ 'data': {
+ 'parent': client.parentId ? client.parentId : null
+ },
+ 'apikey': c.apikey
}
+ console.log(paramsLocation)
+ bodies.push(this.getBody('cmdb.category.save', paramsLocation, 'update_parent'))
// Update the object. Model data.
if (client.system) {
@@ -403,6 +402,54 @@ class IdoitBackend extends ExternalBackends {
bodies.push(this.getBody('cmdb.category.save', params, 'update_model'))
}
+ // Update networks
+ if (client.networks) {
+ let parmReadNetwork = {
+ 'object': client.id,
+ 'objID': client.id,
+ 'category': 'C__CATG__IP',
+ 'apikey': c.apikey
+ }
+ const ips = await this.axiosRequest(c.url, [this.getBody('cmdb.category.read', parmReadNetwork, 'read_ips')], headers)
+ parmReadNetwork.category = 'C__CATG__NETWORK_PORT'
+ const macs = await this.axiosRequest(c.url, [this.getBody('cmdb.category.read', parmReadNetwork, 'read_macs')], headers)
+
+ for (let index in client.networks) {
+ const network = client.networks[index]
+
+ // Update ip addresses
+ let paramsIp = {
+ 'object': client.id,
+ 'objID': client.id,
+ 'category': 'C__CATG__IP',
+ 'data': {
+ 'ipv4_address': network.ip,
+ 'hostname': network.hostname,
+ 'domain': network.domain,
+ 'net': network.net
+ },
+ 'apikey': c.apikey
+ }
+ if (ips[0].result.length > index) paramsIp.entry = parseInt(ips[0].result[index].id)
+ bodies.push(this.getBody('cmdb.category.save', paramsIp, 'update_ip'))
+
+ // Update mac addresses
+ if (network.mac) {
+ let paramsMac = {
+ 'object': client.id,
+ 'objID': client.id,
+ 'category': 'C__CATG__NETWORK_PORT',
+ 'data': {
+ 'mac': network.mac
+ },
+ 'apikey': c.apikey
+ }
+ if (macs[0].result.length > index) paramsMac.entry = parseInt(macs[0].result[index].id)
+ bodies.push(this.getBody('cmdb.category.save', paramsMac, 'update_mac'))
+ }
+ }
+ }
+
// Update the object. CPU data.
// TODO: Delete cpu if exists?
if (client.cpus) {