Skip to content

Commit 2495938

Browse files
Change init order and location initializer (#34)
* __init__.py: Init platforms immediately after manufacturer. locations.py: enable parent location initialization via OPTIONAL_ASSOCS. * Add location parent example YAML definitions * Fixed Tests Co-authored-by: Tobias Genannt <[email protected]>
1 parent 9acdf4e commit 2495938

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

src/netbox_initializers/initializers/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@
2424
"power_panels",
2525
"power_feeds",
2626
"manufacturers",
27+
"platforms",
2728
"device_roles",
2829
"device_types",
2930
"cluster_types",
3031
"cluster_groups",
3132
"clusters",
3233
"devices",
3334
"interfaces",
34-
"platforms",
3535
"route_targets",
3636
"vrfs",
3737
"rirs",

src/netbox_initializers/initializers/locations.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
from . import BaseInitializer, register_initializer
44

5-
MATCH_PARAMS = ["name", "slug", "site"]
6-
REQUIRED_ASSOCS = {"site": (Site, "name")}
5+
OPTIONAL_ASSOCS = {"site": (Site, "name"), "parent": (Location, "name")}
76

87

98
class LocationInitializer(BaseInitializer):
@@ -15,12 +14,13 @@ def load_data(self):
1514
return
1615
for params in locations:
1716

18-
for assoc, details in REQUIRED_ASSOCS.items():
19-
model, field = details
20-
query = {field: params.pop(assoc)}
21-
params[assoc] = model.objects.get(**query)
17+
for assoc, details in OPTIONAL_ASSOCS.items():
18+
if assoc in params:
19+
model, field = details
20+
query = {field: params.pop(assoc)}
21+
params[assoc] = model.objects.get(**query)
2222

23-
matching_params, defaults = self.split_params(params, MATCH_PARAMS)
23+
matching_params, defaults = self.split_params(params)
2424
location, created = Location.objects.get_or_create(**matching_params, defaults=defaults)
2525

2626
if created:
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
# - name: cage 101
22
# slug: cage-101
33
# site: SING 1
4+
# - name: Parent Location
5+
# slug: parent-location
6+
# site: SING 1
7+
# - name: Child location
8+
# slug: child-location
9+
# site: SING 1
10+
# parent: Parent Location

0 commit comments

Comments
 (0)