Select Git revision
netbox_router.py 1.12 KiB
"""A router that must be present in Netbox."""
from typing import Annotated, TypeVar
from pydantic import AfterValidator
from pydantic_forms.types import UUIDstr
from gso.products.product_types.router import Router
from gso.services.netbox_client import NetboxClient
from gso.utils.shared_enums import Vendor
def validate_router_in_netbox(subscription_id: UUIDstr) -> UUIDstr:
"""Verify if a device exists in Netbox.
Raises a :class:`ValueError` if the device is not found.
:param subscription_id: The :term:`UUID` of the router subscription.
:type subscription_id: :class:`UUIDstr`
:return: The :term:`UUID` of the router subscription.
:rtype: :class:`UUIDstr`
"""
router_type = Router.from_subscription(subscription_id)
if router_type.router.vendor == Vendor.NOKIA:
device = NetboxClient().get_device_by_name(router_type.router.router_fqdn)
if not device:
msg = "The selected router does not exist in Netbox."
raise ValueError(msg)
return subscription_id
T = TypeVar("T")
NetboxEnabledRouter = Annotated[T, UUIDstr, AfterValidator(validate_router_in_netbox)]