-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Enable 'ConfigContext' to traverse multi-level region heirarchy #2265
Comments
Also, I didn't think this meritted it's own FR, but the dialog boxes in the WebUI for Would it be possible to make those a little bit larger (at a minimum 8 lines tall for each |
I started playing with this and came up with this simple solution by coercing an MPTT queryset within the Config Context Manager class ConfigContextQuerySet(QuerySet):
def get_for_object(self, obj):
"""
Return all applicable ConfigContexts for a given object. Only active ConfigContexts will be included.
"""
# `device_role` for Device; `role` for VirtualMachine
role = getattr(obj, 'device_role', None) or obj.role
# we must account for nested regions
region = getattr(obj.site, 'region', None)
if region:
regions = region.get_ancestors(include_self=True)
else:
regions = []
return self.filter(
Q(regions__in=regions) | Q(regions=None),
Q(sites=obj.site) | Q(sites=None),
Q(roles=role) | Q(roles=None),
Q(tenants=obj.tenant) | Q(tenants=None),
Q(platforms=obj.platform) | Q(platforms=None),
is_active=True,
).order_by('weight', 'name') Note however that the region hierarchy does not affect the rendering of the context directly. You would still need to apply the appropriate weights to the contexts at each region to basically rebuild the region hierarchy within the Config Context hierarchy. I think this makes sense because it doesn't do anything fancier than what is implemented today, it just allows including the contexts from all the applicable regions in the tree. Not sure if we want to prerender the region tree context or otherwise order it specifically in the resulting queryset? That seems complicated and will likely result in additional queries needed to render a region tree context. |
I don't understand what you're doing with this code, but if it makes it possible to do what I described in the bug report, I'm all for it. However, one thing you mentioned made me somewhat concerned:
From the way I interpret that, I'd prefer not to have to put weights at each level of the region hierarchy if at all possible, that seems tedious. That may not be what you intended to describe, and if not, great. |
So suppose you have three regions forming a tree:
And you have a site assigned to Frankfurt. I think the caveat is that a context applied to Frankfurt or Germany won't automatically take precedence over a context applied to Europe: Weight will be evaluated irrespective of each region's position in the hierarchy. This should be pretty easy to work around so long as you give some forethought to weighting. For example, you might say countries have a weight between 100 and 199, and cities have a weight between 200 and 299. Something like that. Also keep in mind that weights only come into play when two or more contexts have conflicting values. So if you define |
Yeah, okay, that idea about coming up with a schema for weights works for me. Honestly, most of the Only in certain circumstances would a country have it's own values (i.e. China tends to have certain government policies that we'd have to work around and perhaps India too). Very rarely would a city/specific site have it's own settings. |
Issue type
[X] Bug report
Environment
Description
First off, let me personally thank you for implementing this feature. I believe this was a huge effort and will only drive more people to start using NetBox.
In my scenario, I have a multi-level
region
heirarchy as described here.To test this feature out, I created a
config_context
namedtacacs
and associated it with theregion
"United States (us)":Then I tried querying a device which resides in the
site
namedbld
, which is in a childregion
namedBoulder, CO
. When this happened, I received a blank"config_context": {}
.Then, per a recomendation from
jeremystretch
, I moved the association from the parentregion
"United States (us)" to the childregion
"Boulder, CO" and then queried the same device and got back the information I wanted (snipped for brevity):This request it to enable traversing several layers of parent-child
regions
to create the appropriateconfig_context
.The text was updated successfully, but these errors were encountered: