summaryrefslogtreecommitdiffstats
path: root/server/lib/external-backends/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/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/backends')
-rw-r--r--server/lib/external-backends/backends/idoit-backend.js288
1 files changed, 237 insertions, 51 deletions
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 #########################