From 3cb40c9cda0ffe4102901b87c0544c6021071185 Mon Sep 17 00:00:00 2001 From: Jannik Schönartz Date: Mon, 3 Dec 2018 02:05:52 +0000 Subject: [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. --- server/api/clients.js | 4 +- server/api/registrations.js | 72 ++++-- server/app.js | 2 + server/ipxe/grepSystemInfo.sh | 64 ++--- server/lib/external-backends/backendhelper.js | 91 ++++--- .../external-backends/backends/idoit-backend.js | 288 +++++++++++++++++---- server/lib/external-backends/index.js | 25 +- server/package-lock.json | 104 ++++++++ server/package.json | 1 + 9 files changed, 500 insertions(+), 151 deletions(-) (limited to 'server') diff --git a/server/api/clients.js b/server/api/clients.js index 54832eb..147314d 100644 --- a/server/api/clients.js +++ b/server/api/clients.js @@ -43,9 +43,9 @@ module.exports.post = { }, // delete clients - delete: function (req, res) { + delete: async function (req, res) { + await backendHelper.deleteClients(req.body.ids) db.client.destroy({ where: { id: req.body.ids } }).then(count => { - backendHelper.deleteClients() res.send({ count }) }) } diff --git a/server/api/registrations.js b/server/api/registrations.js index 74e7d84..4eaff6d 100644 --- a/server/api/registrations.js +++ b/server/api/registrations.js @@ -4,7 +4,7 @@ var express = require('express') var router = express.Router() var noAuthRouter = express.Router() var db = require(path.join(__appdir, 'lib', 'sequelize')) -// const ExternalBackends = require(path.join(__appdir, 'lib', 'external-backends')) +const ExternalBackends = require(path.join(__appdir, 'lib', 'external-backends')) const backendHelper = require(path.join(__appdir, 'lib', 'external-backends', 'backendhelper')) // GET requests. @@ -13,9 +13,10 @@ const backendHelper = require(path.join(__appdir, 'lib', 'external-backends', 'b * TODO: CURRENTLY TEST FUNCTION */ noAuthRouter.get('/', (req, res) => { - backendHelper.deleteClients().then(r => { - res.send(r) - }) + //backendHelper.deleteClients().then(r => { + // res.send(r) + //}) + /* db.backend.findOne({ where: { id: 1 } }).then(result => { const b = new ExternalBackends() @@ -24,6 +25,13 @@ noAuthRouter.get('/', (req, res) => { res.status(200).send(result) }) }) */ + db.backend.findOne({ where: { id: 3 } }).then(result => { + const b = new ExternalBackends() + const instance = b.getInstance(result.type) + instance.uploadTpm(result.credentials, 99696, 'I-123-d12', null).then(result => { + res.status(200).send(result) + }) + }) }) /* @@ -123,6 +131,7 @@ noAuthRouter.post('/group', (req, res) => { * Adds the client to the database and set parents if a parent was selected. Calls addClient for all external-backends. */ noAuthRouter.post('/add', (req, res) => { + const feedback = req.body.feedback const mac = req.body.mac const uuid = req.body.uuid const ip = req.body.ip @@ -134,45 +143,72 @@ noAuthRouter.post('/add', (req, res) => { var groupids = [] if (parentId) groupids = [parentId] getNextHookScript(groupids).then(resId => { - db.client.create({ name: name, ip: ip, mac: mac, uuid: uuid, registrationState: resId }).then(newClient => { + db.client.create({ name: name, description: 'Client', ip: ip, mac: mac, uuid: uuid, registrationState: resId }).then(newClient => { if (parentId) { newClient.addGroup(parentId) } // Add the client to the backends. - const c = { network: { mac: mac, ip: ip } } + const c = { uuid: uuid, network: { mac: mac, ip: ip } } if (parentId) c.parentId = parentId if (name) c.title = name else c.title = 'Client_' + uuid backendHelper.addClient(c).then(result => { + if (feedback) res.send(result) result.forEach(response => { // If the object was created we need to make the objectid / external id mapping. - if (response.success && response.create) { + if (response.success) { db.backend.findOne({ where: { id: response.backendId }, include: ['mappedClients'] }).then(backend => { backend.addMappedClients(newClient, { through: { externalId: response.id, externalType: response.type } }) }) } }) }) - res.send(`#!ipxe\nchain https://bas.intra.uni-freiburg.de/api/configloader/\${uuid}`) + if (!feedback) res.send(`#!ipxe\nchain https://bas.intra.uni-freiburg.de/api/configloader/\${uuid}`) }) }) } }) }) +noAuthRouter.post('/update', (req, res) => { + const uuid = req.body.uuid + const name = req.body.name + const parentId = req.body.id + const sys_manufacturer = req.body.sys_manufacturer + const sys_model = req.body.sys_model + const sys_serial = req.body.sys_serial + const cpu_model = req.body.cpu_model + const cpu_manufacturer = req.body.cpu_manufacturer + const cpu_type = req.body.cpu_type + const cpu_frequency = req.body.cpu_frequency + const cpu_cores = req.body.cpu_cores + const ram_model = req.body.ram_model + const ram_manufacturer = req.body.ram_manufacturer + const ram_capacity = req.body.ram_capacity + const ram_unit = req.body.ram_unit + + db.client.findOne({ where: { uuid: uuid } }).then(client => { + client.update({ name: name }) + const c = { uuid: uuid, id: client.id } + if (name) c.title = name + if (parentId) c.parentId = parentId + if (sys_manufacturer && sys_model && sys_serial) c.system = { model: sys_model, manufacturer: sys_manufacturer, serialnumber: sys_serial} + if (cpu_model && cpu_manufacturer && cpu_type && cpu_frequency && cpu_cores) c.cpu = { model: cpu_model, manufacturer: cpu_manufacturer, type: cpu_type, frequency: cpu_frequency, cores: cpu_cores } + if (ram_model && ram_manufacturer && ram_capacity && ram_unit) c.ram = [{ model: ram_model, manufacturer: ram_manufacturer, capacity: ram_capacity, unit: ram_unit }] + + backendHelper.updateClient(c).then(result => { + res.send(result) + }) + }) +}) + /* - * Adds additional information for the backends of the client in the firstregistration. + * Mehtod for uploading the tpm key and stuff. */ -noAuthRouter.post('/addInfo', (req, res) => { - const id = req.body.id - const systemModel = req.body.sysmodel - const systemManufacturer = req.body.sysmanu - const systemSerial = req.body.sysserial - - // Add the client to the backends. - backendHelper.addClient({ id: id, system: { model: systemModel, manufacturer: systemManufacturer, serialnumber: systemSerial } }) - res.send({ status: 'success' }) +noAuthRouter.put('/:uuid/tpm', (req, res) => { + console.log(req.files) + res.send() }) /* diff --git a/server/app.js b/server/app.js index bc4a58b..4e72fa5 100644 --- a/server/app.js +++ b/server/app.js @@ -2,6 +2,7 @@ var createError = require('http-errors') var express = require('express') var cookieParser = require('cookie-parser') var compression = require('compression') +var fileUpload = require('express-fileupload') var path = require('path') // var fs = require('fs') @@ -23,6 +24,7 @@ app.use(express.urlencoded({ extended: false })) var logger = require('morgan') app.use(logger('dev')) app.use(cookieParser()) +app.use(fileUpload()) // ############################################################################ // ########################### setup routes ################################## diff --git a/server/ipxe/grepSystemInfo.sh b/server/ipxe/grepSystemInfo.sh index 024fb52..2f8e6cc 100644 --- a/server/ipxe/grepSystemInfo.sh +++ b/server/ipxe/grepSystemInfo.sh @@ -3,73 +3,45 @@ #dmidecode | grep -i UUID # MAC -MAC=$(ip addr show | grep -Eo -m 1 'ether\s.*\sbrd') -MAC=${MAC#"ether "} -MAC=${MAC%" brd"} -echo "MAC: $MAC" +# MAC=$(ip addr show | grep -Eo -m 1 'ether\s.*\sbrd') +# MAC=${MAC#"ether "} +# MAC=${MAC%" brd"} # UUID UUID=$(dmidecode -q -s system-uuid | grep -v '^#' | head -n 1 | tr '[a-z]' '[A-Z]') -echo "UUID: $UUID" -# System Information -echo "" -echo "######## System Informations ########" +# System MANUFACTURER=$(dmidecode -q -s system-manufacturer) -echo "Manufacturer: $MANUFACTURER" - MODEL=$(dmidecode -q -s system-product-name) -echo "Model: $MODEL" - SERIAL=$(dmidecode -q -s system-serial-number) -echo "Serial number: $SERIAL" - VENDOR=$(dmidecode | grep -Eo '(Vendor).*') -VENDOR=${VENDOR#Vendor: } -echo "Vendor: $VENDOR" +VENDOR=${VENDOR#"Vendor: "} # Mainboard -echo "" -echo "######## Mainboard Informations ########" MAINBOARD_MANUFACTURER=$(dmidecode -q -s baseboard-manufacturer) -echo "Manufacturer: $MAINBOARD_MANUFACTURER" MAINBOARD_MODEL=$(dmidecode -q -s baseboard-product-name) -echo "Model: $MAINBOARD_MODEL" MAINBOARD_SERIAL_NUMBER=$(dmidecode -q -s baseboard-serial-number) -echo "Serial: $MAINBOARD_SERIAL_NUMBER" # CPU -echo "" -echo "######## CPU Informations ########" CPU_MODEL=$(dmidecode -q -s processor-version) -echo "Model: $CPU_MODEL" CPU_MANUFACTURER=$(dmidecode -q -s processor-manufacturer) -echo "Manufacturer: $CPU_MANUFACTURER" CPU_TYPE=$(dmidecode -q -s processor-family) -echo "Type: $CPU_TYPE" -CPUCORES=$(cat /sys/devices/system/cpu/cpu*/topology/thread_siblings_list | sort -u | wc -l) -echo "$CPUCORES Cores" +CPU_CORES=$(cat /sys/devices/system/cpu/cpu*/topology/thread_siblings_list | sort -u | wc -l) #CPUMODEL=$(grep -m1 '^model name\s*:' /proc/cpuinfo | sed 's/^model name\s*:\s*//;s/\s\s*/ /g;s/^ //;s/ $//') #echo "$CPUMODEL" CPU_FREQUENCY=$(dmidecode -q -s processor-frequency) -echo "Frequency: $CPU_FREQUENCY" # RAM -echo "" -echo "######## RAM Informations ########" -RAM=$(grep -m1 '^MemTotal:' /proc/meminfo | awk '{print $2}') -RAM=$(( $RAM / 1024 )) -if [ -z "$RAM" ] || [ "$RAM" -lt 500 ]; then - # Fallback to dmidecode - RAM=0 - for c in $(dmidecode -t 17 | grep -o 'Size:.*MB$' | awk '{print $2}'); do - RAM=$(( $RAM + $c )) - done -fi -echo "$RAM MB RAM" - -echo "" -#echo "######## CURL ########" +# RAM=$(grep -m1 '^MemTotal:' /proc/meminfo | awk '{print $2}') +# RAM=$(( $RAM / 1024 )) +# if [ -z "$RAM" ] || [ "$RAM" -lt 500 ]; then +# # Fallback to dmidecode +# RAM=0 +# for c in $(dmidecode -t 17 | grep -o 'Size:.*MB$' | awk '{print $2}'); do +# RAM=$(( $RAM + $c )) +# done +# fi + +# Curl #curl --data "state=6" --insecure https://bas.stfu-kthx.net:8888/api/registrations/$UUID/state -#echo "" -#echo "" +curl -d "uuid=$UUID&name=Client_$UUID&sys_manufacturer=$MANUFACTURER&sys_model=$MODEL&sys_serial=$SERIAL&cpu_model=$CPU_MODEL&cpu_manufacturer=$CPU_MANUFACTURER&cpu_type=$CPU_TYPE&cpu_cores=$CPU_CORES&cpu_frequency=$CPU_FREQUENCY" -H "Content-Type: application/x-www-form-urlencoded" -X POST https://bas.intra.uni-freiburg.de/api/registrations/$UUID/update 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: , title: , parentId: , - * system: { model: , manufacturer: , serialnumber: }, - * cpu: { model: , manufacturer: , type: , frequency: , cores: }, - * ram: [{ model: , manufacturer: , type: , capacity: , unit: }, ...], - * storage: {}, * network: { mac: , ip: } * } */ @@ -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: + * The client parameters are all optional. + * client: { + * id: , title: , parentId: , + * system: { model: , manufacturer: , serialnumber: }, + * cpu: { model: , manufacturer: , type: , frequency: , cores: }, + * ram: [{ model: , manufacturer: , type: , capacity: , 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: * @@ -127,7 +128,21 @@ class ExternalBackends { * 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: , title: , parentId: , + * id: , title: , parentId: , uuid: , + * network: { mac: , ip: } + * } + */ + 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: + * The client parameters are all optional. + * client: { + * id: , title: , parentId: , uuid: , * system: { model: , manufacturer: , serialnumber: }, * cpu: { model: , manufacturer: , type: , frequency: , cores: }, * ram: [{ model: , manufacturer: , type: , capacity: , unit: }, ...], @@ -135,8 +150,12 @@ class ExternalBackends { * network: { mac: , ip: } * } */ - 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' } } } diff --git a/server/package-lock.json b/server/package-lock.json index 0e58316..14fe543 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -380,6 +380,38 @@ "resolved": "https://registry.npmjs.org/builtin-modules/-/builtin-modules-1.1.1.tgz", "integrity": "sha1-Jw8HbFpywC9bZaR9+Uxf46J4iS8=" }, + "busboy": { + "version": "0.2.14", + "resolved": "https://registry.npmjs.org/busboy/-/busboy-0.2.14.tgz", + "integrity": "sha1-bCpiLvz0fFe7vh4qnDetNseSVFM=", + "requires": { + "dicer": "0.2.5", + "readable-stream": "1.1.x" + }, + "dependencies": { + "isarray": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz", + "integrity": "sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8=" + }, + "readable-stream": { + "version": "1.1.14", + "resolved": "http://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz", + "integrity": "sha1-fPTFTvZI44EwhMY23SB54WbAgdk=", + "requires": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.1", + "isarray": "0.0.1", + "string_decoder": "~0.10.x" + } + }, + "string_decoder": { + "version": "0.10.31", + "resolved": "http://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz", + "integrity": "sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ=" + } + } + }, "bytes": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.0.0.tgz", @@ -438,6 +470,11 @@ "integrity": "sha512-mT8iDcrh03qDGRRmoA2hmBJnxpllMR+0/0qlzjqZES6NdiWDcZkCNAk4rPFZ9Q85r27unkiNNg8ZOiwZXBHwcA==", "dev": true }, + "charenc": { + "version": "0.0.2", + "resolved": "https://registry.npmjs.org/charenc/-/charenc-0.0.2.tgz", + "integrity": "sha1-wKHS86cJLgN3S/qD8UwPxXkKhmc=" + }, "circular-json": { "version": "0.3.3", "resolved": "https://registry.npmjs.org/circular-json/-/circular-json-0.3.3.tgz", @@ -674,6 +711,11 @@ "which": "^1.2.9" } }, + "crypt": { + "version": "0.0.2", + "resolved": "https://registry.npmjs.org/crypt/-/crypt-0.0.2.tgz", + "integrity": "sha1-iNf/fsDfuG9xPch7u0LQRNPmxBs=" + }, "d": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/d/-/d-1.0.0.tgz", @@ -776,6 +818,38 @@ "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.0.4.tgz", "integrity": "sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA=" }, + "dicer": { + "version": "0.2.5", + "resolved": "https://registry.npmjs.org/dicer/-/dicer-0.2.5.tgz", + "integrity": "sha1-WZbAhrszIYyBLAkL3cCc0S+stw8=", + "requires": { + "readable-stream": "1.1.x", + "streamsearch": "0.1.2" + }, + "dependencies": { + "isarray": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz", + "integrity": "sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8=" + }, + "readable-stream": { + "version": "1.1.14", + "resolved": "http://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz", + "integrity": "sha1-fPTFTvZI44EwhMY23SB54WbAgdk=", + "requires": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.1", + "isarray": "0.0.1", + "string_decoder": "~0.10.x" + } + }, + "string_decoder": { + "version": "0.10.31", + "resolved": "http://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz", + "integrity": "sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ=" + } + } + }, "doctrine": { "version": "1.5.0", "resolved": "https://registry.npmjs.org/doctrine/-/doctrine-1.5.0.tgz", @@ -1399,6 +1473,16 @@ } } }, + "express-fileupload": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/express-fileupload/-/express-fileupload-1.0.0.tgz", + "integrity": "sha512-6VS9MiPIXXFFKv5+cRS5+iMh3Zw6KadiSEM+SPRMSC3AEoV3ZOfRUk3ogjDtKVr4o9n3EoHTMyyqbuzBj8gMLw==", + "requires": { + "busboy": "^0.2.14", + "md5": "^2.2.1", + "streamifier": "^0.1.1" + } + }, "extend": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/extend/-/extend-3.0.2.tgz", @@ -2188,6 +2272,16 @@ "es5-ext": "~0.10.2" } }, + "md5": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/md5/-/md5-2.2.1.tgz", + "integrity": "sha1-U6s41f48iJG6RlMp6iP6wFQBJvk=", + "requires": { + "charenc": "~0.0.1", + "crypt": "~0.0.1", + "is-buffer": "~1.1.1" + } + }, "media-typer": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", @@ -3658,6 +3752,16 @@ "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.4.0.tgz", "integrity": "sha512-zhSCtt8v2NDrRlPQpCNtw/heZLtfUDqxBM1udqikb/Hbk52LK4nQSwr10u77iopCW5LsyHpuXS0GnEc48mLeew==" }, + "streamifier": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/streamifier/-/streamifier-0.1.1.tgz", + "integrity": "sha1-l+mNj6TRBdYqJpHR3AfoINuN/E8=" + }, + "streamsearch": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-0.1.2.tgz", + "integrity": "sha1-gIudDlb8Jz2Am6VzOOkpkZoanxo=" + }, "string-width": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz", diff --git a/server/package.json b/server/package.json index 7061c78..78ed4e6 100644 --- a/server/package.json +++ b/server/package.json @@ -12,6 +12,7 @@ "cookie-parser": "^1.4.3", "debug": "^3.2.5", "express": "^4.16.3", + "express-fileupload": "^1.0.0", "http-errors": "^1.7.1", "infoblox": "^1.1.5", "jsonwebtoken": "^8.3.0", -- cgit v1.2.3-55-g7522