netbox-docker/startup_scripts/40_regions.py

18 lines
524 B
Python
Raw Normal View History

2018-10-15 15:13:26 +02:00
from dcim.models import Region
from ruamel.yaml import YAML
with open('/opt/netbox/initializers/regions.yml', 'r') as stream:
yaml=YAML(typ='safe')
regions = yaml.load(stream)
if regions is not None:
for region_params in regions:
if "parent" in region_params:
parent = Region.objects.get(name=region_params.pop('parent'))
region_params['parent'] = parent
region, created = Region.objects.get_or_create(**region_params)
if created:
print("Created region", region.name)