commit 85fa74c83b06f873b0987a7cf5de28fef8d8594b Author: Arthur Lu Date: Fri Oct 18 04:28:31 2024 +0000 initial prototyping diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4290158 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +**/go.sum +**/config.json +dist/* \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..494cef7 --- /dev/null +++ b/Makefile @@ -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/* \ No newline at end of file diff --git a/app/app.go b/app/app.go new file mode 100644 index 0000000..8b7beb7 --- /dev/null +++ b/app/app.go @@ -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)) + +} diff --git a/app/proxmox.go b/app/proxmox.go new file mode 100644 index 0000000..70bad7c --- /dev/null +++ b/app/proxmox.go @@ -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 +} diff --git a/app/types.go b/app/types.go new file mode 100644 index 0000000..b015ff5 --- /dev/null +++ b/app/types.go @@ -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 +} diff --git a/app/utils.go b/app/utils.go new file mode 100644 index 0000000..d188e80 --- /dev/null +++ b/app/utils.go @@ -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 +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..9751af1 --- /dev/null +++ b/go.mod @@ -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 +) diff --git a/proxmoxaas-fabric.go b/proxmoxaas-fabric.go new file mode 100644 index 0000000..f1755c8 --- /dev/null +++ b/proxmoxaas-fabric.go @@ -0,0 +1,9 @@ +package main + +import ( + app "proxmoxaas-fabric/app" +) + +func main() { + app.Run() +}