initial prototyping

This commit is contained in:
Arthur Lu 2024-10-18 04:28:31 +00:00
commit 85fa74c83b
8 changed files with 193 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
**/go.sum
**/config.json
dist/*

9
Makefile Normal file
View File

@ -0,0 +1,9 @@
build: clean
CGO_ENABLED=0 go build -ldflags="-s -w" -o dist/ .
test: clean
go run .
clean:
go clean
rm -f dist/*

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
}

47
go.mod Normal file
View File

@ -0,0 +1,47 @@
module proxmoxaas-fabric
go 1.22.4
require (
github.com/gin-contrib/sessions v1.0.1
github.com/gin-gonic/gin v1.10.0
github.com/luthermonson/go-proxmox v0.1.1
)
require (
github.com/buger/goterm v1.0.4 // indirect
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/diskfs/go-diskfs v1.2.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/gorilla/context v1.1.2 // indirect
github.com/gorilla/securecookie v1.1.2 // indirect
github.com/gorilla/sessions v1.2.2 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/jinzhu/copier v0.3.4 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/magefile/mage v1.14.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/djherbis/times.v1 v1.2.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

9
proxmoxaas-fabric.go Normal file
View File

@ -0,0 +1,9 @@
package main
import (
app "proxmoxaas-fabric/app"
)
func main() {
app.Run()
}