summaryrefslogtreecommitdiffstats
path: root/server/api
diff options
context:
space:
mode:
authorJannik Schönartz2019-03-15 05:49:30 +0100
committerJannik Schönartz2019-03-15 05:49:30 +0100
commitc2c64dafe7f2a4fc2accd7ee5f87d382886bbedb (patch)
tree08ac85332cdff8af7e682add94223dcc2bc29e60 /server/api
parentmerge (diff)
downloadbas-c2c64dafe7f2a4fc2accd7ee5f87d382886bbedb.tar.gz
bas-c2c64dafe7f2a4fc2accd7ee5f87d382886bbedb.tar.xz
bas-c2c64dafe7f2a4fc2accd7ee5f87d382886bbedb.zip
[external-backeds] Big idoit rework, to match the updated api
Diffstat (limited to 'server/api')
-rw-r--r--server/api/backends.js133
-rw-r--r--server/api/registration.js141
2 files changed, 128 insertions, 146 deletions
diff --git a/server/api/backends.js b/server/api/backends.js
index 7738ec7..838467a 100644
--- a/server/api/backends.js
+++ b/server/api/backends.js
@@ -59,17 +59,15 @@ router.getAsync('/:id', async (req, res) => {
*
* @return: Returns a list with all objects of the backend.
*/
-router.get('/:id/objects', (req, res) => {
+router.getAsync('/:id/objects', async (req, res) => {
const id = req.params.id
- db.backend.findOne({ where: { id: id } }).then(backend => {
- if (backend) {
- const ba = new ExternalBackends()
- const instance = ba.getInstance(backend.type)
- instance.getObjects(backend.credentials).then(result => {
- res.status(200).send(result)
- })
- } else res.status(500).send({ error: 'INVALID_BACKEND_ID', message: 'The provided backend id is invalid.' })
- })
+ const backend = await db.backend.findOne({ where: { id: id } })
+ if (backend) {
+ const ba = new ExternalBackends()
+ const instance = ba.getInstance(backend.type)
+ const result = await instance.getObjects(backend.credentials)
+ res.status(200).send(result)
+ } else res.status(500).send({ error: 'INVALID_BACKEND_ID', message: 'The provided backend id is invalid.' })
})
/*
@@ -78,18 +76,16 @@ router.get('/:id/objects', (req, res) => {
*
* @return: Returns information about a given object and all childs.
*/
-router.get('/:id/objects/:oid', (req, res) => {
+router.getAsync('/:id/objects/:oid', async (req, res) => {
const id = req.params.id
const oid = req.params.oid
- db.backend.findOne({ where: { id: id } }).then(backend => {
- if (backend) {
- const ba = new ExternalBackends()
- const instance = ba.getInstance(backend.type)
- instance.getObject(backend.credentials, oid).then(result => {
- res.status(200).send(result)
- })
- } else res.status(500).send({ error: 'INVALID_BACKEND_ID', message: 'The provided backend id is invalid.' })
- })
+ const backend = await db.backend.findOne({ where: { id: id } })
+ if (backend) {
+ const ba = new ExternalBackends()
+ const instance = ba.getInstance(backend.type)
+ const result = await instance.getObject(backend.credentials, oid)
+ res.status(200).send(result)
+ } else res.status(500).send({ error: 'INVALID_BACKEND_ID', message: 'The provided backend id is invalid.' })
})
/*
@@ -97,15 +93,13 @@ router.get('/:id/objects/:oid', (req, res) => {
*
* @return: Returns a list of all the object types of the given backend. [{id: <id>, title: <title>}, ...]
*/
-router.get('/:id/objecttypes', (req, res) => {
+router.getAsync('/:id/objecttypes', async (req, res) => {
const id = req.params.id
- db.backend.findOne({ where: { id: id } }).then(backend => {
- const ba = new ExternalBackends()
- const instance = ba.getInstance(backend.type)
- instance.getObjectTypes(backend.credentials).then(result => {
- res.status(200).send(result)
- })
- })
+ const backend = await db.backend.findOne({ where: { id: id } })
+ const ba = new ExternalBackends()
+ const instance = ba.getInstance(backend.type)
+ const result = await instance.getObjectTypes(backend.credentials)
+ res.status(200).send(result)
})
/*
@@ -301,41 +295,37 @@ router.get('/:id/import', (req, res) => {
// TODO: Move to clients?
// TODO: receive files should be authenticated!
// Gets a list of all the files uploaded and connected to the client.
-noAuthRouter.get('/:id/:uuid/files', (req, res) => {
+noAuthRouter.getAsync('/:id/:uuid/files', async (req, res) => {
const id = req.params.id
const uuid = req.params.uuid
- db.backend.findOne({ where: { id: id, '$mappedClients.uuid$': uuid }, include: ['mappedClients'] }).then(backend => {
- if (backend && backend.mappedClients.length === 1) {
- const externalId = backend.mappedClients[0].backend_x_client.externalId
- const b = new ExternalBackends()
- const instance = b.getInstance(backend.type)
- instance.getFileList(backend.credentials, externalId).then(fileList => {
- res.send({ success: true, data: { backendId: id, clientUUID: uuid, externalId: externalId, fileList: fileList } })
- })
- } else {
- res.send({ success: false, error: 'CLIENT_NOT_FOUND', message: 'Couldn\'t find the client' })
- }
- })
+ const backend = await db.backend.findOne({ where: { id: id, '$mappedClients.uuid$': uuid }, include: ['mappedClients'] })
+ if (backend && backend.mappedClients.length === 1) {
+ const externalId = backend.mappedClients[0].backend_x_client.externalId
+ const b = new ExternalBackends()
+ const instance = b.getInstance(backend.type)
+ const fileList = await instance.getFileList(backend.credentials, externalId)
+ res.send({ success: true, data: { backendId: id, clientUUID: uuid, externalId: externalId, fileList: fileList } })
+ } else {
+ res.send({ success: false, error: 'CLIENT_NOT_FOUND', message: 'Couldn\'t find the client' })
+ }
})
// Returns the content of a file
-noAuthRouter.get('/:id/:uuid/files/:filename', (req, res) => {
+noAuthRouter.getAsync('/:id/:uuid/files/:filename', async (req, res) => {
const id = req.params.id
const uuid = req.params.uuid
const filename = req.params.filename
- db.backend.findOne({ where: { id: id, '$mappedClients.uuid$': uuid }, include: ['mappedClients'] }).then(backend => {
- if (backend && backend.mappedClients.length === 1) {
- const externalId = backend.mappedClients[0].backend_x_client.externalId
- const b = new ExternalBackends()
- const instance = b.getInstance(backend.type)
- instance.getFile(backend.credentials, externalId, filename).then(file => {
- file.decoded = Buffer.from(file.value, 'base64').toString('UTF-8')
- res.send(file)
- })
- } else {
- res.send({ success: false, error: 'CLIENT_NOT_FOUND', message: 'Couldn\'t find the client' })
- }
- })
+ const backend = await db.backend.findOne({ where: { id: id, '$mappedClients.uuid$': uuid }, include: ['mappedClients'] })
+ if (backend && backend.mappedClients.length === 1) {
+ const externalId = backend.mappedClients[0].backend_x_client.externalId
+ const b = new ExternalBackends()
+ const instance = b.getInstance(backend.type)
+ const file = await instance.getFile(backend.credentials, externalId, filename)
+ file.decoded = Buffer.from(file.value, 'base64').toString('UTF-8')
+ res.send(file)
+ } else {
+ res.send({ success: false, error: 'CLIENT_NOT_FOUND', message: 'Couldn\'t find the client' })
+ }
})
// POST requests
@@ -347,24 +337,22 @@ noAuthRouter.get('/:id/:uuid/files/:filename', (req, res) => {
* type: <BACKEND_TYPE>
* credentials: <BACKEND_CREDENTIALS>
*
- * If the id is set, the backend in the db ist testet.
- * Else the backend is postet in the request.
+ * If the id is set, the backend in the db is testet.
+ * Else the backend is posted in the request.
*/
-router.post('/:id/connection', (req, res) => {
+router.postAsync('/:id/connection', async (req, res) => {
const id = req.params.id
- var getBackend = null
- if (id !== '0') getBackend = db.backend.findOne({ where: { id: id } })
- else getBackend = new Promise(resolve => resolve(req.body))
+ let backend = null
+ if (id !== '0') backend = await db.backend.findOne({ where: { id: id } })
+ else backend = req.body
- getBackend.then(backend => {
- // Creating the backend instance and calling the specific checkConnection.
- const b = new ExternalBackends()
- const instance = b.getInstance(backend.type)
- instance.checkConnection(backend.credentials).then(connection => {
- res.status(200).send({ success: connection.success, error: connection.error })
- })
- })
+ // Creating the backend instance and calling the specific checkConnection.
+ const b = new ExternalBackends()
+ const instance = b.getInstance(backend.type)
+ const response = await instance.checkConnection(backend.credentials)
+ if (response.error) return res.status(500).send(response)
+ else if (response) res.status(200).send()
})
// PUT requests
@@ -436,12 +424,11 @@ router.put('/:id/syncsettings', (req, res) => {
*
* Deletes the backend to the given id.
*/
-router.post('/delete', (req, res) => {
+router.postAsync('/delete', async (req, res) => {
const backendIds = req.body.id
- db.backend.destroy({ where: { id: backendIds } }).then(function () {
- res.status(200).send('success')
- })
+ await db.backend.destroy({ where: { id: backendIds } })
+ res.status(200).send('success')
})
module.exports.router = router
diff --git a/server/api/registration.js b/server/api/registration.js
index 2a26c4f..3eb36e0 100644
--- a/server/api/registration.js
+++ b/server/api/registration.js
@@ -114,7 +114,7 @@ noAuthRouter.postAsync('/add', async (req, res) => {
const uuid = req.body.uuid
const ip = req.body.ip
var name = req.body.name
- const parentId = req.body.id
+ const parentId = parseInt(req.body.id)
const purpose = req.body.purpose
let parentIds = []
@@ -139,24 +139,15 @@ noAuthRouter.postAsync('/add', async (req, res) => {
}
// Add the client to the backends.
- var c = { title: name, uuid: uuid, network: { mac: mac, ip: ip } }
+ var c = { id: newClient.id, title: name, uuid: uuid, network: { mac: mac, ip: ip } }
if (parentIds.length > 0) c.parents = parentIds
- // if (parentId) c.parentId = parentId
if (purpose) c.purpose = purpose
const result = await backendHelper.addClient(c)
if (feedback) res.send(result)
-
- for (let response of result) {
- // If the object was created we need to make the objectid / external id mapping.
- if (response.success) {
- const backend = await db.backend.findOne({ where: { id: response.backendId }, include: ['mappedClients'] })
- backend.addMappedClients(newClient, { through: { externalId: response.id, externalType: response.type } })
- }
- }
- if (!feedback) res.send(`#!ipxe\nchain https://` + url + `/api/configloader/\${uuid}`)
+ else res.send(`#!ipxe\nchain https://` + url + `/api/configloader/\${uuid}`)
})
-noAuthRouter.post('/:uuid/update', (req, res) => {
+noAuthRouter.postAsync('/:uuid/update', async (req, res) => {
const uuid = req.params.uuid
const name = req.body.name
const parentId = req.body.id
@@ -174,85 +165,89 @@ noAuthRouter.post('/:uuid/update', (req, res) => {
const cpuCores = req.body.cpu_cores
// RAM
- var ramSize = req.body.ram_size.split('\n')
- var ramManufacturer = req.body.ram_manufacturer.split('\n')
- var ramFormfactor = req.body.ram_formfactor.split('\n')
- var ramType = req.body.ram_type.split('\n')
- var ramIsEcc = req.body.ram_isecc.replace('Error Correction Type: ', '')
- var ramModules = []
- // Build ram array
- for (var ram in ramSize) {
- if (ramSize[ram].replace('Size: ', '') !== 'No Module Installed') {
- const size = ramSize[ram].replace('Size: ', '').split(' ')
- var title = ramFormfactor[ram].replace('Form Factor: ', '')
- if (ramIsEcc === 'Single-bit ECC') title += '-ECC'
-
- var ramModule = {
- capacity: size[0],
- unit: size[1],
- manufacturer: ramManufacturer[ram].replace('Manufacturer: ', ''),
- title: title,
- type: ramType[ram].replace('Type: ', '')
+ if (req.body.ram_size) {
+ const ramSize = req.body.ram_size.split('\n')
+ const ramManufacturer = req.body.ram_manufacturer.split('\n')
+ const ramFormfactor = req.body.ram_formfactor.split('\n')
+ const ramType = req.body.ram_type.split('\n')
+ const ramIsEcc = req.body.ram_isecc.replace('Error Correction Type: ', '')
+ var ramModules = []
+
+ // Build ram array
+ for (let ram in ramSize) {
+ if (ramSize[ram].replace('Size: ', '') !== 'No Module Installed') {
+ const size = ramSize[ram].replace('Size: ', '').split(' ')
+ let title = ramFormfactor[ram].replace('Form Factor: ', '')
+ if (ramIsEcc === 'Single-bit ECC') title += '-ECC'
+
+ const ramModule = {
+ capacity: size[0],
+ unit: size[1],
+ manufacturer: ramManufacturer[ram].replace('Manufacturer: ', ''),
+ title: title,
+ type: ramType[ram].replace('Type: ', '')
+ }
+ ramModules.push(ramModule)
}
- ramModules.push(ramModule)
}
}
// SSD / HDD
- var drivesRaw = req.body.drives.split('%OBJECT_SPLITTER%')
- var drives = []
- for (var driveRaw in drivesRaw) {
- if (drivesRaw[driveRaw].length > 0) {
- var dRaw = drivesRaw[driveRaw].split('%ATTRIBUTE_SPLITTER%')
- var drive = {
- model: dRaw[0].trim().replace('Device Model: ', ''),
- serial: dRaw[1].trim().replace('Serial Number: ', ''),
- capacity: dRaw[2].trim().split(' ')[0],
- unit: dRaw[2].trim().split(' ')[1],
- type: dRaw[3].trim().replace('Rotation Rate: ', ''),
- formfactor: dRaw[4].trim().replace('Form Factor: ', ''),
- connection: dRaw[5].trim().replace('SATA Version is: ', '')
+ if (req.body.drives) {
+ const drivesRaw = req.body.drives.split('%OBJECT_SPLITTER%')
+ var drives = []
+ for (let driveRaw in drivesRaw) {
+ if (drivesRaw[driveRaw].length > 0) {
+ const dRaw = drivesRaw[driveRaw].split('%ATTRIBUTE_SPLITTER%')
+ const drive = {
+ model: dRaw[0].trim().replace('Device Model: ', ''),
+ serial: dRaw[1].trim().replace('Serial Number: ', ''),
+ capacity: dRaw[2].trim().split(' ')[0],
+ unit: dRaw[2].trim().split(' ')[1],
+ type: dRaw[3].trim().replace('Rotation Rate: ', ''),
+ formfactor: dRaw[4].trim().replace('Form Factor: ', ''),
+ connection: dRaw[5].trim().replace('SATA Version is: ', '')
+ }
+ drives.push(drive)
}
- drives.push(drive)
}
}
- db.client.findOne({ where: { uuid: uuid } }).then(client => {
- client.update({ name: name })
- var c = { uuid: uuid, id: client.id }
- if (name) c.title = name
- if (parentId) c.parentId = parentId
+ const client = await db.client.findOne({ where: { uuid: uuid } })
+ if (!client) return res.status(404).send({ error: 'CLIENT_NOT_FOUND', message: 'There is no client matching the provided uuid.' })
- // System data. Sometime just string with whitespaces only.
- c.system = {}
- if (/\S/.test(sysManufacturer)) c.system.manufacturer = sysManufacturer
- else c.system.manufacturer = 'Not set'
+ client.update({ name: name })
+ var c = { uuid: uuid, id: client.id }
+ if (name) c.title = name
+ if (parentId) c.parentId = parentId
- if (/\S/.test(sysModel)) c.system.model = sysModel
- else c.system.model = 'Not set'
+ // System data. Sometime just string with whitespaces only.
+ c.system = {}
+ if (/\S/.test(sysManufacturer)) c.system.manufacturer = sysManufacturer
+ else c.system.manufacturer = 'Not set'
- if (/\S/.test(sysSerial)) c.system.serialnumber = sysSerial
- else c.system.serialnumber = 'Not set'
+ if (/\S/.test(sysModel)) c.system.model = sysModel
+ else c.system.model = 'Not set'
- // TODO: MULTI GPU's ?!
- c.cpu = { model: cpuModel, manufacturer: cpuManufacturer, type: cpuType, frequency: cpuFrequency, cores: cpuCores }
- c.ram = ramModules
- c.drives = drives
+ if (/\S/.test(sysSerial)) c.system.serialnumber = sysSerial
+ else c.system.serialnumber = 'Not set'
- backendHelper.updateClient(c).then(result => {
- res.send(result)
- })
- })
+ // TODO: MULTI GPU's ?!
+ c.cpu = { model: cpuModel, manufacturer: cpuManufacturer, type: cpuType, frequency: cpuFrequency, cores: cpuCores }
+ if (ramModules) c.ram = ramModules
+ if (drives) c.drives = drives
+
+ const result = await backendHelper.updateClient(c)
+ res.send(result)
})
/*
* Mehtod for uploading the tpm key and stuff.
*/
-noAuthRouter.put('/:uuid/files', (req, res) => {
- db.client.findOne({ where: { uuid: req.params.uuid } }).then(client => {
- backendHelper.uploadFiles(client.id, req.files)
- res.send()
- })
+noAuthRouter.putAsync('/:uuid/files', async (req, res) => {
+ const client = await db.client.findOne({ where: { uuid: req.params.uuid } })
+ const result = await backendHelper.uploadFiles(client.id, req.files)
+ res.send(result)
})
/*