Merge pull request #196 from jsimonetti/rackgroup_initialiser

Add rack group initialiser
This commit is contained in:
Christian Mäder 2019-12-14 17:31:38 +01:00 committed by GitHub
commit fd955544af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 0 deletions

View File

@ -0,0 +1,3 @@
# - name: cage 101
# slug: cage-101
# site: SING 1

View File

@ -16,6 +16,7 @@
# text_field: Description # text_field: Description
# - site: SING 1 # - site: SING 1
# name: rack-03 # name: rack-03
# group: cage 101
# role: Role 3 # role: Role 3
# type: 4-post cabinet # type: 4-post cabinet
# width: 19 inches # width: 19 inches

View File

@ -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)