summaryrefslogblamecommitdiffstats
path: root/server/api/registration.js
blob: c87431c3bb9ff89164f42f00f3492811ec82fd70 (plain) (tree)





















































































                                                                                                       
/* global __appdir */
var path = require('path')
const Infoblox = require('infoblox')
var express = require('express')
var router = express.Router()
var noAuthRouter = express.Router()
var db = require(path.join(__appdir, 'lib', 'sequelize'))

// GET requests.

/*
 * @return:
 */
router.get('/', (req, res) => {
  var ipam = new Infoblox({
    ip: 'dhcp.uni-freiburg.de',
    apiVersion: '1.7.1'
  })
  ipam.login('js1456', 'Test12345!').then(function (r) {
    if (r) {
      ipam.getIpsFromSubnet('10.21.9.0/24').then(function (response) {
        console.log(response)
        res.send(response)
      })
    }
    // res.status(200).send({ status: 'work in progress ...' })
  })
})

module.exports.router = router

// GET requests.

// POST requests.

/*
 *
 * @return:
 */
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'
  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')
    /*eslint-disable */
    script = script.concat('param id ${group}\r\n')
    /* eslint-enable */
    script = script.concat('chain https://bas.stfu-kthx.net:8888/api/registration/group##params\r\n')
    res.status(200).send(script)
  })
})

noAuthRouter.post('/group', (req, res) => {
  const id = req.body.id

  var script = '#!ipxe\r\n'
  db.group.findOne({ where: { id: id }, include: ['parents', 'subgroups', 'clients'] }).then(group => {
    if (group) {
      group.subgroups.forEach(subgroup => {
        script = script.concat('echo', ' [', subgroup.id, '] ', subgroup.name, '\r\n')
      })

      script = script.concat('read group\r\n')
      script = script.concat('params\r\n')
      /*eslint-disable */
      script = script.concat('param id ${group}\r\n')
      /* eslint-enable */
      script = script.concat('chain https://bas.stfu-kthx.net:8888/api/registration/group##params\r\n')
      res.send(script)
    } else {
      res.status(404).end()
    }
  })
})

module.exports.noAuthRouter = noAuthRouter