From cec327fb7e36538ea86c743f841b8df12d03834c Mon Sep 17 00:00:00 2001 From: A Letessier Date: Thu, 30 Dec 2021 15:15:24 +0100 Subject: [PATCH] parent optional assoc in tenant_groups initializer initialize a tenant group with parent. --- startup_scripts/020_tenant_groups.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/startup_scripts/020_tenant_groups.py b/startup_scripts/020_tenant_groups.py index 65cf155..8673c72 100644 --- a/startup_scripts/020_tenant_groups.py +++ b/startup_scripts/020_tenant_groups.py @@ -1,15 +1,25 @@ import sys -from startup_script_utils import load_yaml from tenancy.models import TenantGroup +from startup_script_utils import load_yaml -tenant_groups = load_yaml("/opt/netbox/initializers/tenant_groups.yml") +tenant_groups = load_yaml('/opt/netbox/initializers/tenant_groups.yml') if tenant_groups is None: - sys.exit() + sys.exit() + +optional_assocs = { + 'parent': (TenantGroup, 'name'), +} for params in tenant_groups: - tenant_group, created = TenantGroup.objects.get_or_create(**params) + 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) - if created: - print("🔳 Created Tenant Group", tenant_group.name) + tenant_group, created = TenantGroup.objects.get_or_create(**params) + + if created: + print("🔳 Created Tenant Group", tenant_group.name)