Skip to content

Commit

Permalink
Add JSON containg Proxmox Version for each card on homepage
Browse files Browse the repository at this point in the history
  • Loading branch information
emersonfelipesp committed Oct 14, 2024
1 parent 091193c commit fdb45e3
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 145 deletions.
52 changes: 11 additions & 41 deletions netbox_proxbox/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@

from netbox import configuration

"""
CORS ORIGINS
"""

plugin_configuration = configuration.PLUGINS_CONFIG

uvicorn_host = plugin_configuration["netbox_proxbox"]["fastapi"]["uvicorn_host"]
Expand All @@ -39,6 +43,8 @@
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}"

Expand All @@ -51,12 +57,17 @@
version="0.0.1"
)

"""
CORS Middleware
"""

origins = [
fastapi_endpoint,
https_fastapi_endpoint,
netbox_endpoint,
https_netbox_endpoint,
netbox_endpoint_port80,
netbox_endpoint_port8000,
fastapi_endpoint_port8000,
fastapi_endpoint_port80,
"http://localhost",
Expand Down Expand Up @@ -108,47 +119,6 @@ async def websocket_endpoint(
# Routes (Endpoints)
#

html = """
<!DOCTYPE html>
<html>
<head>
<title>Chat</title>
</head>
<body>
<h1>Log Messages</h1>
<!-- Disable Input
<form action="" onsubmit="sendMessage(event)">
<input type="text" id="messageText" autocomplete="off"/>
<button>Send</button>
</form>
-->
<ul id='messages'>
</ul>
<script>
var ws = new WebSocket("ws://10.0.30.250:8800/ws");
ws.onmessage = function(event) {
var messages = document.getElementById('messages')
var message = document.createElement('li')
var content = document.createTextNode(event.data)
message.appendChild(content)
messages.appendChild(message)
};
function sendMessage(event) {
var input = document.getElementById("messageText")
ws.send(input.value)
input.value = ''
event.preventDefault()
}
</script>
</body>
</html>
"""

@app.get("/websocket")
async def get():
return HTMLResponse(html)


# Netbox Routes
app.include_router(netbox_router, prefix="/netbox", tags=["netbox"])
app.include_router(nb_dcim_router, prefix="/netbox/dcim", tags=["netbox / dcim"])
Expand Down
230 changes: 126 additions & 104 deletions netbox_proxbox/templates/netbox_proxbox/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ <h1>Log Messages</h1>
fastapi_endpoint = `http://${uvicorn_host}:${uvicorn_port}`
websocket_endpoint = `ws://${uvicorn_host}:${uvicorn_port}/ws`


console.log(fastapi_endpoint)
console.log(websocket_endpoint)

var ws = new WebSocket(websocket_endpoint);
Expand Down Expand Up @@ -54,24 +56,48 @@ <h1>Log Messages</h1>
event.preventDefault()
}

const xhr = new XMLHttpRequest();
xhr.open('GET', `${fastapi_endpoint}?source=netbox&list_all=false&plugin_name=netbox_proxbox`, true);
//function getProxmoxVersion() {

xhr.onload = function() {
if (xhr.status === 200) {
const data = JSON.parse(xhr.responseText);
console.log(data);
} else {
console.error(`Request failed. Status: ${xhr.status}`);
}
};
//console.log(domain)

xhr.onerror = function() {
console.error('Network error');
};
function getBody () {
body = document.getElementsByTagName("body")
body = body[0]

let arrai = Array.from(document.getElementsByTagName("tr"))
console.log("arrai0", arrai)

xhr.send();
body.onload = getVersion
console.log("body", body)

}

getBody()

async function getVersion() {

let elemento = document.getElementsByClassName("proxmox_version")

for (let item of elemento) {

let td = item.getElementsByTagName("td")

if (td[0].id) {

try {
request = await fetch(`${fastapi_endpoint}/proxmox/version?source=netbox&list_all=false&plugin_name=netbox_proxbox&domain=${td[0].id}`)
response = await request.json()

console.log(response[0])
} catch (error) {
console.log(error)
}

td[0].textContent = JSON.stringify(response)

}
}
}
</script>

<div class="row mb-3">
Expand Down Expand Up @@ -105,104 +131,100 @@ <h2>
</div>

{% for px in configuration.netbox_proxbox.proxmox %}
<div class="col col-md-6">
<div class="card">
<div align=center>
<br>
<a href="https://{{ px.domain }}:{{ px.http_port }}" target="_blank">
<img src="{% static 'netbox_proxbox/proxmox-logo.svg' %}" alt="Proxmox Logo" width="200px">
</a>
</div>
<div class="card-body">
<table class="table table-hover attr-table">
<tr>
<th scope="row"><strong>Domain / IP</strong></th>
<div class="col col-md-6">
<div class="card">
<div align=center>
<br>
<a href="https://{{ px.domain }}:{{ px.http_port }}" target="_blank">
<img src="{% static 'netbox_proxbox/proxmox-logo.svg' %}" alt="Proxmox Logo" width="200px">
</a>
</div>
<div class="card-body">
<table class="table table-hover attr-table" >
<tr>
<th scope="row"><strong>Domain / IP</strong></th>
{% if px.domain %}
<td>{{ px.domain }}</td>
{% else %}
<td>{{ default_config.proxmox.domain }} (default)</td>
{% endif %}
</tr>
<tr>
<th scope="row"><strong>HTTP Port</strong></th>
{% if px.http_port %}
<td>{{ px.http_port }}</td>
{% else %}
<td>{{ default_config.proxmox.http_port }} (default)</td>
{% endif %}
</tr>
<tr>
<th scope="row"><strong>Proxmox User</strong></th>
{% if px.user %}
<td>{{ px.user }}</td>
{% else %}
<td>{{ default_config.proxmox.user }} (default)</td>
{% endif %}
</tr>
<tr>
<th scope="row"><strong>Proxmox Password</strong></th>
{% if px.password %}
<td>password defined in configuration.py</td>
{% else %}
<td>(secret) (default)</td>
{% endif %}
</tr>
<th scope="row"><strong>Token Name</strong></th>
{% if px.token.name %}
<td>{{ px.token.name }}</td>
{% else %}
<td>{{ default_config.proxmox.token.name }} (default)</td>
{% endif %}
<tr>
<th scope="row"><strong>Token Value</strong></th>
<td>(secret)</td>
</tr>
<tr>
<th scope="row"><strong>SSL</strong></th>
{% if px.ssl %}
<td>{{ px.ssl }}</td>
{% else %}
<td>{{ default_config.proxmox.ssl }} (default)</td>
{% endif %}
</tr>
<tr class="proxmox_version">
<th scope="row" ><strong>Proxmox Version</strong></th>
<td id="{{ px.domain }}"></td>
</tr>
</table>
<div class="d-flex justify-content-between">
{% if px.domain %}
<td>{{ px.domain }}</td>
{% else %}
<td>{{ default_config.proxmox.domain }} (default)</td>
{% endif %}
</tr>
<tr>
<th scope="row"><strong>HTTP Port</strong></th>
{% if px.http_port %}
<td>{{ px.http_port }}</td>
<form action="single_update/" method="POST" target="_blank">
<input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}">
<input type="hidden" name="proxmox_domain" value="{{ px.domain }}" />
<a
class="btn btn-primary"
href="#"
onclick="event.preventDefault(); this.parentNode.submit()"
>Proxmox Update</a>
</form>
{% else %}
<td>{{ default_config.proxmox.http_port }} (default)</td>
{% endif %}
</tr>
<tr>
<th scope="row"><strong>Proxmox User</strong></th>
{% if px.user %}
<td>{{ px.user }}</td>
{% else %}
<td>{{ default_config.proxmox.user }} (default)</td>
{% endif %}
</tr>
<tr>
<th scope="row"><strong>Proxmox Password</strong></th>
{% if px.password %}
<td>password defined in configuration.py</td>
{% else %}
<td>(secret) (default)</td>
{% endif %}
</tr>
<th scope="row"><strong>Token Name</strong></th>
{% if px.token.name %}
<td>{{ px.token.name }}</td>
{% else %}
<td>{{ default_config.proxmox.token.name }} (default)</td>
{% endif %}
<tr>
<th scope="row"><strong>Token Value</strong></th>
<td>(secret)</td>
</tr>
<tr>
<th scope="row"><strong>SSL</strong></th>
{% if px.ssl %}
<td>{{ px.ssl }}</td>
{% else %}
<td>{{ default_config.proxmox.ssl }} (default)</td>
{% endif %}
</tr>
<tr>
<th scope="row"><strong>Proxmox Version</strong></th>
{% if px.ssl %}
<td>{{ px.ssl }}</td>
{% else %}
<td>{{ default_config.proxmox.ssl }} (default)</td>
{% endif %}
</tr>
</table>
<div class="d-flex justify-content-between">
{% if px.domain %}
<form action="single_update/" method="POST" target="_blank">
<input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}">
<input type="hidden" name="proxmox_domain" value="{{ px.domain }}" />
<a
target="_blank"
class="btn btn-primary"
href="#"
onclick="event.preventDefault(); this.parentNode.submit()"
href="{% url 'plugins:netbox_proxbox:proxmoxvm_single_update' domain=default_config.proxmox.domain %}"
>Proxmox Update</a>
</form>
{% else %}
<a
target="_blank"
class="btn btn-primary"
href="{% url 'plugins:netbox_proxbox:proxmoxvm_single_update' domain=default_config.proxmox.domain %}"
>Proxmox Update</a>
{% endif %}
<button class="btn btn-outline-primary" type="button">
<a
style="text-decoration: none; color: inherit"
target="_blank"
href="https://github.com/netdevopsbr/netbox-proxbox#13-configure-plugin"
>Show Example</a>
</button>
{% endif %}
<button class="btn btn-outline-primary" type="button">
<a
style="text-decoration: none; color: inherit"
target="_blank"
href="https://github.com/netdevopsbr/netbox-proxbox#13-configure-plugin"
>Show Example</a>
</button>
</div>
</div>
</div>
</div>
</div>
{% endfor %}
<div class="col col-md-6">
<div class="card">
Expand Down
6 changes: 6 additions & 0 deletions netbox_proxbox/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ def get(self, request):
# print("plugin_configuration: ", plugin_configuration, "\n\n")
# print("default_config: ", default_config)


uvicorn_host = plugin_configuration["netbox_proxbox"]["fastapi"]["uvicorn_host"]
uvicorn_port = plugin_configuration["netbox_proxbox"]["fastapi"]["uvicorn_port"]

fastapi_endpoint = f"http://{uvicorn_host}:{uvicorn_port}"

return render(
request,
self.template_name,
Expand Down

0 comments on commit fdb45e3

Please sign in to comment.