Skip to content

Commit 0f1cc9e

Browse files
committed
add tenant to aggregate startup_script
1 parent 90018fc commit 0f1cc9e

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

startup_scripts/160_aggregates.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from ipam.models import Aggregate, RIR
44
from netaddr import IPNetwork
55
from startup_script_utils import *
6+
from tenancy.models import Tenant
67

78
aggregates = load_yaml('/opt/netbox/initializers/aggregates.yml')
89

@@ -13,6 +14,10 @@
1314
'rir': (RIR, 'name')
1415
}
1516

17+
optional_assocs = {
18+
'tenant': (Tenant, 'name'),
19+
}
20+
1621
for params in aggregates:
1722
custom_field_data = pop_custom_fields(params)
1823

@@ -24,6 +29,13 @@
2429

2530
params[assoc] = model.objects.get(**query)
2631

32+
for assoc, details in optional_assocs.items():
33+
if assoc in params:
34+
model, field = details
35+
query = { field: params.pop(assoc) }
36+
37+
params[assoc] = model.objects.get(**query)
38+
2739
aggregate, created = Aggregate.objects.get_or_create(**params)
2840

2941
if created:

0 commit comments

Comments
 (0)