2019-12-02 03:23:53 -06:00
|
|
|
import sys
|
|
|
|
|
2021-04-07 02:15:21 -05:00
|
|
|
from dcim.models import Location, Site
|
2022-04-05 01:34:08 -05:00
|
|
|
from startup_script_utils import load_yaml, split_params
|
2021-02-08 04:59:33 -06:00
|
|
|
|
2021-04-07 02:15:21 -05:00
|
|
|
rack_groups = load_yaml("/opt/netbox/initializers/locations.yml")
|
2019-12-02 03:23:53 -06:00
|
|
|
|
2020-02-05 08:31:01 -06:00
|
|
|
if rack_groups is None:
|
2021-02-08 04:59:33 -06:00
|
|
|
sys.exit()
|
2019-12-02 03:23:53 -06:00
|
|
|
|
2022-04-05 01:34:08 -05:00
|
|
|
match_params = ["name", "slug", "site"]
|
2021-02-08 04:59:33 -06:00
|
|
|
required_assocs = {"site": (Site, "name")}
|
2019-12-02 03:23:53 -06:00
|
|
|
|
2020-02-05 08:31:01 -06:00
|
|
|
for params in rack_groups:
|
2019-12-02 03:23:53 -06:00
|
|
|
|
2021-02-08 04:59:33 -06: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 03:23:53 -06:00
|
|
|
|
2022-04-05 01:34:08 -05:00
|
|
|
matching_params, defaults = split_params(params, match_params)
|
|
|
|
location, created = Location.objects.get_or_create(**matching_params, defaults=defaults)
|
2019-12-02 03:23:53 -06:00
|
|
|
|
2021-02-08 04:59:33 -06:00
|
|
|
if created:
|
2021-04-07 02:15:21 -05:00
|
|
|
print("🎨 Created location", location.name)
|