2024-10-18 04:28:31 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2024-12-27 19:59:44 +00:00
|
|
|
"encoding/gob"
|
2024-10-18 04:28:31 +00:00
|
|
|
"flag"
|
2024-12-27 19:59:44 +00:00
|
|
|
"fmt"
|
2024-10-18 04:28:31 +00:00
|
|
|
"log"
|
2025-01-14 04:44:18 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2024-10-18 04:28:31 +00:00
|
|
|
|
2025-01-14 04:44:18 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2024-10-18 04:28:31 +00:00
|
|
|
"github.com/luthermonson/go-proxmox"
|
|
|
|
)
|
|
|
|
|
2025-01-14 04:44:18 +00:00
|
|
|
const APIVersion string = "0.0.2"
|
2024-10-18 04:28:31 +00:00
|
|
|
|
2024-12-27 19:59:44 +00:00
|
|
|
var client ProxmoxClient
|
2024-10-18 04:28:31 +00:00
|
|
|
|
|
|
|
func Run() {
|
2024-12-27 19:59:44 +00:00
|
|
|
gob.Register(proxmox.Client{})
|
|
|
|
|
2024-10-18 04:28:31 +00:00
|
|
|
configPath := flag.String("config", "config.json", "path to config.json file")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
config := GetConfig(*configPath)
|
|
|
|
log.Println("Initialized config from " + *configPath)
|
|
|
|
|
2024-12-27 19:59:44 +00:00
|
|
|
token := fmt.Sprintf(`%s@%s!%s`, config.PVE.Token.USER, config.PVE.Token.REALM, config.PVE.Token.ID)
|
|
|
|
client = NewClient(token, config.PVE.Token.Secret)
|
2024-10-18 04:28:31 +00:00
|
|
|
|
2025-01-14 04:44:18 +00:00
|
|
|
router := gin.Default()
|
2025-01-08 22:42:17 +00:00
|
|
|
|
|
|
|
cluster := Cluster{}
|
|
|
|
cluster.Init(client)
|
|
|
|
cluster.Rebuild()
|
|
|
|
|
2025-01-14 04:44:18 +00:00
|
|
|
router.GET("/version", func(c *gin.Context) {
|
|
|
|
PVEVersion, err := client.Version()
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
|
|
} else {
|
|
|
|
c.JSON(http.StatusOK, gin.H{"api-version": APIVersion, "pve-version": PVEVersion})
|
|
|
|
}
|
|
|
|
})
|
2025-01-08 22:42:17 +00:00
|
|
|
|
2025-01-14 04:44:18 +00:00
|
|
|
router.GET("/nodes/:node", func(c *gin.Context) {
|
|
|
|
node := c.Param("node")
|
|
|
|
Host, ok := cluster.Hosts[node]
|
|
|
|
if !ok {
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("%s not found in cluster", node)})
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
c.JSON(http.StatusOK, gin.H{"node": Host})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
2025-01-08 22:42:17 +00:00
|
|
|
|
2025-01-14 04:44:18 +00:00
|
|
|
router.GET("/nodes/:node/instances/:instance", func(c *gin.Context) {
|
|
|
|
host := c.Param("node")
|
|
|
|
vmid, err := strconv.ParseUint(c.Param("instance"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("%s could not be converted to vmid (uint)", c.Param("instance"))})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
Node, ok := cluster.Hosts[host]
|
|
|
|
if !ok {
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("vmid %s not found in cluster", host)})
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
Instance, ok := Node.Instances[uint(vmid)]
|
|
|
|
if !ok {
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("%d not found in %s", vmid, host)})
|
|
|
|
return
|
2025-01-08 22:42:17 +00:00
|
|
|
} else {
|
2025-01-14 04:44:18 +00:00
|
|
|
c.JSON(http.StatusOK, gin.H{"instance": Instance})
|
|
|
|
return
|
2025-01-08 22:42:17 +00:00
|
|
|
}
|
2025-01-14 04:44:18 +00:00
|
|
|
}
|
|
|
|
})
|
2025-01-08 22:42:17 +00:00
|
|
|
|
2025-01-14 04:44:18 +00:00
|
|
|
router.Run("0.0.0.0:" + strconv.Itoa(config.ListenPort))
|
2024-10-18 04:28:31 +00:00
|
|
|
}
|