2020-08-24 07:20:35 -05:00
|
|
|
import sys
|
|
|
|
|
2019-07-04 07:10:56 -05:00
|
|
|
from dcim.models import Device, Interface
|
2020-08-24 07:20:35 -05:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
|
|
from django.db.models import Q
|
|
|
|
from ipam.models import VRF, IPAddress
|
2019-07-04 07:10:56 -05:00
|
|
|
from netaddr import IPNetwork
|
2020-10-17 14:51:38 -05:00
|
|
|
from startup_script_utils import *
|
2020-08-24 07:20:35 -05:00
|
|
|
from tenancy.models import Tenant
|
|
|
|
from virtualization.models import VirtualMachine, VMInterface
|
2019-07-04 07:10:56 -05:00
|
|
|
|
2020-02-05 08:31:01 -06:00
|
|
|
ip_addresses = load_yaml('/opt/netbox/initializers/ip_addresses.yml')
|
2019-10-10 10:35:06 -05:00
|
|
|
|
2020-02-05 08:31:01 -06:00
|
|
|
if ip_addresses is None:
|
|
|
|
sys.exit()
|
2019-07-04 07:10:56 -05:00
|
|
|
|
2020-02-05 08:31:01 -06:00
|
|
|
optional_assocs = {
|
|
|
|
'tenant': (Tenant, 'name'),
|
|
|
|
'vrf': (VRF, 'name'),
|
2020-08-24 07:20:35 -05:00
|
|
|
'interface': (None, None)
|
2020-02-05 08:31:01 -06:00
|
|
|
}
|
|
|
|
|
2020-08-24 07:20:35 -05:00
|
|
|
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()
|
|
|
|
|
2020-02-05 08:31:01 -06:00
|
|
|
for params in ip_addresses:
|
2020-10-17 14:51:38 -05:00
|
|
|
custom_field_data = pop_custom_fields(params)
|
|
|
|
|
2020-02-05 08:31:01 -06:00
|
|
|
vm = params.pop('virtual_machine', None)
|
|
|
|
device = params.pop('device', None)
|
|
|
|
params['address'] = IPNetwork(params['address'])
|
|
|
|
|
|
|
|
if vm and device:
|
|
|
|
print("IP Address can only specify one of the following: virtual_machine or device.")
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
for assoc, details in optional_assocs.items():
|
|
|
|
if assoc in params:
|
|
|
|
model, field = details
|
|
|
|
if assoc == 'interface':
|
|
|
|
if vm:
|
|
|
|
vm_id = VirtualMachine.objects.get(name=vm).id
|
2020-08-24 07:20:35 -05:00
|
|
|
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
|
2020-02-05 08:31:01 -06:00
|
|
|
elif device:
|
|
|
|
dev_id = Device.objects.get(name=device).id
|
2020-08-24 07:20:35 -05:00
|
|
|
query = { 'name': params.pop(assoc), "device_id": dev_id }
|
|
|
|
params['assigned_object_type'] = interface_ct
|
|
|
|
params['assigned_object_id'] = Interface.objects.get(**query).id
|
2020-02-05 08:31:01 -06:00
|
|
|
else:
|
|
|
|
query = { field: params.pop(assoc) }
|
2020-08-24 07:20:35 -05:00
|
|
|
params[assoc] = model.objects.get(**query)
|
2020-02-05 08:31:01 -06:00
|
|
|
|
|
|
|
ip_address, created = IPAddress.objects.get_or_create(**params)
|
|
|
|
|
|
|
|
if created:
|
2020-10-17 14:51:38 -05:00
|
|
|
set_custom_fields_values(ip_address, custom_field_data)
|
2020-02-05 08:31:01 -06:00
|
|
|
|
|
|
|
print("🧬 Created IP Address", ip_address.address)
|