Skip to content

Commit

Permalink
Spicerack: fix Netbox 4 breaking changes
Browse files Browse the repository at this point in the history
While keeping Netbox < 3.3 compatibility
Tests back on < 3.3.

Bug: T336275
Change-Id: I0050bd557a2ad9c8a7a3965508c7beaeebf88344
  • Loading branch information
XioNoX committed Jul 17, 2024
1 parent 9aff37e commit 83153f9
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions spicerack/netbox.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Netbox module."""

import logging
from ipaddress import IPv4Interface, IPv6Interface, ip_interface
from typing import Any, Optional, Union
Expand Down Expand Up @@ -205,7 +206,8 @@ def __init__(
self._dry_run = dry_run
self._cached_mgmt_fqdn = "" # Cache the management interface as it would require an API call each time

role = server.role.slug if self.virtual else server.device_role.slug
# TODO cleanup after Netbox 4 upgrade
role = (server.device_role if hasattr(server, "device_role") else server.role).slug
if role != SERVER_ROLE_SLUG:
raise NetboxError(f"Object of type {type(server)} has invalid role {role}, only server is allowed")

Expand Down Expand Up @@ -437,10 +439,19 @@ def _find_primary_switch_iface(self) -> pynetbox.core.response.Record:
netbox_iface = primary_ip.assigned_object
if not netbox_iface:
raise NetboxError("Primary IP not assigned to an interface.")
netbox_switch_iface = netbox_iface.connected_endpoint
if not netbox_switch_iface:
# Netbox 3 backward compatibility
if hasattr(netbox_iface, "connected_endpoint"):
netbox_iface_endpoints = netbox_iface.connected_endpoint
else:
netbox_iface_endpoints = netbox_iface.connected_endpoints
if not netbox_iface_endpoints:
raise NetboxError("Primary interface not connected.")
return netbox_switch_iface
# Using connected_endpoints[0] to mimic pre-Netbox 3.3 behavior, when a cable only had one termination
# per side. To be revisited if we start using the multi-termination feature.
# TODO cleanup after Netbox 4 upgrade
if isinstance(netbox_iface_endpoints, list):
return netbox_iface_endpoints[0]
return netbox_iface_endpoints

@property
def access_vlan(self) -> str:
Expand Down

0 comments on commit 83153f9

Please sign in to comment.