optional assoc to cluster & circuit startup_script

This commit is contained in:
ryanmerolle 2020-12-30 19:09:08 -05:00
parent cbaaffc589
commit 8d8c58df54
4 changed files with 17 additions and 1 deletions

View File

@ -1,6 +1,7 @@
# - cid: Circuit_ID-1
# provider: Provider1
# type: Internet
# tenant: tenant1
# - cid: Circuit_ID-2
# provider: Provider2
# type: MPLS

View File

@ -1,6 +1,7 @@
# - name: cluster1
# type: Hyper-V
# group: Group 1
# tenant: tenant1
# - name: cluster2
# type: Hyper-V
# site: SING 1

View File

@ -3,6 +3,7 @@ import sys
from dcim.models import Site
from startup_script_utils import *
from virtualization.models import Cluster, ClusterType, ClusterGroup
from tenancy.models import Tenant
clusters = load_yaml('/opt/netbox/initializers/clusters.yml')
@ -15,7 +16,8 @@ required_assocs = {
optional_assocs = {
'site': (Site, 'name'),
'group': (ClusterGroup, 'name')
'group': (ClusterGroup, 'name'),
'tenant': (Tenant, 'name')
}
for params in clusters:

View File

@ -1,4 +1,5 @@
from circuits.models import Circuit, Provider, CircuitType
from tenancy.models import Tenant
from startup_script_utils import *
import sys
@ -12,6 +13,10 @@ required_assocs = {
'type': (CircuitType, 'name')
}
optional_assocs = {
'tenant': (Tenant, 'name')
}
for params in circuits:
custom_field_data = pop_custom_fields(params)
@ -21,6 +26,13 @@ for params in circuits:
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)
circuit, created = Circuit.objects.get_or_create(**params)
if created: