From b4e45940a59642abb756af8bc2e82297bc6c4d41 Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Sun, 19 Apr 2026 17:30:37 +0000 Subject: [PATCH] add proxmoxaas-common-lib dependency, update types and functions to use proxmoxaas-common-lib --- .gitmodules | 3 ++ app/common/types.go | 101 +++++++----------------------------------- app/common/utils.go | 46 ------------------- app/main.go | 11 ++--- go.mod | 3 ++ proxmoxaas-common-lib | 1 + 6 files changed, 28 insertions(+), 137 deletions(-) create mode 100644 .gitmodules delete mode 100644 app/common/utils.go create mode 160000 proxmoxaas-common-lib diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..69f71d5 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "proxmoxaas-common-lib"] + path = proxmoxaas-common-lib + url = https://git.tronnet.net/tronnet/proxmoxaas-common-lib diff --git a/app/common/types.go b/app/common/types.go index 1288ee6..b7017b9 100644 --- a/app/common/types.go +++ b/app/common/types.go @@ -1,5 +1,7 @@ package app +import paas "proxmoxaas-common-lib" + type Backend interface { NewPool(poolname string) (int, error) DelPool(poolname string) (int, error) @@ -13,89 +15,16 @@ type Backend interface { DelUserFromGroup(username Username, groupname Groupname) (int, error) } -type Pool struct { - PoolID string `json:"poolid"` - Path string `json:"-"` // typically /pool/poolid from proxmox, only used internally - Groups []Group `json:"groups"` - Resources map[string]any `json:"resources"` - Templates Templates `json:"templates"` - AllowedNodes map[string]bool `json:"nodes-allowed"` - VMIDRange VMID `json:"vmid-allowed"` - Backups Backups `json:"backups-allowed"` // measured in numbers -} - -// proxmox typically formats as gid-realm for non pve realms -// proxmox realms are formatted without realm values -// I assume that backends store groups by ID only and only proxmox will append the realm string -type Groupname struct { - GroupID string `json:"gid"` - Realm string `json:"realm"` -} - -type Group struct { - Groupname Groupname `json:"groupname"` - Role string `json:"role"` // role in owner pool - Users []User `json:"users"` -} - -type Username struct { // ie userid@realm - UserID string `json:"uid"` - Realm string `json:"realm"` -} - -type User struct { - Username Username `json:"username"` - CN string `json:"cn"` // aka first name - SN string `json:"sn"` // aka last name - Mail string `json:"mail"` - Password string `json:"password"` // only used for POST requests -} - -type VMID struct { - Min int `json:"min"` - MAx int `json:"max"` -} - -type Backups struct { - Max int `json:"max"` -} - -type Templates struct { - Instances struct { - LXC map[string]ResourceTemplate `json:"lxc"` - QEMU map[string]ResourceTemplate `json:"qemu"` - } `json:"instances"` -} - -type SimpleResource struct { - Limits struct { - Global SimpleLimit `json:"global"` - Nodes map[string]SimpleLimit `json:"nodes"` - } `json:"limits"` -} - -type SimpleLimit struct { - Max int `json:"max"` -} - -type MatchResource struct { - Limits struct { - Global []MatchLimit `json:"global"` - Nodes map[string][]MatchLimit `json:"nodes"` - } `json:"limits"` -} - -type MatchLimit struct { - Match string `json:"match"` - Name string `json:"name"` - Max int `json:"max"` -} - -type ResourceTemplate struct { - Value string `json:"value"` - Resource struct { - Enabled bool `json:"enabled"` - Name string `json:"name"` - Amount int `json:"amount"` - } `json:"resource"` -} +type Pool = paas.Pool +type Groupname = paas.Groupname +type Group = paas.Group +type Username = paas.Username +type User = paas.User +type VMID = paas.VMID +type Backups = paas.Backups +type Templates = paas.Templates +type SimpleResource = paas.SimpleResource +type SimpleLimit = paas.SimpleLimit +type MatchResource = paas.MatchResource +type MatchLimit = paas.MatchLimit +type ResourceTemplate = paas.ResourceTemplate diff --git a/app/common/utils.go b/app/common/utils.go deleted file mode 100644 index 999f357..0000000 --- a/app/common/utils.go +++ /dev/null @@ -1,46 +0,0 @@ -package app - -import ( - "fmt" - "strings" -) - -// returns an error if the groupname format was not correct -// TODO: handle group names with x-y format where y is not a registered realm -// TODO: handle group names with x-y-z-... format -func ParseGroupname(groupname string) (Groupname, error) { - g := Groupname{} - x := strings.Split(groupname, "-") - if len(x) == 1 { - g.GroupID = groupname - g.Realm = "pve" - return g, nil - } else if len(x) == 2 { - g.GroupID = x[0] - g.Realm = x[1] - return g, nil - } else { - return g, fmt.Errorf("groupid did not follow the format or -") - } -} - -// returns an error if the username format was not correct -func ParseUsername(username string) (Username, error) { - u := Username{} - x := strings.Split(username, "@") - if len(x) == 2 { - u.UserID = x[0] - u.Realm = x[1] - return u, nil - } else { - return u, fmt.Errorf("userid did not follow the format @") - } -} - -func (g Groupname) ToString() string { - return fmt.Sprintf("%s-%s", g.GroupID, g.Realm) -} - -func (u Username) ToString() string { - return fmt.Sprintf("%s-%s", u.UserID, u.Realm) -} diff --git a/app/main.go b/app/main.go index fd9be37..f128857 100644 --- a/app/main.go +++ b/app/main.go @@ -9,6 +9,7 @@ import ( "net/http" "strconv" + paas "proxmoxaas-common-lib" common "user-manager-api/app/common" ldap "user-manager-api/app/ldap" pve "user-manager-api/app/pve" @@ -58,7 +59,7 @@ func Run(configPath *string) { } // attempt to parse username - body.Username, err = common.ParseUsername(body.UsernameRaw) + body.Username, err = paas.ParseUsername(body.UsernameRaw) if err != nil { // username format incorrect c.JSON(http.StatusBadRequest, gin.H{"auth": false, "error": err.Error()}) return @@ -167,7 +168,7 @@ func Run(configPath *string) { c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Errorf("Missing required path parameter groupid")}) return } - groupname, err := common.ParseGroupname(groupid) + groupname, err := paas.ParseGroupname(groupid) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err}) return @@ -193,7 +194,7 @@ func Run(configPath *string) { c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Errorf("Missing required path parameter groupid")}) return } - groupname, err := common.ParseGroupname(groupid) + groupname, err := paas.ParseGroupname(groupid) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err}) return @@ -225,7 +226,7 @@ func Run(configPath *string) { return } - groupname, err := common.ParseGroupname(groupid) + groupname, err := paas.ParseGroupname(groupid) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err}) return @@ -257,7 +258,7 @@ func Run(configPath *string) { return } - groupname, err := common.ParseGroupname(groupid) + groupname, err := paas.ParseGroupname(groupid) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err}) return diff --git a/go.mod b/go.mod index 720dcc9..7852158 100644 --- a/go.mod +++ b/go.mod @@ -8,8 +8,11 @@ require ( github.com/go-ldap/ldap/v3 v3.4.13 github.com/luthermonson/go-proxmox v0.4.1 github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d + proxmoxaas-common-lib v0.0.0 ) +replace proxmoxaas-common-lib => ./proxmoxaas-common-lib + require ( github.com/Azure/go-ntlmssp v0.1.0 // indirect github.com/buger/goterm v1.0.4 // indirect diff --git a/proxmoxaas-common-lib b/proxmoxaas-common-lib new file mode 160000 index 0000000..eeaf366 --- /dev/null +++ b/proxmoxaas-common-lib @@ -0,0 +1 @@ +Subproject commit eeaf366e28394ecae09c84c950a4e4481a74687e