/* 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: , status: '', error: '' } */ async checkConnection (credentials) { var c = this.mapCredentials(credentials) var ipam = new Infoblox({ ip: c.url, apiVersion: c.version }) const response = await ipam.login(c.username, c.password) if (response) { return true } else { return { error: 'LOGIN_FAILED', message: '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' } } }) } async checkIp (credentials, ipv4) { const c = this.mapCredentials(credentials) // Login const ipam = new Infoblox({ ip: c.url, apiVersion: c.version }) const login = await ipam.login(c.username, c.password) if (!login) return false // Get the host and check the leased state let host = JSON.parse(await ipam.getHost(ipv4))[0] if (host.lease_state && host.lease_state === 'ACTIVE') { // If leased return the next 20 free ips of the subnet. const dhcpNetwork = JSON.parse(await ipam.getNetworkFromIp(ipv4)) const nextIps = await ipam.getNext(dhcpNetwork[0]._ref, 20) return nextIps } return false } async setIp (credentials, ipv4, mac, name, setNextIp = false) { const c = this.mapCredentials(credentials) const ipam = new Infoblox({ ip: c.url, apiVersion: c.version }) const login = await ipam.login(c.username, c.password) if (!login) return { error: 'LOGIN_FAILED' } // If setNextIp is true, use the next available ip from the subnet if (setNextIp) { const network = JSON.parse(await ipam.getNetworkFromIp(ipv4)) ipv4 = 'func:nextavailableip:' + network[0].network } const domain = (await ipam.getDomain())[0] // Set fixed ip if the name is not set (Automatic registration) let path = '' let data = {} if (!name) { path = 'fixedaddress?_return_fields%2B=ipv4addr&_return_as_object=1' data = { 'ipv4addr': ipv4, 'mac': mac } } else { path = 'record:host?_return_fields%2B=ipv4addrs&_return_as_object=1' data = { 'name': name + '.' + domain, 'ipv4addrs': [ { 'ipv4addr': ipv4, 'mac': mac } ] } } const createHost = await ipam.create(path, data) // Return error if there is one if (createHost.Error) { return { error: 'ERROR_INFOBLOX', msg: createHost.text } } else { if (!name) return { ip: createHost.result.ipv4addr } else return { host: createHost.result.ipv4addrs[0].host, ip: createHost.result.ipv4addrs[0].ipv4addr } } } isDhcp () { return true } // ############################################################################ // ####################### 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 ### async test (credentials) { const c = this.mapCredentials(credentials) const ipam = new Infoblox({ ip: c.url, apiVersion: c.version }) const login = await ipam.login(c.username, c.password) let result = {} if (!login) return { error: 'LOGIN_FAILED' } // Get network from ip result['getNetworkFromIp'] = JSON.parse(await ipam.getNetworkFromIp('10.21.9.78')) // Get host // result["getHost.78"] = JSON.parse(await ipam.getHost('10.21.9.78')) // result["getHost.219"] = JSON.parse(await ipam.getHost('10.21.9.219')) // Get ips from subnet // result["getIpsFromSubnet"] = JSON.parse(await ipam.getIpsFromSubnet('10.21.9.0/24')) // List records // result["list"] = JSON.parse(await ipam.list('record:host')) // Get Domain result['getDomain'] = await ipam.getDomain() // Get next ip // result["getNextIp.1"] = await ipam.getNext(result["getNetworkFromIp"][0]._ref, 1) // Get next 20 ips // result["getNextIp.20"] = await ipam.getNext(result["getNetworkFromIp"][0]._ref, 20) // Create Host // result['createHost'] = await ipam.create('record:host?_return_fields%2B=ipv4addrs&_return_as_object=1', {'name': 'wapiTest2.lp.privat', 'ipv4addrs': [{'ipv4addr': '10.21.9.218', 'mac': 'aa:bb:cc:11:22:21'}]}) /* result['create'] = await ipam.create('record:host?_return_fields%2B=ipv4addrs&_return_as_object=1', { 'name': 'test.lp.privat', 'ipv4addrs': [ { 'ipv4addr': 'func:nextavailableip:' + result['getNetworkFromIp'][0].network, 'mac': 'aa:bb:cc:11:22:21' } ] }) result['create'] */ return result } } module.exports = InfobloxBackend