@@ -23,6 +23,9 @@ class InfobloxNotFoundException(Exception):
2323class InfobloxNoIPavailableException (Exception ):
2424 pass
2525
26+ class InfobloxNoNetworkAvailableException (Exception ):
27+ pass
28+
2629class InfobloxGeneralException (Exception ):
2730 pass
2831
@@ -35,6 +38,7 @@ class Infoblox(object):
3538 delete_network
3639 create_networkcontainer
3740 delete_networkcontainer
41+ get_next_available_network
3842 create_host_record
3943 create_txt_record
4044 delete_txt_record
@@ -998,3 +1002,42 @@ def delete_networkcontainer(self, networkcontainer):
9981002 raise Exception (r )
9991003 except Exception :
10001004 raise
1005+
1006+ def get_next_available_network (self , networkcontainer , cidr ):
1007+ """ Implements IBA REST API call to retrieve next available network of network container
1008+ Returns network address in CIDR format
1009+ :param networkcontainer: network container address in CIDR format
1010+ :param cidr: requested network length (from 0 to 32)
1011+ """
1012+ rest_url = 'https://' + self .iba_host + '/wapi/v' + self .iba_wapi_version + '/networkcontainer?network=' + networkcontainer + '&network_view=' + self .iba_network_view
1013+ try :
1014+ r = requests .get (url = rest_url , auth = (self .iba_user , self .iba_password ), verify = self .iba_verify_ssl )
1015+ r_json = r .json ()
1016+ if r .status_code == 200 :
1017+ if len (r_json ) > 0 :
1018+ net_ref = r_json [0 ]['_ref' ]
1019+ rest_url = 'https://' + self .iba_host + '/wapi/v' + self .iba_wapi_version + '/' + net_ref + '?_function=next_available_network&cidr=' + str (cidr ) + '&num=1'
1020+ r = requests .post (url = rest_url , auth = (self .iba_user , self .iba_password ), verify = self .iba_verify_ssl )
1021+ r_json = r .json ()
1022+ if r .status_code == 200 :
1023+ network = r_json ['networks' ][0 ]
1024+ return network
1025+ else :
1026+ if 'text' in r_json :
1027+ if 'code' in r_json and r_json ['code' ] == 'Client.Ibap.Data' :
1028+ raise InfobloxNoNetworkAvailableException (r_json ['text' ])
1029+ else :
1030+ raise InfobloxGeneralException (r_json ['text' ])
1031+ else :
1032+ r .raise_for_status ()
1033+ else :
1034+ raise InfobloxNotFoundException ("No requested network container found: " + networkcontainer )
1035+ else :
1036+ if 'text' in r_json :
1037+ raise InfobloxGeneralException (r_json ['text' ])
1038+ else :
1039+ r .raise_for_status ()
1040+ except ValueError :
1041+ raise Exception (r )
1042+ except Exception :
1043+ raise
0 commit comments