Skip to content

Instantly share code, notes, and snippets.

@bbrk364
Forked from candlerb/add-device-ports.py
Created April 19, 2022 14:38
Show Gist options
  • Select an option

  • Save bbrk364/ecb3a7e7a9c21f42c44f0bb4d7f1bcf6 to your computer and use it in GitHub Desktop.

Select an option

Save bbrk364/ecb3a7e7a9c21f42c44f0bb4d7f1bcf6 to your computer and use it in GitHub Desktop.

Revisions

  1. @candlerb candlerb revised this gist Sep 17, 2021. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions add-device-ports.py
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    # To run:
    # PYTHONPATH=/opt/netbox/netbox /opt/netbox/venv/bin/python add-device-ports.py

    #!/opt/netbox/venv/bin/python
    import django
    import os
    import sys
    sys.path.append('/opt/netbox/netbox')
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'netbox.settings')
    django.setup()

  2. @candlerb candlerb revised this gist Sep 17, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions add-device-ports.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    # source /opt/netbox/venv/bin/activate
    # PYTHONPATH=/opt/netbox/netbox python3 add-device-ports.py
    # To run:
    # PYTHONPATH=/opt/netbox/netbox /opt/netbox/venv/bin/python add-device-ports.py

    import django
    import os
  3. @candlerb candlerb revised this gist Oct 22, 2020. 1 changed file with 22 additions and 13 deletions.
    35 changes: 22 additions & 13 deletions add-device-ports.py
    Original file line number Diff line number Diff line change
    @@ -1,35 +1,41 @@
    # source /opt/netbox/venv/bin/activate
    # PYTHONPATH=/opt/netbox/netbox python3 add-device-ports.py

    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


    from django.db import transaction
    transaction.set_autocommit(False)

    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()
    device.device_type.consoleporttemplates.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()
    device.device_type.consoleserverporttemplates.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()
    device.device_type.powerporttemplates.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()
    device.device_type.poweroutlettemplates.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()
    [Interface(device=device, name=template.name, type=template.type,
    mgmt_only=template.mgmt_only) for template in device.device_type.interfacetemplates.all()
    if template.name not in {i.name for i in device.interfaces.all()}]
    )
    RearPort.objects.bulk_create([
    @@ -38,7 +44,7 @@
    name=template.name,
    type=template.type,
    positions=template.positions
    ) for template in device.device_type.rearport_templates.all()
    ) for template in device.device_type.rearporttemplates.all()
    if template.name not in {i.name for i in device.rearports.all()}
    ])
    FrontPort.objects.bulk_create([
    @@ -48,11 +54,14 @@
    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()
    ) for template in device.device_type.frontporttemplates.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()}]
    )
    device.device_type.devicebaytemplates.all()
    if template.name not in {i.name for i in device.devicebays.all()}]
    )

    #transaction.rollback()
    transaction.commit()
  4. @candlerb candlerb renamed this gist Apr 3, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. @candlerb candlerb revised this gist Apr 3, 2019. No changes.
  6. @candlerb candlerb created this gist Apr 3, 2019.
    58 changes: 58 additions & 0 deletions sync_components.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    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()}]
    )