mirror of
https://github.com/netbox-community/netbox-docker.git
synced 2025-08-09 08:05:08 +02:00
Add function to load YAML files
This commit starts to remove some code redundancy from the startup scripts for easier maintenance.
This commit is contained in:
@ -1,31 +1,26 @@
|
||||
from dcim.models import Region
|
||||
from ruamel.yaml import YAML
|
||||
from pathlib import Path
|
||||
from startup_script_utils import load_yaml
|
||||
import sys
|
||||
|
||||
file = Path('/opt/netbox/initializers/regions.yml')
|
||||
if not file.is_file():
|
||||
regions = load_yaml('/opt/netbox/initializers/regions.yml')
|
||||
|
||||
if regions is None:
|
||||
sys.exit()
|
||||
|
||||
with file.open('r') as stream:
|
||||
yaml=YAML(typ='safe')
|
||||
regions = yaml.load(stream)
|
||||
optional_assocs = {
|
||||
'parent': (Region, 'name')
|
||||
}
|
||||
|
||||
optional_assocs = {
|
||||
'parent': (Region, 'name')
|
||||
}
|
||||
for params in regions:
|
||||
|
||||
if regions is not None:
|
||||
for params in regions:
|
||||
for assoc, details in optional_assocs.items():
|
||||
if assoc in params:
|
||||
model, field = details
|
||||
query = { field: params.pop(assoc) }
|
||||
|
||||
for assoc, details in optional_assocs.items():
|
||||
if assoc in params:
|
||||
model, field = details
|
||||
query = { field: params.pop(assoc) }
|
||||
params[assoc] = model.objects.get(**query)
|
||||
|
||||
params[assoc] = model.objects.get(**query)
|
||||
region, created = Region.objects.get_or_create(**params)
|
||||
|
||||
region, created = Region.objects.get_or_create(**params)
|
||||
|
||||
if created:
|
||||
print("🌐 Created region", region.name)
|
||||
if created:
|
||||
print("🌐 Created region", region.name)
|
||||
|
Reference in New Issue
Block a user