summaryrefslogblamecommitdiffstats
path: root/server/lib/external-backends/backends/infoblox-backend.js
blob: 9d070ac63615c03b30633b1b7156776511dac406 (plain) (tree)


























                                                                                               
                                 







                                                                

                                
                                                        


       























                                                                                                                  



















































                                                                                                                                                                                                                                 
/* global __appdir */
const path = require('path')
const ExternalBackends = require(path.join(__appdir, 'lib', 'external-backends'))
const Infoblox = require('infoblox')

class InfobloxBackend extends ExternalBackends {
  // ############################################################################
  // ######################## needed functions  #################################

  /*
   * Returns the credential structure / fields, defined in the backends.
   */
  getCredentials () {
    return [
      { type: 'text', id: 1, name: 'API url', icon: 'link' },
      { type: 'text', id: 2, name: 'API version', icon: 'info' },
      { type: 'text', id: 3, name: 'Username', icon: 'person_outline' },
      { type: 'password', id: 4, name: 'Password', icon: 'vpn_key', show: false }
    ]
  }

  /*
   * Checks the connection of a given backend.
   * infoblox: If we can login the connection to the server and also the login is successfull.
   *
   * return: { success: <boolean>, status: '<STATUS_CODE_IF_ERROR>', error: '<ERROR_MESSAGE>' }
   */
  checkConnection (credentials) {
    var c = this.mapCredentials(credentials)

    var ipam = new Infoblox({
      ip: c.url,
      apiVersion: c.version
    })
    return ipam.login(c.username, c.password).then(response => {
      if (response) {
        return { success: true }
      } else {
        return { success: false, error: 'Login failed' }
      }
    })
  }

  /*
   * Gets the client via IP, because only fixed pcs can be found via mac. But we also want so support leased pc's?
   *
   */
  async getClient (credentials, client) {
    var c = this.mapCredentials(credentials)

    var ipam = new Infoblox({
      ip: c.url,
      apiVersion: c.version
    })

    return ipam.login(c.username, c.password).then(response => {
      if (response) {
        return ipam.getHost('10.21.9.228').then(response => {
          return JSON.parse(response)
        })
      } else {
        return { success: false, error: 'Login failed' }
      }
    })
  }

  // ############################################################################
  // ####################### helper/optional functions  #########################
  // Helper function, to map the array of credential objects into a single js object.
  mapCredentials (credentials) {
    const c = JSON.parse(credentials)
    var mapped = {
      url: c.find(x => x.id === 1).value,
      version: c.find(x => x.id === 2).value,
      username: c.find(x => x.id === 3).value,
      password: c.find(x => x.id === 4).value
    }

    return mapped
  }

  // #### TEST FUNCTIONS ###
  test (credentials) {
    var c = this.mapCredentials(credentials)

    var ipam = new Infoblox({
      ip: c.url,
      apiVersion: c.version
    })

    var promise = new Promise((resolve, reject) => {
      ipam.login(c.username, c.password).then(response => {
        if (response) {
          // ipam.create('record:host?_return_fields%2B=na13me,ipv4addrs&_return_as_object=1', {'name': 'wapiTest.lp.privat', 'ipv4addrs': [{'ipv4addr': '10.21.9.141', 'mac': 'aa:bb:cc:11:22:21'}]}).then(function (response) {
          //   return response
          // })
          // Get the network from the ip.
          // ipam.getHost('10.21.9.78').then(function (response) {
          ipam.getNetworkFromIp('10.21.9.78').then(response => {
          // ipam.getIpsFromSubnet('10.21.9.0/24').then(function (response) {
          // ipam.list('record:host').then(function (response) {
          // ipam.getDomain().then(function (response) {
            response = JSON.parse(response)
            // ipam.getNext(response[0]._ref, 1).then(r => {
            resolve(response)
            // })
            // res.send(JSON.parse(response))
          })
        } else {
          return { success: false, error: 'TEST' }
        }
      })
    })
    return promise
  }
}

module.exports = InfobloxBackend