add tenant to aggregate startup_script

This commit is contained in:
ryanmerolle 2020-12-30 18:22:12 -05:00
parent 90018fc6d7
commit 0f1cc9eea0
1 changed files with 12 additions and 0 deletions

View File

@ -3,6 +3,7 @@ import sys
from ipam.models import Aggregate, RIR
from netaddr import IPNetwork
from startup_script_utils import *
from tenancy.models import Tenant
aggregates = load_yaml('/opt/netbox/initializers/aggregates.yml')
@ -13,6 +14,10 @@ required_assocs = {
'rir': (RIR, 'name')
}
optional_assocs = {
'tenant': (Tenant, 'name'),
}
for params in aggregates:
custom_field_data = pop_custom_fields(params)
@ -24,6 +29,13 @@ for params in aggregates:
params[assoc] = model.objects.get(**query)
for assoc, details in optional_assocs.items():
if assoc in params:
model, field = details
query = { field: params.pop(assoc) }
params[assoc] = model.objects.get(**query)
aggregate, created = Aggregate.objects.get_or_create(**params)
if created: