@@ -81,30 +81,34 @@ def get_for_virtualmachine(self, vm):
8181
8282 # Find all relevant VLANGroups
8383 q = Q ()
84- if vm .cluster .site :
85- if vm .cluster .site .region :
84+ site = vm .site or vm .cluster .site
85+ if vm .cluster :
86+ # Add VLANGroups scoped to the assigned cluster (or its group)
87+ q |= Q (
88+ scope_type = ContentType .objects .get_by_natural_key ('virtualization' , 'cluster' ),
89+ scope_id = vm .cluster_id
90+ )
91+ if vm .cluster .group :
92+ q |= Q (
93+ scope_type = ContentType .objects .get_by_natural_key ('virtualization' , 'clustergroup' ),
94+ scope_id = vm .cluster .group_id
95+ )
96+ if site :
97+ # Add VLANGroups scoped to the assigned site (or its group or region)
98+ q |= Q (
99+ scope_type = ContentType .objects .get_by_natural_key ('dcim' , 'site' ),
100+ scope_id = site .pk
101+ )
102+ if site .region :
86103 q |= Q (
87104 scope_type = ContentType .objects .get_by_natural_key ('dcim' , 'region' ),
88- scope_id__in = vm . cluster . site .region .get_ancestors (include_self = True )
105+ scope_id__in = site .region .get_ancestors (include_self = True )
89106 )
90- if vm . cluster . site .group :
107+ if site .group :
91108 q |= Q (
92109 scope_type = ContentType .objects .get_by_natural_key ('dcim' , 'sitegroup' ),
93- scope_id__in = vm . cluster . site .group .get_ancestors (include_self = True )
110+ scope_id__in = site .group .get_ancestors (include_self = True )
94111 )
95- q |= Q (
96- scope_type = ContentType .objects .get_by_natural_key ('dcim' , 'site' ),
97- scope_id = vm .cluster .site_id
98- )
99- if vm .cluster .group :
100- q |= Q (
101- scope_type = ContentType .objects .get_by_natural_key ('virtualization' , 'clustergroup' ),
102- scope_id = vm .cluster .group_id
103- )
104- q |= Q (
105- scope_type = ContentType .objects .get_by_natural_key ('virtualization' , 'cluster' ),
106- scope_id = vm .cluster_id
107- )
108112 vlan_groups = VLANGroup .objects .filter (q )
109113
110114 # Return all applicable VLANs
@@ -113,7 +117,7 @@ def get_for_virtualmachine(self, vm):
113117 Q (group__scope_id__isnull = True , site__isnull = True ) | # Global group VLANs
114118 Q (group__isnull = True , site__isnull = True ) # Global VLANs
115119 )
116- if vm . cluster . site :
117- q |= Q (site = vm . cluster . site )
120+ if site :
121+ q |= Q (site = site )
118122
119123 return self .filter (q )
0 commit comments