summaryrefslogtreecommitdiffstats
path: root/server/lib/external-backends
diff options
context:
space:
mode:
authorJannik Schönartz2018-12-03 03:05:52 +0100
committerJannik Schönartz2018-12-03 03:05:52 +0100
commit3cb40c9cda0ffe4102901b87c0544c6021071185 (patch)
tree232b5c435a037d9378ae733037f9f08d8be51bd7 /server/lib/external-backends
parentFix Error when no parent has a config (diff)
downloadbas-3cb40c9cda0ffe4102901b87c0544c6021071185.tar.gz
bas-3cb40c9cda0ffe4102901b87c0544c6021071185.tar.xz
bas-3cb40c9cda0ffe4102901b87c0544c6021071185.zip
[idoit] Add idoit add / update functionality
backend_x_ db changed. (Not storing NULL clientids anymore) Therefore the backendobject needs to be deleted before the client in the bas db is deleted. Uploading a file in idoit method added. Testing method for uploading files via curl for the tpm stuff. Added feedback option, to get a api-viable returnvalue and not the ipxe script. idoit update method for the additional hw values added. hwinfo script should now collect and update some informations. Testing inc.
Diffstat (limited to 'server/lib/external-backends')
-rw-r--r--server/lib/external-backends/backendhelper.js91
-rw-r--r--server/lib/external-backends/backends/idoit-backend.js288
-rw-r--r--server/lib/external-backends/index.js25
3 files changed, 319 insertions, 85 deletions
diff --git a/server/lib/external-backends/backendhelper.js b/server/lib/external-backends/backendhelper.js
index 89a9658..077d573 100644
--- a/server/lib/external-backends/backendhelper.js
+++ b/server/lib/external-backends/backendhelper.js
@@ -4,50 +4,79 @@ const ExternalBackends = require(path.join(__appdir, 'lib', 'external-backends')
var db = require(path.join(__appdir, 'lib', 'sequelize'))
module.exports = {
- addClient: function (client) {
+ addClient: async function (client) {
// Get all backends and call addClient for each instance.
- return db.backend.findAll({ include: ['mappedGroups', 'mappedClients'] }).then(backends => {
- var promises = []
- var backendids = []
- backends.forEach(backend => {
- const ba = new ExternalBackends()
- const instance = ba.getInstance(backend.type)
- var tmpClient = JSON.parse(JSON.stringify(client))
+ var backends = await db.backend.findAll({ include: ['mappedGroups', 'mappedClients'] })
+ var result = []
- // If the client id is set we need to get the external id.
- if (client.id) {
- var exid = backend.mappedClients.find(y => y.id === parseInt(client.id))
- if (exid) tmpClient.id = exid.backend_x_client.externalId
- }
+ 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 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
+ // If the client id is set we need to get the external id.
+ if (client.id) {
+ var exid = backend.mappedClients.find(y => y.id === parseInt(client.id))
+ if (exid) tmpClient.id = exid.backend_x_client.externalId
+ } else {
+ // If we don't already have the client id, we need to check if there is already a client with the provided uuid in the backend.
+ var cl = await instance.getClient(backend.credentials, { uuid: tmpClient.uuid })
+ if (cl.succes) {
+ tmpClient.id = cl.data
}
+ }
- // TODO: Stuff to do if client already exists
- backendids.push(backend.id)
- promises.push(instance.addClient(backend.credentials, tmpClient))
- })
- return Promise.all(promises).then(result => {
- result.forEach(object => {
- object.backendId = backendids.shift()
- })
- return result
- })
- })
+ // 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
+ }
+
+ var client = await instance.addClient(backend.credentials, tmpClient)
+ client.backendId = backend.id
+ result.push(client)
+ }
+ return result
+ },
+
+ updateClient: 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))
+
+ // 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
+ }
+
+ var client = await instance.updateClient(backend.credentials, tmpClient)
+ client.backendId = backend.id
+ result.push(client)
+ }
+ return result
},
- deleteClients: function () {
+ deleteClients: function (clientids) {
// Get all backends and call deleteClient for each instance.
- db.backend.findAll({ include: ['clientMappings'] }).then(backends => {
+ return db.backend.findAll({ where: { '$clientMappings.clientid$': clientids }, include: ['clientMappings'] }).then(backends => {
backends.forEach(backend => {
const ba = new ExternalBackends()
const instance = ba.getInstance(backend.type)
var objectsToDelete = []
backend.clientMappings.forEach(mapping => {
- if (mapping.clientId === null) objectsToDelete.push(mapping.externalId)
+ objectsToDelete.push(mapping.externalId)
})
// If there are objects to delete -> delete them.
if (objectsToDelete.length > 0) instance.deleteObjects(backend.credentials, objectsToDelete)
diff --git a/server/lib/external-backends/backends/idoit-backend.js b/server/lib/external-backends/backends/idoit-backend.js
index fc3bbca..d44c07c 100644
--- a/server/lib/external-backends/backends/idoit-backend.js
+++ b/server/lib/external-backends/backends/idoit-backend.js
@@ -79,61 +79,54 @@ class IdoitBackend extends ExternalBackends {
var c = this.mapCredentials(credentials)
var login = await this.getSession(c)
var sid = login.data.result['session-id']
+ var uuid = client.uuid
// Headers
var headers = {
- 'X-RPC-Auth-Session': sid
+ 'X-RPC-Auth-Session': sid,
+ 'Content-Type': 'application/json'
}
- // Params
+ // Params to get all clients.
var params = {
- 'id': 1450,
'apikey': c.apikey,
- 'language': 'en'
+ 'language': 'en',
+ 'filter': {
+ 'type': 'C__OBJTYPE__CLIENT'
+ }
}
- // Get common object data
- var result = {}
- result.common = await this.axiosRequest(c.url, 'cmdb.object.read', params, headers)
- result.common = result.common.data.result
-
- // Get objecttypes
- delete params.id
- // params.type = result.common.objecttype
- // result.categories = await this.axiosRequest(c.url, 'cmdb.object_type_categories', params, headers)
- // result.categories = result.categories.data.result
-
- // result.categories = result.categories.data.result
- /*
- var cat = []
- var self = this
- result.categories.catg.forEach(async c => {
- delete params.type
- params.objID = 1450
- params.category = c.const
- console.log(params)
- //var tmp = await self.axiosRequest(c.url, 'cmdb.category.read', params, headers).catch(error => {console.log(error)})
- //tmp = tmp.data.result
-
- //cat.push(tmp)
+ // Get all client objects.
+ var clients = await this.axiosRequest(c.url, 'cmdb.objects', params, headers)
+ clients = clients.data.result
+
+ var bodies = []
+
+ clients.forEach(client => {
+ bodies.push({
+ 'version': '2.0',
+ 'method': 'cmdb.category.read',
+ 'params': {
+ 'objID': client.id,
+ 'apikey': c.apikey,
+ 'language': 'en',
+ 'category': 'C__CATG__MODEL'
+ },
+ 'id': client.id
+ })
})
- result.cat = cat
- */
-
- // Get all ip addresses
- // delete params.type
- params.objID = 1450
- params.category = 'C__CATG__IP'
- var tmp = await this.axiosRequest(c.url, 'cmdb.category.read', params, headers)
- tmp = tmp.data.result
-
- tmp.forEach(element => {
- if (element.primary_hostaddress) {
- result.ip = element.primary_hostaddress.ref_title
+
+ var config = {
+ timeout: 180000,
+ headers: {
+ 'X-RPC-Auth-Session': sid,
+ 'Content-Type': 'application/json'
}
- })
+ }
+ var requestClient = await axios.post(c.url, bodies, config)
+ requestClient = requestClient.data.filter(x => x.result.length >= 1 && x.result[0].productid === client.uuid)
- return result
+ return requestClient.length >= 1 ? { succes: true, data: requestClient[0].result[0].objID } : { success: false, error: 'NO_CLIENT_FOUND', msg: 'There is no client matching with the provided uuid.' }
}
async getObjects (credentials) {
@@ -319,10 +312,6 @@ class IdoitBackend extends 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: <CLIENT_ID>, title: <CLIENT_TITLE>, parentId: <PARENT_ID>,
- * system: { model: <SYSTEM_MODEL>, manufacturer: <SYSTEM_MANUFACTURER>, serialnumber: <SYSTEM_SERIALNUMBER> },
- * cpu: { model: <CPU_MODEL>, manufacturer: <CPU_MANUFACTURER>, type: <CPU_TYPE>, frequency: <CPU_FREQUENCY>, cores: <CPU_CORES> },
- * ram: [{ model: <RAM_MODEL>, manufacturer: <RAM_MANUFACTURER>, type: <RAM_TYPE>, capacity: <RAM_CAPACITY>, unit: <RAM_UNIT> }, ...],
- * storage: {},
* network: { mac: <MAC_ADDRESS>, ip: <IP_ADDRESS> }
* }
*/
@@ -338,6 +327,7 @@ class IdoitBackend extends ExternalBackends {
'Content-Type': 'application/json'
}
}
+ var bodies = []
var clientid
if (!client.id) {
@@ -354,12 +344,45 @@ class IdoitBackend extends ExternalBackends {
clientid = requestCreate.data.result.id
} else {
clientid = client.id
+
+ // Update the client title
+ bodies.push({
+ 'version': '2.0',
+ 'method': 'cmdb.object.update',
+ 'params': {
+ 'id': clientid,
+ 'title': client.title,
+ 'apikey': c.apikey,
+ 'language': 'en'
+ },
+ 'id': 'update_title'
+ })
+ }
+
+ if (client.uuid) {
+ // Update the productid to the uuid.
+ bodies.push({
+ 'version': '2.0',
+ 'method': 'cmdb.category.update',
+ 'params': {
+ 'objID': clientid,
+ 'category': 'C__CATG__MODEL',
+ 'data': {
+ 'productid': client.uuid
+ },
+ 'apikey': c.apikey,
+ 'language': 'en'
+ },
+ 'id': 'update_uuid'
+ })
}
- var bodies = []
if (client.network) {
// Update the object. MAC-Address
if (client.network.mac) {
+ // First read if there are current entries.
+ // Delete the previous entries.
+ // Finally create the new entry.
bodies.push({
'version': '2.0',
'method': 'cmdb.category.create',
@@ -415,6 +438,101 @@ class IdoitBackend extends ExternalBackends {
})
}
+ var requestUpdate = await axios.post(c.url, bodies, config)
+
+ var result = {
+ success: true,
+ id: clientid,
+ type: 'C__OBJTYPE__CLIENT',
+ create: requestCreate ? requestCreate.success : false,
+ update: requestUpdate ? requestUpdate.success : false,
+ createData: requestCreate ? requestCreate.data : false,
+ updateData: requestUpdate ? requestUpdate.data : false
+ }
+
+ return result
+ }
+
+ /*
+ * Updates the client information in the backend.
+ *
+ * credentials: <BACKEND_CREDENTIALS>
+ * The client parameters are all optional.
+ * client: {
+ * id: <CLIENT_ID>, title: <CLIENT_TITLE>, parentId: <PARENT_ID>,
+ * system: { model: <SYSTEM_MODEL>, manufacturer: <SYSTEM_MANUFACTURER>, serialnumber: <SYSTEM_SERIALNUMBER> },
+ * cpu: { model: <CPU_MODEL>, manufacturer: <CPU_MANUFACTURER>, type: <CPU_TYPE>, frequency: <CPU_FREQUENCY>, cores: <CPU_CORES> },
+ * ram: [{ model: <RAM_MODEL>, manufacturer: <RAM_MANUFACTURER>, type: <RAM_TYPE>, capacity: <RAM_CAPACITY>, unit: <RAM_UNIT> }, ...],
+ * storage: {}
+ * }
+ */
+ async updateClient (credentials, client) {
+ var c = this.mapCredentials(credentials)
+ var login = await this.getSession(c)
+ var sid = login.data.result['session-id']
+
+ var config = {
+ timeout: 180000,
+ headers: {
+ 'X-RPC-Auth-Session': sid,
+ 'Content-Type': 'application/json'
+ }
+ }
+
+ var clientid = client.id
+ var bodies = []
+
+ if (client.title) {
+ // Update title of the object
+ bodies.push({
+ 'version': '2.0',
+ 'method': 'cmdb.object.update',
+ 'params': {
+ 'id': clientid,
+ 'title': client.title,
+ 'apikey': c.apikey,
+ 'language': 'en'
+ },
+ 'id': 'update_title'
+ })
+ }
+
+ if (client.uuid) {
+ // Update the productid to the uuid.
+ bodies.push({
+ 'version': '2.0',
+ 'method': 'cmdb.category.update',
+ 'params': {
+ 'objID': clientid,
+ 'category': 'C__CATG__MODEL',
+ 'data': {
+ 'productid': client.uuid
+ },
+ 'apikey': c.apikey,
+ 'language': 'en'
+ },
+ 'id': 'update_uuid'
+ })
+ }
+
+ // Update the object. Location
+ if (client.parentId) {
+ bodies.push({
+ 'version': '2.0',
+ 'method': 'cmdb.category.update',
+ 'params': {
+ 'objID': clientid,
+ 'category': 'C__CATG__LOCATION',
+ 'data': {
+ 'parent': client.parentId
+ },
+ 'apikey': c.apikey,
+ 'language': 'en'
+ },
+ 'id': 'update_parent'
+ })
+ }
+
if (client.system) {
// Update the object.
bodies.push({
@@ -436,6 +554,8 @@ class IdoitBackend extends ExternalBackends {
}
if (client.cpu) {
+ // TODO: Delete cpu if exists?
+
// Update the object.
bodies.push({
'version': '2.0',
@@ -449,7 +569,7 @@ class IdoitBackend extends ExternalBackends {
'title': client.cpu.model,
'type': client.cpu.type,
'frequency': client.cpu.frequency,
- 'frequency_unit': 1,
+ 'frequency_unit': 3,
'cores': client.cpu.cores
},
'apikey': c.apikey,
@@ -493,15 +613,81 @@ class IdoitBackend extends ExternalBackends {
success: true,
id: clientid,
type: 'C__OBJTYPE__CLIENT',
- create: requestCreate ? requestCreate.success : false,
update: requestUpdate ? requestUpdate.success : false,
- createData: requestCreate ? requestCreate.data : false,
updateData: requestUpdate ? requestUpdate.data : false
}
return result
}
+ async uploadTpm (credentials, clientid, clientuuid, tpm) {
+ var c = this.mapCredentials(credentials)
+ var login = await this.getSession(c)
+ var sid = login.data.result['session-id']
+
+ var config = {
+ timeout: 180000,
+ headers: {
+ 'X-RPC-Auth-Session': sid,
+ 'Content-Type': 'application/json'
+ }
+ }
+
+ // Create file object in idoit.
+ var body = {
+ "version": "2.0",
+ "method": "cmdb.object.create",
+ "params": {
+ "type": "C__OBJTYPE__FILE",
+ "title": "Just a test",
+ "apikey": c.apikey,
+ "language": "en"
+ },
+ "id": 2
+ }
+ var fileobject = await axios.post(c.url, body, config)
+ console.log(fileobject.data.result.id)
+
+ // Upload file to fileobject.
+ body = {
+ "version": "2.0",
+ "method": "cmdb.category.create",
+ "params": {
+ "objID": fileobject.data.result.id,
+ "data": {
+ "file_content": "dGVzdAo=",
+ "file_physical": "test.txt",
+ "file_title": "Just a test",
+ "version_description": "Just a test"
+ },
+ "category": "C__CMDB__SUBCAT__FILE_VERSIONS",
+ "apikey": c.apikey,
+ "language": "en"
+ },
+ "id": 3
+ }
+ var fileupload = await axios.post(c.url, body, config)
+
+ // Combine fileobject with object.
+ body = {
+ "version": "2.0",
+ "method": "cmdb.category.create",
+ "params": {
+ "objID": clientid,
+ "data": {
+ "file": fileobject.data.result.id
+ },
+ "category": "C__CATG__FILE",
+ "apikey": c.apikey,
+ "language": "en"
+ },
+ "id": 4
+ }
+ var concat = await axios.post(c.url, body, config)
+
+ return { create: fileobject.data.result, upload: fileupload.data.result, concat: concat.data.result }
+ }
+
// ############################################################################
// ####################### helper/optional functions #########################
diff --git a/server/lib/external-backends/index.js b/server/lib/external-backends/index.js
index 00d0da3..1adb100 100644
--- a/server/lib/external-backends/index.js
+++ b/server/lib/external-backends/index.js
@@ -64,6 +64,7 @@ class ExternalBackends {
async getClient (credentials, client) {
return { status: 'NOT_IMPLEMENTED_EXCEPTION', error: 'The provided backend does not have a getClient method' }
}
+
/*
* credendtials: <BACKEND_CREDENTIALS>
*
@@ -127,7 +128,21 @@ class ExternalBackends {
* credentials: <BACKEND_CREDENTIALS>
* 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: <CLIENT_ID>, title: <CLIENT_TITLE>, parentId: <PARENT_ID>,
+ * id: <CLIENT_ID>, title: <CLIENT_TITLE>, parentId: <PARENT_ID>, uuid: <CLIENT_UUID>,
+ * network: { mac: <MAC_ADDRESS>, ip: <IP_ADDRESS> }
+ * }
+ */
+ async addClient (credentials, client) {
+ return { success: false, status: 'NOT_IMPLEMENTED_EXCEPTION', error: 'The provided backend does not have an addClient method' }
+ }
+
+ /*
+ * Updates the client in the backend.
+ *
+ * credentials: <BACKEND_CREDENTIALS>
+ * The client parameters are all optional.
+ * client: {
+ * id: <CLIENT_ID>, title: <CLIENT_TITLE>, parentId: <PARENT_ID>, uuid: <CLIENT_UUID>,
* system: { model: <SYSTEM_MODEL>, manufacturer: <SYSTEM_MANUFACTURER>, serialnumber: <SYSTEM_SERIALNUMBER> },
* cpu: { model: <CPU_MODEL>, manufacturer: <CPU_MANUFACTURER>, type: <CPU_TYPE>, frequency: <CPU_FREQUENCY>, cores: <CPU_CORES> },
* ram: [{ model: <RAM_MODEL>, manufacturer: <RAM_MANUFACTURER>, type: <RAM_TYPE>, capacity: <RAM_CAPACITY>, unit: <RAM_UNIT> }, ...],
@@ -135,8 +150,12 @@ class ExternalBackends {
* network: { mac: <MAC_ADDRESS>, ip: <IP_ADDRESS> }
* }
*/
- async addClient (credentials, client) {
- return { success: false, status: 'NOT_IMPLEMENTED_EXCEPTION', error: 'The provided backend does not have an addClient method' }
+ async updateClient (credentials, client) {
+ return { success: false, status: 'NOT_IMPLEMENTED_EXCEPTION', error: 'The provided backend does not have an updateClient method' }
+ }
+
+ async uploadTpm (credentials, clientid, clientuuid, tpm) {
+ return { success: false, status: 'NOT_IMPLEMENTED_EXCEPTION', error: 'The provided backend does not have an uploadTpm method' }
}
}