summaryrefslogtreecommitdiffstats
path: root/server/api/registration.js
diff options
context:
space:
mode:
authorJannik Schönartz2019-03-18 18:27:55 +0100
committerJannik Schönartz2019-03-18 18:27:55 +0100
commitf486a08621eec271889c4e2108e0038a98e78946 (patch)
tree18150cd68cdcdee42a2a62ce2a124badaf6f4cbb /server/api/registration.js
parent[server/log] sql JSON to TEXT for mariadb compatibility (diff)
downloadbas-f486a08621eec271889c4e2108e0038a98e78946.tar.gz
bas-f486a08621eec271889c4e2108e0038a98e78946.tar.xz
bas-f486a08621eec271889c4e2108e0038a98e78946.zip
[server/registration] Add server registration
Diffstat (limited to 'server/api/registration.js')
-rw-r--r--server/api/registration.js43
1 files changed, 39 insertions, 4 deletions
diff --git a/server/api/registration.js b/server/api/registration.js
index 63a3915..4026615 100644
--- a/server/api/registration.js
+++ b/server/api/registration.js
@@ -107,6 +107,41 @@ noAuthRouter.post('/group', (req, res) => {
})
/*
+ * Reworked add method for adding a client or server.
+ */
+noAuthRouter.postAsync('/', async (req, res) => {
+ console.log(req.body)
+ let client = req.body.client
+ const ipxe = req.body.ipxe
+ if (!client.type) client.type = 'CLIENT'
+ if (!client.title) client.title = client.type + '_' + client.uuid
+
+ // If the client already exists return the configloader ipxe script.
+ const clientDb = await db.client.findOne({ where: { uuid: client.uuid } })
+ if (clientDb) {
+ if (ipxe) return res.send(`#!ipxe\nchain https://` + url + `/api/configloader/\${uuid}`)
+ else return res.send({ error: 'CLIENT_ALREADY_EXISTS', msg: 'A client with the provided UUID does already exist.' })
+ }
+
+ // Client does not exist.
+ if (!client.parents) client.parents = []
+ const createClient = { name: client.title, description: client.type, ip: client.network.ip, mac: client.network.mac, uuid: client.uuid }
+ if (client.type === 'CLIENT') createClient.registrationState = await getNextHookScript(client.parents)
+ const newClient = await db.client.create(createClient)
+ client.id = newClient.id
+
+ // Add groups to the client.
+ if (client.parents.length === 0) client.parents = await ipHelper.getGroups(client.network.ip)
+ client.parents.forEach(pid => { newClient.addGroup(pid) })
+ log({ category: 'CLIENT_REGISTRATION', description: client.type + ' added successfully.', clientId: newClient.id })
+
+ // Add the client to the backends.
+ const result = await backendHelper.addClient(client)
+ if (ipxe) return res.send(`#!ipxe\nchain https://` + url + `/api/configloader/\${uuid}`)
+ else return res.send(result)
+})
+
+/*
* Adds the client to the database and set parents if a parent was selected. Calls addClient for all external-backends.
*/
noAuthRouter.postAsync('/add', async (req, res) => {
@@ -114,17 +149,18 @@ noAuthRouter.postAsync('/add', async (req, res) => {
const mac = req.body.mac
const uuid = req.body.uuid
const ip = req.body.ip
- var name = req.body.name
+ let name = req.body.name
const parentId = parseInt(req.body.id)
const purpose = req.body.purpose
let parentIds = []
if (!name) name = 'Client_' + uuid
+ // If the client already exists return the configloader ipxe script.
const client = await db.client.findOne({ where: { uuid: uuid } })
-
if (client) return res.send(`#!ipxe\nchain https://` + url + `/api/configloader/\${uuid}`)
- // Else
+
+ // Else (Client does not exist)
var groupids = []
if (parentId) groupids = [parentId]
const resId = await getNextHookScript(groupids)
@@ -136,7 +172,6 @@ noAuthRouter.postAsync('/add', async (req, res) => {
// 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
}
log({ category: 'CLIENT_REGISTRATION', description: 'Client added successfully.', clientId: newClient.id })