From 0f1cc9eea09235ab229602f906ad58bc9aca4ec0 Mon Sep 17 00:00:00 2001 From: ryanmerolle Date: Wed, 30 Dec 2020 18:22:12 -0500 Subject: [PATCH] add tenant to aggregate startup_script --- startup_scripts/160_aggregates.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/startup_scripts/160_aggregates.py b/startup_scripts/160_aggregates.py index cc1d220..40aea1b 100644 --- a/startup_scripts/160_aggregates.py +++ b/startup_scripts/160_aggregates.py @@ -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: