From 9fae2b0f74875d2c484b00def192554255c9c360 Mon Sep 17 00:00:00 2001 From: Tobias Genannt Date: Mon, 24 Aug 2020 11:04:06 +0200 Subject: [PATCH 1/3] Fixed VM interface creation for Netbox 2.9 --- startup_scripts/240_virtualization_interfaces.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/startup_scripts/240_virtualization_interfaces.py b/startup_scripts/240_virtualization_interfaces.py index e5d75f7..f04f30b 100644 --- a/startup_scripts/240_virtualization_interfaces.py +++ b/startup_scripts/240_virtualization_interfaces.py @@ -1,5 +1,4 @@ -from dcim.models import Interface -from virtualization.models import VirtualMachine +from virtualization.models import VirtualMachine, VMInterface from extras.models import CustomField, CustomFieldValue from startup_script_utils import load_yaml import sys @@ -22,7 +21,7 @@ for params in interfaces: params[assoc] = model.objects.get(**query) - interface, created = Interface.objects.get_or_create(**params) + interface, created = VMInterface.objects.get_or_create(**params) if created: if custom_fields is not None: From 3ace32dfc2bccfb13104a0ec67ac479d38d165b2 Mon Sep 17 00:00:00 2001 From: Tobias Genannt Date: Mon, 24 Aug 2020 11:00:48 +0200 Subject: [PATCH 2/3] Fixed creation of passwords for Netbox 2.9 --- startup_scripts/000_users.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/startup_scripts/000_users.py b/startup_scripts/000_users.py index 6605424..a801d85 100644 --- a/startup_scripts/000_users.py +++ b/startup_scripts/000_users.py @@ -12,7 +12,7 @@ for username, user_details in users.items(): if not User.objects.filter(username=username): user = User.objects.create_user( username = username, - password = user_details.get('password', 0) or User.objects.make_random_password) + password = user_details.get('password', 0) or User.objects.make_random_password()) print("👤 Created user",username) From b02a93904e03ec48b40d6dc4b25cfd6fcebe8a84 Mon Sep 17 00:00:00 2001 From: Tobias Genannt Date: Mon, 24 Aug 2020 14:20:35 +0200 Subject: [PATCH 3/3] Fixed IP address creation for Netbox 2.9 --- startup_scripts/260_ip_addresses.py | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/startup_scripts/260_ip_addresses.py b/startup_scripts/260_ip_addresses.py index 96ec4b0..7d164fd 100644 --- a/startup_scripts/260_ip_addresses.py +++ b/startup_scripts/260_ip_addresses.py @@ -1,12 +1,14 @@ -from ipam.models import IPAddress, VRF -from dcim.models import Device, Interface -from virtualization.models import VirtualMachine -from tenancy.models import Tenant -from extras.models import CustomField, CustomFieldValue +import sys +from dcim.models import Device, Interface +from django.contrib.contenttypes.models import ContentType +from django.db.models import Q +from extras.models import CustomField, CustomFieldValue +from ipam.models import VRF, IPAddress from netaddr import IPNetwork from startup_script_utils import load_yaml -import sys +from tenancy.models import Tenant +from virtualization.models import VirtualMachine, VMInterface ip_addresses = load_yaml('/opt/netbox/initializers/ip_addresses.yml') @@ -16,9 +18,12 @@ if ip_addresses is None: optional_assocs = { 'tenant': (Tenant, 'name'), 'vrf': (VRF, 'name'), - 'interface': (Interface, 'name') + 'interface': (None, None) } +vm_interface_ct = ContentType.objects.filter(Q(app_label='virtualization', model='vminterface')).first() +interface_ct = ContentType.objects.filter(Q(app_label='dcim', model='interface')).first() + for params in ip_addresses: vm = params.pop('virtual_machine', None) device = params.pop('device', None) @@ -35,13 +40,17 @@ for params in ip_addresses: if assoc == 'interface': if vm: vm_id = VirtualMachine.objects.get(name=vm).id - query = { field: params.pop(assoc), "virtual_machine_id": vm_id } + query = { 'name': params.pop(assoc), "virtual_machine_id": vm_id } + params['assigned_object_type'] = vm_interface_ct + params['assigned_object_id'] = VMInterface.objects.get(**query).id elif device: dev_id = Device.objects.get(name=device).id - query = { field: params.pop(assoc), "device_id": dev_id } + query = { 'name': params.pop(assoc), "device_id": dev_id } + params['assigned_object_type'] = interface_ct + params['assigned_object_id'] = Interface.objects.get(**query).id else: query = { field: params.pop(assoc) } - params[assoc] = model.objects.get(**query) + params[assoc] = model.objects.get(**query) ip_address, created = IPAddress.objects.get_or_create(**params)