From 60f7de18986ada6deba4d471f4ad3f40514418fa Mon Sep 17 00:00:00 2001 From: Aleksandar Radunovic Date: Mon, 15 Oct 2018 15:15:09 +0200 Subject: [PATCH] Add rack seeds --- initializers/racks.yml | 24 ++++++++++++++++++++++ startup_scripts/45_racks.py | 41 +++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 initializers/racks.yml create mode 100644 startup_scripts/45_racks.py diff --git a/initializers/racks.yml b/initializers/racks.yml new file mode 100644 index 0000000..7a06b08 --- /dev/null +++ b/initializers/racks.yml @@ -0,0 +1,24 @@ +# - site: AMS 1 +# name: rack-01 +# role: role-1 +# type: 4-post cabinet +# width: 19 inches +# u_height: 47 +# custom_fields: +# text_field: Description +# - site: AMS 2 +# name: rack-02 +# role: role-2 +# type: 4-post cabinet +# width: 19 inches +# u_height: 47 +# custom_fields: +# text_field: Description +# - site: SING 1 +# name: rack-03 +# role: role-3 +# type: 4-post cabinet +# width: 19 inches +# u_height: 47 +# custom_fields: +# text_field: Description diff --git a/startup_scripts/45_racks.py b/startup_scripts/45_racks.py new file mode 100644 index 0000000..a041ab3 --- /dev/null +++ b/startup_scripts/45_racks.py @@ -0,0 +1,41 @@ +from dcim.models import Site, RackRole, Rack, RackGroup +from tenancy.models import Tenant +from extras.models import CustomField, CustomFieldValue +from dcim.constants import RACK_TYPE_CHOICES, RACK_WIDTH_CHOICES +from ruamel.yaml import YAML + +with open('/opt/netbox/initializers/racks.yml', 'r') as stream: + yaml = YAML(typ='safe') + racks = yaml.load(stream) + + optional_assocs = dict(role=RackRole, tenant=Tenant, group=RackGroup) + + if racks is not None: + for rack_params in racks: + custom_fields = rack_params.pop('custom_fields', None) + + rack_params['site'] = Site.objects.get(name=rack_params.pop('site')) + + for param_name, model in optional_assoc.items(): + if param_name in rack_params: + rack_params[param_name] = model.objects.get(name=rack_params.pop(param_name)) + + for rack_type in RACK_TYPE_CHOICES: + if rack_params['type'] in rack_type: + rack_params['type'] = rack_type[0] + + for rack_width in RACK_WIDTH_CHOICES: + if rack_params['width'] in rack_width: + rack_params['width'] = rack_width[0] + + rack, created = Rack.objects.get_or_create(**rack_params) + + if created: + if custom_fields is not None: + for cf_name, cf_value in custom_fields.items(): + custom_field = CustomField.objects.get(name=cf_name) + custom_field_value = CustomFieldValue.objects.create(field=custom_field, obj=rack, value=cf_value) + + rack.custom_field_values.add(custom_field_value) + + print("Created rack", rack.site, rack.name)