Merge pull request #326 from tobiasge/fix-startup-2.9

Fixes for Netbox 2.9
This commit is contained in:
Tobias Genannt 2020-09-01 10:37:37 +02:00 committed by GitHub
commit e2711ca205
No account linked to committer's email address
3 changed files with 22 additions and 14 deletions

View File

@ -12,7 +12,7 @@ for username, user_details in users.items():
if not User.objects.filter(username=username): if not User.objects.filter(username=username):
user = User.objects.create_user( user = User.objects.create_user(
username = username, 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) print("👤 Created user",username)

View File

@ -1,5 +1,4 @@
from dcim.models import Interface from virtualization.models import VirtualMachine, VMInterface
from virtualization.models import VirtualMachine
from extras.models import CustomField, CustomFieldValue from extras.models import CustomField, CustomFieldValue
from startup_script_utils import load_yaml from startup_script_utils import load_yaml
import sys import sys
@ -22,7 +21,7 @@ for params in interfaces:
params[assoc] = model.objects.get(**query) 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 created:
if custom_fields is not None: if custom_fields is not None:

View File

@ -1,12 +1,14 @@
from ipam.models import IPAddress, VRF import sys
from dcim.models import Device, Interface
from virtualization.models import VirtualMachine
from tenancy.models import Tenant
from extras.models import CustomField, CustomFieldValue
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 netaddr import IPNetwork
from startup_script_utils import load_yaml 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') ip_addresses = load_yaml('/opt/netbox/initializers/ip_addresses.yml')
@ -16,9 +18,12 @@ if ip_addresses is None:
optional_assocs = { optional_assocs = {
'tenant': (Tenant, 'name'), 'tenant': (Tenant, 'name'),
'vrf': (VRF, '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: for params in ip_addresses:
vm = params.pop('virtual_machine', None) vm = params.pop('virtual_machine', None)
device = params.pop('device', None) device = params.pop('device', None)
@ -35,10 +40,14 @@ for params in ip_addresses:
if assoc == 'interface': if assoc == 'interface':
if vm: if vm:
vm_id = VirtualMachine.objects.get(name=vm).id 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: elif device:
dev_id = Device.objects.get(name=device).id 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: else:
query = { field: params.pop(assoc) } query = { field: params.pop(assoc) }
params[assoc] = model.objects.get(**query) params[assoc] = model.objects.get(**query)