From dfb0327340d370fd3ebece5d62e8cf5a61842b95 Mon Sep 17 00:00:00 2001 From: ryanmerolle Date: Wed, 30 Dec 2020 19:10:30 -0500 Subject: [PATCH] Added power startup_scripts --- initializers/power_feeds.yml | 16 +++++++++++ initializers/power_panels.yml | 5 ++++ startup_scripts/330_power_panels.py | 41 +++++++++++++++++++++++++++++ startup_scripts/340_power_feeds.py | 41 +++++++++++++++++++++++++++++ 4 files changed, 103 insertions(+) create mode 100644 initializers/power_feeds.yml create mode 100644 initializers/power_panels.yml create mode 100644 startup_scripts/330_power_panels.py create mode 100644 startup_scripts/340_power_feeds.py diff --git a/initializers/power_feeds.yml b/initializers/power_feeds.yml new file mode 100644 index 0000000..1c7b222 --- /dev/null +++ b/initializers/power_feeds.yml @@ -0,0 +1,16 @@ +# - name: power feed 1 +# #site: AMS 1 +# power_panel: power panel AMS 1 +# voltage: 208 +# amperage: 50 +# max_utilization: 80 +# phase: Single phase +# rack: rack-01 +# - name: power feed 2 +# #site: SING 1 +# power_panel: power panel SING 1 +# voltage: 208 +# amperage: 50 +# max_utilization: 80 +# phase: Three-phase +# rack: rack-03 diff --git a/initializers/power_panels.yml b/initializers/power_panels.yml new file mode 100644 index 0000000..1205851 --- /dev/null +++ b/initializers/power_panels.yml @@ -0,0 +1,5 @@ +# - name: power panel AMS 1 +# site: AMS 1 +# - name: power panel SING 1 +# site: SING 1 +# rack_group: cage 101 diff --git a/startup_scripts/330_power_panels.py b/startup_scripts/330_power_panels.py new file mode 100644 index 0000000..3057a96 --- /dev/null +++ b/startup_scripts/330_power_panels.py @@ -0,0 +1,41 @@ +import sys + +from dcim.models import Site, RackGroup, PowerPanel +from startup_script_utils import * +from tenancy.models import Tenant + +power_panels = load_yaml('/opt/netbox/initializers/power_panels.yml') + +if power_panels is None: + sys.exit() + +required_assocs = { + 'site': (Site, 'name') +} + +optional_assocs = { + 'rack_group': (RackGroup, 'name') +} + +for params in power_panels: + custom_field_data = pop_custom_fields(params) + + for assoc, details in required_assocs.items(): + model, field = details + query = { field: params.pop(assoc) } + + params[assoc] = model.objects.get(**query) + + for assoc, details in optional_assocs.items(): + if assoc in params: + model, field = details + query = { field: params.pop(assoc) } + + params[assoc] = model.objects.get(**query) + + power_panel, created = PowerPanel.objects.get_or_create(**params) + + if created: + set_custom_fields_values(power_panel, custom_field_data) + + print("⚡ Created Power Panel", power_panel.site, power_panel.name) diff --git a/startup_scripts/340_power_feeds.py b/startup_scripts/340_power_feeds.py new file mode 100644 index 0000000..fc7ee38 --- /dev/null +++ b/startup_scripts/340_power_feeds.py @@ -0,0 +1,41 @@ +import sys + +from dcim.models import Rack, PowerPanel, PowerFeed +from startup_script_utils import * +from tenancy.models import Tenant + +power_feeds = load_yaml('/opt/netbox/initializers/power_feeds.yml') + +if power_feeds is None: + sys.exit() + +required_assocs = { + 'power_panel': (PowerPanel, 'name') +} + +optional_assocs = { + 'rack': (Rack, 'name') +} + +for params in power_feeds: + custom_field_data = pop_custom_fields(params) + + for assoc, details in required_assocs.items(): + model, field = details + query = { field: params.pop(assoc) } + + params[assoc] = model.objects.get(**query) + + for assoc, details in optional_assocs.items(): + if assoc in params: + model, field = details + query = { field: params.pop(assoc) } + + params[assoc] = model.objects.get(**query) + + power_feed, created = PowerFeed.objects.get_or_create(**params) + + if created: + set_custom_fields_values(power_feed, custom_field_data) + + print("⚡ Created Power Feed", power_feed.name)