mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-17 21:18:16 -06:00
Issue #14962 VM to merge directly related site context
This commit is contained in:
parent
1b9e6bed55
commit
2f19c79b4c
@ -120,17 +120,14 @@ class ConfigContextModelQuerySet(RestrictedQuerySet):
|
|||||||
if self.model._meta.model_name == 'device':
|
if self.model._meta.model_name == 'device':
|
||||||
base_query.add((Q(locations=OuterRef('location')) | Q(locations=None)), Q.AND)
|
base_query.add((Q(locations=OuterRef('location')) | Q(locations=None)), Q.AND)
|
||||||
base_query.add((Q(device_types=OuterRef('device_type')) | Q(device_types=None)), Q.AND)
|
base_query.add((Q(device_types=OuterRef('device_type')) | Q(device_types=None)), Q.AND)
|
||||||
base_query.add((Q(roles=OuterRef('role')) | Q(roles=None)), Q.AND)
|
|
||||||
base_query.add((Q(sites=OuterRef('site')) | Q(sites=None)), Q.AND)
|
|
||||||
region_field = 'site__region'
|
|
||||||
sitegroup_field = 'site__group'
|
|
||||||
|
|
||||||
elif self.model._meta.model_name == 'virtualmachine':
|
elif self.model._meta.model_name == 'virtualmachine':
|
||||||
base_query.add((Q(roles=OuterRef('role')) | Q(roles=None)), Q.AND)
|
|
||||||
base_query.add((Q(sites=OuterRef('cluster__site')) | Q(sites=None)), Q.AND)
|
|
||||||
base_query.add(Q(device_types=None), Q.AND)
|
base_query.add(Q(device_types=None), Q.AND)
|
||||||
region_field = 'cluster__site__region'
|
|
||||||
sitegroup_field = 'cluster__site__group'
|
base_query.add((Q(roles=OuterRef('role')) | Q(roles=None)), Q.AND)
|
||||||
|
base_query.add((Q(sites=OuterRef('site')) | Q(sites=None)), Q.AND)
|
||||||
|
region_field = 'site__region'
|
||||||
|
sitegroup_field = 'site__group'
|
||||||
|
|
||||||
base_query.add(
|
base_query.add(
|
||||||
(Q(
|
(Q(
|
||||||
|
@ -270,7 +270,12 @@ class ConfigContextTest(TestCase):
|
|||||||
tag = Tag.objects.first()
|
tag = Tag.objects.first()
|
||||||
cluster_type = ClusterType.objects.create(name="Cluster Type")
|
cluster_type = ClusterType.objects.create(name="Cluster Type")
|
||||||
cluster_group = ClusterGroup.objects.create(name="Cluster Group")
|
cluster_group = ClusterGroup.objects.create(name="Cluster Group")
|
||||||
cluster = Cluster.objects.create(name="Cluster", group=cluster_group, type=cluster_type)
|
cluster = Cluster.objects.create(
|
||||||
|
name="Cluster",
|
||||||
|
group=cluster_group,
|
||||||
|
type=cluster_type,
|
||||||
|
site=site,
|
||||||
|
)
|
||||||
|
|
||||||
region_context = ConfigContext.objects.create(
|
region_context = ConfigContext.objects.create(
|
||||||
name="region",
|
name="region",
|
||||||
@ -354,6 +359,106 @@ class ConfigContextTest(TestCase):
|
|||||||
annotated_queryset = VirtualMachine.objects.filter(name=virtual_machine.name).annotate_config_context_data()
|
annotated_queryset = VirtualMachine.objects.filter(name=virtual_machine.name).annotate_config_context_data()
|
||||||
self.assertEqual(virtual_machine.get_config_context(), annotated_queryset[0].get_config_context())
|
self.assertEqual(virtual_machine.get_config_context(), annotated_queryset[0].get_config_context())
|
||||||
|
|
||||||
|
def test_annotation_same_as_get_for_object_virtualmachine_relations_direct_site(self):
|
||||||
|
region = Region.objects.first()
|
||||||
|
sitegroup = SiteGroup.objects.first()
|
||||||
|
site = Site.objects.first()
|
||||||
|
platform = Platform.objects.first()
|
||||||
|
tenantgroup = TenantGroup.objects.first()
|
||||||
|
tenant = Tenant.objects.first()
|
||||||
|
tag = Tag.objects.first()
|
||||||
|
cluster_type = ClusterType.objects.create(name="Cluster Type")
|
||||||
|
cluster_group = ClusterGroup.objects.create(name="Cluster Group")
|
||||||
|
cluster = Cluster.objects.create(
|
||||||
|
name="Cluster",
|
||||||
|
group=cluster_group,
|
||||||
|
type=cluster_type,
|
||||||
|
site=site,
|
||||||
|
)
|
||||||
|
|
||||||
|
region_context = ConfigContext.objects.create(
|
||||||
|
name="region",
|
||||||
|
weight=100,
|
||||||
|
data={"region": 1}
|
||||||
|
)
|
||||||
|
region_context.regions.add(region)
|
||||||
|
|
||||||
|
sitegroup_context = ConfigContext.objects.create(
|
||||||
|
name="sitegroup",
|
||||||
|
weight=100,
|
||||||
|
data={"sitegroup": 1}
|
||||||
|
)
|
||||||
|
sitegroup_context.site_groups.add(sitegroup)
|
||||||
|
|
||||||
|
site_context = ConfigContext.objects.create(
|
||||||
|
name="site",
|
||||||
|
weight=100,
|
||||||
|
data={"site": 1}
|
||||||
|
)
|
||||||
|
site_context.sites.add(site)
|
||||||
|
|
||||||
|
platform_context = ConfigContext.objects.create(
|
||||||
|
name="platform",
|
||||||
|
weight=100,
|
||||||
|
data={"platform": 1}
|
||||||
|
)
|
||||||
|
platform_context.platforms.add(platform)
|
||||||
|
|
||||||
|
tenant_group_context = ConfigContext.objects.create(
|
||||||
|
name="tenant group",
|
||||||
|
weight=100,
|
||||||
|
data={"tenant_group": 1}
|
||||||
|
)
|
||||||
|
tenant_group_context.tenant_groups.add(tenantgroup)
|
||||||
|
|
||||||
|
tenant_context = ConfigContext.objects.create(
|
||||||
|
name="tenant",
|
||||||
|
weight=100,
|
||||||
|
data={"tenant": 1}
|
||||||
|
)
|
||||||
|
tenant_context.tenants.add(tenant)
|
||||||
|
|
||||||
|
tag_context = ConfigContext.objects.create(
|
||||||
|
name="tag",
|
||||||
|
weight=100,
|
||||||
|
data={"tag": 1}
|
||||||
|
)
|
||||||
|
tag_context.tags.add(tag)
|
||||||
|
|
||||||
|
cluster_type_context = ConfigContext.objects.create(
|
||||||
|
name="cluster type",
|
||||||
|
weight=100,
|
||||||
|
data={"cluster_type": 1}
|
||||||
|
)
|
||||||
|
cluster_type_context.cluster_types.add(cluster_type)
|
||||||
|
|
||||||
|
cluster_group_context = ConfigContext.objects.create(
|
||||||
|
name="cluster group",
|
||||||
|
weight=100,
|
||||||
|
data={"cluster_group": 1}
|
||||||
|
)
|
||||||
|
cluster_group_context.cluster_groups.add(cluster_group)
|
||||||
|
|
||||||
|
cluster_context = ConfigContext.objects.create(
|
||||||
|
name="cluster",
|
||||||
|
weight=100,
|
||||||
|
data={"cluster": 1}
|
||||||
|
)
|
||||||
|
cluster_context.clusters.add(cluster)
|
||||||
|
|
||||||
|
virtual_machine = VirtualMachine.objects.create(
|
||||||
|
name="VM 2",
|
||||||
|
site=site,
|
||||||
|
tenant=tenant,
|
||||||
|
platform=platform,
|
||||||
|
role=DeviceRole.objects.first()
|
||||||
|
)
|
||||||
|
virtual_machine.tags.add(tag)
|
||||||
|
|
||||||
|
annotated_queryset = VirtualMachine.objects.filter(name=virtual_machine.name).annotate_config_context_data()
|
||||||
|
|
||||||
|
self.assertEqual(virtual_machine.get_config_context(), annotated_queryset[0].get_config_context())
|
||||||
|
|
||||||
def test_multiple_tags_return_distinct_objects(self):
|
def test_multiple_tags_return_distinct_objects(self):
|
||||||
"""
|
"""
|
||||||
Tagged items use a generic relationship, which results in duplicate rows being returned when queried.
|
Tagged items use a generic relationship, which results in duplicate rows being returned when queried.
|
||||||
|
Loading…
Reference in New Issue
Block a user