2019-12-02 10:23:53 +01:00
|
|
|
from dcim.models import Site,RackGroup
|
2020-02-05 15:31:01 +01:00
|
|
|
from startup_script_utils import load_yaml
|
2019-12-02 10:23:53 +01:00
|
|
|
import sys
|
|
|
|
|
2020-02-05 15:31:01 +01:00
|
|
|
rack_groups = load_yaml('/opt/netbox/initializers/rack_groups.yml')
|
2019-12-02 10:23:53 +01:00
|
|
|
|
2020-02-05 15:31:01 +01:00
|
|
|
if rack_groups is None:
|
|
|
|
sys.exit()
|
2019-12-02 10:23:53 +01:00
|
|
|
|
2020-02-05 15:31:01 +01:00
|
|
|
required_assocs = {
|
|
|
|
'site': (Site, 'name')
|
|
|
|
}
|
2019-12-02 10:23:53 +01:00
|
|
|
|
2020-02-05 15:31:01 +01:00
|
|
|
for params in rack_groups:
|
2019-12-02 10:23:53 +01:00
|
|
|
|
2020-02-05 15:31:01 +01:00
|
|
|
for assoc, details in required_assocs.items():
|
|
|
|
model, field = details
|
|
|
|
query = { field: params.pop(assoc) }
|
|
|
|
params[assoc] = model.objects.get(**query)
|
2019-12-02 10:23:53 +01:00
|
|
|
|
2020-02-05 15:31:01 +01:00
|
|
|
rack_group, created = RackGroup.objects.get_or_create(**params)
|
2019-12-02 10:23:53 +01:00
|
|
|
|
2020-02-05 15:31:01 +01:00
|
|
|
if created:
|
|
|
|
print("🎨 Created rack group", rack_group.name)
|
2019-12-02 10:23:53 +01:00
|
|
|
|