Skip to content

Commit

Permalink
Add try-and-except on Netbox PLUGINS_CONFIG import
Browse files Browse the repository at this point in the history
  • Loading branch information
emersonfelipesp committed Oct 28, 2024
1 parent a620abc commit da71007
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 15 deletions.
25 changes: 19 additions & 6 deletions netbox_proxbox/backend/routes/netbox/dcim/device_roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
58 changes: 49 additions & 9 deletions netbox_proxbox/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit da71007

Please sign in to comment.