summaryrefslogtreecommitdiffstats
path: root/server/api/registrations.js
diff options
context:
space:
mode:
authorJannik Schönartz2018-08-29 13:29:57 +0200
committerJannik Schönartz2018-08-29 13:29:57 +0200
commit10d47887c98d1432f7023d0c03a65879b71936fa (patch)
treeff934b874a4600041d10c31f367243dd70610cac /server/api/registrations.js
parent[external-backends] Add getClient methods for all backends (diff)
downloadbas-10d47887c98d1432f7023d0c03a65879b71936fa.tar.gz
bas-10d47887c98d1432f7023d0c03a65879b71936fa.tar.xz
bas-10d47887c98d1432f7023d0c03a65879b71936fa.zip
[registration] Add spaces and parent path to the manual registration
Diffstat (limited to 'server/api/registrations.js')
-rw-r--r--server/api/registrations.js86
1 files changed, 53 insertions, 33 deletions
diff --git a/server/api/registrations.js b/server/api/registrations.js
index 7537124..2253be6 100644
--- a/server/api/registrations.js
+++ b/server/api/registrations.js
@@ -9,13 +9,13 @@ const ExternalBackends = require(path.join(__appdir, 'lib', 'external-backends')
// GET requests.
/*
- * @return:
+ * TODO: CURRENTLY TEST FUNCTION
*/
router.get('/', (req, res) => {
- db.backend.findOne({ where: { type: 'infoblox' } }).then(result => {
+ db.backend.findOne({ where: { id: 1 } }).then(result => {
const b = new ExternalBackends()
const instance = b.getInstance(result.type)
- instance.test(result.credentials).then(result => {
+ instance.getClient(result.credentials, {}).then(result => {
res.status(200).send(result)
})
})
@@ -28,38 +28,20 @@ module.exports.router = router
// POST requests.
/*
+ * Returns all root parents or all childs of a group.
*
- * @return:
+ * @return: Ipxe menu with a list of groups.
*/
-noAuthRouter.post('/client', (req, res) => {
- // const mac = req.body.mac
- // const uuid = req.body.uuid
- // const ip = req.body.ip
- // const name = req.body.name
- // console.log(name.concat(' ', ip, ' ', mac, ' ', uuid))
-
- var script = '#!ipxe\r\n'
- script.concat(`console --picture \${img} --x 800 --y 600 || shell\r\n`)
- db.group.findAll({ where: { '$parents.id$': null }, include: ['parents'] }).then(groups => {
- groups.forEach(g => {
- script = script.concat('echo', ' [', g.id, '] ', g.name, '\r\n')
- })
-
- script = script.concat('read group\r\n')
- script = script.concat('params\r\n')
- script = script.concat(`param id \${group}\r\n`)
- script = script.concat('chain https://bas.stfu-kthx.net:8888/api/registrations/group##params\r\n')
- res.status(200).send(script)
- })
-})
-
noAuthRouter.post('/group', (req, res) => {
const id = req.body.id
- console.log(id)
+ var parents = []
+ console.log(req.body.parents)
+ if (req.body.parents) parents = JSON.parse(req.body.parents)
+ console.log(parents)
if (id === '0') {
db.group.findAll({ where: { '$parents.id$': null }, include: ['parents'] }).then(groups => {
if (groups) {
- res.send(buildIpxeMenu(id, 'Root', groups))
+ res.send(buildIpxeMenu(id, 'Root', groups, []))
} else {
res.status(404).end()
}
@@ -67,7 +49,7 @@ noAuthRouter.post('/group', (req, res) => {
} else {
db.group.findOne({ where: { id: id }, include: ['parents', 'subgroups', 'clients'] }).then(group => {
if (group) {
- res.send(buildIpxeMenu(id, group.name, group.subgroups))
+ res.send(buildIpxeMenu(id, group.name, group.subgroups, parents))
} else {
res.status(404).end()
}
@@ -75,6 +57,9 @@ noAuthRouter.post('/group', (req, res) => {
}
})
+/*
+ * Adds the client in the database and set parents if a parent was selected.
+ */
noAuthRouter.post('/add', (req, res) => {
const mac = req.body.mac
const uuid = req.body.uuid
@@ -96,29 +81,64 @@ noAuthRouter.post('/add', (req, res) => {
module.exports.noAuthRouter = noAuthRouter
-function buildIpxeMenu (id, name, groups) {
+/*
+ * id: id of the current selected location.
+ * name: Name of the current selected location
+ * groups: List of group [{ id: <GROUP_ID>, name: <GROUP_NAME> }, ...]
+ *
+ * Build the ipxe menu out of the list of groups.
+ * Used by the manual registration.
+ */
+function buildIpxeMenu (id, name, groups, parents) {
+ console.log(parents)
var script = '#!ipxe\r\n'
- script.concat(`console --picture \${img} --x 800 --y 600 || shell\r\n`)
+ // script = script.concat(`console --picture \${img} --x 800 --y 600 || shell\r\n`)
+
+ // Add parent to keep track of the path we clicked through.
+ parents.push({ id: id, name: name })
+ script = script.concat(`set space:hex 20:20\r\n`)
+ script = script.concat(`set space \${space:string}\r\n`)
+ script = script.concat(`set parents `, JSON.stringify(parents), '\r\n\r\n')
+
+ // Menu
var menuscript = ''
script = script.concat(':start\r\n')
script = script.concat('menu Choose the group you want the client to be saved in\r\n')
+
+ // Parent menu entries.
+ var spacer = ''
+ parents.forEach(parent => {
+ script = script.concat('item --gap ', spacer, '[', parent.id, '] ', parent.name, '\r\n')
+ spacer = spacer.concat(`\${space}`)
+ })
+
+ // Group menu entries. First 1-9 are pressable via key?
var counter = '1'
groups.forEach(group => {
// script = script.concat('echo', ' [', subgroup.id, '] ', subgroup.name, '\r\n')
- script = script.concat('item --key ', counter, ' ', counter, ' [', group.id, '] ', group.name, '\r\n')
- menuscript = menuscript.concat(':', counter, '\r\n', 'params\r\nparam id ', group.id, '\r\nchain https://bas.stfu-kthx.net:8888/api/registrations/group##params\r\n\r\n')
+ script = script.concat('item --key ', counter, ' ', counter, ' ', spacer, '[', group.id, '] ', group.name, '\r\n')
+ menuscript = menuscript.concat(':', counter, '\r\n', 'params\r\nparam id ', group.id, `\r\nparam parents \${parents}`)
+ menuscript = menuscript.concat('\r\nchain https://bas.stfu-kthx.net:8888/api/registrations/group##params\r\n\r\n')
counter++
})
+
+ // Add client menu
script = script.concat('item select Add client to ', name, '\r\n')
menuscript = menuscript.concat(`:select\r\necho Enter client name\r\nread clientname\r\nparams\r\nparam name \${clientname}\r\n`)
menuscript = menuscript.concat('param id ', id, `\r\nparam mac \${net0/mac}\r\nparam uuid \${uuid}\r\nparam ip \${net0/ip}\r\n`)
menuscript = menuscript.concat('chain https://bas.stfu-kthx.net:8888/api/registrations/add##params\r\n\r\n')
+
+ // Goto start menu
if (id !== '0') {
script = script.concat('item reset Go to start\r\n')
menuscript = menuscript.concat(':reset\r\nparams\r\nparam id ', 0, '\r\nchain https://bas.stfu-kthx.net:8888/api/registrations/group##params\r\n\r\n')
}
+
+ // Exit menu
script = script.concat('item exit Exit manual registration\r\n')
menuscript = menuscript.concat(':exit\r\nexit 1\r\n\r\n')
+
+ // Concat script + menuscript and return it.
script = script.concat(`choose target && goto \${target}\r\n\r\n`)
script = script.concat(menuscript)
return script