infobloxopen / infoblox-client

Infoblox NIOS Python WAPI Client
Apache License 2.0
143 stars 103 forks source link

Question about IPAllocation class #145

Closed Zamboughnuts closed 3 years ago

Zamboughnuts commented 7 years ago

Is there a way to pull an IP address as a string from the objects.IPAllocation.next_available_ip_from_cidr?

Here's what I'm talking about... The code: opts = {'host': 'hostname', 'username': 'api_user', 'password': 'password', 'wapi_version': '2.2.2'} conn = connector.Connector(opts) network_view = conn.get_object('networkview') next_IP = obj.IPAllocation.next_available_ip_from_cidr(network_view, 10.10.1.0/24)

When I run that command, I get the following output:

next IP: func:nextavailableip:10.10.1.0/24,[{u'is_default': True, u'_ref': u'networkview/<networkview>', u'name': u'default'}]

Even though I know that the next available IP is 10.10.1.10, I'm not getting it in that format. Am I missing something here?

yuewko commented 7 years ago

Hi,

If the intention is to allocate the next available IP from Infoblox, then the call to use would be InfobloxObjectManager.create_fixed_address_from_cidr(). This will return an object that contains the allocated IP.

The purpose of the method obj.IPAllocation.next_available_ip_from_cidr() is to create an object that is used to query Infoblox. In fact, it is used by InfobloxObjectManager.create_fixed_address_from_cidr() to construct the query.

Hope this helps!

Zamboughnuts commented 7 years ago

So it isn't meant to be human-readable then?

I can understand the purpose being to make a query string that can create a new IP address, but my use case is slightly different - That is, I'm writing a script to generate configuration files that can be pasted into a CLI interface. I still have use for creating the host, obviously, but I also need to be able to get whatever IP that is in plain text so it can be written to a file. Do you know of any modifications that would allow this?

yuewko commented 7 years ago

The following is the create_fixed_address_from_cidr() method I mentioned earlier:

def create_fixed_address_from_cidr(self, netview, mac, cidr, extattrs):
        ip = obj.IPAllocation.next_available_ip_from_cidr(netview, cidr)
        return obj.FixedAddress.create(self.connector,
                                       network_view=netview,
                                       ip=ip,
                                       mac=mac,
                                       extattrs=extattrs,
                                       check_if_exists=False)

As you can see, it first calls next_available_from_cidr() which returns an object that represents a query for the next available IP. That query need to be sent to Infoblox via the FixedAddress.create() that follows (passed as the parameter, ip), which tells Infoblox to allocate the next available IP.

The object that is returned by the FixedAddress.create() would then contain the allocated IP. When printed, you will see the humanly readable IP that you expect, e.g. 10.10.1.10 - i.e. the address that has actually been allocated.

Hope this helps.

Zamboughnuts commented 7 years ago

Thank you. I have one other problem that came up while using this, and I cannot for the life of me figure out what the issue is. It seems that when I try to call the "create_fixed_address_from_cidr" function, I'm unable to find the network, despite having a valid network view:

infoblox_client.exceptions.InfobloxCannotCreateObject: Cannot create 'fixedaddress' object(s): { "Error": "AdmConDataNotFoundError: Network View [{u'_ref': u'network/<networkpath>:<subnet>/default', u'network': u'<10.10.X.X/24>', u'network_view': u'default'}] not found", "code": "Client.Ibap.Data.NotFound", "text": "Network View [{u'_ref': u'network/<networkpath>:<10.10.X.X/24>/default', u'network': u'<subnet>', u'network_view': u'default'}] not found" } [code 404]

I've changed variables, and nothing seems to work. I suspect that it may have something to do with the fact that 10.10.X.X/24 is in a network container of 10.0.0.0/8. I either get that network view, or a network view of "none" when I set it up with the "conn.get_object('network,{'network: <10.10.X.X/24>', 'network_view' : 'default'})" command

I do have proper network names and CIDR notation, but I've redacted it for obvious reasons.

yuewko commented 7 years ago

When you call create_fixed_address_from_cidr(), the 'netview' parameter should be a string which is the name of the Infoblox network view where the network resides. In your case, based on your last comment, should be something like 'default'.

Hope this helps.

Zamboughnuts commented 7 years ago

Thank you for all the help, again. As it turns out, the WAPI 2.2.2 doesn't support the "next available IP from range" command the same way that the WAPI did when this code was written. As a workaround, I added a "create_host_record_from_cidr" function to object_manager.py.

Now, to still select a range, it is entirely possible to do, but the API expects a "exclude" command as found here:

https://community.infoblox.com/t5/API-Integration/The-definitive-list-of-REST-examples/td-p/1214ipv

That "exclude" command takes a CSV of IP addresses, which could theoretically be parsed from the range given, but I don't have the programming skill to do it. I have a workaround on another variation of the script, but it's a much simpler process.

achernevskii commented 3 years ago

@Zamboughnuts hi. Is this issue still relevant to you?

If so, please explain in more depth what you are trying to achieve and what problem did you met. Also, a code snippet with the create_host_record_from_cidr function would be great.

The link you gave to the Infoblox community is broken, so I can't understand what's the problem yet.

Zamboughnuts commented 3 years ago

I honestly have no idea at this point, I haven't used this project in years.