cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
375
Views
2
Helpful
1
Replies

Python : Detecting full scope at allocation creation time ?

mdeprete
Cisco Employee
Cisco Employee

Hi team,

(NSO 4.4.2, IP allocator 1.3)

I am working on a Python script that uses the ip-allocator package, and trying to use primary and secondary scopes. My code happily creates allocations from pools with free address blocks.

    alloc1 = root.resource_pools.ip_address_pool[ippool.name].allocation.create(str(hostname))

    alloc1.id = hostname

    alloc1.request.subnet_size = 28

    alloc1.username = username

    print str(alloc1.response.error)     # Always returns "None"

However, I want to test pool exhaustion and if needed, getting an allocation from another scope associated to a same site.

I have, for one site, multiple pools (various subnet sizes) : POOL1, POOL2 and POOL3. If I try to get an allocation from a full subnet (let's say POOL1), I don't get an error message at the create() or apply() time. I only see the failed allocation in NSO console :

NAME                 ID                            ERROR                                                                            SUBNET           FROM

---------------------------------------------------------------------------------------------------------------------------------------------------------------------

POOL1                test-full-scope               -                                                                                10.1.1.0/24      10.1.1.0/24

                     test-device                   com.tailf.pkg.ipam.exceptions.AddressPoolEmptyException: No addresses available  -                -

I can also find this out if I read the allocation back from my python script :

com.tailf.pkg.ipam.exceptions.AddressPoolEmptyException: No addresses available

but this happens after the apply, so I can only find out that the allocation failed after commiting the changes.

What is the best way to achieve what I'm trying to do here ? Is there a way to immediately see before calling apply() if the allocation creation won't work due to a full scope ?

Cheers,

Marc

1 Reply 1

frjansso
Cisco Employee
Cisco Employee

As you have noticed, you will not get an error at commit. The reason is that the allocators are implemented using the Reactive FastMap pattern. So the idea is that you write your allocation request, and when it becomes available (or fails) your service is redeployed.

If you use the helper python functions that come with the IP/ID allocators, you'll get this behavior for "free" and when you call the function to receive (net_read below) the allocated ID/IP, it will throw an exception in case something went wrong (e.g. exhausted pool).

  import ipaddress_allocator.ipaddress_allocator as ip_allocator

          ip_allocator.net_request(service,

                                   "/services/vl:loop-python[name='%s']" % (service.name),

                                   tctx.username,

                                   pool_name,

                                   alloc_name,

                                   service.cidr_length)

          net = ip_allocator.net_read(tctx.username, root,

                                      pool_name, alloc_name)

          if not net:

              self.log.info("Alloc not ready")

              return