Last active
January 16, 2022 15:00
-
-
Save candlerb/5ed9189cdd6770480e5f254c855b5d10 to your computer and use it in GitHub Desktop.
Standalone script to create all the missing device components which exist in DeviceType template but not in Device instances
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import django | |
import os | |
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'netbox.settings') | |
django.setup() | |
from dcim.models import Device, ConsolePort, ConsoleServerPort, PowerPort, PowerOutlet, Interface, RearPort, FrontPort, DeviceBay | |
for device in Device.objects.all(): | |
# Based on Device.save() | |
ConsolePort.objects.bulk_create( | |
[ConsolePort(device=device, name=template.name) for template in | |
device.device_type.consoleport_templates.all() | |
if template.name not in {i.name for i in device.consoleports.all()}] | |
) | |
ConsoleServerPort.objects.bulk_create( | |
[ConsoleServerPort(device=device, name=template.name) for template in | |
device.device_type.consoleserverport_templates.all() | |
if template.name not in {i.name for i in device.consoleserverports.all()}] | |
) | |
PowerPort.objects.bulk_create( | |
[PowerPort(device=device, name=template.name) for template in | |
device.device_type.powerport_templates.all() | |
if template.name not in {i.name for i in device.powerports.all()}] | |
) | |
PowerOutlet.objects.bulk_create( | |
[PowerOutlet(device=device, name=template.name) for template in | |
device.device_type.poweroutlet_templates.all() | |
if template.name not in {i.name for i in device.poweroutlets.all()}] | |
) | |
Interface.objects.bulk_create( | |
[Interface(device=device, name=template.name, form_factor=template.form_factor, | |
mgmt_only=template.mgmt_only) for template in device.device_type.interface_templates.all() | |
if template.name not in {i.name for i in device.interfaces.all()}] | |
) | |
RearPort.objects.bulk_create([ | |
RearPort( | |
device=device, | |
name=template.name, | |
type=template.type, | |
positions=template.positions | |
) for template in device.device_type.rearport_templates.all() | |
if template.name not in {i.name for i in device.rearports.all()} | |
]) | |
FrontPort.objects.bulk_create([ | |
FrontPort( | |
device=device, | |
name=template.name, | |
type=template.type, | |
rear_port=RearPort.objects.get(device=device, name=template.rear_port.name), | |
rear_port_position=template.rear_port_position, | |
) for template in device.device_type.frontport_templates.all() | |
if template.name not in {i.name for i in device.frontports.all()} | |
]) | |
DeviceBay.objects.bulk_create( | |
[DeviceBay(device=device, name=template.name) for template in | |
device.device_type.device_bay_templates.all() | |
if template.name not in {i.name for i in device.device_bays.all()}] | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment