From f486a08621eec271889c4e2108e0038a98e78946 Mon Sep 17 00:00:00 2001 From: Jannik Schönartz Date: Mon, 18 Mar 2019 17:27:55 +0000 Subject: [server/registration] Add server registration --- server/api/registration.js | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) (limited to 'server/api/registration.js') 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 @@ -106,6 +106,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. */ @@ -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 }) -- cgit v1.2.3-55-g7522