From 819f325bd54cc916be5dc64517feda65752c1068 Mon Sep 17 00:00:00 2001 From: Aleksandar Radunovic Date: Mon, 15 Oct 2018 15:13:26 +0200 Subject: [PATCH] Add region seeds --- initializers/regions.yml | 10 ++++++++++ startup_scripts/40_regions.py | 17 +++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 initializers/regions.yml create mode 100644 startup_scripts/40_regions.py diff --git a/initializers/regions.yml b/initializers/regions.yml new file mode 100644 index 0000000..1353e05 --- /dev/null +++ b/initializers/regions.yml @@ -0,0 +1,10 @@ +# - name: Singapore +# slug: singapore +# - name: Amsterdam +# slug: amsterdam +# - name: Downtown +# slug: downtown +# parent: Amsterdam +# - name: Suburbs +# slug: suburbs +# parent: Amsterdam diff --git a/startup_scripts/40_regions.py b/startup_scripts/40_regions.py new file mode 100644 index 0000000..b40574d --- /dev/null +++ b/startup_scripts/40_regions.py @@ -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)