Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set a flag in the /dcim/interfaces/ to show that an address has been assigned #1931

Closed
bdlamprecht opened this issue Feb 28, 2018 · 3 comments
Labels
status: accepted This issue has been accepted for implementation type: feature Introduction of new functionality to the application

Comments

@bdlamprecht
Copy link
Contributor

Issue type

[X] Feature request

Environment

  • Python version: 3.6
  • NetBox version: v2.3.0

Description

This came up in the NetworkToCode Slack channel today and while I have a basic understanding of why it isn't currently implemented, I'm just posing the question here just to see if it was at all possible and if so, what performance hits would occur.

The question came up when I discovered that you could see the relationship between address and interface at the endpoint /ipam/ip-address/, but not the other way around (i.e. no way to see if an interface even has an address assigned at /dcim/interfaces/).

What I'm trying to accomplish is to be able to look at /dcim/interfaces/ and immediately tell if I should use a Layer2 interface template (no assigned address) or a Layer3 interface template (an address IS assigned).

If there is an address, I don't even care it is, as I've already coded the logic to loop through the addresses assigned to a device.

Perhaps this is just a simple mind thinking out loud here, but wouldn't it be possible to set a flag on the /dcim/interfaces/ model when an address is assigned and then clear that flag if the address assignment was removed?

While I don't really understand the intricate details of the Django ORM, this example came up:

Joey Wilhelm [4:10 PM]
If you would like to add a `has_ipaddresses` boolean in the API, you would want to start with something like this:
>>> from django.db.models import Exists, OuterRef
>>> 
>>> from dcim.models import Interface
>>> from ipam.models import IPAddress
>>>
>>> ip_addresses = IPAddress.objects.filter(interface_id=OuterRef('pk'))
>>> interfaces = Interface.objects.annotate(has_ipaddresses=Exists(ip_addresses))

Thoughts one way or another?

@bdlamprecht
Copy link
Contributor Author

I've been playing around with this some today and I think I may have found an alternative to setting a flag for when an IP address is assigned that wouldn't require any "penalties" due to primary-key and foreign-key lookups (pardon my terminology if it is not accurate).

As far as I can gather, the only instance where an interface carries specific vlans (access, tagged, tagged_all) AND has an IP address assigned is in the case of sub-interfaces. However, the current way to model that scenario within NetBox is to define each one separately (parent and child relationship are not tied together within NetBox).

If some type of check were created to ensure that interfaces can only be assigned VLANs OR IP addresses (similar to how you cannot have mode:access and tagged_vlans: { } at the same time), I believe this would work.

Can anyone think of why this would not be do-able?

@jeremystretch jeremystretch added the status: under review Further discussion is needed to determine this issue's scope and/or implementation label Jul 18, 2018
@flycam
Copy link

flycam commented Jul 30, 2018

If some type of check were created to ensure that interfaces can only be assigned VLANs OR IP addresses (similar to how you cannot have mode:access and tagged_vlans: { } at the same time), I believe this would work.

On Linux, it is possible to set an ip directly to an interface, and then also have tagged vlans on that interface as well.

@bdlamprecht
Copy link
Contributor Author

Thanks for the information, that is interesting.

When you say something is impossible, leave it to Linux to prove otherwise. 😄

@jeremystretch jeremystretch added status: accepted This issue has been accepted for implementation type: feature Introduction of new functionality to the application API change and removed status: under review Further discussion is needed to determine this issue's scope and/or implementation labels Nov 5, 2018
jeremystretch added a commit that referenced this issue Nov 5, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Jan 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: accepted This issue has been accepted for implementation type: feature Introduction of new functionality to the application
Projects
None yet
Development

No branches or pull requests

3 participants