Change logic to prevent sys.exit from stopping script processing

This commit is contained in:
Hank Preston 2020-05-06 13:25:09 -04:00
parent 5769684c98
commit 5292afaae0
28 changed files with 544 additions and 572 deletions

View file

@ -5,35 +5,34 @@ import sys
tenants = load_yaml('/opt/netbox/initializers/tenants.yml')
if tenants is None:
sys.exit()
if not tenants is None:
optional_assocs = {
'group': (TenantGroup, 'name')
}
optional_assocs = {
'group': (TenantGroup, 'name')
}
for params in tenants:
custom_fields = params.pop('custom_fields', None)
for params in tenants:
custom_fields = params.pop('custom_fields', None)
for assoc, details in optional_assocs.items():
if assoc in params:
model, field = details
query = { field: params.pop(assoc) }
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)
params[assoc] = model.objects.get(**query)
tenant, created = Tenant.objects.get_or_create(**params)
tenant, created = Tenant.objects.get_or_create(**params)
if created:
if custom_fields is not None:
for cf_name, cf_value in custom_fields.items():
custom_field = CustomField.objects.get(name=cf_name)
custom_field_value = CustomFieldValue.objects.create(
field=custom_field,
obj=tenant,
value=cf_value
)
if created:
if custom_fields is not None:
for cf_name, cf_value in custom_fields.items():
custom_field = CustomField.objects.get(name=cf_name)
custom_field_value = CustomFieldValue.objects.create(
field=custom_field,
obj=tenant,
value=cf_value
)
tenant.custom_field_values.add(custom_field_value)
tenant.custom_field_values.add(custom_field_value)
print("👩‍💻 Created Tenant", tenant.name)
print("👩‍💻 Created Tenant", tenant.name)