Add region seeds

This commit is contained in:
Aleksandar Radunovic 2018-10-15 15:13:26 +02:00
parent f1ebd4d246
commit 819f325bd5
2 changed files with 27 additions and 0 deletions

10
initializers/regions.yml Normal file
View File

@ -0,0 +1,10 @@
# - name: Singapore
# slug: singapore
# - name: Amsterdam
# slug: amsterdam
# - name: Downtown
# slug: downtown
# parent: Amsterdam
# - name: Suburbs
# slug: suburbs
# parent: Amsterdam

View File

@ -0,0 +1,17 @@
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)