resman/backend/public/js/resources.js

166 lines
4.2 KiB
JavaScript

window.resources = []
window.loadResources = async function(){
window.resources = await api(API + "/resources/active")
const resources = window.resources
const mappings = await api(API + "/domainmap")
const table = document.getElementById("resources")
table.innerHTML = ""
resources.forEach(r => {
let ips = ""
if(Array.isArray(r.ips)){
ips = r.ips.map(ip =>
"<span class='ip'>" + (ip.type || "") + " " + ip.ip + "</span>"
).join("")
}
let domains = mappings
.filter(m => m.resource_id == r.id)
.map(m => "<span class='ip'>🌐 " + m.domain_name + "</span>")
.join("")
const tr = document.createElement("tr")
tr.innerHTML = `
<td>
<span style="cursor:pointer;color:#1e3a8a;font-weight:bold"
onclick='openServerDetail(${JSON.stringify(r)})'>
${r.name}
</span>
</td>
<td>${r.produkt || ""}</td>
<td><span class="provider">${r.provider || ""}</span></td>
<td id="status-${r.id}">...</td>
<td>
<div class="system">
${r.cpu ? r.cpu + " CPU" : ""}
${r.ram ? " • " + r.ram + " RAM" : ""}
${r.disk ? " • " + r.disk : ""}
${r.os ? " • " + r.os : ""}
</div>
</td>
<td>${domains}</td>
<td>
${ips}
<br>
<button onclick="openIPManager(${r.id})">IPs</button>
</td>
<td>
<button onclick='openEdit(${JSON.stringify(r)})'>Edit</button>
<button onclick="deleteResource(${r.id})">Delete</button>
</td>
`
table.appendChild(tr)
checkServerStatus(r)
})
}
window.saveResource = async function(){
const id = document.getElementById("resource_id").value
let kostenMonat=document.getElementById("kosten_monat").value
let kostenJahr=document.getElementById("kosten_jahr").value
if(kostenMonat) kostenMonat=kostenMonat.replace(",",".")
if(kostenJahr) kostenJahr=kostenJahr.replace(",",".")
const data={
name:name.value,
produkt:produkt.value,
provider:provider.value,
art:art.value,
cpu:cpu.value,
ram:ram.value,
disk:disk.value,
os:os.value,
kosten_monat:kostenMonat || null,
kosten_jahr:kostenJahr || null,
providername:providername.value,
ipv6_net:ipv6_net.value,
bestelldatum:bestelldatum.value || null,
kuendbar_ab:kuendbar_ab.value || null,
kuendigungsdatum:kuendigungsdatum.value || null,
status:status.value,
bemerkung:bemerkung.value
}
if(id){
await api(API+"/resources/"+id,{method:"PUT",headers:{'Content-Type':'application/json'},body:JSON.stringify(data)})
}else{
await api(API+"/resources",{method:"POST",headers:{'Content-Type':'application/json'},body:JSON.stringify(data)})
}
closeModal()
loadResources()
}
window.deleteResource = async function(id){
if(!confirm("delete resource?")) return
await api(API+"/resources/"+id,{method:"DELETE"})
loadResources()
}
window.checkServerStatus = async function(resource){
if(!Array.isArray(resource.ips)) return
let ip = resource.ips.find(i => i.type?.includes("public")) || resource.ips[0]
if(!ip) return
try{
const res = await fetch(API+"/ping/"+ip.ip)
const data = await res.json()
const el = document.getElementById("status-"+resource.id)
if(el){
el.innerHTML = data.status === "online" ? "🟢" : "🔴"
}
}catch(e){
console.log(e)
}
}
window.loadCancelled = async function(){
const data = await api(API + "/resources/cancelled")
const table = document.getElementById("cancelled")
table.innerHTML = ""
data.forEach(r => {
const tr = document.createElement("tr")
tr.innerHTML = `
<td>${r.name}</td>
<td>${r.produkt || ""}</td>
<td>${r.provider || ""}</td>
<td>${r.cpu || ""}</td>
<td>${r.ram || ""}</td>
<td>${r.disk || ""}</td>
<td>${r.status || ""}</td>
`
table.appendChild(tr)
})
}