question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow searching by IP 'address' on related models

See original GitHub issue

Environment

  • Python version: 3.6.8
  • Nautobot version: 1.0.1

Proposed Functionality

Since IP address is now computed field, it is not possible to search by it on models that are linked to IPAddress model. The only way to find instances of that model linked to certain IPs is to use ID of that IP, which is not very user friendly.

Please add filter object capable of searching IP Address address calculated field from another linked model.

[optional] Please add address field to IPAddress.objects.all() queryset.

Use Case

I have a custom plugin that links to IPAddress model with OneToOne relation. With Nautobot I used this code to search for my objects that are linked to certain IP by address:

ip__address = django_filters.ModelMultipleChoiceFilter(
    field_name="ip__address",
    queryset=IPAddress.objects.all(),
    to_field_name="address",
    label="IP Address",
)

But it is not possible anymore since django-filters works on DB and address field is no longer in DB. I would be able to search for it directly from my queryset, but queryset IPAddress.objects.all() does not contain calculated field address. So I had to work around and mimic how Nautobot filters for that field - basically creating custom manager a copying 2 function from Nautobot.

It would also be useful for core Nautobot, where there are 5 core models with DB relation to IPAddress model:

  • virtualization.VirtualMachine[primary_ip4, primary_ip6]
  • virtualization.VMInterface[ip_addresses]
  • dcim.Interface[ip_addresses]
  • dcim.Device[primary_ip4, primary_ip6]
  • ipam.Service[ipaddresses])

Being able to search these by IP Address would be quite handy.

And adding address field to IPAddress.objects.all() queryset would mean any jobs could access it and don’t have to calculate it themselves.

Database Changes

None

External Dependencies

None

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:12 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
lampwinscommented, May 11, 2021

We did add support for querying via address directly on an IPAddress queryset by overriding get and filter such that this is possible:

Device.objects.get(primary_ip4=IPAddress.objects.get(address="10.11.192.2/32"))

However, this is of course suboptimal because it results in an additional query, rather than just a join.

I wonder if we could support this with a custom lookup expression?

1reaction
jathanismcommented, May 11, 2021

@Thetacz I also wanted to add that I think your idea has a lot of merit. I am going to do a little homework to make sure that I’m not totally full of crap on the annotate concerns. Particularly as it relates to the cost/benefit of using annotations in the way you’re suggesting, vs. constructing the address in Python using a computed field.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Django Match IP in database - python - Stack Overflow
I am trying to check if the user's IP is in a model object I have. I added my IP in the database,...
Read more >
How to Find IP Addresses on a Network and Monitor their Usage
Learn how to find IP addresses on your network with just a few commands, and map devices on your network and the IP...
Read more >
Configure IP allow lists using Splunk Web
IP allow lists control which IP addresses on your network have access to specified features in your Splunk Cloud Platform deployment.
Read more >
IP Address Lookup & Geolocation Tool - Neustar
Neustar's IP address lookup and IP geolocation tool gives you an approximation of where any IP address is located, plus city, ZIP, latitude,...
Read more >
Wireshark Tutorial: Identifying Hosts and Users - Unit 42
If you have access to full packet capture of your network traffic, a pcap retrieved on an internal IP address should reveal an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found