Skip to content

Commit

Permalink
Increase corner case handling on import config from plugin. Also add …
Browse files Browse the repository at this point in the history
…default fallback config on FastAPI setup.
  • Loading branch information
emersonfelipesp committed Oct 28, 2024
1 parent da71007 commit 5e713a3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 20 deletions.
8 changes: 8 additions & 0 deletions netbox_proxbox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ class ProxboxConfig(PluginConfig):
'node_role_id' : 0,
'site_id': 0
}
},
'fastapi': {
'uvicorn_host': 'localhost',
'uvicorn_port': 8800,
'sudo': {
'user': "linuxUserWithSudo",
'password': "Strong@P4ssword",
}
}
}

Expand Down
58 changes: 38 additions & 20 deletions netbox_proxbox/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,44 +36,62 @@

netbox_host: str = "localhost"
netbox_port: int = 80



configuration = None
default_config: dict = {}
plugin_configuration: dict = {}
proxbox_cfg: dict = {}

try:
from netbox import configuration
plugin_configuration = configuration.PLUGINS_CONFIG
proxbox_cfg: dict = plugin_configuration.get("netbox_proxbox", {})

except ImportError:
print(cfg_not_found_msg)

if configuration:
plugin_configuration = configuration.PLUGINS_CONFIG
# TODO
# Look for the 'configuration.py' via 'os' module.

proxbox_cfg = plugin_configuration.get("netbox_proxbox", False)

try:
from . import ProxboxConfig
default_config: dict = ProxboxConfig.default_settings

except ImportError:
print(cfg_not_found_msg)

# TODO
# Look for the 'ProxboxConfig' via 'os' module.


try:
if proxbox_cfg and default_config:
print("Netbox Proxbox configuration and Proxbox Default config found.")

if proxbox_cfg:
print("Netbox Proxbox configuration found.")

fastapi_cfg = proxbox_cfg.get("fastapi", False)
fastapi_cfg: dict = proxbox_cfg.get("fastapi", default_config.get('fastapi'))

if fastapi_cfg:
uvicorn_host = fastapi_cfg.get("uvicorn_host", "localhost")
uvicorn_port = fastapi_cfg.get("uvicorn_port", 8800)
uvicorn_host: str = fastapi_cfg.get("uvicorn_host", default_config['fastapi']['uvicorn_host'])
uvicorn_port: int = fastapi_cfg.get("uvicorn_port", default_config['fastapi']['uvicorn_port'])

netbox_cfg = proxbox_cfg.get("netbox", False)
netbox_cfg: dict = proxbox_cfg.get("netbox", default_config.get('netbox'))

if netbox_cfg:
netbox_host = netbox_cfg.get("domain", "localhost")
netbox_port = netbox_cfg.get("http_port", 80)
netbox_host: str = netbox_cfg.get("domain", default_config['netbox']['domain'])
netbox_port: int = netbox_cfg.get("http_port", default_config['netbox']['http_port'])

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.

except Exception as error:
print(f"Error while trying to get configuration from Netbox.\n > {error}")


fastapi_endpoint = f"http://{uvicorn_host}:{uvicorn_port}"
Expand All @@ -87,7 +105,7 @@
https_netbox_endpoint = f"https://{netbox_host}:{netbox_port}"


PROXBOX_PLUGIN_NAME = "netbox_proxbox"
PROXBOX_PLUGIN_NAME: str = "netbox_proxbox"


# Init FastAPI
Expand Down

0 comments on commit 5e713a3

Please sign in to comment.