diff --git a/initializers/sites.yml b/initializers/sites.yml index 0015f4e..7ace3aa 100644 --- a/initializers/sites.yml +++ b/initializers/sites.yml @@ -20,6 +20,7 @@ # status: active # facility: Amsterdam 3 # asn: 67890 +# tenant: tenant1 # custom_field_data: # text_field: Description for AMS3 # - name: SING 1 @@ -28,5 +29,6 @@ # status: active # facility: Singapore 1 # asn: 09876 +# tenant: tenant2 # custom_field_data: # text_field: Description for SING1 diff --git a/startup_scripts/020_tenant_groups.py b/startup_scripts/020_tenant_groups.py new file mode 100644 index 0000000..65cf155 --- /dev/null +++ b/startup_scripts/020_tenant_groups.py @@ -0,0 +1,15 @@ +import sys + +from startup_script_utils import load_yaml +from tenancy.models import TenantGroup + +tenant_groups = load_yaml("/opt/netbox/initializers/tenant_groups.yml") + +if tenant_groups is None: + sys.exit() + +for params in tenant_groups: + tenant_group, created = TenantGroup.objects.get_or_create(**params) + + if created: + print("🔳 Created Tenant Group", tenant_group.name) diff --git a/startup_scripts/030_tenants.py b/startup_scripts/030_tenants.py new file mode 100644 index 0000000..7b1a629 --- /dev/null +++ b/startup_scripts/030_tenants.py @@ -0,0 +1,28 @@ +import sys + +from startup_script_utils import load_yaml, pop_custom_fields, set_custom_fields_values +from tenancy.models import Tenant, TenantGroup + +tenants = load_yaml("/opt/netbox/initializers/tenants.yml") + +if tenants is None: + sys.exit() + +optional_assocs = {"group": (TenantGroup, "name")} + +for params in tenants: + custom_field_data = pop_custom_fields(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) + + tenant, created = Tenant.objects.get_or_create(**params) + + if created: + set_custom_fields_values(tenant, custom_field_data) + + print("👩‍💻 Created Tenant", tenant.name) diff --git a/startup_scripts/110_tenant_groups.py b/startup_scripts/110_tenant_groups.py deleted file mode 100644 index 65cf155..0000000 --- a/startup_scripts/110_tenant_groups.py +++ /dev/null @@ -1,15 +0,0 @@ -import sys - -from startup_script_utils import load_yaml -from tenancy.models import TenantGroup - -tenant_groups = load_yaml("/opt/netbox/initializers/tenant_groups.yml") - -if tenant_groups is None: - sys.exit() - -for params in tenant_groups: - tenant_group, created = TenantGroup.objects.get_or_create(**params) - - if created: - print("🔳 Created Tenant Group", tenant_group.name) diff --git a/startup_scripts/120_tenants.py b/startup_scripts/120_tenants.py deleted file mode 100644 index 7b1a629..0000000 --- a/startup_scripts/120_tenants.py +++ /dev/null @@ -1,28 +0,0 @@ -import sys - -from startup_script_utils import load_yaml, pop_custom_fields, set_custom_fields_values -from tenancy.models import Tenant, TenantGroup - -tenants = load_yaml("/opt/netbox/initializers/tenants.yml") - -if tenants is None: - sys.exit() - -optional_assocs = {"group": (TenantGroup, "name")} - -for params in tenants: - custom_field_data = pop_custom_fields(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) - - tenant, created = Tenant.objects.get_or_create(**params) - - if created: - set_custom_fields_values(tenant, custom_field_data) - - print("👩‍💻 Created Tenant", tenant.name)