-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
112 lines (90 loc) · 2.49 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package proxmox
import (
"fmt"
"net/http"
"net/url"
"strings"
)
const (
defaultBaseURL = "https://localhost:8006/"
apiPath = "api2/json/"
)
// Client for the Proxmox API
type Client struct {
// HTTP retryable client for the API
client *http.Client
// Base URL for API requests. Defaults to https://localhost:8006/,
// but can be changed to any remote endpoint.
baseURL *url.URL
// tokenID is the identifier given for a Proxmox API token
tokenID string
// token is the token secret
token string
// Services for each resource in the Proxmox API
Nodes *NodeService
Cluster *ClusterService
}
// NewClient returns a new Proxmox API client
func NewClient(tokenID string, token string, options ...ClientOptionFunc) (*Client, error) {
if token == "" || tokenID == "" {
return nil, fmt.Errorf("can not create Proxmox API client without a token ID and token")
}
c := &Client{
tokenID: tokenID,
token: token,
}
// Set the client default fields
_ = c.setBaseURL(defaultBaseURL)
_ = c.setHTTPClient(&http.Client{})
// Apply any given options
for _, fn := range options {
if fn == nil {
continue
}
if err := fn(c); err != nil {
return nil, err
}
}
// Create all the Proxmox API services
c.Nodes = &NodeService{client: c}
c.Cluster = &ClusterService{client: c}
return c, nil
}
// ClientOptionFunc can be used to customize a new Proxmox API client
type ClientOptionFunc func(*Client) error
// WithBaseURL sets the URL for API requests to something other than localhost.
// API path is applied automatically if unspecified.
// Default: "https://localhost:8006/"
func WithBaseURL(urlStr string) ClientOptionFunc {
return func(c *Client) error {
return c.setBaseURL(urlStr)
}
}
// setBaseURL sets the URL for API requests
func (c *Client) setBaseURL(urlStr string) error {
// Make sure the given URL end with a slash
if !strings.HasSuffix(urlStr, "/") {
urlStr += "/"
}
baseURL, err := url.Parse(urlStr)
if err != nil {
return err
}
if !strings.HasSuffix(baseURL.Path, apiPath) {
baseURL.Path += apiPath
}
// Update the base URL of the client
c.baseURL = baseURL
return nil
}
// WithHTTPClient sets the HTTP client for API requests to something other than the default Go http Client
func WithHTTPClient(client *http.Client) ClientOptionFunc {
return func(c *Client) error {
return c.setHTTPClient(client)
}
}
// setHTTPClient sets the HTTP client for API requests
func (c *Client) setHTTPClient(client *http.Client) error {
c.client = client
return nil
}