feat: Make startup scripts idempotent
This commit is contained in:
parent
a6eb4fef00
commit
9be7b0e109
42 changed files with 275 additions and 95 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue