From aa68548f41a73bfb6efb906a747fbeefd6973ffc Mon Sep 17 00:00:00 2001 From: Aleksandar Radunovic Date: Tue, 30 Oct 2018 14:22:04 +0100 Subject: [PATCH] Add Plaform seeds --- initializers/platforms.yml | 19 ++++++++++++++ startup_scripts/050_manufacturers.py | 1 - startup_scripts/100_platforms.py | 25 +++++++++++++++++++ .../{100_devices.py => 110_devices.py} | 0 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 initializers/platforms.yml create mode 100644 startup_scripts/100_platforms.py rename startup_scripts/{100_devices.py => 110_devices.py} (100%) diff --git a/initializers/platforms.yml b/initializers/platforms.yml new file mode 100644 index 0000000..bc9926a --- /dev/null +++ b/initializers/platforms.yml @@ -0,0 +1,19 @@ +# # Allowed rpc clients are: juniper-junos, cisco-ios, opengear +# - name: Platform 1 +# slug: platform-1 +# manufacturer: Manufacturer 1 +# napalm_driver: driver1 +# napalm_args: "{'arg1': 'value1', 'arg2': 'value2'}" +# rpc_client: juniper-junos +# - name: Platform 2 +# slug: platform-2 +# manufacturer: Manufacturer 2 +# napalm_driver: driver2 +# napalm_args: "{'arg1': 'value1', 'arg2': 'value2'}" +# rpc_client: opengear +# - name: Platform 3 +# slug: platform-3 +# manufacturer: NoName +# napalm_driver: driver3 +# napalm_args: "{'arg1': 'value1', 'arg2': 'value2'}" +# rpc_client: juniper-junos diff --git a/startup_scripts/050_manufacturers.py b/startup_scripts/050_manufacturers.py index fdee2b4..6f43fd1 100644 --- a/startup_scripts/050_manufacturers.py +++ b/startup_scripts/050_manufacturers.py @@ -11,4 +11,3 @@ with open('/opt/netbox/initializers/manufacturers.yml', 'r') as stream: if created: print("🏭 Created Manufacturer", manufacturer.name) - diff --git a/startup_scripts/100_platforms.py b/startup_scripts/100_platforms.py new file mode 100644 index 0000000..c7916b9 --- /dev/null +++ b/startup_scripts/100_platforms.py @@ -0,0 +1,25 @@ +from dcim.models import Manufacturer, Platform +from ruamel.yaml import YAML + +with open('/opt/netbox/initializers/platforms.yml', 'r') as stream: + yaml = YAML(typ='safe') + platforms = yaml.load(stream) + + optional_assocs = { + 'manufacturer': (Manufacturer, 'name'), + } + + if platforms is not None: + for params in platforms: + + 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) + + platform, created = Platform.objects.get_or_create(**params) + + if created: + print("💾 Created platform", platform.name) diff --git a/startup_scripts/100_devices.py b/startup_scripts/110_devices.py similarity index 100% rename from startup_scripts/100_devices.py rename to startup_scripts/110_devices.py