summaryrefslogtreecommitdiffstats
path: root/server/api/registrations.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/api/registrations.js')
-rw-r--r--server/api/registrations.js97
1 files changed, 94 insertions, 3 deletions
diff --git a/server/api/registrations.js b/server/api/registrations.js
index eb9a7a0..f6a8f87 100644
--- a/server/api/registrations.js
+++ b/server/api/registrations.js
@@ -5,6 +5,7 @@ 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 backendHelper = require(path.join(__appdir, 'lib', 'external-backends', 'backendhelper'))
// GET requests.
@@ -21,6 +22,65 @@ router.get('/', (req, res) => {
})
})
+/*
+ * Returns all registration hooks sorted by sortValue.
+ *
+ * @return: List of registration hooks
+ */
+router.get('/hooks', (req, res) => {
+ db.registrationhook.findAll({ order: [ ['sortValue', 'ASC'] ], include: [{ model: db.group, as: 'groups', attributes: ['id'] }] }).then(hooks => {
+ res.send(hooks)
+ })
+})
+
+// POST requests.
+
+/*
+ * Reorders the registration hooks based on an array of hook ids.
+ */
+router.post('/hookorder', async (req, res) => {
+ var idSortvalueMap = {}
+ req.body.ids.forEach((id, index) => {
+ idSortvalueMap[id] = index
+ })
+ var hooks = await db.registrationhook.findAll()
+ var promises = []
+ hooks.forEach(hook => {
+ promises.push(hook.update({ sortvalue: idSortvalueMap[hook.id] }))
+ })
+ await Promise.all(promises)
+ res.end()
+})
+
+router.post(['/hooks', '/hooks/:id'], async (req, res) => {
+ var item = {
+ name: req.body.name,
+ description: req.body.description,
+ type: req.body.type,
+ script: req.body.script
+ }
+ var hook = null
+ if (req.params.id > 0) {
+ hook = await db.registrationhook.findOne({ where: { id: req.params.id } })
+ if (hook) await hook.update(item)
+ } else {
+ var maxSortvalue = await db.registrationhook.max('sortvalue')
+ item.sortvalue = maxSortvalue ? maxSortvalue + 1 : 1
+ hook = await db.registrationhook.create(item)
+ }
+ if (hook) {
+ hook.setGroups(req.body.groups)
+ res.send({ id: hook.id })
+ }
+ res.end()
+})
+
+// DELETE requests.
+
+router.delete(['/hooks', '/hooks/:id'], (req, res) => {
+ db.registrationhook.destroy({ where: { id: req.params.id || req.body.ids } }).then(count => { res.send({ count }) })
+})
+
module.exports.router = router
// GET requests.
@@ -56,7 +116,7 @@ noAuthRouter.post('/group', (req, res) => {
})
/*
- * Adds the client in the database and set parents if a parent was selected.
+ * 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 mac = req.body.mac
@@ -70,10 +130,27 @@ noAuthRouter.post('/add', (req, res) => {
var groupids = []
if (parentId) groupids = [parentId]
getNextHookScript(groupids, 0).then(resId => {
- db.client.create({ name: name, ip: ip, mac: mac, uuid: uuid, registrationState: resId }).then(client => {
+ db.client.create({ name: name, ip: ip, mac: mac, uuid: uuid, registrationState: resId }).then(newClient => {
if (parentId) {
- client.addGroup(parentId)
+ newClient.addGroup(parentId)
}
+
+ // Add the client to the backends.
+ const c = { 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 => {
+ result.forEach(response => {
+ // If the object was created we need to make the objectid / external id mapping.
+ if (response.success && response.create) {
+ db.backend.findOne({ where: { id: response.backendId }, include: ['mappedClients'] }).then(backend => {
+ backend.addMappedClients(newClient, { through: { externalId: response.id, externalType: response.type } })
+ })
+ }
+ })
+ })
+
res.send('#!ipxe\r\nreboot')
})
})
@@ -82,6 +159,20 @@ noAuthRouter.post('/add', (req, res) => {
})
/*
+ * Adds additional information for the backends of the client in the firstregistration.
+ */
+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' })
+})
+
+/*
* Open api method for setting the registration state of a given uuid.
*/
noAuthRouter.post('/:uuid/success', (req, res) => {