move some util definitions and code to common lib

This commit is contained in:
2026-06-08 20:28:39 +00:00
parent 00fa5f3152
commit 10ef24e76b
9 changed files with 74 additions and 89 deletions
+6 -8
View File
@@ -96,7 +96,7 @@ var Green = color.RGB{
}
func HandleGETAccount(c *gin.Context) {
auth, err := common.GetAuth(c)
auth, err := common.GetAuthFromRequest(c)
if err == nil {
account, err := GetUser(auth)
@@ -186,11 +186,10 @@ func HandleGETAccount(c *gin.Context) {
}
}
func GetUser(auth common.Auth) (Account, error) {
func GetUser(auth paas.Auth) (Account, error) {
account := Account{}
ctx := common.GetRequestContextFromCookies(auth)
body := map[string]any{}
res, code, err := common.RequestGetAPI(fmt.Sprintf("/access/users/%s", auth.Username), ctx, &body)
res, code, err := common.RequestGetAPI(fmt.Sprintf("/access/users/%s", auth.Username), &auth, &body)
if err != nil {
return account, err
}
@@ -201,13 +200,12 @@ func GetUser(auth common.Auth) (Account, error) {
return account, err
}
func GetUserPools(auth common.Auth) (map[string]paas.Pool, error) {
func GetUserPools(auth paas.Auth) (map[string]paas.Pool, error) {
pools := map[string]paas.Pool{}
// get all pools
ctx := common.GetRequestContextFromCookies(auth)
body := map[string]any{}
res, code, err := common.RequestGetAPI("/access/pools", ctx, &body)
res, code, err := common.RequestGetAPI("/access/pools", &auth, &body)
if err != nil {
return pools, err
}
@@ -222,7 +220,7 @@ func GetUserPools(auth common.Auth) (map[string]paas.Pool, error) {
// get global config for resource type metadata
body = map[string]any{}
// get resource meta data
res, code, err = common.RequestGetAPI("/global/config/resources", ctx, &body)
res, code, err = common.RequestGetAPI("/global/config/resources", &auth, &body)
if err != nil {
return pools, err
}
+8 -8
View File
@@ -3,6 +3,7 @@ package routes
import (
"fmt"
"net/http"
paas "proxmoxaas-common-lib"
"proxmoxaas-dashboard/app/common"
"time"
@@ -20,9 +21,9 @@ type InstanceBackup struct {
}
func HandleGETBackups(c *gin.Context) {
auth, err := common.GetAuth(c)
auth, err := common.GetAuthFromRequest(c)
if err == nil {
vm_path, err := common.ExtractVMPath(c)
vm_path, err := common.GetInstancePathFromRequest(c)
if err != nil {
common.HandleNonFatalError(c, err)
return
@@ -50,9 +51,9 @@ func HandleGETBackups(c *gin.Context) {
}
func HandleGETBackupsFragment(c *gin.Context) {
auth, err := common.GetAuth(c)
auth, err := common.GetAuthFromRequest(c)
if err == nil { // user should be authed, try to return index with population
vm_path, err := common.ExtractVMPath(c)
vm_path, err := common.GetInstancePathFromRequest(c)
if err != nil {
common.HandleNonFatalError(c, err)
return
@@ -77,12 +78,11 @@ func HandleGETBackupsFragment(c *gin.Context) {
}
}
func GetInstanceBackups(vm common.VMPath, auth common.Auth) ([]InstanceBackup, error) {
func GetInstanceBackups(vm paas.InstancePath, auth paas.Auth) ([]InstanceBackup, error) {
backups := []InstanceBackup{}
path := fmt.Sprintf("/cluster/%s/%s/%s/backup", vm.Node, vm.Type, vm.VMID)
ctx := common.GetRequestContextFromCookies(auth)
path := fmt.Sprintf("/cluster/%s/%s/%d/backup", vm.NodeName, string(vm.InstanceType), uint64(vm.InstanceID))
body := []any{}
res, code, err := common.RequestGetAPI(path, ctx, &body)
res, code, err := common.RequestGetAPI(path, &auth, &body)
if err != nil {
return backups, err
}
+20 -22
View File
@@ -34,9 +34,9 @@ type PoolConfig struct {
}
func HandleGETConfig(c *gin.Context) {
auth, err := common.GetAuth(c)
auth, err := common.GetAuthFromRequest(c)
if err == nil {
vm_path, err := common.ExtractVMPath(c)
vm_path, err := common.GetInstancePathFromRequest(c)
if err != nil {
common.HandleNonFatalError(c, err)
return
@@ -70,9 +70,9 @@ func HandleGETConfig(c *gin.Context) {
}
func HandleGETConfigVolumesFragment(c *gin.Context) {
auth, err := common.GetAuth(c)
auth, err := common.GetAuthFromRequest(c)
if err == nil {
vm_path, err := common.ExtractVMPath(c)
vm_path, err := common.GetInstancePathFromRequest(c)
if err != nil {
common.HandleNonFatalError(c, err)
return
@@ -98,9 +98,9 @@ func HandleGETConfigVolumesFragment(c *gin.Context) {
}
func HandleGETConfigNetsFragment(c *gin.Context) {
auth, err := common.GetAuth(c)
auth, err := common.GetAuthFromRequest(c)
if err == nil {
vm_path, err := common.ExtractVMPath(c)
vm_path, err := common.GetInstancePathFromRequest(c)
if err != nil {
common.HandleNonFatalError(c, err)
return
@@ -126,9 +126,9 @@ func HandleGETConfigNetsFragment(c *gin.Context) {
}
func HandleGETConfigDevicesFragment(c *gin.Context) {
auth, err := common.GetAuth(c)
auth, err := common.GetAuthFromRequest(c)
if err == nil {
vm_path, err := common.ExtractVMPath(c)
vm_path, err := common.GetInstancePathFromRequest(c)
if err != nil {
common.HandleNonFatalError(c, err)
return
@@ -154,9 +154,9 @@ func HandleGETConfigDevicesFragment(c *gin.Context) {
}
func HandleGETConfigBootFragment(c *gin.Context) {
auth, err := common.GetAuth(c)
auth, err := common.GetAuthFromRequest(c)
if err == nil {
vm_path, err := common.ExtractVMPath(c)
vm_path, err := common.GetInstancePathFromRequest(c)
if err != nil {
common.HandleNonFatalError(c, err)
return
@@ -181,12 +181,11 @@ func HandleGETConfigBootFragment(c *gin.Context) {
}
}
func GetInstanceConfig(vm common.VMPath, auth common.Auth) (InstanceConfig, error) {
func GetInstanceConfig(vm paas.InstancePath, auth paas.Auth) (InstanceConfig, error) {
config := InstanceConfig{}
path := fmt.Sprintf("/cluster/%s/%s/%s", vm.Node, vm.Type, vm.VMID)
ctx := common.GetRequestContextFromCookies(auth)
path := fmt.Sprintf("/cluster/%s/%s/%d/", vm.NodeName, string(vm.InstanceType), uint64(vm.InstanceID))
body := map[string]any{}
res, code, err := common.RequestGetAPI(path, ctx, &body)
res, code, err := common.RequestGetAPI(path, &auth, &body)
if err != nil {
return config, err
}
@@ -205,17 +204,16 @@ func GetInstanceConfig(vm common.VMPath, auth common.Auth) (InstanceConfig, erro
return config, nil
}
func GetCPUTypes(vm common.VMPath, pool string, auth common.Auth) (common.Select, error) {
func GetCPUTypes(vm paas.InstancePath, pool string, auth paas.Auth) (common.Select, error) {
cputypes := common.Select{
ID: "proctype",
Required: true,
}
// get global resource config
ctx := common.GetRequestContextFromCookies(auth)
body := map[string]any{}
path := "/global/config/resources"
res, code, err := common.RequestGetAPI(path, ctx, &body)
res, code, err := common.RequestGetAPI(path, &auth, &body)
if err != nil {
return cputypes, err
}
@@ -231,7 +229,7 @@ func GetCPUTypes(vm common.VMPath, pool string, auth common.Auth) (common.Select
// get pool resource config
body = map[string]any{}
path = fmt.Sprintf("/access/pools/%s", pool)
res, code, err = common.RequestGetAPI(path, ctx, &body)
res, code, err = common.RequestGetAPI(path, &auth, &body)
if err != nil {
return cputypes, err
}
@@ -246,8 +244,8 @@ func GetCPUTypes(vm common.VMPath, pool string, auth common.Auth) (common.Select
// use node specific rules if present, otherwise use global rules
var userCPU []paas.MatchLimit
if _, ok := poolCPUConfig.CPU.Nodes[vm.Node]; ok {
userCPU = poolCPUConfig.CPU.Nodes[vm.Node]
if _, ok := poolCPUConfig.CPU.Nodes[vm.NodeName]; ok {
userCPU = poolCPUConfig.CPU.Nodes[vm.NodeName]
} else {
userCPU = poolCPUConfig.CPU.Global
}
@@ -262,8 +260,8 @@ func GetCPUTypes(vm common.VMPath, pool string, auth common.Auth) (common.Select
} else { // cpu is a blacklist
// get the supported cpu types from the node
body = map[string]any{}
path = fmt.Sprintf("/proxmox/nodes/%s/capabilities/qemu/cpu", vm.Node)
res, code, err = common.RequestGetAPI(path, ctx, &body)
path = fmt.Sprintf("/proxmox/nodes/%s/capabilities/qemu/cpu", vm.NodeName)
res, code, err = common.RequestGetAPI(path, &auth, &body)
if err != nil {
return cputypes, err
}
+7 -7
View File
@@ -3,6 +3,7 @@ package routes
import (
"fmt"
"net/http"
paas "proxmoxaas-common-lib"
"proxmoxaas-dashboard/app/common"
"strconv"
@@ -45,7 +46,7 @@ type InstanceStatus struct {
}
func HandleGETIndex(c *gin.Context) {
auth, err := common.GetAuth(c)
auth, err := common.GetAuthFromRequest(c)
if err == nil { // user should be authed, try to return index with population
instances, _, err := GetClusterResources(auth)
if err != nil {
@@ -64,7 +65,7 @@ func HandleGETIndex(c *gin.Context) {
}
func HandleGETInstancesFragment(c *gin.Context) {
auth, err := common.GetAuth(c)
auth, err := common.GetAuthFromRequest(c)
if err == nil { // user should be authed, try to return index with population
instances, _, err := GetClusterResources(auth)
if err != nil {
@@ -86,10 +87,9 @@ func HandleGETInstancesFragment(c *gin.Context) {
}
func GetClusterResources(auth common.Auth) (map[uint]InstanceCard, map[string]Node, error) {
ctx := common.GetRequestContextFromCookies(auth)
func GetClusterResources(auth paas.Auth) (map[uint]InstanceCard, map[string]Node, error) {
body := []any{}
res, code, err := common.RequestGetAPI("/proxmox/cluster/resources", ctx, &body)
res, code, err := common.RequestGetAPI("/proxmox/cluster/resources", &auth, &body)
if err != nil {
return nil, nil, err
}
@@ -140,7 +140,7 @@ func GetClusterResources(auth common.Auth) (map[uint]InstanceCard, map[string]No
}
body = []any{}
res, code, err = common.RequestGetAPI("/proxmox/cluster/tasks", ctx, &body)
res, code, err = common.RequestGetAPI("/proxmox/cluster/tasks", &auth, &body)
if err != nil {
return nil, nil, err
}
@@ -198,7 +198,7 @@ func GetClusterResources(auth common.Auth) (map[uint]InstanceCard, map[string]No
instance := instances[vmid]
path := fmt.Sprintf("/proxmox/nodes/%s/%s/%d/status/current", instance.Node, instance.Type, instance.VMID)
body := map[string]any{}
res, code, err := common.RequestGetAPI(path, ctx, &body)
res, code, err := common.RequestGetAPI(path, &auth, &body)
if err != nil {
return nil, nil, err
}
+1 -4
View File
@@ -24,11 +24,8 @@ type Realm struct {
func GetLoginRealms() ([]Realm, error) {
realms := []Realm{}
ctx := common.RequestContext{
Cookies: nil,
}
body := []any{}
res, code, err := common.RequestGetAPI("/proxmox/access/domains", ctx, &body)
res, code, err := common.RequestGetAPI("/proxmox/access/domains", nil, &body)
if err != nil {
return realms, err
}
+1 -1
View File
@@ -8,7 +8,7 @@ import (
)
func HandleGETSettings(c *gin.Context) {
_, err := common.GetAuth(c)
_, err := common.GetAuthFromRequest(c)
if err == nil {
c.HTML(http.StatusOK, "html/settings.html", gin.H{
"global": common.Global,