Infoblox-Development / Infoblox-API-Python

Implements the subset of Infoblox API via REST API
Apache License 2.0
90 stars 59 forks source link

Added basic "exclude" range for next_available_ip #19

Open Zamboughnuts opened 8 years ago

Zamboughnuts commented 8 years ago

In my organization, we try to reserve the first 10 IPs in any given network. I modified the "get_next_available_ip" function to include the ability to exclude a range starting at the first address in the network. This could easily be further modified for any range. I've named the new function "get_next_available_ip_long":

def get_next_available_ip_long(self, network, num):
    """ Implements IBA next_available_ip REST API call
    Returns IP v4 address
    :param network: network in CIDR format
    """

    """
    Modified to include param num
    param num is integer, will exclude that many addresses starting at .0
    """
    def ip_exclude(start, count):
        """
        This sub-function creates a list of excluded IPs for the next_available IP
        Right now it only works to exclude the first x addresses in a range
        """

        start_addr = start[:-3]             #chomps the "/24" off the network name
        start_addr_list = start_addr.split(".")     #splits the network on each dot
        start_oct4 = int(start_addr_list[3])        #changes the 4th octet to an int
        start_oct4_str = start_addr_list[3]     #creates a string value for the 4th octet

        count = int(count)
        end_oct4 = start_oct4 + count           #sets the end of the reserved range
        end_oct4_str = str(end_oct4)            #makes a string variable for that
        end_addr = start_addr_list[0] + "." + start_addr_list[1] + "." +  start_addr_list[2] + "." +  end_oct4_str

        exclude_range = []
        while start_oct4 < end_oct4:
            start_oct4 = start_oct4 + 1
            temp_oct4 = str(start_oct4)
            temp_addr = start_addr_list[0] + "." + start_addr_list[1] + "." +  start_addr_list[2] + "." + temp_oct4
            exclude_range.append(temp_addr)

        return exclude_range                #returns a list of excluded addresses for post_data

    exclude = ip_exclude(network, num)

    rest_url = 'https://' + self.iba_host + '/wapi/v' + self.iba_wapi_version + '/network?network=' + network + '&network_view=' + self.iba_network_view
    try:
        r = requests.get(url=rest_url, auth=(self.iba_user, self.iba_password), verify=self.iba_verify_ssl)
        r_json = r.json()
        if r.status_code == 200:
        if len(r_json) > 0:
            net_ref = r_json[0]['_ref']
            rest_url = 'https://' + self.iba_host + '/wapi/v' + self.iba_wapi_version + '/' + net_ref + '?_function=next_available_ip'
            post_data = {"num":1,'exclude':exclude}
            #this is the infoblox-approved "long-form" version of the "next available IP" call
            r = requests.post(url=rest_url, auth=(self.iba_user, self.iba_password), verify=self.iba_verify_ssl, data=json.dumps(post_data))
            r_json = r.json()
            if r.status_code == 200:
            ip_v4 = r_json['ips'][0]
            return ip_v4
            else:
            if 'text' in r_json:
                if 'code' in r_json and r_json['code'] == 'Client.Ibap.Data':
                raise InfobloxNoIPavailableException(r_json['text'])
                else:
                raise InfobloxGeneralException(r_json['text'])
            else:
                r.raise_for_status()
        else:
            raise InfobloxNotFoundException("No requested network found: " + network)
        else:
        if 'text' in r_json:
            raise InfobloxGeneralException(r_json['text'])
        else:
            r.raise_for_status()
    except ValueError:
        raise Exception(r)
    except Exception:
        raise
johnbelamaric commented 8 years ago

You may want to check out https://github.com/infobloxopen/infoblox-client which is what we use in the Infoblox IPAM driver for OpenStack. This is the one that has active development by Infoblox.