summaryrefslogtreecommitdiffstats
path: root/server/api/registration.js
diff options
context:
space:
mode:
authorJannik Schönartz2019-03-07 20:20:25 +0100
committerJannik Schönartz2019-03-07 20:20:25 +0100
commit0999302d99156200ff174d5ec56d6831c8afd332 (patch)
tree56dfcf175c3d5b15dd44eb8d156e4448caaa5455 /server/api/registration.js
parent[server/ipranges] Forgot to commit the lib. ¯\_(ツ)_/¯ (diff)
downloadbas-0999302d99156200ff174d5ec56d6831c8afd332.tar.gz
bas-0999302d99156200ff174d5ec56d6831c8afd332.tar.xz
bas-0999302d99156200ff174d5ec56d6831c8afd332.zip
[server] New clients are automaticly added to the groups of the fitting subranges
Add conflict models Sequelize string operators depricated fix IPv4 is now saved as decimal in the database Add host to config instead of hardcoding Rename ip.js lib to iphelper.js
Diffstat (limited to 'server/api/registration.js')
-rw-r--r--server/api/registration.js110
1 files changed, 46 insertions, 64 deletions
diff --git a/server/api/registration.js b/server/api/registration.js
index 2da1431..2a26c4f 100644
--- a/server/api/registration.js
+++ b/server/api/registration.js
@@ -1,40 +1,18 @@
/* global __appdir */
var path = require('path')
var express = require('express')
-var router = express.Router()
-var noAuthRouter = express.Router()
+const { decorateApp } = require('@awaitjs/express')
+var router = decorateApp(express.Router())
+var noAuthRouter = decorateApp(express.Router())
var db = require(path.join(__appdir, 'lib', 'sequelize'))
-const ExternalBackends = require(path.join(__appdir, 'lib', 'external-backends'))
const backendHelper = require(path.join(__appdir, 'lib', 'external-backends', 'backendhelper'))
+const ipHelper = require(path.join(__appdir, 'lib', 'iphelper'))
+const config = require(path.join(__appdir, 'config', 'config'))
+const url = config.https.host + ':' + config.https.port
// GET requests.
/*
- * TODO: CURRENTLY TEST FUNCTION
- */
-noAuthRouter.get('/', (req, res) => {
- // backendHelper.deleteClients().then(r => {
- // res.send(r)
- // })
-
- /*
- db.backend.findOne({ where: { id: 1 } }).then(result => {
- const b = new ExternalBackends()
- const instance = b.getInstance(result.type)
- instance.getClient(result.credentials, {}).then(result => {
- 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)
- })
- })
-})
-
-/*
* Returns all registration hooks sorted by sortValue.
*
* @return: List of registration hooks
@@ -50,7 +28,7 @@ router.get('/hooks', (req, res) => {
/*
* Reorders the registration hooks based on an array of hook ids.
*/
-router.post('/hookorder', async (req, res) => {
+router.postAsync('/hookorder', async (req, res) => {
var idSortvalueMap = {}
req.body.ids.forEach((id, index) => {
idSortvalueMap[id] = index
@@ -64,7 +42,7 @@ router.post('/hookorder', async (req, res) => {
res.end()
})
-router.post(['/hooks', '/hooks/:id'], async (req, res) => {
+router.postAsync(['/hooks', '/hooks/:id'], async (req, res) => {
var item = {
name: req.body.name,
description: req.body.description,
@@ -130,7 +108,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) => {
+noAuthRouter.postAsync('/add', async (req, res) => {
const feedback = req.body.feedback
const mac = req.body.mac
const uuid = req.body.uuid
@@ -138,40 +116,44 @@ noAuthRouter.post('/add', (req, res) => {
var name = req.body.name
const parentId = req.body.id
const purpose = req.body.purpose
+ let parentIds = []
if (!name) name = 'Client_' + uuid
- db.client.findOne({ where: { uuid: uuid } }).then(client => {
- if (client) res.send(`#!ipxe\nchain https://bas.intra.uni-freiburg.de/api/configloader/\${uuid}`)
- else {
- var groupids = []
- if (parentId) groupids = [parentId]
- getNextHookScript(groupids).then(resId => {
- 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.
- var c = { title: name, uuid: uuid, network: { mac: mac, ip: ip } }
- if (parentId) c.parentId = parentId
- if (purpose) c.purpose = purpose
- 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) {
- db.backend.findOne({ where: { id: response.backendId }, include: ['mappedClients'] }).then(backend => {
- backend.addMappedClients(newClient, { through: { externalId: response.id, externalType: response.type } })
- })
- }
- })
- })
- if (!feedback) res.send(`#!ipxe\nchain https://bas.intra.uni-freiburg.de/api/configloader/\${uuid}`)
- })
- })
+ const client = await db.client.findOne({ where: { uuid: uuid } })
+
+ if (client) return res.send(`#!ipxe\nchain https://` + url + `/api/configloader/\${uuid}`)
+ // Else
+ var groupids = []
+ if (parentId) groupids = [parentId]
+ const resId = await getNextHookScript(groupids)
+ const newClient = await db.client.create({ name: name, description: 'Client', ip: ip, mac: mac, uuid: uuid, registrationState: resId })
+ if (parentId) {
+ newClient.addGroup(parentId)
+ parentIds.push(parentId)
+ } else {
+ // Filtered list with all group we will add the client
+ parentIds = await ipHelper.getGroups(ip)
+ parentIds.forEach(pid => { newClient.addGroup(pid) })
+ // TODO: check if there are multiple groups which are mapped to a backend if so --> merge conflict
+ }
+
+ // Add the client to the backends.
+ var c = { 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}`)
})
noAuthRouter.post('/:uuid/update', (req, res) => {
@@ -345,8 +327,8 @@ function getNextHookScript (groupids, sortvalue) {
// Gets the list of all groupids inclusive the recursive parents.
return getRecursiveParents(groupids).then(gids => {
// Get the list of all hooks where the parent is null or those who fullfill the group dependency.
- var options = { where: { '$groups.id$': { $or: [null, gids] } }, include: ['groups'], order: [['sortvalue', 'ASC']] }
- if (sortvalue !== undefined) options.where.sortvalue = { $gt: sortvalue }
+ var options = { where: { '$groups.id$': { [db.Op.or]: [null, gids] } }, include: ['groups'], order: [['sortvalue', 'ASC']] }
+ if (sortvalue !== undefined) options.where.sortvalue = { [db.Op.gt]: sortvalue }
return db.registrationhook.findAll(options).then(result => {
var resID = null
if (result.length >= 1) resID = result[0].id
@@ -386,7 +368,7 @@ function getRecursiveParents (groupIds) {
* Used by the manual registration.
*/
function buildIpxeMenu (id, name, groups, parents) {
- var basUrl = 'https://bas.intra.uni-freiburg.de'
+ var basUrl = 'https://' + url
var script = '#!ipxe\r\n'
// script = script.concat(`console --picture \${img} --x 800 --y 600 || shell\r\n`)