From 819f325bd54cc916be5dc64517feda65752c1068 Mon Sep 17 00:00:00 2001 From: Aleksandar Radunovic Date: Mon, 15 Oct 2018 15:13:26 +0200 Subject: [PATCH 01/18] Add region seeds --- initializers/regions.yml | 10 ++++++++++ startup_scripts/40_regions.py | 17 +++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 initializers/regions.yml create mode 100644 startup_scripts/40_regions.py diff --git a/initializers/regions.yml b/initializers/regions.yml new file mode 100644 index 0000000..1353e05 --- /dev/null +++ b/initializers/regions.yml @@ -0,0 +1,10 @@ +# - name: Singapore +# slug: singapore +# - name: Amsterdam +# slug: amsterdam +# - name: Downtown +# slug: downtown +# parent: Amsterdam +# - name: Suburbs +# slug: suburbs +# parent: Amsterdam diff --git a/startup_scripts/40_regions.py b/startup_scripts/40_regions.py new file mode 100644 index 0000000..b40574d --- /dev/null +++ b/startup_scripts/40_regions.py @@ -0,0 +1,17 @@ +from dcim.models import Region +from ruamel.yaml import YAML + +with open('/opt/netbox/initializers/regions.yml', 'r') as stream: + yaml=YAML(typ='safe') + regions = yaml.load(stream) + + if regions is not None: + for region_params in regions: + if "parent" in region_params: + parent = Region.objects.get(name=region_params.pop('parent')) + region_params['parent'] = parent + + region, created = Region.objects.get_or_create(**region_params) + + if created: + print("Created region", region.name) From 791027f77b0565724a2f83ef596ba35f85bc8c4e Mon Sep 17 00:00:00 2001 From: Aleksandar Radunovic Date: Mon, 15 Oct 2018 15:13:51 +0200 Subject: [PATCH 02/18] Add site seeds --- initializers/sites.yml | 24 ++++++++++++++++++++++++ startup_scripts/41_sites.py | 20 ++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 initializers/sites.yml create mode 100644 startup_scripts/41_sites.py diff --git a/initializers/sites.yml b/initializers/sites.yml new file mode 100644 index 0000000..2d48feb --- /dev/null +++ b/initializers/sites.yml @@ -0,0 +1,24 @@ +# - name: AMS 1 +# slug: ams1 +# region: Downtown +# status: 1 +# facility: Amsterdam 1 +# asn: 12345 +# - name: AMS 2 +# slug: ams2 +# region: Downtown +# status: 1 +# facility: Amsterdam 2 +# asn: 54321 +# - name: AMS 3 +# slug: ams3 +# region: Suburbs +# status: 1 +# facility: Amsterdam 3 +# asn: 67890 +# - name: SING 1 +# slug: sing1 +# region: Singapore +# status: 1 +# facility: Singapore 1 +# asn: 09876 diff --git a/startup_scripts/41_sites.py b/startup_scripts/41_sites.py new file mode 100644 index 0000000..7240689 --- /dev/null +++ b/startup_scripts/41_sites.py @@ -0,0 +1,20 @@ +from dcim.models import Site +from dcim.models import Region +from ruamel.yaml import YAML + +with open('/opt/netbox/initializers/sites.yml', 'r') as stream: + yaml = YAML(typ='safe') + sites = yaml.load(stream) + + if sites is not None: + for site_params in sites: + if "region" in site_params: + region = Region.objects.get(name=site_params.pop('region', None)) + site_params['region'] = region + + + site, created = Site.objects.get_or_create(**site_params) + + if created: + print("Created site", site.name) + From 86675278ab0d2bcd05ce7475e21603b8ebf09318 Mon Sep 17 00:00:00 2001 From: Aleksandar Radunovic Date: Mon, 15 Oct 2018 15:14:11 +0200 Subject: [PATCH 03/18] Add manufacturer seeds --- initializers/manufacturers.yml | 6 ++++++ startup_scripts/42_manufacturers.py | 14 ++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 initializers/manufacturers.yml create mode 100644 startup_scripts/42_manufacturers.py diff --git a/initializers/manufacturers.yml b/initializers/manufacturers.yml new file mode 100644 index 0000000..18aef10 --- /dev/null +++ b/initializers/manufacturers.yml @@ -0,0 +1,6 @@ +# - name: Cisco +# slug: cisco +# - name: Intel +# slug: intel +# - name: NoName +# slug: noname diff --git a/startup_scripts/42_manufacturers.py b/startup_scripts/42_manufacturers.py new file mode 100644 index 0000000..49714bd --- /dev/null +++ b/startup_scripts/42_manufacturers.py @@ -0,0 +1,14 @@ +from dcim.models import Manufacturer +from ruamel.yaml import YAML + +with open('/opt/netbox/initializers/manufacturers.yml', 'r') as stream: + yaml = YAML(typ='safe') + manufacturers = yaml.load(stream) + + if manufacturers is not None: + for manufacturer_params in manufacturers: + manufacturer, created = Manufacturer.objects.get_or_create(**manufacturer_params) + + if created: + print("Created Manufacturer", manufacturer.name) + From a2b08a6ca58c6db5d0dd620659847fcfc921b62c Mon Sep 17 00:00:00 2001 From: Aleksandar Radunovic Date: Mon, 15 Oct 2018 15:14:27 +0200 Subject: [PATCH 04/18] Add device type seeds --- initializers/device_types.yml | 15 +++++++++++++++ startup_scripts/43_device_types.py | 27 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 initializers/device_types.yml create mode 100644 startup_scripts/43_device_types.py diff --git a/initializers/device_types.yml b/initializers/device_types.yml new file mode 100644 index 0000000..b41e31d --- /dev/null +++ b/initializers/device_types.yml @@ -0,0 +1,15 @@ +# - manufacturer: Intel +# model: Model 1 +# slug: model-1 +# u_height: 2 +# - manufacturer: Intel +# model: Model 2 +# slug: model-2 +# - manufacturer: Intel +# model: Model 3 +# slug: model-3 +# is_full_depth: false +# u_height: 0 +# - manufacturer: NoName +# model: Other +# slug: other diff --git a/startup_scripts/43_device_types.py b/startup_scripts/43_device_types.py new file mode 100644 index 0000000..99ac44d --- /dev/null +++ b/startup_scripts/43_device_types.py @@ -0,0 +1,27 @@ +from dcim.models import DeviceType, Manufacturer +from extras.models import CustomField, CustomFieldValue +from ruamel.yaml import YAML + +with open('/opt/netbox/initializers/device_types.yml', 'r') as stream: + yaml = YAML(typ='safe') + device_types = yaml.load(stream) + + if device_types is not None: + for device_type_params in device_types: + custom_fields = device_type_params.pop('custom_fields', None) + + manufacturer = Manufacturer.objects.get(name=device_type_params.pop('manufacturer')) + device_type_params['manufacturer'] = manufacturer + + device_type, created = DeviceType.objects.get_or_create(**device_type_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=device_type, value=cf_value) + + device_type.custom_field_values.add(custom_field_value) + + print("Created device type", device_type.manufacturer, device_type.model) + From 89fddbe0abc7281452908468f7f6fea151cb0e15 Mon Sep 17 00:00:00 2001 From: Aleksandar Radunovic Date: Mon, 15 Oct 2018 15:14:42 +0200 Subject: [PATCH 05/18] Add rack role seeds --- initializers/rack_roles.yml | 12 ++++++++++++ startup_scripts/44_rack_roles.py | 20 ++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 initializers/rack_roles.yml create mode 100644 startup_scripts/44_rack_roles.py diff --git a/initializers/rack_roles.yml b/initializers/rack_roles.yml new file mode 100644 index 0000000..e8d1e3e --- /dev/null +++ b/initializers/rack_roles.yml @@ -0,0 +1,12 @@ +# - name: Role 1 +# slug: role-1 +# color: Pink +# - name: Role 2 +# slug: role-2 +# color: Cyan +# - name: Role 3 +# slug: role-3 +# color: Grey +# - name: Role 4 +# slug: role-4 +# color: Teal diff --git a/startup_scripts/44_rack_roles.py b/startup_scripts/44_rack_roles.py new file mode 100644 index 0000000..71b7309 --- /dev/null +++ b/startup_scripts/44_rack_roles.py @@ -0,0 +1,20 @@ +from dcim.models import RackRole +from ruamel.yaml import YAML +from utilities.forms import COLOR_CHOICES + +with open('/opt/netbox/initializers/rack_roles.yml', 'r') as stream: + yaml=YAML(typ='safe') + rack_roles = yaml.load(stream) + + if rack_roles is not None: + for rack_role_params in rack_roles: + color = rack_role_params.pop('color', None) + + for color_tpl in COLOR_CHOICES: + if color in color_tpl: + rack_role_params['color'] = color_tpl[0] + + rack_role, created = RackRole.objects.get_or_create(**rack_role_params) + + if created: + print("Created rack role", rack_role.name) From 60f7de18986ada6deba4d471f4ad3f40514418fa Mon Sep 17 00:00:00 2001 From: Aleksandar Radunovic Date: Mon, 15 Oct 2018 15:15:09 +0200 Subject: [PATCH 06/18] 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) From ebb7779b5f6413dba57509a87837459c08b25b49 Mon Sep 17 00:00:00 2001 From: Aleksandar Radunovic Date: Mon, 15 Oct 2018 15:15:23 +0200 Subject: [PATCH 07/18] Add device role seeds --- initializers/device_roles.yml | 15 +++++++++++++++ startup_scripts/46_device_roles.py | 20 ++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 initializers/device_roles.yml create mode 100644 startup_scripts/46_device_roles.py diff --git a/initializers/device_roles.yml b/initializers/device_roles.yml new file mode 100644 index 0000000..ee4234f --- /dev/null +++ b/initializers/device_roles.yml @@ -0,0 +1,15 @@ +# - name: switch +# slug: switch +# color: Grey +# - name: router +# slug: router +# color: Cyan +# - name: load-balancer +# slug: load-balancer +# color: Red +# - name: server +# slug: server +# color: Blue +# - name: patchpanel +# slug: patchpanel +# color: Black diff --git a/startup_scripts/46_device_roles.py b/startup_scripts/46_device_roles.py new file mode 100644 index 0000000..af606be --- /dev/null +++ b/startup_scripts/46_device_roles.py @@ -0,0 +1,20 @@ +from dcim.models import DeviceRole +from ruamel.yaml import YAML +from utilities.forms import COLOR_CHOICES + +with open('/opt/netbox/initializers/device_roles.yml', 'r') as stream: + yaml=YAML(typ='safe') + device_roles = yaml.load(stream) + + if device_roles is not None: + for device_role_params in device_roles: + color = device_role_params.pop('color') + + for color_tpl in COLOR_CHOICES: + if color in color_tpl: + device_role_params['color'] = color_tpl[0] + + device_role, created = DeviceRole.objects.get_or_create(**device_role_params) + + if created: + print("Created device role", device_role.name) From 90ae5cf01d2a1ec59e89d949a73644208bdc49f0 Mon Sep 17 00:00:00 2001 From: Aleksandar Radunovic Date: Mon, 15 Oct 2018 15:15:41 +0200 Subject: [PATCH 08/18] Add device seeds --- initializers/devices.yml | 27 ++++++++++++++++++++ startup_scripts/47_devices.py | 46 +++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 initializers/devices.yml create mode 100644 startup_scripts/47_devices.py diff --git a/initializers/devices.yml b/initializers/devices.yml new file mode 100644 index 0000000..93334c9 --- /dev/null +++ b/initializers/devices.yml @@ -0,0 +1,27 @@ +# - name: server01 +# device_role: server +# device_type: other +# site: AMS 1 +# rack: rack-01 +# face: Front +# position: 1 +# custom_fields: +# text_field: Description +# - name: server02 +# device_role: server +# device_type: other +# site: AMS 2 +# rack: rack-02 +# face: Front +# position: 2 +# custom_fields: +# text_field: Description +# - name: server03 +# device_role: server +# device_type: other +# site: SING 1 +# rack: rack-03 +# face: Front +# position: 3 +# custom_fields: +# text_field: Description diff --git a/startup_scripts/47_devices.py b/startup_scripts/47_devices.py new file mode 100644 index 0000000..2088208 --- /dev/null +++ b/startup_scripts/47_devices.py @@ -0,0 +1,46 @@ +from dcim.models import Site, Rack, DeviceRole, DeviceType, Device, Platform +from dcim.constants import RACK_FACE_CHOICES +from ipam.models import IPAddress +from tenancy.models import Tenant +from extras.models import CustomField, CustomFieldValue +from ruamel.yaml import YAML + +with open('/opt/netbox/initializers/devices.yml', 'r') as stream: + yaml = YAML(typ='safe') + devices = yaml.load(stream) + + optional_assocs = { + 'platform': Platform, + 'tenant': Tenant + 'rack': Rack + 'primary_ip4': IPAddress + 'primary_ip6': IPAddress + } + + if devices is not None: + for device_params in devices: + custom_fields = device_params.pop('custom_fields', None) + + device_params['device_role'] = DeviceRole.objects.get(name=device_params.pop('device_role')) + device_params['device_type'] = DeviceType.objects.get(model=device_params.pop('device_type')) + device_params['site'] = Site.objects.get(name=device_params.pop('site')) + + for param_name, model in optional_assoc.items(): + if param_name in device_params: + device_params[param_name] = model.objects.get(name=device_params.pop(param_name)) + + for rack_face in RACK_FACE_CHOICES: + if device_params['face'] in rack_face: + device_params['face'] = rack_face[0] + + device, created = Device.objects.get_or_create(**device_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=device, value=cf_value) + + device.custom_field_values.add(custom_field_value) + + print("Created device", device.name) From ab0ce20971cf503db85354e81b2e1cd7f62c3377 Mon Sep 17 00:00:00 2001 From: Aleksandar Radunovic Date: Mon, 15 Oct 2018 15:15:56 +0200 Subject: [PATCH 09/18] Update README --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b8b541c..d72a589 100644 --- a/README.md +++ b/README.md @@ -161,7 +161,8 @@ if created: #### Initializers -Initializers are built-in startup scripts for defining NetBox custom fields, groups and users. +Initializers are built-in startup scripts for defining NetBox +custom fields, groups, users and many other resources. All you need to do is to mount you own `initializers` folder ([see `docker-compose.yml`][netbox-docker-compose]). Look at the [`initializers` folder][netbox-docker-initializers] to learn how the files must look like. From 6cc4c67387a2ec53e659e27ff5a7e300d9591d59 Mon Sep 17 00:00:00 2001 From: Aleksandar Radunovic Date: Tue, 16 Oct 2018 09:12:43 +0200 Subject: [PATCH 10/18] Remove default pop value --- startup_scripts/41_sites.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/startup_scripts/41_sites.py b/startup_scripts/41_sites.py index 7240689..d78cfd9 100644 --- a/startup_scripts/41_sites.py +++ b/startup_scripts/41_sites.py @@ -9,7 +9,7 @@ with open('/opt/netbox/initializers/sites.yml', 'r') as stream: if sites is not None: for site_params in sites: if "region" in site_params: - region = Region.objects.get(name=site_params.pop('region', None)) + region = Region.objects.get(name=site_params.pop('region')) site_params['region'] = region @@ -17,4 +17,3 @@ with open('/opt/netbox/initializers/sites.yml', 'r') as stream: if created: print("Created site", site.name) - From b53e886f8f00989cedbd359514a6ec8e41cab108 Mon Sep 17 00:00:00 2001 From: Aleksandar Radunovic Date: Tue, 16 Oct 2018 11:05:28 +0200 Subject: [PATCH 11/18] Handle all associations --- startup_scripts/40_regions.py | 20 +++++++---- startup_scripts/41_sites.py | 34 ++++++++++++++---- startup_scripts/42_manufacturers.py | 6 ++-- startup_scripts/43_device_types.py | 35 +++++++++++++++---- startup_scripts/44_rack_roles.py | 13 +++---- startup_scripts/45_racks.py | 45 +++++++++++++++++------- startup_scripts/46_device_roles.py | 14 ++++---- startup_scripts/47_devices.py | 54 +++++++++++++++++++---------- 8 files changed, 156 insertions(+), 65 deletions(-) diff --git a/startup_scripts/40_regions.py b/startup_scripts/40_regions.py index b40574d..033e26c 100644 --- a/startup_scripts/40_regions.py +++ b/startup_scripts/40_regions.py @@ -5,13 +5,21 @@ with open('/opt/netbox/initializers/regions.yml', 'r') as stream: yaml=YAML(typ='safe') regions = yaml.load(stream) - if regions is not None: - for region_params in regions: - if "parent" in region_params: - parent = Region.objects.get(name=region_params.pop('parent')) - region_params['parent'] = parent + optional_assocs = { + 'parent': (Region, 'name') + } - region, created = Region.objects.get_or_create(**region_params) + if regions is not None: + for params in regions: + + for assoc, details in optional_assocs.items(): + if assoc in params: + model, field = details + query = dict(field=params.pop(assoc)) + + params[assoc] = model.objects.get(**query) + + region, created = Region.objects.get_or_create(**params) if created: print("Created region", region.name) diff --git a/startup_scripts/41_sites.py b/startup_scripts/41_sites.py index d78cfd9..599f8b7 100644 --- a/startup_scripts/41_sites.py +++ b/startup_scripts/41_sites.py @@ -1,19 +1,39 @@ -from dcim.models import Site -from dcim.models import Region +from dcim.models import Region, Site +from extras.models import CustomField, CustomFieldValue +from tenancy.models import Tenant from ruamel.yaml import YAML with open('/opt/netbox/initializers/sites.yml', 'r') as stream: yaml = YAML(typ='safe') sites = yaml.load(stream) + optional_assocs = { + 'region': (Region, 'name'), + 'tenant': (Tenant, 'name') + } + if sites is not None: - for site_params in sites: - if "region" in site_params: - region = Region.objects.get(name=site_params.pop('region')) - site_params['region'] = region + for params in sites: + for assoc, details in optional_assocs.items(): + if assoc in params: + model, field = details + query = { field: params.pop(assoc) } - site, created = Site.objects.get_or_create(**site_params) + params[assoc] = model.objects.get(**query) + + site, created = Site.objects.get_or_create(**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=device_type, + value=cf_value + ) + + site.custom_field_values.add(custom_field_value) + print("Created site", site.name) diff --git a/startup_scripts/42_manufacturers.py b/startup_scripts/42_manufacturers.py index 49714bd..13cad50 100644 --- a/startup_scripts/42_manufacturers.py +++ b/startup_scripts/42_manufacturers.py @@ -6,9 +6,9 @@ with open('/opt/netbox/initializers/manufacturers.yml', 'r') as stream: manufacturers = yaml.load(stream) if manufacturers is not None: - for manufacturer_params in manufacturers: - manufacturer, created = Manufacturer.objects.get_or_create(**manufacturer_params) + for params in manufacturers: + manufacturer, created = Manufacturer.objects.get_or_create(**params) if created: - print("Created Manufacturer", manufacturer.name) + print("Created Manufacturer", manufacturer.name) diff --git a/startup_scripts/43_device_types.py b/startup_scripts/43_device_types.py index 99ac44d..e27ca00 100644 --- a/startup_scripts/43_device_types.py +++ b/startup_scripts/43_device_types.py @@ -6,20 +6,43 @@ with open('/opt/netbox/initializers/device_types.yml', 'r') as stream: yaml = YAML(typ='safe') device_types = yaml.load(stream) + required_assocs = { + 'manufacturer': (Manufacturer, 'name') + } + + optional_assocs = { + 'region': (Region, 'name'), + 'tenant': (Tenant, 'name') + } + if device_types is not None: - for device_type_params in device_types: - custom_fields = device_type_params.pop('custom_fields', None) + for params in device_types: + custom_fields = params.pop('custom_fields', None) - manufacturer = Manufacturer.objects.get(name=device_type_params.pop('manufacturer')) - device_type_params['manufacturer'] = manufacturer + for assoc, details in required.items(): + model, field = details + query = dict(field=params.pop(assoc)) - device_type, created = DeviceType.objects.get_or_create(**device_type_params) + params[assoc] = model.objects.get(**query) + + for assoc, details in optional_assocs.items(): + if assoc in params: + model, field = details + query = dict(field=params.pop(assoc)) + + params[assoc] = model.objects.get(**query) + + device_type, created = DeviceType.objects.get_or_create(**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=device_type, value=cf_value) + custom_field_value = CustomFieldValue.objects.create( + field=custom_field, + obj=device_type, + value=cf_value + ) device_type.custom_field_values.add(custom_field_value) diff --git a/startup_scripts/44_rack_roles.py b/startup_scripts/44_rack_roles.py index 71b7309..53517c2 100644 --- a/startup_scripts/44_rack_roles.py +++ b/startup_scripts/44_rack_roles.py @@ -7,14 +7,15 @@ with open('/opt/netbox/initializers/rack_roles.yml', 'r') as stream: rack_roles = yaml.load(stream) if rack_roles is not None: - for rack_role_params in rack_roles: - color = rack_role_params.pop('color', None) + for params in rack_roles: + if 'color' in params: + color = params.pop('color') - for color_tpl in COLOR_CHOICES: - if color in color_tpl: - rack_role_params['color'] = color_tpl[0] + for color_tpl in COLOR_CHOICES: + if color in color_tpl: + params['color'] = color_tpl[0] - rack_role, created = RackRole.objects.get_or_create(**rack_role_params) + rack_role, created = RackRole.objects.get_or_create(**params) if created: print("Created rack role", rack_role.name) diff --git a/startup_scripts/45_racks.py b/startup_scripts/45_racks.py index a041ab3..6391b46 100644 --- a/startup_scripts/45_racks.py +++ b/startup_scripts/45_racks.py @@ -8,33 +8,52 @@ 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) + required_assocs = { + 'site': (Site, 'name') + } + + optional_assocs = { + 'role': (RackRole, 'name'), + 'tenant': (Tenant, 'name'), + 'group': (RackGroup, 'name') + } if racks is not None: - for rack_params in racks: - custom_fields = rack_params.pop('custom_fields', None) + for params in racks: + custom_fields = params.pop('custom_fields', None) - rack_params['site'] = Site.objects.get(name=rack_params.pop('site')) + for assoc, details in required.items(): + model, field = details + query = dict(field=params.pop(assoc)) - 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)) + params[assoc] = model.objects.get(**query) + + for assoc, details in optional_assocs.items(): + if assoc in params: + model, field = details + query = dict(field=params.pop(assoc)) + + params[assoc] = model.objects.get(**query) for rack_type in RACK_TYPE_CHOICES: - if rack_params['type'] in rack_type: - rack_params['type'] = rack_type[0] + if params['type'] in rack_type: + 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] + if params['width'] in rack_width: + params['width'] = rack_width[0] - rack, created = Rack.objects.get_or_create(**rack_params) + rack, created = Rack.objects.get_or_create(**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) + custom_field_value = CustomFieldValue.objects.create( + field=custom_field, + obj=device_type, + value=cf_value + ) rack.custom_field_values.add(custom_field_value) diff --git a/startup_scripts/46_device_roles.py b/startup_scripts/46_device_roles.py index af606be..d3f52d8 100644 --- a/startup_scripts/46_device_roles.py +++ b/startup_scripts/46_device_roles.py @@ -7,14 +7,16 @@ with open('/opt/netbox/initializers/device_roles.yml', 'r') as stream: device_roles = yaml.load(stream) if device_roles is not None: - for device_role_params in device_roles: - color = device_role_params.pop('color') + for params in device_roles: - for color_tpl in COLOR_CHOICES: - if color in color_tpl: - device_role_params['color'] = color_tpl[0] + if 'color' in params: + color = params.pop('color') - device_role, created = DeviceRole.objects.get_or_create(**device_role_params) + for color_tpl in COLOR_CHOICES: + if color in color_tpl: + params['color'] = color_tpl[0] + + device_role, created = DeviceRole.objects.get_or_create(**params) if created: print("Created device role", device_role.name) diff --git a/startup_scripts/47_devices.py b/startup_scripts/47_devices.py index 2088208..46e8188 100644 --- a/startup_scripts/47_devices.py +++ b/startup_scripts/47_devices.py @@ -1,6 +1,7 @@ from dcim.models import Site, Rack, DeviceRole, DeviceType, Device, Platform from dcim.constants import RACK_FACE_CHOICES from ipam.models import IPAddress +from virtualization.models import Cluster from tenancy.models import Tenant from extras.models import CustomField, CustomFieldValue from ruamel.yaml import YAML @@ -9,37 +10,54 @@ with open('/opt/netbox/initializers/devices.yml', 'r') as stream: yaml = YAML(typ='safe') devices = yaml.load(stream) + required_assocs = { + 'device_role': (DeviceRole, 'name'), + 'device_type': (DeviceType, 'model'), + 'site': (Site, 'name') + } + optional_assocs = { - 'platform': Platform, - 'tenant': Tenant - 'rack': Rack - 'primary_ip4': IPAddress - 'primary_ip6': IPAddress + 'tenant': (Tenant, 'name'), + 'platform': (Platform, 'name'), + 'rack': (Rack, 'name'), + 'cluster': (Cluster, 'name'), + 'primary_ip4': (IPAddress, 'address') + 'primary_ip6': (IPAddress, 'address') } if devices is not None: - for device_params in devices: - custom_fields = device_params.pop('custom_fields', None) + for params in devices: + custom_fields = params.pop('custom_fields', None) - device_params['device_role'] = DeviceRole.objects.get(name=device_params.pop('device_role')) - device_params['device_type'] = DeviceType.objects.get(model=device_params.pop('device_type')) - device_params['site'] = Site.objects.get(name=device_params.pop('site')) + for assoc, details in required.items(): + model, field = details + query = dict(field=params.pop(assoc)) - for param_name, model in optional_assoc.items(): - if param_name in device_params: - device_params[param_name] = model.objects.get(name=device_params.pop(param_name)) + params[assoc] = model.objects.get(**query) - for rack_face in RACK_FACE_CHOICES: - if device_params['face'] in rack_face: - device_params['face'] = rack_face[0] + for assoc, details in optional_assocs.items(): + if assoc in params: + model, field = details + query = dict(field=params.pop(assoc)) - device, created = Device.objects.get_or_create(**device_params) + params[assoc] = model.objects.get(**query) + + if 'face' in params: + for rack_face in RACK_FACE_CHOICES: + if params['face'] in rack_face: + params['face'] = rack_face[0] + + device, created = Device.objects.get_or_create(**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=device, value=cf_value) + custom_field_value = CustomFieldValue.objects.create( + field=custom_field, + obj=device_type, + value=cf_value + ) device.custom_field_values.add(custom_field_value) From 97477556e058d5eeed544164e70d95dcba43e982 Mon Sep 17 00:00:00 2001 From: Aleksandar Radunovic Date: Tue, 16 Oct 2018 13:26:13 +0200 Subject: [PATCH 12/18] Increase order prefix to 3 digits --- startup_scripts/{00_users.py => 000_users.py} | 0 startup_scripts/{10_groups.py => 010_groups.py} | 0 .../{20_custom_fields.py => 020_custom_fields.py} | 0 startup_scripts/{40_regions.py => 030_regions.py} | 2 +- startup_scripts/{41_sites.py => 040_sites.py} | 3 ++- .../{42_manufacturers.py => 050_manufacturers.py} | 0 .../{43_device_types.py => 060_device_types.py} | 9 +++++---- .../{44_rack_roles.py => 070_rack_roles.py} | 2 +- startup_scripts/{45_racks.py => 080_racks.py} | 8 ++++---- .../{46_device_roles.py => 090_device_roles.py} | 0 startup_scripts/{47_devices.py => 100_devices.py} | 10 +++++----- 11 files changed, 18 insertions(+), 16 deletions(-) rename startup_scripts/{00_users.py => 000_users.py} (100%) rename startup_scripts/{10_groups.py => 010_groups.py} (100%) rename startup_scripts/{20_custom_fields.py => 020_custom_fields.py} (100%) rename startup_scripts/{40_regions.py => 030_regions.py} (92%) rename startup_scripts/{41_sites.py => 040_sites.py} (93%) rename startup_scripts/{42_manufacturers.py => 050_manufacturers.py} (100%) rename startup_scripts/{43_device_types.py => 060_device_types.py} (84%) rename startup_scripts/{44_rack_roles.py => 070_rack_roles.py} (92%) rename startup_scripts/{45_racks.py => 080_racks.py} (90%) rename startup_scripts/{46_device_roles.py => 090_device_roles.py} (100%) rename startup_scripts/{47_devices.py => 100_devices.py} (89%) diff --git a/startup_scripts/00_users.py b/startup_scripts/000_users.py similarity index 100% rename from startup_scripts/00_users.py rename to startup_scripts/000_users.py diff --git a/startup_scripts/10_groups.py b/startup_scripts/010_groups.py similarity index 100% rename from startup_scripts/10_groups.py rename to startup_scripts/010_groups.py diff --git a/startup_scripts/20_custom_fields.py b/startup_scripts/020_custom_fields.py similarity index 100% rename from startup_scripts/20_custom_fields.py rename to startup_scripts/020_custom_fields.py diff --git a/startup_scripts/40_regions.py b/startup_scripts/030_regions.py similarity index 92% rename from startup_scripts/40_regions.py rename to startup_scripts/030_regions.py index 033e26c..888f031 100644 --- a/startup_scripts/40_regions.py +++ b/startup_scripts/030_regions.py @@ -15,7 +15,7 @@ with open('/opt/netbox/initializers/regions.yml', 'r') as stream: for assoc, details in optional_assocs.items(): if assoc in params: model, field = details - query = dict(field=params.pop(assoc)) + query = { field: params.pop(assoc) } params[assoc] = model.objects.get(**query) diff --git a/startup_scripts/41_sites.py b/startup_scripts/040_sites.py similarity index 93% rename from startup_scripts/41_sites.py rename to startup_scripts/040_sites.py index 599f8b7..3dd5a57 100644 --- a/startup_scripts/41_sites.py +++ b/startup_scripts/040_sites.py @@ -14,6 +14,7 @@ with open('/opt/netbox/initializers/sites.yml', 'r') as stream: if sites is not None: for params in sites: + custom_fields = params.pop('custom_fields', None) for assoc, details in optional_assocs.items(): if assoc in params: @@ -30,7 +31,7 @@ with open('/opt/netbox/initializers/sites.yml', 'r') as stream: custom_field = CustomField.objects.get(name=cf_name) custom_field_value = CustomFieldValue.objects.create( field=custom_field, - obj=device_type, + obj=site, value=cf_value ) diff --git a/startup_scripts/42_manufacturers.py b/startup_scripts/050_manufacturers.py similarity index 100% rename from startup_scripts/42_manufacturers.py rename to startup_scripts/050_manufacturers.py diff --git a/startup_scripts/43_device_types.py b/startup_scripts/060_device_types.py similarity index 84% rename from startup_scripts/43_device_types.py rename to startup_scripts/060_device_types.py index e27ca00..f4d5ae2 100644 --- a/startup_scripts/43_device_types.py +++ b/startup_scripts/060_device_types.py @@ -1,4 +1,5 @@ -from dcim.models import DeviceType, Manufacturer +from dcim.models import DeviceType, Manufacturer, Region +from tenancy.models import Tenant from extras.models import CustomField, CustomFieldValue from ruamel.yaml import YAML @@ -19,16 +20,16 @@ with open('/opt/netbox/initializers/device_types.yml', 'r') as stream: for params in device_types: custom_fields = params.pop('custom_fields', None) - for assoc, details in required.items(): + for assoc, details in required_assocs.items(): model, field = details - query = dict(field=params.pop(assoc)) + 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 = dict(field=params.pop(assoc)) + query = { field: params.pop(assoc) } params[assoc] = model.objects.get(**query) diff --git a/startup_scripts/44_rack_roles.py b/startup_scripts/070_rack_roles.py similarity index 92% rename from startup_scripts/44_rack_roles.py rename to startup_scripts/070_rack_roles.py index 53517c2..ca82125 100644 --- a/startup_scripts/44_rack_roles.py +++ b/startup_scripts/070_rack_roles.py @@ -13,7 +13,7 @@ with open('/opt/netbox/initializers/rack_roles.yml', 'r') as stream: for color_tpl in COLOR_CHOICES: if color in color_tpl: - params['color'] = color_tpl[0] + params['color'] = color_tpl[0] rack_role, created = RackRole.objects.get_or_create(**params) diff --git a/startup_scripts/45_racks.py b/startup_scripts/080_racks.py similarity index 90% rename from startup_scripts/45_racks.py rename to startup_scripts/080_racks.py index 6391b46..2f5ea75 100644 --- a/startup_scripts/45_racks.py +++ b/startup_scripts/080_racks.py @@ -22,16 +22,16 @@ with open('/opt/netbox/initializers/racks.yml', 'r') as stream: for params in racks: custom_fields = params.pop('custom_fields', None) - for assoc, details in required.items(): + for assoc, details in required_assocs.items(): model, field = details - query = dict(field=params.pop(assoc)) + 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 = dict(field=params.pop(assoc)) + query = { field: params.pop(assoc) } params[assoc] = model.objects.get(**query) @@ -51,7 +51,7 @@ with open('/opt/netbox/initializers/racks.yml', 'r') as stream: custom_field = CustomField.objects.get(name=cf_name) custom_field_value = CustomFieldValue.objects.create( field=custom_field, - obj=device_type, + obj=rack, value=cf_value ) diff --git a/startup_scripts/46_device_roles.py b/startup_scripts/090_device_roles.py similarity index 100% rename from startup_scripts/46_device_roles.py rename to startup_scripts/090_device_roles.py diff --git a/startup_scripts/47_devices.py b/startup_scripts/100_devices.py similarity index 89% rename from startup_scripts/47_devices.py rename to startup_scripts/100_devices.py index 46e8188..b4f0323 100644 --- a/startup_scripts/47_devices.py +++ b/startup_scripts/100_devices.py @@ -21,7 +21,7 @@ with open('/opt/netbox/initializers/devices.yml', 'r') as stream: 'platform': (Platform, 'name'), 'rack': (Rack, 'name'), 'cluster': (Cluster, 'name'), - 'primary_ip4': (IPAddress, 'address') + 'primary_ip4': (IPAddress, 'address'), 'primary_ip6': (IPAddress, 'address') } @@ -29,16 +29,16 @@ with open('/opt/netbox/initializers/devices.yml', 'r') as stream: for params in devices: custom_fields = params.pop('custom_fields', None) - for assoc, details in required.items(): + for assoc, details in required_assocs.items(): model, field = details - query = dict(field=params.pop(assoc)) + 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 = dict(field=params.pop(assoc)) + query = { field: params.pop(assoc) } params[assoc] = model.objects.get(**query) @@ -55,7 +55,7 @@ with open('/opt/netbox/initializers/devices.yml', 'r') as stream: custom_field = CustomField.objects.get(name=cf_name) custom_field_value = CustomFieldValue.objects.create( field=custom_field, - obj=device_type, + obj=device, value=cf_value ) From a120a95184f779ae3b91f97ce41618f6cb9d9307 Mon Sep 17 00:00:00 2001 From: Aleksandar Radunovic Date: Tue, 16 Oct 2018 13:26:23 +0200 Subject: [PATCH 13/18] Fix initializers --- initializers/custom_fields.yml | 2 ++ initializers/device_types.yml | 8 ++++++++ initializers/devices.yml | 6 +++--- initializers/racks.yml | 6 +++--- initializers/sites.yml | 8 ++++++++ 5 files changed, 24 insertions(+), 6 deletions(-) diff --git a/initializers/custom_fields.yml b/initializers/custom_fields.yml index e70d6e5..0b6472a 100644 --- a/initializers/custom_fields.yml +++ b/initializers/custom_fields.yml @@ -7,6 +7,8 @@ # on_objects: # - dcim.models.Device # - dcim.models.Rack +# - dcim.models.Site +# - dcim.models.DeviceType # - ipam.models.IPAddress # - ipam.models.Prefix # - tenancy.models.Tenant diff --git a/initializers/device_types.yml b/initializers/device_types.yml index b41e31d..4177ac4 100644 --- a/initializers/device_types.yml +++ b/initializers/device_types.yml @@ -2,14 +2,22 @@ # model: Model 1 # slug: model-1 # u_height: 2 +# custom_fields: +# text_field: Description # - manufacturer: Intel # model: Model 2 # slug: model-2 +# custom_fields: +# text_field: Description # - manufacturer: Intel # model: Model 3 # slug: model-3 # is_full_depth: false # u_height: 0 +# custom_fields: +# text_field: Description # - manufacturer: NoName # model: Other # slug: other +# custom_fields: +# text_field: Description diff --git a/initializers/devices.yml b/initializers/devices.yml index 93334c9..0beb6f2 100644 --- a/initializers/devices.yml +++ b/initializers/devices.yml @@ -1,6 +1,6 @@ # - name: server01 # device_role: server -# device_type: other +# device_type: Other # site: AMS 1 # rack: rack-01 # face: Front @@ -9,7 +9,7 @@ # text_field: Description # - name: server02 # device_role: server -# device_type: other +# device_type: Other # site: AMS 2 # rack: rack-02 # face: Front @@ -18,7 +18,7 @@ # text_field: Description # - name: server03 # device_role: server -# device_type: other +# device_type: Other # site: SING 1 # rack: rack-03 # face: Front diff --git a/initializers/racks.yml b/initializers/racks.yml index 7a06b08..9a71743 100644 --- a/initializers/racks.yml +++ b/initializers/racks.yml @@ -1,6 +1,6 @@ # - site: AMS 1 # name: rack-01 -# role: role-1 +# role: Role 1 # type: 4-post cabinet # width: 19 inches # u_height: 47 @@ -8,7 +8,7 @@ # text_field: Description # - site: AMS 2 # name: rack-02 -# role: role-2 +# role: Role 2 # type: 4-post cabinet # width: 19 inches # u_height: 47 @@ -16,7 +16,7 @@ # text_field: Description # - site: SING 1 # name: rack-03 -# role: role-3 +# role: Role 3 # type: 4-post cabinet # width: 19 inches # u_height: 47 diff --git a/initializers/sites.yml b/initializers/sites.yml index 2d48feb..2381f99 100644 --- a/initializers/sites.yml +++ b/initializers/sites.yml @@ -4,21 +4,29 @@ # status: 1 # facility: Amsterdam 1 # asn: 12345 +# custom_fields: +# text_field: Description # - name: AMS 2 # slug: ams2 # region: Downtown # status: 1 # facility: Amsterdam 2 # asn: 54321 +# custom_fields: +# text_field: Description # - name: AMS 3 # slug: ams3 # region: Suburbs # status: 1 # facility: Amsterdam 3 # asn: 67890 +# custom_fields: +# text_field: Description # - name: SING 1 # slug: sing1 # region: Singapore # status: 1 # facility: Singapore 1 # asn: 09876 +# custom_fields: +# text_field: Description From d145e9c7198395db05f2294f5c31286f6c82d4d7 Mon Sep 17 00:00:00 2001 From: Aleksandar Radunovic Date: Tue, 30 Oct 2018 10:07:33 +0100 Subject: [PATCH 14/18] Comply to README style --- README.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index c01e9c9..01dc87f 100644 --- a/README.md +++ b/README.md @@ -161,8 +161,7 @@ if created: #### Initializers -Initializers are built-in startup scripts for defining NetBox -custom fields, groups, users and many other resources. +Initializers are built-in startup scripts for defining NetBox custom fields, groups, users and many other resources. All you need to do is to mount you own `initializers` folder ([see `docker-compose.yml`][netbox-docker-compose]). Look at the [`initializers` folder][netbox-docker-initializers] to learn how the files must look like. @@ -326,10 +325,10 @@ Then make sure that the `redis` container and at least one `netbox-worker` are r # check the container status $ docker-compose ps -Name Command State Ports +Name Command State Ports -------------------------------------------------------------------------------------------------------- -netbox-docker_netbox-worker_1 /opt/netbox/docker-entrypo ... Up -netbox-docker_netbox_1 /opt/netbox/docker-entrypo ... Up +netbox-docker_netbox-worker_1 /opt/netbox/docker-entrypo ... Up +netbox-docker_netbox_1 /opt/netbox/docker-entrypo ... Up netbox-docker_nginx_1 nginx -c /etc/netbox-nginx ... Up 80/tcp, 0.0.0.0:32776->8080/tcp netbox-docker_postgres_1 docker-entrypoint.sh postgres Up 5432/tcp netbox-docker_redis_1 docker-entrypoint.sh redis ... Up 6379/tcp From 8b8620864cde405037a424d85aae887889149d0f Mon Sep 17 00:00:00 2001 From: Aleksandar Radunovic Date: Tue, 30 Oct 2018 10:11:05 +0100 Subject: [PATCH 15/18] Replace trademarked names with generic ones --- initializers/device_types.yml | 6 +++--- initializers/manufacturers.yml | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/initializers/device_types.yml b/initializers/device_types.yml index 4177ac4..f2dd81b 100644 --- a/initializers/device_types.yml +++ b/initializers/device_types.yml @@ -1,15 +1,15 @@ -# - manufacturer: Intel +# - manufacturer: Manufacturer 1 # model: Model 1 # slug: model-1 # u_height: 2 # custom_fields: # text_field: Description -# - manufacturer: Intel +# - manufacturer: Manufacturer 1 # model: Model 2 # slug: model-2 # custom_fields: # text_field: Description -# - manufacturer: Intel +# - manufacturer: Manufacturer 1 # model: Model 3 # slug: model-3 # is_full_depth: false diff --git a/initializers/manufacturers.yml b/initializers/manufacturers.yml index 18aef10..d737a5f 100644 --- a/initializers/manufacturers.yml +++ b/initializers/manufacturers.yml @@ -1,6 +1,6 @@ -# - name: Cisco -# slug: cisco -# - name: Intel -# slug: intel +# - name: Manufacturer 1 +# slug: manufacturer-1 +# - name: Manufacturer 2 +# slug: manufacturer-2 # - name: NoName # slug: noname From a10cd805ae42c56cec6f69350fb5a2bc752e047f Mon Sep 17 00:00:00 2001 From: Aleksandar Radunovic Date: Tue, 30 Oct 2018 10:51:43 +0100 Subject: [PATCH 16/18] Prefix output messages with appropriate emoji --- startup_scripts/030_regions.py | 2 +- startup_scripts/040_sites.py | 2 +- startup_scripts/050_manufacturers.py | 2 +- startup_scripts/060_device_types.py | 2 +- startup_scripts/070_rack_roles.py | 2 +- startup_scripts/080_racks.py | 2 +- startup_scripts/090_device_roles.py | 2 +- startup_scripts/100_devices.py | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/startup_scripts/030_regions.py b/startup_scripts/030_regions.py index 888f031..eed0cac 100644 --- a/startup_scripts/030_regions.py +++ b/startup_scripts/030_regions.py @@ -22,4 +22,4 @@ with open('/opt/netbox/initializers/regions.yml', 'r') as stream: region, created = Region.objects.get_or_create(**params) if created: - print("Created region", region.name) + print("🌐 Created region", region.name) diff --git a/startup_scripts/040_sites.py b/startup_scripts/040_sites.py index 3dd5a57..b974a82 100644 --- a/startup_scripts/040_sites.py +++ b/startup_scripts/040_sites.py @@ -37,4 +37,4 @@ with open('/opt/netbox/initializers/sites.yml', 'r') as stream: site.custom_field_values.add(custom_field_value) - print("Created site", site.name) + print("📍 Created site", site.name) diff --git a/startup_scripts/050_manufacturers.py b/startup_scripts/050_manufacturers.py index 13cad50..fdee2b4 100644 --- a/startup_scripts/050_manufacturers.py +++ b/startup_scripts/050_manufacturers.py @@ -10,5 +10,5 @@ with open('/opt/netbox/initializers/manufacturers.yml', 'r') as stream: manufacturer, created = Manufacturer.objects.get_or_create(**params) if created: - print("Created Manufacturer", manufacturer.name) + print("🏭 Created Manufacturer", manufacturer.name) diff --git a/startup_scripts/060_device_types.py b/startup_scripts/060_device_types.py index f4d5ae2..e663172 100644 --- a/startup_scripts/060_device_types.py +++ b/startup_scripts/060_device_types.py @@ -47,5 +47,5 @@ with open('/opt/netbox/initializers/device_types.yml', 'r') as stream: device_type.custom_field_values.add(custom_field_value) - print("Created device type", device_type.manufacturer, device_type.model) + print("🔡 Created device type", device_type.manufacturer, device_type.model) diff --git a/startup_scripts/070_rack_roles.py b/startup_scripts/070_rack_roles.py index ca82125..e4023e3 100644 --- a/startup_scripts/070_rack_roles.py +++ b/startup_scripts/070_rack_roles.py @@ -18,4 +18,4 @@ with open('/opt/netbox/initializers/rack_roles.yml', 'r') as stream: rack_role, created = RackRole.objects.get_or_create(**params) if created: - print("Created rack role", rack_role.name) + print("🎨 Created rack role", rack_role.name) diff --git a/startup_scripts/080_racks.py b/startup_scripts/080_racks.py index 2f5ea75..c90e20a 100644 --- a/startup_scripts/080_racks.py +++ b/startup_scripts/080_racks.py @@ -57,4 +57,4 @@ with open('/opt/netbox/initializers/racks.yml', 'r') as stream: rack.custom_field_values.add(custom_field_value) - print("Created rack", rack.site, rack.name) + print("🔳 Created rack", rack.site, rack.name) diff --git a/startup_scripts/090_device_roles.py b/startup_scripts/090_device_roles.py index d3f52d8..6a8a16b 100644 --- a/startup_scripts/090_device_roles.py +++ b/startup_scripts/090_device_roles.py @@ -19,4 +19,4 @@ with open('/opt/netbox/initializers/device_roles.yml', 'r') as stream: device_role, created = DeviceRole.objects.get_or_create(**params) if created: - print("Created device role", device_role.name) + print("🎨 Created device role", device_role.name) diff --git a/startup_scripts/100_devices.py b/startup_scripts/100_devices.py index b4f0323..1818431 100644 --- a/startup_scripts/100_devices.py +++ b/startup_scripts/100_devices.py @@ -61,4 +61,4 @@ with open('/opt/netbox/initializers/devices.yml', 'r') as stream: device.custom_field_values.add(custom_field_value) - print("Created device", device.name) + print("🖥️ Created device", device.name) From aa68548f41a73bfb6efb906a747fbeefd6973ffc Mon Sep 17 00:00:00 2001 From: Aleksandar Radunovic Date: Tue, 30 Oct 2018 14:22:04 +0100 Subject: [PATCH 17/18] 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 From 2fe139cb3c3e43b5770646459e6ac20950f6a861 Mon Sep 17 00:00:00 2001 From: Aleksandar Radunovic Date: Wed, 31 Oct 2018 10:58:42 +0100 Subject: [PATCH 18/18] Reorder device types data --- initializers/device_types.yml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/initializers/device_types.yml b/initializers/device_types.yml index f2dd81b..d0e4c36 100644 --- a/initializers/device_types.yml +++ b/initializers/device_types.yml @@ -1,23 +1,23 @@ -# - manufacturer: Manufacturer 1 -# model: Model 1 +# - model: Model 1 +# manufacturer: Manufacturer 1 # slug: model-1 # u_height: 2 # custom_fields: # text_field: Description -# - manufacturer: Manufacturer 1 -# model: Model 2 +# - model: Model 2 +# manufacturer: Manufacturer 1 # slug: model-2 # custom_fields: # text_field: Description -# - manufacturer: Manufacturer 1 -# model: Model 3 +# - model: Model 3 +# manufacturer: Manufacturer 1 # slug: model-3 # is_full_depth: false # u_height: 0 # custom_fields: # text_field: Description -# - manufacturer: NoName -# model: Other +# - model: Other +# manufacturer: NoName # slug: other # custom_fields: # text_field: Description