fix proxmox return codes
tronnet/workflows: Static Analysis -- Golang / static-go (push) Successful in 59s

This commit is contained in:
alu
2026-08-18 23:18:52 +00:00
parent 88d7e97b2a
commit 670f7d4999
3 changed files with 20 additions and 10 deletions
+2 -1
View File
@@ -1,4 +1,5 @@
go.sum go.sum
localdb.json localdb.json
dist/* dist/*
**/config.json **/config.json
.vscode/*
+17 -8
View File
@@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"slices" "slices"
"strings"
common "access-manager-api/app/common" common "access-manager-api/app/common"
@@ -17,6 +18,14 @@ type ProxmoxClient struct {
client *proxmox.Client client *proxmox.Client
} }
func IsProxmoxNotFound(err error) bool {
if err != nil {
// for whatever reason proxmox returns 500 for user/group/pool not found
return proxmox.IsNotFound(err) || strings.Contains(err.Error(), "no such user") || strings.Contains(err.Error(), "does not exist")
}
return false
}
// creates a new client binding with associated permissions // creates a new client binding with associated permissions
func NewClientFromCredentials(config common.PVEConfig, username common.Username, password string) (*ProxmoxClient, int, error) { func NewClientFromCredentials(config common.PVEConfig, username common.Username, password string) (*ProxmoxClient, int, error) {
HTTPClient := http.Client{ HTTPClient := http.Client{
@@ -84,7 +93,7 @@ func (pve ProxmoxClient) GetPool(poolname string) (common.Pool, []string, int, e
members := []string{} members := []string{}
pvepool, err := pve.client.Pool(context.Background(), poolname) pvepool, err := pve.client.Pool(context.Background(), poolname)
if proxmox.IsNotFound(err) { // errors if pool does not exist if IsProxmoxNotFound(err) { // errors if pool does not exist
return pool, members, http.StatusNotFound, err return pool, members, http.StatusNotFound, err
} else if err != nil { } else if err != nil {
return pool, members, http.StatusInternalServerError, err return pool, members, http.StatusInternalServerError, err
@@ -113,7 +122,7 @@ func (pve ProxmoxClient) DelPool(poolname string) (int, error) {
pvepool, err := pve.client.Pool(context.Background(), poolname) pvepool, err := pve.client.Pool(context.Background(), poolname)
if proxmox.IsNotAuthorized(err) { // not authorized to delete if proxmox.IsNotAuthorized(err) { // not authorized to delete
return http.StatusUnauthorized, err return http.StatusUnauthorized, err
} else if proxmox.IsNotFound(err) { // errors if pool does not exist } else if IsProxmoxNotFound(err) { // errors if pool does not exist
return http.StatusNotFound, err return http.StatusNotFound, err
} else if err != nil { } else if err != nil {
return http.StatusInternalServerError, err return http.StatusInternalServerError, err
@@ -145,7 +154,7 @@ func (pve ProxmoxClient) GetGroup(groupname common.Groupname) (common.Group, []s
group := common.Group{} group := common.Group{}
members := []string{} members := []string{}
pvegroup, err := pve.client.Group(context.Background(), groupname.ToString()) pvegroup, err := pve.client.Group(context.Background(), groupname.ToString())
if proxmox.IsNotFound(err) { // errors if pool does not exist if IsProxmoxNotFound(err) { // errors if pool does not exist
return group, members, http.StatusNotFound, err return group, members, http.StatusNotFound, err
} else if err != nil { } else if err != nil {
return group, members, http.StatusInternalServerError, err return group, members, http.StatusInternalServerError, err
@@ -162,7 +171,7 @@ func (pve ProxmoxClient) DelGroup(groupname common.Groupname) (int, error) {
pvegroup, err := pve.client.Group(context.Background(), groupname.GroupID) pvegroup, err := pve.client.Group(context.Background(), groupname.GroupID)
if proxmox.IsNotAuthorized(err) { if proxmox.IsNotAuthorized(err) {
return http.StatusUnauthorized, err return http.StatusUnauthorized, err
} else if proxmox.IsNotFound(err) { } else if IsProxmoxNotFound(err) {
return http.StatusNotFound, err return http.StatusNotFound, err
} else if err != nil { } else if err != nil {
return http.StatusInternalServerError, err return http.StatusInternalServerError, err
@@ -234,7 +243,7 @@ func (pve ProxmoxClient) NewUser(username common.Username, user common.User) (in
func (pve ProxmoxClient) GetUser(username common.Username) (common.User, int, error) { func (pve ProxmoxClient) GetUser(username common.Username) (common.User, int, error) {
user := common.User{} user := common.User{}
pveuser, err := pve.client.User(context.Background(), username.ToString()) pveuser, err := pve.client.User(context.Background(), username.ToString())
if proxmox.IsNotFound(err) { // errors if pool does not exist if IsProxmoxNotFound(err) { // errors if user does not exist
return user, http.StatusNotFound, err return user, http.StatusNotFound, err
} else if err != nil { } else if err != nil {
return user, http.StatusInternalServerError, err return user, http.StatusInternalServerError, err
@@ -251,7 +260,7 @@ func (pve ProxmoxClient) DelUser(username common.Username) (int, error) {
user, err := pve.client.User(context.Background(), username.ToString()) user, err := pve.client.User(context.Background(), username.ToString())
if proxmox.IsNotAuthorized(err) { if proxmox.IsNotAuthorized(err) {
return http.StatusUnauthorized, err return http.StatusUnauthorized, err
} else if proxmox.IsNotFound(err) { } else if IsProxmoxNotFound(err) {
return http.StatusNotFound, err return http.StatusNotFound, err
} else if err != nil { } else if err != nil {
return http.StatusInternalServerError, err return http.StatusInternalServerError, err
@@ -272,7 +281,7 @@ func (pve ProxmoxClient) AddUserToGroup(username common.Username, groupname comm
user, err := pve.client.User(context.Background(), username.ToString()) user, err := pve.client.User(context.Background(), username.ToString())
if proxmox.IsNotAuthorized(err) { if proxmox.IsNotAuthorized(err) {
return http.StatusUnauthorized, err return http.StatusUnauthorized, err
} else if proxmox.IsNotFound(err) { } else if IsProxmoxNotFound(err) {
return http.StatusNotFound, err return http.StatusNotFound, err
} else if err != nil { } else if err != nil {
return http.StatusInternalServerError, err return http.StatusInternalServerError, err
@@ -296,7 +305,7 @@ func (pve ProxmoxClient) DelUserFromGroup(username common.Username, groupname co
user, err := pve.client.User(context.Background(), username.ToString()) user, err := pve.client.User(context.Background(), username.ToString())
if proxmox.IsNotAuthorized(err) { if proxmox.IsNotAuthorized(err) {
return http.StatusUnauthorized, err return http.StatusUnauthorized, err
} else if proxmox.IsNotFound(err) { } else if IsProxmoxNotFound(err) {
return http.StatusNotFound, err return http.StatusNotFound, err
} else if err != nil { } else if err != nil {
return http.StatusInternalServerError, err return http.StatusInternalServerError, err