Skip to content

Commit 67cd0c0

Browse files
author
igor-feoktistov
committed
Added support for WAPI 1.6
Taking advantage of newer WAPI where IP address in POST request for a new host record can be supplied as “func:nextavailableip:network”. The library call “create_host_record” can now accept either IP address or NET address in CIDR format to allocate IP from.
1 parent 87be972 commit 67cd0c0

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

README.md

+3-7
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,12 @@ Example:
4040
```
4141
import infoblox
4242
43-
iba_api = infoblox.Infoblox('10.10.20.32', 'admin', 'secret', '1.0', 'internal', 'default')
43+
iba_api = infoblox.Infoblox('10.10.20.32', 'admin', 'secret', '1.6', 'internal', 'default')
4444
4545
try:
46-
ip = iba_api.get_next_available_ip('192.168.0.0/24')
47-
iba_api.create_host_record(ip, 'mytest.example.com')
46+
ip = iba_api.create_host_record('192.168.0.0/24', 'mytest.example.com')
47+
print ip
4848
except Exception as e:
4949
print e
5050
5151
```
52-
53-
### Limitations
54-
55-
Currently supports WAPI 1.0 only. 1.2.1 is coming soon.

infoblox.py

+17-6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ class InfobloxNoIPavailableException(Exception):
2626
class InfobloxGeneralException(Exception):
2727
pass
2828

29+
class InfobloxBadInputParameter(Exception):
30+
pass
31+
2932
class Infoblox(object):
3033
""" Implements the following subset of Infoblox IPAM API via REST API
3134
create_network
@@ -110,18 +113,26 @@ def get_next_available_ip(self, network):
110113
except Exception:
111114
raise
112115

113-
def create_host_record(self, ip_v4, fqdn):
114-
""" Implements IBA REST API call to add IBA host record
115-
:param ip_v4: host IP v4 address
116+
def create_host_record(self, address, fqdn):
117+
""" Implements IBA REST API call to create IBA host record
118+
Returns IP v4 address assigned to the host
119+
:param address: IP v4 address or NET v4 address in CIDR format to get next_available_ip from
116120
:param fqdn: hostname in FQDN
117121
"""
118-
rest_url = 'https://' + self.iba_host + '/wapi/v' + self.iba_wapi_version + '/record:host'
119-
payload = '{"ipv4addrs": [{"configure_for_dhcp": false,"ipv4addr": "' + ip_v4 + '"}],"name": "' + fqdn + '","view": "' + self.iba_dns_view + '"}'
122+
if re.match("^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\/[0-9]+$", address):
123+
ipv4addr = 'func:nextavailableip:' + address
124+
else:
125+
if re.match("^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$", address):
126+
ipv4addr = address
127+
else:
128+
raise InfobloxBadInputParameter('Expected IP or NET address in CIDR format')
129+
rest_url = 'https://' + self.iba_host + '/wapi/v' + self.iba_wapi_version + '/record:host' + '?_return_fields=ipv4addrs'
130+
payload = '{"ipv4addrs": [{"configure_for_dhcp": false,"ipv4addr": "' + ipv4addr + '"}],"name": "' + fqdn + '","view": "' + self.iba_dns_view + '"}'
120131
try:
121132
r = requests.post(url=rest_url, auth=(self.iba_user, self.iba_password), verify=self.iba_verify_ssl, data=payload)
122133
r_json = r.json()
123134
if r.status_code == 200 or r.status_code == 201:
124-
return
135+
return r_json['ipv4addrs'][0]['ipv4addr']
125136
else:
126137
if 'text' in r_json:
127138
raise InfobloxGeneralException(r_json['text'])

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
setup(
99
name='infoblox',
10-
version='1.0',
10+
version='1.1',
1111
description='The module implements Infoblox IPAM API via REST API',
1212
long_description=README,
1313
license = 'Licensed under the Apache License, Version 2.0',

0 commit comments

Comments
 (0)