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
localdb.json
dist/*
**/config.json
**/config.json
.vscode/*
+17 -8
View File
@@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"slices"
"strings"
common "access-manager-api/app/common"
@@ -17,6 +18,14 @@ type ProxmoxClient struct {
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
func NewClientFromCredentials(config common.PVEConfig, username common.Username, password string) (*ProxmoxClient, int, error) {
HTTPClient := http.Client{
@@ -84,7 +93,7 @@ func (pve ProxmoxClient) GetPool(poolname string) (common.Pool, []string, int, e
members := []string{}
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
} else if err != nil {
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)
if proxmox.IsNotAuthorized(err) { // not authorized to delete
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
} else if err != nil {
return http.StatusInternalServerError, err
@@ -145,7 +154,7 @@ func (pve ProxmoxClient) GetGroup(groupname common.Groupname) (common.Group, []s
group := common.Group{}
members := []string{}
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
} else if err != nil {
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)
if proxmox.IsNotAuthorized(err) {
return http.StatusUnauthorized, err
} else if proxmox.IsNotFound(err) {
} else if IsProxmoxNotFound(err) {
return http.StatusNotFound, err
} else if err != nil {
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) {
user := common.User{}
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
} else if err != nil {
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())
if proxmox.IsNotAuthorized(err) {
return http.StatusUnauthorized, err
} else if proxmox.IsNotFound(err) {
} else if IsProxmoxNotFound(err) {
return http.StatusNotFound, err
} else if err != nil {
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())
if proxmox.IsNotAuthorized(err) {
return http.StatusUnauthorized, err
} else if proxmox.IsNotFound(err) {
} else if IsProxmoxNotFound(err) {
return http.StatusNotFound, err
} else if err != nil {
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())
if proxmox.IsNotAuthorized(err) {
return http.StatusUnauthorized, err
} else if proxmox.IsNotFound(err) {
} else if IsProxmoxNotFound(err) {
return http.StatusNotFound, err
} else if err != nil {
return http.StatusInternalServerError, err