Skip to content

Commit 0d29e57

Browse files
committed
Update get_for_virtualmachine to support lookup by cluster location
scope Update test case to include location scoped cluster
1 parent cbe14b7 commit 0d29e57

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

netbox/ipam/querysets.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def get_for_virtualmachine(self, vm):
148148

149149
# Find all relevant VLANGroups
150150
q = Q()
151-
site = vm.site or vm.cluster._site
151+
site = vm.site
152152
if vm.cluster:
153153
# Add VLANGroups scoped to the assigned cluster (or its group)
154154
q |= Q(
@@ -160,6 +160,30 @@ def get_for_virtualmachine(self, vm):
160160
scope_type=ContentType.objects.get_by_natural_key('virtualization', 'clustergroup'),
161161
scope_id=vm.cluster.group_id
162162
)
163+
# Looking all possible cluster scopes
164+
if vm.cluster.scope_type == ContentType.objects.get_by_natural_key('dcim', 'location'):
165+
site = site or vm.cluster.scope.site
166+
q |= Q(
167+
scope_type=ContentType.objects.get_by_natural_key('dcim', 'location'),
168+
scope_id__in=vm.cluster.scope.get_ancestors(include_self=True)
169+
)
170+
elif vm.cluster.scope_type == ContentType.objects.get_by_natural_key('dcim', 'site'):
171+
site = site or vm.cluster.scope
172+
q |= Q(
173+
scope_type=ContentType.objects.get_by_natural_key('dcim', 'site'),
174+
scope_id=vm.cluster.scope.pk
175+
)
176+
elif vm.cluster.scope_type == ContentType.objects.get_by_natural_key('dcim', 'sitegroup'):
177+
q |= Q(
178+
scope_type=ContentType.objects.get_by_natural_key('dcim', 'sitegroup'),
179+
scope_id__in=vm.cluster.scope.get_ancestors(include_self=True)
180+
)
181+
elif vm.cluster.scope_type == ContentType.objects.get_by_natural_key('dcim', 'region'):
182+
q |= Q(
183+
scope_type=ContentType.objects.get_by_natural_key('dcim', 'region'),
184+
scope_id__in=vm.cluster.scope.get_ancestors(include_self=True)
185+
)
186+
# VM can be assigned to a site without a cluster so checking assigned site independently
163187
if site:
164188
# Add VLANGroups scoped to the assigned site (or its group or region)
165189
q |= Q(

netbox/ipam/tests/test_filtersets.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,6 +1849,7 @@ def setUpTestData(cls):
18491849
Cluster(name='Cluster 1', type=cluster_type, group=cluster_groups[0], scope=sites[0]),
18501850
Cluster(name='Cluster 2', type=cluster_type, group=cluster_groups[1], scope=sites[1]),
18511851
Cluster(name='Cluster 3', type=cluster_type, group=cluster_groups[2], scope=sites[2]),
1852+
Cluster(name='Cluster 4', type=cluster_type, group=cluster_groups[0], scope=locations[0]),
18521853
)
18531854
for cluster in clusters:
18541855
cluster.save()
@@ -1857,13 +1858,15 @@ def setUpTestData(cls):
18571858
VirtualMachine(name='Virtual Machine 1', cluster=clusters[0]),
18581859
VirtualMachine(name='Virtual Machine 2', cluster=clusters[1]),
18591860
VirtualMachine(name='Virtual Machine 3', cluster=clusters[2]),
1861+
VirtualMachine(name='Virtual Machine 4', cluster=clusters[3]),
18601862
)
18611863
VirtualMachine.objects.bulk_create(virtual_machines)
18621864

18631865
vm_interfaces = (
18641866
VMInterface(virtual_machine=virtual_machines[0], name='VM Interface 1'),
18651867
VMInterface(virtual_machine=virtual_machines[1], name='VM Interface 2'),
18661868
VMInterface(virtual_machine=virtual_machines[2], name='VM Interface 3'),
1869+
VMInterface(virtual_machine=virtual_machines[3], name='VM Interface 4'),
18671870
)
18681871
VMInterface.objects.bulk_create(vm_interfaces)
18691872

@@ -1890,6 +1893,7 @@ def setUpTestData(cls):
18901893
VLANGroup(name='Cluster 1', slug='cluster-1', scope=clusters[0]),
18911894
VLANGroup(name='Cluster 2', slug='cluster-2', scope=clusters[1]),
18921895
VLANGroup(name='Cluster 3', slug='cluster-3', scope=clusters[2]),
1896+
VLANGroup(name='Cluster 4', slug='cluster-4', scope=clusters[3]),
18931897

18941898
# General purpose VLAN groups
18951899
VLANGroup(name='VLAN Group 1', slug='vlan-group-1'),
@@ -1944,11 +1948,12 @@ def setUpTestData(cls):
19441948
VLAN(vid=19, name='Cluster 1', group=groups[18]),
19451949
VLAN(vid=20, name='Cluster 2', group=groups[19]),
19461950
VLAN(vid=21, name='Cluster 3', group=groups[20]),
1951+
VLAN(vid=22, name='Cluster 4', group=groups[21]),
19471952
VLAN(
19481953
vid=101,
19491954
name='VLAN 101',
19501955
site=sites[3],
1951-
group=groups[21],
1956+
group=groups[22],
19521957
role=roles[0],
19531958
tenant=tenants[0],
19541959
status=VLANStatusChoices.STATUS_ACTIVE,
@@ -1957,7 +1962,7 @@ def setUpTestData(cls):
19571962
vid=102,
19581963
name='VLAN 102',
19591964
site=sites[3],
1960-
group=groups[21],
1965+
group=groups[22],
19611966
role=roles[0],
19621967
tenant=tenants[0],
19631968
status=VLANStatusChoices.STATUS_ACTIVE,
@@ -1966,7 +1971,7 @@ def setUpTestData(cls):
19661971
vid=201,
19671972
name='VLAN 201',
19681973
site=sites[4],
1969-
group=groups[22],
1974+
group=groups[23],
19701975
role=roles[1],
19711976
tenant=tenants[1],
19721977
status=VLANStatusChoices.STATUS_DEPRECATED,
@@ -1975,7 +1980,7 @@ def setUpTestData(cls):
19751980
vid=202,
19761981
name='VLAN 202',
19771982
site=sites[4],
1978-
group=groups[22],
1983+
group=groups[23],
19791984
role=roles[1],
19801985
tenant=tenants[1],
19811986
status=VLANStatusChoices.STATUS_DEPRECATED,
@@ -1984,7 +1989,7 @@ def setUpTestData(cls):
19841989
vid=301,
19851990
name='VLAN 301',
19861991
site=sites[5],
1987-
group=groups[23],
1992+
group=groups[24],
19881993
role=roles[2],
19891994
tenant=tenants[2],
19901995
status=VLANStatusChoices.STATUS_RESERVED,
@@ -1993,13 +1998,13 @@ def setUpTestData(cls):
19931998
vid=302,
19941999
name='VLAN 302',
19952000
site=sites[5],
1996-
group=groups[23],
2001+
group=groups[24],
19972002
role=roles[2],
19982003
tenant=tenants[2],
19992004
status=VLANStatusChoices.STATUS_RESERVED,
20002005
),
20012006
# Create one globally available VLAN on a VLAN group
2002-
VLAN(vid=500, name='VLAN Group 1', group=groups[24]),
2007+
VLAN(vid=500, name='VLAN Group 1', group=groups[25]),
20032008
# Create one globally available VLAN
20042009
VLAN(vid=1000, name='Global VLAN'),
20052010
# Create some Q-in-Q service VLANs
@@ -2130,6 +2135,9 @@ def test_available_on_virtualmachine(self):
21302135
vm_id = VirtualMachine.objects.first().pk
21312136
params = {'available_on_virtualmachine': vm_id}
21322137
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 7) # 5 scoped + 1 global group + 1 global
2138+
vm_id = VirtualMachine.objects.get(name='Virtual Machine 4').pk
2139+
params = {'available_on_virtualmachine': vm_id}
2140+
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 8) # 6 scoped + 1 global group + 1 global
21332141

21342142
def test_available_at_site(self):
21352143
site_id = Site.objects.first().pk

0 commit comments

Comments
 (0)