From 5496f6878a4d160df04ff05499084ca64fa43f8e Mon Sep 17 00:00:00 2001 From: Leo Silva Date: Wed, 7 Aug 2024 06:31:48 +0200 Subject: [PATCH 01/13] Add virtual disks --- .../initializers/__init__.py | 2 + .../initializers/virtualization_disks.py | 38 +++++++++++++++++++ .../yaml/virtualization_disks.yml | 9 +++++ 3 files changed, 49 insertions(+) create mode 100644 src/netbox_initializers/initializers/virtualization_disks.py create mode 100644 src/netbox_initializers/initializers/yaml/virtualization_disks.yml diff --git a/src/netbox_initializers/initializers/__init__.py b/src/netbox_initializers/initializers/__init__.py index b7a2499..3a763c0 100644 --- a/src/netbox_initializers/initializers/__init__.py +++ b/src/netbox_initializers/initializers/__init__.py @@ -43,6 +43,7 @@ "vrfs", "aggregates", "virtual_machines", + "virtualization_disks", "virtualization_interfaces", "prefixes", "ip_addresses", @@ -213,6 +214,7 @@ def register_initializer(name: str, initializer): from .tenants import TenantInitializer from .users import UserInitializer from .virtual_machines import VirtualMachineInitializer +from .virtualization_disks import VMDiskInitializer from .virtualization_interfaces import VMInterfaceInitializer from .vlan_groups import VLANGroupInitializer from .vlans import VLANInitializer diff --git a/src/netbox_initializers/initializers/virtualization_disks.py b/src/netbox_initializers/initializers/virtualization_disks.py new file mode 100644 index 0000000..cdbba62 --- /dev/null +++ b/src/netbox_initializers/initializers/virtualization_disks.py @@ -0,0 +1,38 @@ +from virtualization.models import VirtualMachine, VirtualDisk + +from . import BaseInitializer, register_initializer + +MATCH_PARAMS = ["name", "virtual_machine"] +REQUIRED_ASSOCS = {"virtual_machine": (VirtualMachine, "name")} + + +class VMDiskInitializer(BaseInitializer): + data_file_name = "virtualization_disks.yml" + + def load_data(self): + disks = self.load_yaml() + if disks is None: + return + for params in disks: + custom_field_data = self.pop_custom_fields(params) + tags = params.pop("tags", None) + + for assoc, details in REQUIRED_ASSOCS.items(): + model, field = details + query = {field: params.pop(assoc)} + + params[assoc] = model.objects.get(**query) + + matching_params, defaults = self.split_params(params, MATCH_PARAMS) + disk, created = VirtualDisk.objects.get_or_create( + **matching_params, defaults=defaults + ) + + if created: + print("๐Ÿ’ฝ Created Disk", disk.name, disk.virtual_machine.name) + + self.set_custom_fields_values(disk, custom_field_data) + self.set_tags(disk, tags) + + +register_initializer("virtualization_disks", VMDiskInitializer) diff --git a/src/netbox_initializers/initializers/yaml/virtualization_disks.yml b/src/netbox_initializers/initializers/yaml/virtualization_disks.yml new file mode 100644 index 0000000..f48b454 --- /dev/null +++ b/src/netbox_initializers/initializers/yaml/virtualization_disks.yml @@ -0,0 +1,9 @@ +# - name: Virtual Disk 1 +# description: Virtual Disk 1 +# size: 256 +# virtual_machine: virtual machine 1 + +# - name: Virtual Disk 2 +# description: Virtual Disk 2 +# size: 1024 +# virtual_machine: virtual machine 1 From c1f28ed6a0695fe4afe6e6ebefa51dd786bef6ef Mon Sep 17 00:00:00 2001 From: Leo Silva Date: Wed, 7 Aug 2024 06:36:07 +0200 Subject: [PATCH 02/13] Add OOB IP to devices --- src/netbox_initializers/initializers/devices.py | 2 ++ src/netbox_initializers/initializers/primary_ips.py | 3 ++- src/netbox_initializers/initializers/yaml/devices.yml | 2 ++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/netbox_initializers/initializers/devices.py b/src/netbox_initializers/initializers/devices.py index c73a925..fd41b5d 100644 --- a/src/netbox_initializers/initializers/devices.py +++ b/src/netbox_initializers/initializers/devices.py @@ -35,8 +35,10 @@ def load_data(self): # primary ips are handled later in `380_primary_ips.py` params.pop("primary_ip4", None) params.pop("primary_ip6", None) + params.pop("oob_ip", None) params.pop("primary_ip4_vrf", None) params.pop("primary_ip6_vrf", None) + params.pop("oob_ip_vrf", None) for assoc, details in REQUIRED_ASSOCS.items(): model, field = details diff --git a/src/netbox_initializers/initializers/primary_ips.py b/src/netbox_initializers/initializers/primary_ips.py index 8f576bc..754a2e3 100644 --- a/src/netbox_initializers/initializers/primary_ips.py +++ b/src/netbox_initializers/initializers/primary_ips.py @@ -7,6 +7,7 @@ OPTIONAL_ASSOCS = { "primary_ip4": (IPAddress, "address"), "primary_ip6": (IPAddress, "address"), + "oob_ip": (IPAddress, "address"), } @@ -26,7 +27,7 @@ def get_vrf_id(vrf_name): def link_primary_ip(assets, asset_model): for params in assets: - primary_ip_fields = set(params) & {"primary_ip4", "primary_ip6"} + primary_ip_fields = set(params) & {"primary_ip4", "primary_ip6", "oob_ip"} if not primary_ip_fields: continue diff --git a/src/netbox_initializers/initializers/yaml/devices.yml b/src/netbox_initializers/initializers/yaml/devices.yml index 4e7155d..59deeda 100644 --- a/src/netbox_initializers/initializers/yaml/devices.yml +++ b/src/netbox_initializers/initializers/yaml/devices.yml @@ -33,6 +33,8 @@ # primary_ip4_vrf: vrf1 # primary_ip6: 2001:db8:a000:1::2/64 # primary_ip6_vrf: vrf1 +# oob_ip: 192.168.100.2/24 +# oob_ip_vrf: vrf2 # custom_field_data: # text_field: Description # - name: server03 From ac142c3ba1e95585cfc0bc19a8128685296d816f Mon Sep 17 00:00:00 2001 From: Leo Silva Date: Wed, 7 Aug 2024 06:42:44 +0200 Subject: [PATCH 03/13] Fix cables.py route on NetBox 4 --- .../initializers/cables.py | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/src/netbox_initializers/initializers/cables.py b/src/netbox_initializers/initializers/cables.py index ef4f32e..1137ad7 100644 --- a/src/netbox_initializers/initializers/cables.py +++ b/src/netbox_initializers/initializers/cables.py @@ -231,23 +231,8 @@ def load_data(self): check_terminations_are_free(term_a, term_b) - cable = Cable.objects.create(**params) - - params_a_term = { - "termination_id": term_a.id, - "termination_type": term_a_ct, - "cable": cable, - "cable_end": "A", - } - CableTermination.objects.create(**params_a_term) - - params_b_term = { - "termination_id": term_b.id, - "termination_type": term_b_ct, - "cable": cable, - "cable_end": "B", - } - CableTermination.objects.create(**params_b_term) + cable = Cable(a_terminations = [term_a], b_terminations = [term_b], **params) + cable.save() print(f"๐Ÿงท Created cable {cable} {cable_name}") self.set_tags(cable, tags) From 333ccd2544a7a801c640b0e392690840072a26ed Mon Sep 17 00:00:00 2001 From: Leo Silva Date: Wed, 7 Aug 2024 06:46:48 +0200 Subject: [PATCH 04/13] Change custom_fields.py to match the new "ui_visible" field and its options --- src/netbox_initializers/initializers/custom_fields.py | 4 ++-- .../initializers/yaml/custom_fields.yml | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/netbox_initializers/initializers/custom_fields.py b/src/netbox_initializers/initializers/custom_fields.py index 7fa8307..f127d47 100644 --- a/src/netbox_initializers/initializers/custom_fields.py +++ b/src/netbox_initializers/initializers/custom_fields.py @@ -52,8 +52,8 @@ def load_data(self): if cf_details.get("group_name", False): custom_field.group_name = cf_details["group_name"] - if cf_details.get("ui_visibility", False): - custom_field.ui_visibility = cf_details["ui_visibility"] + if cf_details.get("ui_visible", False): + custom_field.ui_visible = cf_details["ui_visible"] if cf_details.get("search_weight", -1) >= 0: custom_field.search_weight = cf_details["search_weight"] diff --git a/src/netbox_initializers/initializers/yaml/custom_fields.yml b/src/netbox_initializers/initializers/yaml/custom_fields.yml index e2b471d..5111cab 100644 --- a/src/netbox_initializers/initializers/yaml/custom_fields.yml +++ b/src/netbox_initializers/initializers/yaml/custom_fields.yml @@ -13,9 +13,9 @@ ## - disabled ## - loose ## - exact -## ui_visibility: -## - read-write -## - read-only +## ui_visible: +## - aways +## - if-set ## - hidden ## ## Examples: @@ -27,7 +27,7 @@ # required: false # weight: 0 # group_name: group1 -# ui_visibility: read-only +# ui_visible: if-set # search_weight: 100 # on_objects: # - dcim.models.Device From 861dad21f2685a7b0c36b5d6dcb6442b336906ce Mon Sep 17 00:00:00 2001 From: Leo Silva Date: Wed, 7 Aug 2024 06:50:24 +0200 Subject: [PATCH 05/13] Change sites.py to only show the "Assigned asn X to site Y" when they don't exist yet --- src/netbox_initializers/initializers/sites.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/netbox_initializers/initializers/sites.py b/src/netbox_initializers/initializers/sites.py index 0aac182..01e236c 100644 --- a/src/netbox_initializers/initializers/sites.py +++ b/src/netbox_initializers/initializers/sites.py @@ -56,7 +56,10 @@ def load_data(self): self.set_custom_fields_values(site, custom_field_data) self.set_tags(site, tags) + site_asns = site.asns.all() for asn in asnFounds: + if asn in site_asns: + continue site.asns.add(asn) print(" ๐Ÿ‘ค Assigned asn %s to site %s" % (asn, site.name)) From e12c8dd69714cd1e7f989787d2fa71709f544b8e Mon Sep 17 00:00:00 2001 From: Leo Silva Date: Wed, 7 Aug 2024 06:59:08 +0200 Subject: [PATCH 06/13] Fix custom_fields.py for "object" and "multiobject" fields --- src/netbox_initializers/initializers/__init__.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/netbox_initializers/initializers/__init__.py b/src/netbox_initializers/initializers/__init__.py index 3a763c0..21b787c 100644 --- a/src/netbox_initializers/initializers/__init__.py +++ b/src/netbox_initializers/initializers/__init__.py @@ -113,13 +113,23 @@ def set_custom_fields_values(self, entity, custom_field_data): "Please check the 'on_objects' for that custom field in custom_fields.yml" ) elif key not in entity.custom_field_data: + # If the type is a object, we need to get its related type + if cf.type == "object": + model = cf.related_object_type.model_class() + value = model.objects.get(**value).id + + if cf.type == "multiobject": + yaml_value = value + model = cf.related_object_type.model_class() + value = [ model.objects.get(**x).id for x in yaml_value ] + entity.custom_field_data[key] = value save = True if missing_cfs: raise Exception( f"โš ๏ธ Custom field(s) '{missing_cfs}' requested for {entity} but not found in Netbox!" - "Please chceck the custom_fields.yml" + "Please check the custom_fields.yml" ) if save: From 89837fa2f5a30f54971ca0f08e6f502b9a3bad1a Mon Sep 17 00:00:00 2001 From: Leo Silva Date: Wed, 7 Aug 2024 07:09:06 +0200 Subject: [PATCH 07/13] Add optional associations to device_types.py and locations.py --- src/netbox_initializers/initializers/device_types.py | 4 ++-- src/netbox_initializers/initializers/locations.py | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/netbox_initializers/initializers/device_types.py b/src/netbox_initializers/initializers/device_types.py index 7ea87d5..f22d16d 100644 --- a/src/netbox_initializers/initializers/device_types.py +++ b/src/netbox_initializers/initializers/device_types.py @@ -1,6 +1,6 @@ from typing import List -from dcim.models import DeviceType, Manufacturer, Region +from dcim.models import DeviceType, Manufacturer, Platform, Region from dcim.models.device_component_templates import ( ConsolePortTemplate, ConsoleServerPortTemplate, @@ -18,7 +18,7 @@ MATCH_PARAMS = ["manufacturer", "model", "slug"] REQUIRED_ASSOCS = {"manufacturer": (Manufacturer, "name")} -OPTIONAL_ASSOCS = {"region": (Region, "name"), "tenant": (Tenant, "name")} +OPTIONAL_ASSOCS = {"region": (Region, "name"), "tenant": (Tenant, "name"), "default_platform": (Platform, "name")} NESTED_ASSOCS = {"rear_port": (RearPortTemplate, "name"), "power_port": (PowerPortTemplate, "name")} SUPPORTED_COMPONENTS = { "interfaces": (InterfaceTemplate, ["name"]), diff --git a/src/netbox_initializers/initializers/locations.py b/src/netbox_initializers/initializers/locations.py index 30ffce6..20cbd1b 100644 --- a/src/netbox_initializers/initializers/locations.py +++ b/src/netbox_initializers/initializers/locations.py @@ -1,8 +1,9 @@ from dcim.models import Location, Site +from tenancy.models import Tenant from . import BaseInitializer, register_initializer -OPTIONAL_ASSOCS = {"site": (Site, "name"), "parent": (Location, "name")} +OPTIONAL_ASSOCS = {"site": (Site, "name"), "parent": (Location, "name"), "tenant": (Tenant, "name")} class LocationInitializer(BaseInitializer): From 8a13a174fe8ee29070c07ec327559b63834f0ac5 Mon Sep 17 00:00:00 2001 From: Leo Silva Date: Wed, 7 Aug 2024 07:11:50 +0200 Subject: [PATCH 08/13] Fix custom_field.py sequence so it will not reset all values when adding object_types --- src/netbox_initializers/initializers/custom_fields.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/netbox_initializers/initializers/custom_fields.py b/src/netbox_initializers/initializers/custom_fields.py index f127d47..9204ec2 100644 --- a/src/netbox_initializers/initializers/custom_fields.py +++ b/src/netbox_initializers/initializers/custom_fields.py @@ -25,6 +25,9 @@ def load_data(self): custom_field, created = CustomField.objects.get_or_create(name=cf_name) if created: + for object_type in cf_details.get("on_objects", []): + custom_field.object_types.add(get_class_for_class_path(object_type)) + if cf_details.get("default", False): custom_field.default = cf_details["default"] @@ -34,9 +37,6 @@ def load_data(self): if cf_details.get("label", False): custom_field.label = cf_details["label"] - for object_type in cf_details.get("on_objects", []): - custom_field.object_types.add(get_class_for_class_path(object_type)) - if cf_details.get("required", False): custom_field.required = cf_details["required"] From aee5b4b13355ff29a17f3945ff93b86f2d5a6c6e Mon Sep 17 00:00:00 2001 From: Leo Silva Date: Wed, 7 Aug 2024 07:13:16 +0200 Subject: [PATCH 09/13] Change custom_links.py to work with multiple content types --- .../initializers/custom_links.py | 18 +++++++----------- .../initializers/yaml/custom_links.yml | 5 ++++- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/netbox_initializers/initializers/custom_links.py b/src/netbox_initializers/initializers/custom_links.py index 7b149f1..fc151b9 100644 --- a/src/netbox_initializers/initializers/custom_links.py +++ b/src/netbox_initializers/initializers/custom_links.py @@ -8,7 +8,7 @@ def get_content_type(content_type): try: return ObjectType.objects.get(model=content_type) except ObjectType.DoesNotExist: - pass + print(f"โš ๏ธ The content_type '{content_type}' is unknown") return None @@ -20,14 +20,10 @@ def load_data(self): if custom_links is None: return for link in custom_links: - content_type_name = link.pop("content_type") - content_type = get_content_type(content_type_name) - if content_type is None: - print( - "โš ๏ธ Unable to create Custom Link '{0}': The content_type '{1}' is unknown".format( - link.get("name"), content_type - ) - ) + content_types = [ get_content_type(x) for x in link.pop("content_type") ] + + if None in content_types: + print(f"โš ๏ธ Unable to create Custom Link '{ link.get('name') }' due to unknown content_type") continue matching_params, defaults = self.split_params(link) @@ -36,9 +32,9 @@ def load_data(self): ) if created: - custom_link.object_types.add(content_type) + custom_link.object_types.set(content_types) custom_link.save() - print("๐Ÿ”— Created Custom Link '{0}'".format(custom_link.name)) + print(f"๐Ÿ”— Created Custom Link '{custom_link.name}'") register_initializer("custom_links", CustomLinkInitializer) diff --git a/src/netbox_initializers/initializers/yaml/custom_links.yml b/src/netbox_initializers/initializers/yaml/custom_links.yml index eb733ca..c738032 100644 --- a/src/netbox_initializers/initializers/yaml/custom_links.yml +++ b/src/netbox_initializers/initializers/yaml/custom_links.yml @@ -14,8 +14,11 @@ # link_url: 'https://github.com/netbox-community/netbox-docker' # new_window: False # content_type: device + # - name: link_to_localhost # link_text: 'Link to localhost' # link_url: 'http://localhost' # new_window: True -# content_type: device +# content_type: +# - device +# - site From 49601deac7b39abfb3ddd22654025f39c2234cf0 Mon Sep 17 00:00:00 2001 From: Leo Silva Date: Wed, 7 Aug 2024 07:18:57 +0200 Subject: [PATCH 10/13] Fix devices.py broken CounterCacheFields --- .../initializers/devices.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/netbox_initializers/initializers/devices.py b/src/netbox_initializers/initializers/devices.py index fd41b5d..a3088b4 100644 --- a/src/netbox_initializers/initializers/devices.py +++ b/src/netbox_initializers/initializers/devices.py @@ -3,6 +3,9 @@ from tenancy.models import Tenant from virtualization.models import Cluster +from utilities.fields import CounterCacheField +from django.apps import apps + from . import BaseInitializer, register_initializer MATCH_PARAMS = ["device_type", "name", "site"] @@ -24,6 +27,19 @@ class DeviceInitializer(BaseInitializer): data_file_name = "devices.yml" + def recalculate_counters(self, device: Device): + counter_fields = [ + field for field in device._meta.get_fields() if type(field) is CounterCacheField + ] + + updated_values = {} + for field in counter_fields: + model = apps.get_model(field.to_model_name) + count = model.objects.filter(**{field.to_field_name: device}).count() + updated_values[field.name] = count + + Device.objects.filter(pk = device.pk).update(**updated_values) + def load_data(self): devices = self.load_yaml() if devices is None: @@ -62,5 +78,8 @@ def load_data(self): self.set_custom_fields_values(device, custom_field_data) self.set_tags(device, tags) + # If we set any tags or custom fields, we need to recalculate all `CounterCacheField` + self.recalculate_counters(device) + register_initializer("devices", DeviceInitializer) From eb9350d5044998a0243203382760049bdafbf731 Mon Sep 17 00:00:00 2001 From: Leo Silva Date: Wed, 7 Aug 2024 08:05:00 +0200 Subject: [PATCH 11/13] Lint files --- src/netbox_initializers/initializers/__init__.py | 4 ++-- src/netbox_initializers/initializers/cables.py | 2 +- src/netbox_initializers/initializers/custom_links.py | 6 ++++-- src/netbox_initializers/initializers/device_types.py | 6 +++++- src/netbox_initializers/initializers/devices.py | 7 +++---- .../initializers/virtualization_disks.py | 6 ++---- 6 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/netbox_initializers/initializers/__init__.py b/src/netbox_initializers/initializers/__init__.py index 21b787c..7db07dd 100644 --- a/src/netbox_initializers/initializers/__init__.py +++ b/src/netbox_initializers/initializers/__init__.py @@ -117,11 +117,11 @@ def set_custom_fields_values(self, entity, custom_field_data): if cf.type == "object": model = cf.related_object_type.model_class() value = model.objects.get(**value).id - + if cf.type == "multiobject": yaml_value = value model = cf.related_object_type.model_class() - value = [ model.objects.get(**x).id for x in yaml_value ] + value = [model.objects.get(**x).id for x in yaml_value] entity.custom_field_data[key] = value save = True diff --git a/src/netbox_initializers/initializers/cables.py b/src/netbox_initializers/initializers/cables.py index 1137ad7..645adc1 100644 --- a/src/netbox_initializers/initializers/cables.py +++ b/src/netbox_initializers/initializers/cables.py @@ -231,7 +231,7 @@ def load_data(self): check_terminations_are_free(term_a, term_b) - cable = Cable(a_terminations = [term_a], b_terminations = [term_b], **params) + cable = Cable(a_terminations=[term_a], b_terminations=[term_b], **params) cable.save() print(f"๐Ÿงท Created cable {cable} {cable_name}") diff --git a/src/netbox_initializers/initializers/custom_links.py b/src/netbox_initializers/initializers/custom_links.py index fc151b9..3efe0f3 100644 --- a/src/netbox_initializers/initializers/custom_links.py +++ b/src/netbox_initializers/initializers/custom_links.py @@ -20,10 +20,12 @@ def load_data(self): if custom_links is None: return for link in custom_links: - content_types = [ get_content_type(x) for x in link.pop("content_type") ] + content_types = [get_content_type(x) for x in link.pop("content_type")] if None in content_types: - print(f"โš ๏ธ Unable to create Custom Link '{ link.get('name') }' due to unknown content_type") + print( + f"โš ๏ธ Unable to create Custom Link '{ link.get('name') }' due to unknown content_type" + ) continue matching_params, defaults = self.split_params(link) diff --git a/src/netbox_initializers/initializers/device_types.py b/src/netbox_initializers/initializers/device_types.py index f22d16d..f41cf02 100644 --- a/src/netbox_initializers/initializers/device_types.py +++ b/src/netbox_initializers/initializers/device_types.py @@ -18,7 +18,11 @@ MATCH_PARAMS = ["manufacturer", "model", "slug"] REQUIRED_ASSOCS = {"manufacturer": (Manufacturer, "name")} -OPTIONAL_ASSOCS = {"region": (Region, "name"), "tenant": (Tenant, "name"), "default_platform": (Platform, "name")} +OPTIONAL_ASSOCS = { + "region": (Region, "name"), + "tenant": (Tenant, "name"), + "default_platform": (Platform, "name") +} NESTED_ASSOCS = {"rear_port": (RearPortTemplate, "name"), "power_port": (PowerPortTemplate, "name")} SUPPORTED_COMPONENTS = { "interfaces": (InterfaceTemplate, ["name"]), diff --git a/src/netbox_initializers/initializers/devices.py b/src/netbox_initializers/initializers/devices.py index a3088b4..02a8e0a 100644 --- a/src/netbox_initializers/initializers/devices.py +++ b/src/netbox_initializers/initializers/devices.py @@ -1,10 +1,9 @@ from dcim.models import Device, DeviceRole, DeviceType, Location, Platform, Rack, Site +from django.apps import apps from extras.models import ConfigTemplate from tenancy.models import Tenant -from virtualization.models import Cluster - from utilities.fields import CounterCacheField -from django.apps import apps +from virtualization.models import Cluster from . import BaseInitializer, register_initializer @@ -38,7 +37,7 @@ def recalculate_counters(self, device: Device): count = model.objects.filter(**{field.to_field_name: device}).count() updated_values[field.name] = count - Device.objects.filter(pk = device.pk).update(**updated_values) + Device.objects.filter(pk=device.pk).update(**updated_values) def load_data(self): devices = self.load_yaml() diff --git a/src/netbox_initializers/initializers/virtualization_disks.py b/src/netbox_initializers/initializers/virtualization_disks.py index cdbba62..488fcb6 100644 --- a/src/netbox_initializers/initializers/virtualization_disks.py +++ b/src/netbox_initializers/initializers/virtualization_disks.py @@ -1,4 +1,4 @@ -from virtualization.models import VirtualMachine, VirtualDisk +from virtualization.models import VirtualDisk, VirtualMachine from . import BaseInitializer, register_initializer @@ -24,9 +24,7 @@ def load_data(self): params[assoc] = model.objects.get(**query) matching_params, defaults = self.split_params(params, MATCH_PARAMS) - disk, created = VirtualDisk.objects.get_or_create( - **matching_params, defaults=defaults - ) + disk, created = VirtualDisk.objects.get_or_create(**matching_params, defaults=defaults) if created: print("๐Ÿ’ฝ Created Disk", disk.name, disk.virtual_machine.name) From 285a058b3c4f188481afb25e99b8191148c64109 Mon Sep 17 00:00:00 2001 From: Leo Silva Date: Wed, 7 Aug 2024 08:37:26 +0200 Subject: [PATCH 12/13] lint devices.py --- src/netbox_initializers/initializers/devices.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/netbox_initializers/initializers/devices.py b/src/netbox_initializers/initializers/devices.py index 02a8e0a..25733d6 100644 --- a/src/netbox_initializers/initializers/devices.py +++ b/src/netbox_initializers/initializers/devices.py @@ -36,7 +36,7 @@ def recalculate_counters(self, device: Device): model = apps.get_model(field.to_model_name) count = model.objects.filter(**{field.to_field_name: device}).count() updated_values[field.name] = count - + Device.objects.filter(pk=device.pk).update(**updated_values) def load_data(self): From cb26a7e57b0788536a94725857721d3ad6a1185b Mon Sep 17 00:00:00 2001 From: Leo Silva Date: Wed, 7 Aug 2024 08:37:44 +0200 Subject: [PATCH 13/13] update max netbox version --- src/netbox_initializers/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/netbox_initializers/__init__.py b/src/netbox_initializers/__init__.py index 2099aa0..ea89f52 100644 --- a/src/netbox_initializers/__init__.py +++ b/src/netbox_initializers/__init__.py @@ -8,7 +8,7 @@ class NetBoxInitializersConfig(PluginConfig): version = "4.0.0" base_url = "initializers" min_version = "4.0-beta1" - max_version = "4.0.99" + max_version = "4.1.99" config = NetBoxInitializersConfig