|
1 |
| -from ipam.models import IPAddress, VRF |
| 1 | +import sys |
| 2 | + |
2 | 3 | from dcim.models import Device, Interface
|
3 |
| -from virtualization.models import VirtualMachine |
4 |
| -from tenancy.models import Tenant |
| 4 | +from django.contrib.contenttypes.models import ContentType |
| 5 | +from django.db.models import Q |
5 | 6 | from extras.models import CustomField, CustomFieldValue
|
6 |
| - |
| 7 | +from ipam.models import VRF, IPAddress |
7 | 8 | from netaddr import IPNetwork
|
8 | 9 | from startup_script_utils import load_yaml
|
9 |
| -import sys |
| 10 | +from tenancy.models import Tenant |
| 11 | +from virtualization.models import VirtualMachine, VMInterface |
10 | 12 |
|
11 | 13 | ip_addresses = load_yaml('/opt/netbox/initializers/ip_addresses.yml')
|
12 | 14 |
|
|
16 | 18 | optional_assocs = {
|
17 | 19 | 'tenant': (Tenant, 'name'),
|
18 | 20 | 'vrf': (VRF, 'name'),
|
19 |
| - 'interface': (Interface, 'name') |
| 21 | + 'interface': (None, None) |
20 | 22 | }
|
21 | 23 |
|
| 24 | +vm_interface_ct = ContentType.objects.filter(Q(app_label='virtualization', model='vminterface')).first() |
| 25 | +interface_ct = ContentType.objects.filter(Q(app_label='dcim', model='interface')).first() |
| 26 | + |
22 | 27 | for params in ip_addresses:
|
23 | 28 | vm = params.pop('virtual_machine', None)
|
24 | 29 | device = params.pop('device', None)
|
|
35 | 40 | if assoc == 'interface':
|
36 | 41 | if vm:
|
37 | 42 | vm_id = VirtualMachine.objects.get(name=vm).id
|
38 |
| - query = { field: params.pop(assoc), "virtual_machine_id": vm_id } |
| 43 | + query = { 'name': params.pop(assoc), "virtual_machine_id": vm_id } |
| 44 | + params['assigned_object_type'] = vm_interface_ct |
| 45 | + params['assigned_object_id'] = VMInterface.objects.get(**query).id |
39 | 46 | elif device:
|
40 | 47 | dev_id = Device.objects.get(name=device).id
|
41 |
| - query = { field: params.pop(assoc), "device_id": dev_id } |
| 48 | + query = { 'name': params.pop(assoc), "device_id": dev_id } |
| 49 | + params['assigned_object_type'] = interface_ct |
| 50 | + params['assigned_object_id'] = Interface.objects.get(**query).id |
42 | 51 | else:
|
43 | 52 | query = { field: params.pop(assoc) }
|
44 |
| - params[assoc] = model.objects.get(**query) |
| 53 | + params[assoc] = model.objects.get(**query) |
45 | 54 |
|
46 | 55 | ip_address, created = IPAddress.objects.get_or_create(**params)
|
47 | 56 |
|
|
0 commit comments