initial prototyping

This commit is contained in:
2025-02-11 07:11:05 +00:00
commit 19550a78d4
8 changed files with 193 additions and 0 deletions

40
app/app.go Normal file
View File

@@ -0,0 +1,40 @@
package app
import (
"context"
"flag"
"log"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/luthermonson/go-proxmox"
)
const APIVersion string = "0.0.1"
var client *proxmox.Client = nil
func Run() {
configPath := flag.String("config", "config.json", "path to config.json file")
flag.Parse()
config := GetConfig(*configPath)
log.Println("Initialized config from " + *configPath)
client = NewClient(config.PVE.Token.ID, config.PVE.Token.Secret)
router := gin.Default()
router.GET("/version", func(c *gin.Context) {
PVEVersion, err := client.Version(context.Background())
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})
}
})
router.Run("0.0.0.0:" + strconv.Itoa(config.ListenPort))
}

18
app/proxmox.go Normal file
View File

@@ -0,0 +1,18 @@
package app
import (
"net/http"
"github.com/luthermonson/go-proxmox"
)
func NewClient(tokenID string, secret string) *proxmox.Client {
HTTPClient := http.Client{}
client := proxmox.NewClient("https://pve.tronnet.net/api2/json",
proxmox.WithHTTPClient(&HTTPClient),
proxmox.WithAPIToken(tokenID, secret),
)
return client
}

37
app/types.go Normal file
View File

@@ -0,0 +1,37 @@
package app
type PVEBus int
const (
IDE PVEBus = iota
SATA
)
type PVEDrive struct{}
type PVEDisk struct{}
type PVENet struct{}
type PVEDevice struct{}
type QEMUInstance struct {
Name string
Proctype string
Cores int16
Memory int32
Drive map[int]PVEDrive
Disk map[int]PVEDisk
Net map[int]PVENet
Device map[int]PVEDevice
}
type LXCInstance struct {
Name string
Cores int16
Memory int32
Swap int32
RootDisk PVEDrive
MP map[int]PVEDisk
Net map[int]PVENet
}

30
app/utils.go Normal file
View File

@@ -0,0 +1,30 @@
package app
import (
"encoding/json"
"log"
"os"
)
type Config struct {
ListenPort int `json:"listenPort"`
PVE struct {
Token struct {
ID string `json:"id"`
Secret string `json:"secret"`
}
}
}
func GetConfig(configPath string) Config {
content, err := os.ReadFile(configPath)
if err != nil {
log.Fatal("Error when opening config file: ", err)
}
var config Config
err = json.Unmarshal(content, &config)
if err != nil {
log.Fatal("Error during parsing config file: ", err)
}
return config
}