Skip to content

Commit 9874cef

Browse files
committed
VLAN Groups can be scoped to multiple types
1 parent 1c4b674 commit 9874cef

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

initializers/vlan_groups.yml

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
# - name: VLAN group 1
2-
# site: AMS 1
2+
# scope_type: "dcim.region"
3+
# scope: Amsterdam
34
# slug: vlan-group-1
45
# - name: VLAN group 2
5-
# site: AMS 1
6+
# scope_type: "dcim.site"
7+
# scope: AMS 1
68
# slug: vlan-group-2
9+
# - name: VLAN group 3
10+
# scope_type: "dcim.location"
11+
# scope: cage 101
12+
# slug: vlan-group-3
13+
# - name: VLAN group 4
14+
# scope_type: "dcim.rack"
15+
# scope: rack-01
16+
# slug: vlan-group-4
17+
# - name: VLAN group 5
18+
# scope_type: "virtualization.cluster"
19+
# scope: cluster1
20+
# slug: vlan-group-5
21+
# - name: VLAN group 6
22+
# scope_type: "virtualization.clustergroup"
23+
# scope: Group 1
24+
# slug: vlan-group-6

startup_scripts/200_vlan_groups.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import sys
22

3-
from dcim.models import Site
3+
from django.contrib.contenttypes.models import ContentType
44
from ipam.models import VLANGroup
55
from startup_script_utils import load_yaml, pop_custom_fields, set_custom_fields_values
66

@@ -9,7 +9,7 @@
99
if vlan_groups is None:
1010
sys.exit()
1111

12-
optional_assocs = {"site": (Site, "name")}
12+
optional_assocs = {"scope": (None, "name")}
1313

1414
for params in vlan_groups:
1515
custom_field_data = pop_custom_fields(params)
@@ -18,9 +18,17 @@
1818
if assoc in params:
1919
model, field = details
2020
query = {field: params.pop(assoc)}
21-
22-
params[assoc] = model.objects.get(**query)
23-
21+
# Get model from Contenttype
22+
scope_type = params.pop("scope_type", None)
23+
if not scope_type:
24+
print("scope_type is missing from VLAN Group")
25+
continue
26+
app_label, model = str(scope_type).split(".")
27+
ct = ContentType.objects.get(app_label=app_label, model=model)
28+
if not ct:
29+
print(f"ContentType for app_label = '{app_label}' and model = '{model}' not found")
30+
continue
31+
params["scope_id"] = ct.model_class().objects.get(**query).id
2432
vlan_group, created = VLANGroup.objects.get_or_create(**params)
2533

2634
if created:

0 commit comments

Comments
 (0)