Skip to main content
Flexible Top Header

We often have users delete A records, but neglect the “Delete linked IP address if orphaned” checkbox. This results in IP addresses that are unused, but in the web UI they still appear with a blue icon (state = “STATIC”) instead of gray icon (state = “UNASSIGNED”). I am trying to search a subnet for unused IP addresses, that are statically assigned but with no records pointing to them.

 

I’m able to get the network easily enough.

url = f"{self.url_base}/networks&filter=range:eq('{target_cidr}')"

And then I’m able to get a list of all the IP addresses in the network easily enough

url = f"{self.url_base}/networks/{networkn'id']}/addresses

(I was also able to use a filter to get “UNASSIGNED” addresses in one go. Worked great, if only that was all I needed to get).

I’m able to loop over all the IP addresses. This works perfectly fine, but performance is terrible, because I’m hitting the API for every IP address, 100ms per call * 300 calls = 30 sec to complete. By comparison when I browse the web UI and click on this subnet, the whole page appears in about 1 second, so the web UI must be getting the same data a more efficient way. 

        for address in response_json.get('data', ]):

            if address/'state'] == 'UNASSIGNED':

                unassigned_addresses.append(address)

                continue

            if address 'state'] == 'STATIC':

                # get resource records that point at this address

address_url = f"{self.url_base}/addresses/{addressd'id']}/resourceRecords?limit={self.limit}"

                address_response = requests.get(address_url, headers=self.headers, verify=self.verify_ssl)

                address_response.raise_for_status()

                address_response_json = address_response.json()


                if address_response_json 'count'] == 0:

                    unassigned_addresses.append(address)

Again, this works, but it seems to be very inefficient. I can’t seem to find a better way in the API to search for these orphaned IP addresses.

Any suggestions?

Thanks!

Embed the resourceRecords collection in your fetch of statics and look for objects that have an empty list for the _embedded.resourceRecords result.

 

/api/v2/networks/<collection id>/addresses?fields=embed(resourceRecords)&filter=state:eq(‘STATIC’)

 


@tmaestas Yesss!!! Thank you.  🙂 I also changed all my requests.get() calls to use requests.session, so I re-use a single https connection instead of constantly performing new https handshakes. The session improvement, combined with the embed improvement, got the query down to 1.3 seconds. So I would say that’s probably as good as it can be, and totally acceptable.

Thanks again.  :-)


Reply