From 93dee744599b520beff6412939216a3b95104aae Mon Sep 17 00:00:00 2001 From: Jeroen Simonetti Date: Mon, 2 Dec 2019 10:23:53 +0100 Subject: [PATCH] Add rack group initialiser Fixes #192 Signed-off-by: Jeroen Simonetti --- initializers/rack_groups.yml | 3 +++ initializers/racks.yml | 1 + startup_scripts/075_rack_groups.py | 31 ++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 initializers/rack_groups.yml create mode 100644 startup_scripts/075_rack_groups.py diff --git a/initializers/rack_groups.yml b/initializers/rack_groups.yml new file mode 100644 index 0000000..244fc00 --- /dev/null +++ b/initializers/rack_groups.yml @@ -0,0 +1,3 @@ +# - name: cage 101 +# slug: cage-101 +# site: SING 1 diff --git a/initializers/racks.yml b/initializers/racks.yml index 9a71743..3a31526 100644 --- a/initializers/racks.yml +++ b/initializers/racks.yml @@ -16,6 +16,7 @@ # text_field: Description # - site: SING 1 # name: rack-03 +# group: cage 101 # role: Role 3 # type: 4-post cabinet # width: 19 inches diff --git a/startup_scripts/075_rack_groups.py b/startup_scripts/075_rack_groups.py new file mode 100644 index 0000000..7deaa11 --- /dev/null +++ b/startup_scripts/075_rack_groups.py @@ -0,0 +1,31 @@ +from dcim.models import Site,RackGroup +from ruamel.yaml import YAML + +from pathlib import Path +import sys + +file = Path('/opt/netbox/initializers/rack_groups.yml') +if not file.is_file(): + sys.exit() + +with file.open('r') as stream: + yaml=YAML(typ='safe') + rack_groups= yaml.load(stream) + + required_assocs = { + 'site': (Site, 'name') + } + + if rack_groups is not None: + for params in rack_groups: + + for assoc, details in required_assocs.items(): + model, field = details + query = { field: params.pop(assoc) } + params[assoc] = model.objects.get(**query) + + rack_group, created = RackGroup.objects.get_or_create(**params) + + if created: + print("🎨 Created rack group", rack_group.name) +