Skip to content

Commit

Permalink
Merge pull request #158 from netdevopsbr/nextjs
Browse files Browse the repository at this point in the history
Deploy basic Next.js app to start Standalone version of Proxbox
  • Loading branch information
emersonfelipesp authored Nov 15, 2023
2 parents a1ef41a + 70bf70e commit d362f9a
Show file tree
Hide file tree
Showing 60 changed files with 6,625 additions and 193 deletions.
10 changes: 5 additions & 5 deletions contrib/proxbox.service
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[Unit]
Description=Proxbox Service
Documentation=https://docs.netbox.dev.br/
Description=Proxbox Backend Service (FastAPI)
Documentation=https://github.com/netdevopsbr/netbox-proxbox
After=network-online.target
Wants=network-online.target

Expand All @@ -9,10 +9,10 @@ Type=simple

User=netbox
Group=netbox
#PIDFile=/var/tmp/netbox.pid
WorkingDirectory=/opt/netbox
PIDFile=/var/tmp/proxbox.pid
WorkingDirectory=/opt/netbox/netbox/netbox-proxbox

ExecStart=/opt/netbox/venv/bin/uvicorn opt.netboxnetbox.netbox-proxbox.netbox_proxbox.main:app --reload --port 10000
ExecStart=/opt/netbox/venv/bin/uvicorn netbox-proxbox.netbox_proxbox.main:app --host 0.0.0.0 --port 8800 --app-dir /opt/netbox/netbox

Restart=on-failure
RestartSec=30
Expand Down
10 changes: 3 additions & 7 deletions netbox_proxbox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,22 @@ class ProxboxConfig(PluginConfig):
'password': 'Strong@P4ssword',
'token': {
'name': 'proxbox',
'value': '039az154-23b2-4be0-8d20-b66abc8c4686'
'value': 'PASTE_YOUR_TOKEN_HERE'
},
'ssl': False
}
],
'netbox': {
'domain': 'netbox.example.com', # May also be IP address
'http_port': 80,
'token': '0dd7cddfaee3b38bbffbd2937d44c4a03f9c9d38',
'token': 'PASTE_YOUR_TOKEN_HERE',
'ssl': False,
'settings': {
'virtualmachine_role_id' : 0,
'node_role_id' : 0,
'site_id': 0
}
},
'fastapi': {
'uvicorn_host' : '0.0.0.0',
'uvicorn_port' : '8002',
},
}
}

config = ProxboxConfig
Expand Down
Empty file.
78 changes: 78 additions & 0 deletions netbox_proxbox/backend/enum/proxmox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from enum import Enum

class ProxmoxModeOptions(str, Enum):
single = "single"
multi = "multi"

class ProxmoxUpperPaths(str, Enum):
access = "access"
cluster = "cluster"
nodes = "nodes"
storage = "storage"
version = "version"

class ProxmoxAccessPaths(str, Enum):
domains = "domains"
groups = "groups"
openid = "openid"
roles = "roles"
tfa = "tfa"
users = "users"
acl = "acl"
password = "password"
permissions = "permissions"
ticket = "ticket"

class ProxmoxClusterPaths(str, Enum):
acme = "acme"
backup = "backup"
backup_info = "backup-info"
ceph = "ceph"
config = "config"
firewall = "firewall"
ha = "ha"
jobs = "jobs"
mapping = "mapping"
metrics = "metrics"
replication = "replication"
sdn = "sdn"
log = "log"
nextid = "nextid"
options = "options"
resources = "resources"
status = "status"
tasks = "tasks"

class ClusterResourcesType(str, Enum):
vm = "vm"
storage = "storage"
node = "node"
sdn = "sdn"


class ClusterResourcesTypeResponse(str, Enum):
node = "node"
storage = "storage"
pool = "pool"
qemu = "qemu"
lxc = "lxc"
openvz = "openvz"
sdn = "sdn"


class ProxmoxNodesPaths(str, Enum):
node = "node"

class ResourceType(Enum):
node = "node"
storage = "storage"
pool = "pool"
qemu = "qemu"
lxc = "lxc"
openvz = "openvz"
sdn = "sdn"

class NodeStatus(Enum):
unknown = "unknown"
online = "online"
offline = "offline"
10 changes: 10 additions & 0 deletions netbox_proxbox/backend/exception.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class ProxboxException(Exception):
def __init__(
self,
message: str,
detail: str | None = None,
python_exception: str | None = None,
):
self.message = message
self.detail = detail
self.python_exception = python_exception
Empty file.
50 changes: 50 additions & 0 deletions netbox_proxbox/backend/routes/netbox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from fastapi import APIRouter, Depends

from typing import Annotated, Any

from netbox_proxbox.backend.schemas import PluginConfig
from netbox_proxbox.backend.schemas.netbox import NetboxSessionSchema
from netbox_proxbox.backend.session.netbox import NetboxSession
from netbox_proxbox.backend.routes.proxbox import netbox_settings

router = APIRouter()

async def netbox_session(
netbox_settings: Annotated[NetboxSessionSchema, Depends(netbox_settings)],
):
"""Instantiate 'NetboxSession' class with user parameters and return Netbox HTTP connection to make API calls"""
return await NetboxSession(netbox_settings).pynetbox()

# Make Session reusable
NetboxSessionDep = Annotated[Any, Depends(netbox_session)]

@router.get("/status")
async def netbox_status(
nb: NetboxSessionDep
):
return nb.status()

@router.get("/devices")
async def netbox_devices(nb: NetboxSessionDep):
"Return a list of all devices registered on Netbox."
raw_list = []

device_list = nb.nb_session.dcim.device_roles.all()
for device in device_list:
raw_list.append(device)

return raw_list

@router.get("/openapi")
async def netbox_devices(nb: NetboxSessionDep):
return nb.openapi()

@router.get("/")
async def netbox(
status: Annotated[Any, Depends(netbox_status)],
config: Annotated[Any, Depends(netbox_settings)]
):
return {
"config": config,
"status": status
}
129 changes: 129 additions & 0 deletions netbox_proxbox/backend/routes/proxbox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
from typing import Annotated

from fastapi import APIRouter, Depends, Query

from netbox_proxbox.backend.schemas import PluginConfig
from netbox_proxbox.backend.schemas.netbox import NetboxSessionSchema
from netbox_proxbox.backend.schemas.proxmox import ProxmoxMultiClusterConfig
from netbox_proxbox.backend.exception import ProxboxException

router = APIRouter()

PROXBOX_PLUGIN_NAME = "netbox_proxbox"

@router.get("/netbox/plugins-config")
async def netbox_plugins_config(
plugin_name: Annotated[
str,
Query(
title="Netbox Plugin Name",
description="Netbox plugin name to get configuration from PLUGINS_CONFIG variable located at Netbox 'configuration.py' file."
)
] | None = PROXBOX_PLUGIN_NAME,
list_all: Annotated[
bool,
Query(
title="List All Plugins",
description="Return all plugins configuration from PLUGINS_CONFIG variable located at Netbox 'configuration.py' file.",
)
] | None = False
):
"""
PLUGIN_CONFIG variable defined by user in Netbox 'configuration.py' file
"""

try:
from netbox.settings import PLUGINS_CONFIG
except Exception as e:
raise ProxboxException(
message = "Could not import PLUGINS CONFIG from configuration.py",
python_exception = f"{e}"
)

# If ?list=all=True
# Return complete PLUGINS_CONFIG (including other plugins)
if list_all:
return PLUGINS_CONFIG

# Message error to not found plugins.
plugin_not_found = f"Could not get '{plugin_name}' plugin config from 'PLUGINS_CONFIG' variable located at Netbox 'configuration.py'"

# Search for configuration of another plugin. This feature is not recommended and may cause security issues, use at your own risk.
if plugin_name != PROXBOX_PLUGIN_NAME:
return PLUGINS_CONFIG.get(plugin_name, {
"error": {
"message": plugin_not_found
}
}
)

try:
return PluginConfig(**PLUGINS_CONFIG.get(plugin_name, {
"error": {
"message": plugin_not_found
}
}
)
)

except Exception as e:
raise ProxboxException(
message = "Plugin configuration at PLUGINS_CONFIG (configuration.py) is probably incorrect.",
detail = "Could not feed 'PluginConfig' pydantic model with config provided from 'PLUGINS_CONFIG'.",
python_exception = f"{e}",
)


@router.get("/netbox/default-settings")
async def proxbox_netbox_default():
"""
Default Plugins settings
"""

from netbox_proxbox import ProxboxConfig
return ProxboxConfig.default_settings


@router.get("/settings")
async def proxbox_settings(
plugins_config: Annotated[PluginConfig, Depends(netbox_plugins_config)],
default_settings: Annotated[PluginConfig, Depends(proxbox_netbox_default)],
list_all: Annotated[
bool,
Query(
title="List All Plugins",
description="Return all plugins configuration from PLUGINS_CONFIG variable located at Netbox 'configuration.py' file.",
)
] | None = False
):
"""
TODO: Compare PLUGINS_CONFIG defined by user with default settings from ProxboxConfig.default_settings
"""
return plugins_config

ProxboxConfigDep = Annotated[PluginConfig, Depends(proxbox_settings)]

@router.get("/settings/netbox")
async def netbox_settings(
proxbox_config: ProxboxConfigDep
):
"""
Get user configuration for Netbox from PLUGINS_CONFIG
"""
return proxbox_config.netbox



@router.get("/settings/proxmox")
async def proxmox_settings(
proxbox_config: ProxboxConfigDep
):
"""
Get user configuration for Proxmox from PLUGINS_CONFIG
"""
return proxbox_config.proxmox


NetboxConfigDep = Annotated[NetboxSessionSchema, Depends(netbox_settings)]
ProxmoxConfigDep = Annotated[ProxmoxMultiClusterConfig, Depends(proxmox_settings)]

Loading

0 comments on commit d362f9a

Please sign in to comment.