ninech / netbox-client-ruby

A ruby client library for Netbox v2.
MIT License
24 stars 21 forks source link

Search for IP address with given address and pagination #19

Closed Cosaquee closed 6 years ago

Cosaquee commented 6 years ago

Hi! First of all, thanks for you library it is really awesome! Everything works nicely.

I'm making OpenStack to NetBox exporter and I want to check if IP address with given IP exists or not. I can obtain all ip addresses and then search if there is object with address same as one I obtain from OpenStack but I wonder if there is a way to search for IP with given address. It is not a big issue but requesting couple of thousands IP addresses and search in them is not so effective as one search query.

I tried NetboxClientRuby.ipam.ip_addresses.filter(address: '192.168.4.40/24') but it doesn't work for me.

Pagination also seems to be not documented(just one entry in example config file). It would be really nice to see an example code block about working with pagination IPAM module.

EDIT: Ok, in code I found out that you can use #all method to get all objects. But you need to remember to set your MAX_PAGE_SIZE to 0 in configuration.py in your NetBox installation if you want to get all objects at once from api.

So is it possible?

cimnine commented 6 years ago

To work with the pagination, you can do:

NetboxClientRuby.ipam.ip_addresses.limit(100).offset(100) # addresses 101 - 200
# or
NetboxClientRuby.ipam.ip_addresses.page(0) # first page

limit and offset work just like documented in netbox. page is a convenience method, which just calculates the offset from the current (default) limit.

cimnine commented 6 years ago

As for this one:

I tried NetboxClientRuby.ipam.ip_addresses.filter(address: '192.168.4.40/24') but it doesn't work for me.

You're using the API wrong.

In you browser, go to http://YOUR_NETBOX/api/ipam/ip-addresses/?address=192.168.4.40/24. You'll see that all IPs you have in there are returned.

But if you go to http://YOUR_NETBOX/api/ipam/ip-addresses/?q=192.168.4.40/24 instead, you'll see that only the one IP is returned.

cimnine commented 6 years ago

Btw, I'm sorry for the long response time. I did not see that someone opened an issue.