mirror of
https://github.com/netbox-community/netbox-docker.git
synced 2025-08-15 02:22:29 +02:00
feat: Make startup scripts idempotent
This commit is contained in:
@ -1,13 +1,14 @@
|
||||
import sys
|
||||
|
||||
from dcim.models import Location, Site
|
||||
from startup_script_utils import load_yaml
|
||||
from startup_script_utils import load_yaml, split_params
|
||||
|
||||
rack_groups = load_yaml("/opt/netbox/initializers/locations.yml")
|
||||
|
||||
if rack_groups is None:
|
||||
sys.exit()
|
||||
|
||||
match_params = ["name", "slug", "site"]
|
||||
required_assocs = {"site": (Site, "name")}
|
||||
|
||||
for params in rack_groups:
|
||||
@ -17,7 +18,8 @@ for params in rack_groups:
|
||||
query = {field: params.pop(assoc)}
|
||||
params[assoc] = model.objects.get(**query)
|
||||
|
||||
location, created = Location.objects.get_or_create(**params)
|
||||
matching_params, defaults = split_params(params, match_params)
|
||||
location, created = Location.objects.get_or_create(**matching_params, defaults=defaults)
|
||||
|
||||
if created:
|
||||
print("🎨 Created location", location.name)
|
||||
|
Reference in New Issue
Block a user