From da7100766bbb8fd76ea50fd62ad2de81aa4fa1d5 Mon Sep 17 00:00:00 2001 From: Emerson Felipe Date: Mon, 28 Oct 2024 19:01:48 +0000 Subject: [PATCH] Add try-and-except on Netbox PLUGINS_CONFIG import --- .../routes/netbox/dcim/device_roles.py | 25 ++++++-- netbox_proxbox/main.py | 58 ++++++++++++++++--- 2 files changed, 68 insertions(+), 15 deletions(-) diff --git a/netbox_proxbox/backend/routes/netbox/dcim/device_roles.py b/netbox_proxbox/backend/routes/netbox/dcim/device_roles.py index 1dcade9..349c19c 100755 --- a/netbox_proxbox/backend/routes/netbox/dcim/device_roles.py +++ b/netbox_proxbox/backend/routes/netbox/dcim/device_roles.py @@ -2,14 +2,27 @@ from typing import Any class DeviceRole(NetboxBase): + """ + DeviceRole class represents a device role in Netbox for Proxmox nodes. + Attributes: + default_name (str): The default name for the device role. + default_slug (str): The default slug for the device role. + default_description (str): The default description for the device role. + app (str): The application name associated with the device role. + endpoint (str): The API endpoint for the device role. + object_name (str): The name of the object type. + Methods: + get_base_dict(): + Asynchronously returns a dictionary with the base attributes for the device role. + """ - default_name = "Proxmox Node (Server)" - default_slug = "proxbox-node" - default_description = "Proxbox Basic Device Role" + default_name: str = "Proxmox Node (Server)" + default_slug: str = "proxbox-node" + default_description: str = "Proxbox Basic Device Role" - app = "dcim" - endpoint = "device_roles" - object_name = "Device Types" + app: str = "dcim" + endpoint: str = "device_roles" + object_name: str = "Device Types" async def get_base_dict(self): return { diff --git a/netbox_proxbox/main.py b/netbox_proxbox/main.py index 9b9ae83..5f29654 100755 --- a/netbox_proxbox/main.py +++ b/netbox_proxbox/main.py @@ -23,33 +23,73 @@ from .backend.schemas import * -from netbox import configuration - """ CORS ORIGINS """ -plugin_configuration = configuration.PLUGINS_CONFIG +cfg_not_found_msg = "Netbox configuration not found. Using default configuration." + +plugin_configuration: dict = {} + +uvicorn_host: str = "localhost" +uvicorn_port: int = 8800 + +netbox_host: str = "localhost" +netbox_port: int = 80 + +try: + from netbox import configuration + + if configuration: + plugin_configuration = configuration.PLUGINS_CONFIG -uvicorn_host = plugin_configuration["netbox_proxbox"]["fastapi"]["uvicorn_host"] -uvicorn_port = plugin_configuration["netbox_proxbox"]["fastapi"]["uvicorn_port"] + proxbox_cfg = plugin_configuration.get("netbox_proxbox", False) + + if proxbox_cfg: + print("Netbox Proxbox configuration found.") + + fastapi_cfg = proxbox_cfg.get("fastapi", False) + + if fastapi_cfg: + uvicorn_host = fastapi_cfg.get("uvicorn_host", "localhost") + uvicorn_port = fastapi_cfg.get("uvicorn_port", 8800) + + netbox_cfg = proxbox_cfg.get("netbox", False) + + if netbox_cfg: + netbox_host = netbox_cfg.get("domain", "localhost") + netbox_port = netbox_cfg.get("http_port", 80) + + else: + print("PLUGINS_CONFIG found, but 'netbox_proxbox' configuration not found.") + + # TODO + # Raise an exception here. + + + +except ImportError: + print(cfg_not_found_msg) + + # TODO + # Look for the 'configuration.py' via 'os' module. + + fastapi_endpoint = f"http://{uvicorn_host}:{uvicorn_port}" https_fastapi_endpoint = f"https://{uvicorn_host}:{uvicorn_port}" fastapi_endpoint_port8000 = f"http://{uvicorn_host}:8000" fastapi_endpoint_port80 = f"http://{uvicorn_host}:80" -netbox_host = plugin_configuration["netbox_proxbox"]["netbox"]["domain"] -netbox_port = plugin_configuration["netbox_proxbox"]["netbox"]["http_port"] - netbox_endpoint_port80 = f"http://{netbox_host}:80" netbox_endpoint_port8000 = f"http://{netbox_host}:8000" - netbox_endpoint = f"http://{netbox_host}:{netbox_port}" https_netbox_endpoint = f"https://{netbox_host}:{netbox_port}" + PROXBOX_PLUGIN_NAME = "netbox_proxbox" + # Init FastAPI app = FastAPI( title="Proxbox Backend",