Skip to content

Commit 8d8c58d

Browse files
committed
optional assoc to cluster & circuit startup_script
1 parent cbaaffc commit 8d8c58d

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

initializers/circuits.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# - cid: Circuit_ID-1
22
# provider: Provider1
33
# type: Internet
4+
# tenant: tenant1
45
# - cid: Circuit_ID-2
56
# provider: Provider2
67
# type: MPLS

initializers/clusters.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# - name: cluster1
22
# type: Hyper-V
33
# group: Group 1
4+
# tenant: tenant1
45
# - name: cluster2
56
# type: Hyper-V
67
# site: SING 1

startup_scripts/170_clusters.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from dcim.models import Site
44
from startup_script_utils import *
55
from virtualization.models import Cluster, ClusterType, ClusterGroup
6+
from tenancy.models import Tenant
67

78
clusters = load_yaml('/opt/netbox/initializers/clusters.yml')
89

@@ -15,7 +16,8 @@
1516

1617
optional_assocs = {
1718
'site': (Site, 'name'),
18-
'group': (ClusterGroup, 'name')
19+
'group': (ClusterGroup, 'name'),
20+
'tenant': (Tenant, 'name')
1921
}
2022

2123
for params in clusters:

startup_scripts/300_circuits.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from circuits.models import Circuit, Provider, CircuitType
2+
from tenancy.models import Tenant
23
from startup_script_utils import *
34
import sys
45

@@ -12,6 +13,10 @@
1213
'type': (CircuitType, 'name')
1314
}
1415

16+
optional_assocs = {
17+
'tenant': (Tenant, 'name')
18+
}
19+
1520
for params in circuits:
1621
custom_field_data = pop_custom_fields(params)
1722

@@ -21,6 +26,13 @@
2126

2227
params[assoc] = model.objects.get(**query)
2328

29+
for assoc, details in optional_assocs.items():
30+
if assoc in params:
31+
model, field = details
32+
query = { field: params.pop(assoc) }
33+
34+
params[assoc] = model.objects.get(**query)
35+
2436
circuit, created = Circuit.objects.get_or_create(**params)
2537

2638
if created:

0 commit comments

Comments
 (0)