From 670f7d4999b3ad1ab5d9e4b7171422061c47d011 Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Tue, 18 Aug 2026 23:18:52 +0000 Subject: [PATCH] fix proxmox return codes --- .gitignore | 3 ++- app/pve/pve.go | 25 +++++++++++++++++-------- proxmoxaas-common-lib | 2 +- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 0e06f13..72a3ce7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ go.sum localdb.json dist/* -**/config.json \ No newline at end of file +**/config.json +.vscode/* \ No newline at end of file diff --git a/app/pve/pve.go b/app/pve/pve.go index e70002f..1167136 100644 --- a/app/pve/pve.go +++ b/app/pve/pve.go @@ -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 diff --git a/proxmoxaas-common-lib b/proxmoxaas-common-lib index f4deeb1..8284ffb 160000 --- a/proxmoxaas-common-lib +++ b/proxmoxaas-common-lib @@ -1 +1 @@ -Subproject commit f4deeb1ab65bfbaf07c0286b5d3b7915fa129f21 +Subproject commit 8284ffbc43a3be694652399942615f35784df4ef