List of available IPs in a form

61 views
Skip to first unread message

Mats Brorson

unread,
May 11, 2021, 5:31:34 AMMay 11
to NetBox
I have a usecase, where the users whould like to have a list of available IPs in an prefix so they can select which to use. I have trouble finding out how to do this. My basic code:

    tenant = ObjectVar(
        model=Tenant,
        required=True,
        label='Select Tenant'
        )

    vm_name = StringVar(label="VM name")

    vlan = ObjectVar(
        model=VLAN,
        label='Select VLAN',
        query_params={
            'tenant_id': '$tenant'
            }          
    )

    prefix = ObjectVar(
        model=Prefix,
        query_params={
            'vlan_id': '$vlan'
            },  
        display_field='prefix'
    )

Now I would like to have an ObjectVar like this:

    ips = ObjectVar(
        model=Prefix,
        required=True,
        label='Select IP'
        )

However, I have yet to figure out how I would get the results from the method get_all_ips().

Any ideas? 



Mats Brorson

unread,
May 11, 2021, 4:03:57 PMMay 11
to NetBox
Messing around with nbshell, I get confused. I was trying get_available_ips() on a prefix, expecting the same result as the API, but it returns a list of available prefixes instead of IPs. Nbshell snippet:


Am II doing things wrong, or have I found a bug?

Jeremy Stretch

unread,
May 11, 2021, 4:30:41 PMMay 11
to Mats Brorson, NetBox
That's not a list of prefixes; it's a netaddr IPSet. See the documentation here for help: https://netaddr.readthedocs.io/en/latest/tutorial_03.html

Jeremy Stretch
Distinguished Engineer, NS1
Office of the CTO
 
website: ns1.com
email: jstretch@ns1.com
 
twitterlinkedin
 
banner


--
You received this message because you are subscribed to the Google Groups "NetBox" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netbox-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netbox-discuss/322b5675-f3bf-4a27-9a72-fbc6a4c1a0a4n%40googlegroups.com.

Mats Brorson

unread,
May 11, 2021, 6:10:44 PMMay 11
to NetBox
That makes no sence to me. According to help(Prefix):

 |  get_available_ips(self)
 |      Return all available IPs within this prefix as an IPSet.

And I find no way to get this info from an IP set. Or I'm stupid:-)

Brian Candler

unread,
May 12, 2021, 4:28:15 AMMay 12
to NetBox
From the document Jeremy linked:

You can iterate over all the IP addresses that are members of the IP set.

    for ip in IPSet(['192.0.2.0/28', '::192.0.2.0/124']):
        print(ip)

Just do that until you've seen as many addresses as you want.

If you want an actual list then in principle you could do this:

available_ips = list(y.get_available_ips())

However that will crash your machine if the range of addresses is huge, such as with IPv6 blocks.  So you should limit it to the first N like this:

import itertools
available_ips = list(itertools.islice(y.get_available_ips(), 100))

Mats Brorson

unread,
May 12, 2021, 8:53:56 AMMay 12
to NetBox
Yea, I must confess, I missed that i need to call the function on the Perefix object itself....

Now, one hurdle gone, now I just need to figure out how to get the response into a SelectVar(). Having problem passing my ObjectVar 'prefix' object to the above.

Rob Duffy

unread,
Jun 4, 2021, 8:46:11 AMJun 4
to Mats Brorson, NetBox
Did you end up finding a solution to this?  I'm stuck on the same problem.  

Rob Duffy

unread,
Jun 11, 2021, 12:03:34 PMJun 11
to Mats Brorson, NetBox
I'm just bumping this in the hope that someone can help point me in the right direction. 

I'm basically just trying to get all available IPs for a given prefix (selected in a different ObjectVar) into an ObjectVar.  Is this even possible?  I feel like I've tried everything at this point.  If not, I'll just get the user to select the number of IPs they want and have the script pick them.  
Reply all
Reply to author
Forward
0 new messages