@@ -26,6 +26,9 @@ class InfobloxNoIPavailableException(Exception):
26
26
class InfobloxGeneralException (Exception ):
27
27
pass
28
28
29
+ class InfobloxBadInputParameter (Exception ):
30
+ pass
31
+
29
32
class Infoblox (object ):
30
33
""" Implements the following subset of Infoblox IPAM API via REST API
31
34
create_network
@@ -110,18 +113,26 @@ def get_next_available_ip(self, network):
110
113
except Exception :
111
114
raise
112
115
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
116
120
:param fqdn: hostname in FQDN
117
121
"""
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 + '"}'
120
131
try :
121
132
r = requests .post (url = rest_url , auth = (self .iba_user , self .iba_password ), verify = self .iba_verify_ssl , data = payload )
122
133
r_json = r .json ()
123
134
if r .status_code == 200 or r .status_code == 201 :
124
- return
135
+ return r_json [ 'ipv4addrs' ][ 0 ][ 'ipv4addr' ]
125
136
else :
126
137
if 'text' in r_json :
127
138
raise InfobloxGeneralException (r_json ['text' ])
0 commit comments