[GH-ISSUE #196] Customizing client getter for objects #53

Open
opened 2026-03-03 15:29:54 +03:00 by kerem · 0 comments
Owner

Originally created by @mhkarimi1383 on GitHub (May 31, 2025).
Original GitHub issue: https://github.com/luthermonson/go-proxmox/issues/196

Hi

We are using our custom client getter in our project like so

import (
	"context"
	"crypto/tls"
	"errors"
	"net/http"
	"pve-vncproxy/types/configuration"

	"github.com/luthermonson/go-proxmox"
)

var client *proxmox.Client

func Init() {
	insecureHTTPClient := http.Client{
		Transport: &http.Transport{
			TLSClientConfig: &tls.Config{
				InsecureSkipVerify: true,
			},
		},
	}

	credentials := proxmox.Credentials{
		Username: configuration.CurrentConfig.ProxmoxUsername,
		Password: configuration.CurrentConfig.ProxmoxPassword,
	}
	client = proxmox.NewClient(configuration.CurrentConfig.ProxmoxAPIBase,
		proxmox.WithHTTPClient(&insecureHTTPClient),
		proxmox.WithCredentials(&credentials),
	)
}

const maxRetries = 3

func getClient(ctx context.Context) (*proxmox.Client, error) {
	return getClientWithRetry(ctx, 0)
}

func getClientWithRetry(ctx context.Context, retry int) (*proxmox.Client, error) {
	if client == nil {
		Init()
	}
	if _, err := client.Cluster(ctx); err != nil {
		if retry < maxRetries {
			Init()
			return getClientWithRetry(ctx, retry+1)
		}
		return nil, errors.New("failed to get Proxmox client after multiple retries")
	}
	return client, nil
}

Since we are getting

Failed to get Cluster Resources: not authorized to access endpoint

After keeping client for a long time

But for VirtualMachine instances we are unable to use a custom client

Originally created by @mhkarimi1383 on GitHub (May 31, 2025). Original GitHub issue: https://github.com/luthermonson/go-proxmox/issues/196 Hi We are using our custom client getter in our project like so ```go import ( "context" "crypto/tls" "errors" "net/http" "pve-vncproxy/types/configuration" "github.com/luthermonson/go-proxmox" ) var client *proxmox.Client func Init() { insecureHTTPClient := http.Client{ Transport: &http.Transport{ TLSClientConfig: &tls.Config{ InsecureSkipVerify: true, }, }, } credentials := proxmox.Credentials{ Username: configuration.CurrentConfig.ProxmoxUsername, Password: configuration.CurrentConfig.ProxmoxPassword, } client = proxmox.NewClient(configuration.CurrentConfig.ProxmoxAPIBase, proxmox.WithHTTPClient(&insecureHTTPClient), proxmox.WithCredentials(&credentials), ) } const maxRetries = 3 func getClient(ctx context.Context) (*proxmox.Client, error) { return getClientWithRetry(ctx, 0) } func getClientWithRetry(ctx context.Context, retry int) (*proxmox.Client, error) { if client == nil { Init() } if _, err := client.Cluster(ctx); err != nil { if retry < maxRetries { Init() return getClientWithRetry(ctx, retry+1) } return nil, errors.New("failed to get Proxmox client after multiple retries") } return client, nil } ``` Since we are getting `Failed to get Cluster Resources: not authorized to access endpoint` After keeping client for a long time But for VirtualMachine instances we are unable to use a custom client
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/go-proxmox#53
No description provided.