Skip to content

Commit ad2109e

Browse files
committed
use context
1 parent a63d7cb commit ad2109e

File tree

15 files changed

+135
-82
lines changed

15 files changed

+135
-82
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ go test ./... -v -skip ^TestSuiteIntegration
1414

1515
### Integration Testing
1616
```sh
17-
export PROXMOX_URL="http://localhost:8006/api2/json"
18-
export PROXMOX_USERNAME="root@pam"
19-
export PROXMOX_PASSWORD="password"
17+
export PROXMOX_URL='http://localhost:8006/api2/json'
18+
# tokenid & secret
19+
export PROXMOX_TOKENID='root@pam!your-token-id'
20+
export PROXMOX_SECRET='aaaaaaaaa-bbb-cccc-dddd-ef0123456789'
21+
# or username & password
22+
# export PROXMOX_USERNAME='root@pam'
23+
# export PROXMOX_PASSWORD='password'
2024

2125
go test ./... -v -run ^TestSuiteIntegration
2226
```

rest/access.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package rest
22

33
import (
4+
"context"
5+
46
"github.com/sp-yduck/proxmox-go/api"
57
)
68

7-
func (c *RESTClient) PostTicket(req TicketRequest) (*api.Session, error) {
8-
if err := c.Post("/access/ticket", req, &c.session); err != nil {
9+
func (c *RESTClient) PostTicket(ctx context.Context, req TicketRequest) (*api.Session, error) {
10+
if err := c.Post(ctx, "/access/ticket", req, &c.session); err != nil {
911
return nil, err
1012
}
1113
return c.session, nil

rest/client.go

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package rest
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/json"
67
"fmt"
78
"io"
89
"io/ioutil"
910
"net/http"
1011
"net/url"
1112
"strings"
13+
"time"
1214

1315
"github.com/pkg/errors"
1416

@@ -38,22 +40,34 @@ type ClientOption func(*RESTClient)
3840

3941
func NewRESTClient(baseUrl string, opts ...ClientOption) (*RESTClient, error) {
4042
client := &RESTClient{
41-
endpoint: baseUrl,
43+
endpoint: complementURL(baseUrl),
4244
httpClient: &http.Client{},
4345
}
46+
4447
for _, option := range opts {
4548
option(client)
4649
}
50+
4751
if client.token == "" && client.session == nil && client.credentials != nil {
48-
var err error
49-
client.session, err = client.PostTicket(*client.credentials)
50-
if err != nil {
52+
ctx, cancel := context.WithTimeout(context.TODO(), 1*time.Minute)
53+
defer cancel()
54+
if err := client.makeNewSession(ctx); err != nil {
5155
return nil, err
5256
}
5357
}
5458
return client, nil
5559
}
5660

61+
func complementURL(url string) string {
62+
if !strings.HasPrefix(url, "http") {
63+
url = "http://" + url
64+
}
65+
if !strings.HasSuffix(url, "/api2/json") {
66+
url += "/api2/json"
67+
}
68+
return url
69+
}
70+
5771
func WithClient(client *http.Client) ClientOption {
5872
return func(c *RESTClient) {
5973
c.httpClient = client
@@ -78,18 +92,13 @@ func WithUserPassword(username, password string) ClientOption {
7892
}
7993
}
8094

81-
func withLogin() ClientOption {
95+
func WithAPIToken(tokenid, secret string) ClientOption {
8296
return func(c *RESTClient) {
97+
c.token = fmt.Sprintf("%s=%s", tokenid, secret)
8398
}
8499
}
85100

86-
func (c *RESTClient) Do(httpMethod, urlPath string, req, v interface{}) error {
87-
if !strings.HasPrefix(c.endpoint, "http") {
88-
c.endpoint = "http://" + c.endpoint
89-
}
90-
if !strings.HasSuffix(c.endpoint, "/api2/json") {
91-
c.endpoint += "/api2/json"
92-
}
101+
func (c *RESTClient) Do(ctx context.Context, httpMethod, urlPath string, req, v interface{}) error {
93102
url, err := url.JoinPath(c.endpoint, urlPath)
94103
if err != nil {
95104
return err
@@ -104,7 +113,7 @@ func (c *RESTClient) Do(httpMethod, urlPath string, req, v interface{}) error {
104113
body = bytes.NewReader(jsonReq)
105114
}
106115

107-
httpReq, err := http.NewRequest(httpMethod, url, body)
116+
httpReq, err := http.NewRequestWithContext(ctx, httpMethod, url, body)
108117
if err != nil {
109118
return err
110119
}
@@ -138,20 +147,20 @@ func (c *RESTClient) Do(httpMethod, urlPath string, req, v interface{}) error {
138147
return json.Unmarshal(buf, &v)
139148
}
140149

141-
func (c *RESTClient) Get(path string, res interface{}) error {
142-
return c.Do(http.MethodGet, path, nil, res)
150+
func (c *RESTClient) Get(ctx context.Context, path string, res interface{}) error {
151+
return c.Do(ctx, http.MethodGet, path, nil, res)
143152
}
144153

145-
func (c *RESTClient) Post(path string, req, res interface{}) error {
146-
return c.Do(http.MethodPost, path, req, res)
154+
func (c *RESTClient) Post(ctx context.Context, path string, req, res interface{}) error {
155+
return c.Do(ctx, http.MethodPost, path, req, res)
147156
}
148157

149-
func (c *RESTClient) Put(path string, req, res interface{}) error {
150-
return c.Do(http.MethodPut, path, req, res)
158+
func (c *RESTClient) Put(ctx context.Context, path string, req, res interface{}) error {
159+
return c.Do(ctx, http.MethodPut, path, req, res)
151160
}
152161

153-
func (c *RESTClient) Delete(path string, req, res interface{}) error {
154-
return c.Do(http.MethodDelete, path, req, res)
162+
func (c *RESTClient) Delete(ctx context.Context, path string, req, res interface{}) error {
163+
return c.Do(ctx, http.MethodDelete, path, req, res)
155164
}
156165

157166
func (c *RESTClient) makeAuthHeaders() http.Header {
@@ -167,9 +176,9 @@ func (c *RESTClient) makeAuthHeaders() http.Header {
167176
return header
168177
}
169178

170-
func (c *RESTClient) makeNewSession() error {
179+
func (c *RESTClient) makeNewSession(ctx context.Context) error {
171180
var err error
172-
c.session, err = c.PostTicket(*c.credentials)
181+
c.session, err = c.PostTicket(ctx, *c.credentials)
173182
if err != nil {
174183
return err
175184
}

rest/cluster.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package rest
22

33
import (
4+
"context"
45
"encoding/json"
56
)
67

7-
func (c *RESTClient) GetNextID() (int, error) {
8+
func (c *RESTClient) GetNextID(ctx context.Context) (int, error) {
89
var res json.Number
9-
if err := c.Get("/cluster/nextid", &res); err != nil {
10+
if err := c.Get(ctx, "/cluster/nextid", &res); err != nil {
1011
return 0, err
1112
}
1213
nextid, err := res.Int64()

rest/cluster_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package rest
22

3+
import "context"
4+
35
func (s *TestSuite) TestGetNextID() {
4-
nextid, err := s.restclient.GetNextID()
6+
nextid, err := s.restclient.GetNextID(context.TODO())
57
if err != nil {
68
s.T().Errorf("failed to get next id: %v", err)
79
}

rest/node.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
package rest
22

33
import (
4+
"context"
5+
46
"github.com/sp-yduck/proxmox-go/api"
57
)
68

7-
func (c *RESTClient) GetNodes() ([]*api.Node, error) {
9+
func (c *RESTClient) GetNodes(ctx context.Context) ([]*api.Node, error) {
810
var nodes []*api.Node
9-
if err := c.Get("/nodes", &nodes); err != nil {
11+
if err := c.Get(ctx, "/nodes", &nodes); err != nil {
1012
return nil, err
1113
}
1214
return nodes, nil
1315
}
1416

15-
func (c *RESTClient) GetNode(name string) (*api.Node, error) {
16-
nodes, err := c.GetNodes()
17+
func (c *RESTClient) GetNode(ctx context.Context, name string) (*api.Node, error) {
18+
nodes, err := c.GetNodes(ctx)
1719
if err != nil {
1820
return nil, err
1921
}

rest/qemu.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
package rest
22

33
import (
4+
"context"
45
"fmt"
56

67
"github.com/sp-yduck/proxmox-go/api"
78
)
89

9-
func (c *RESTClient) GetVirtualMachines(node string) ([]*api.VirtualMachine, error) {
10+
func (c *RESTClient) GetVirtualMachines(ctx context.Context, node string) ([]*api.VirtualMachine, error) {
1011
path := fmt.Sprintf("/nodes/%s/qemu", node)
1112
var vms []*api.VirtualMachine
12-
if err := c.Get(path, &vms); err != nil {
13+
if err := c.Get(ctx, path, &vms); err != nil {
1314
return nil, err
1415
}
1516
return vms, nil
1617
}
1718

18-
func (c *RESTClient) GetVirtualMachine(node string, vmid int) (*api.VirtualMachine, error) {
19-
vms, err := c.GetVirtualMachines(node)
19+
func (c *RESTClient) GetVirtualMachine(ctx context.Context, node string, vmid int) (*api.VirtualMachine, error) {
20+
vms, err := c.GetVirtualMachines(ctx, node)
2021
if err != nil {
2122
return nil, err
2223
}
@@ -28,30 +29,39 @@ func (c *RESTClient) GetVirtualMachine(node string, vmid int) (*api.VirtualMachi
2829
return nil, NotFoundErr
2930
}
3031

31-
func (c *RESTClient) CreateVirtualMachine(node string, vmid int, options api.VirtualMachineCreateOptions) (*string, error) {
32+
func (c *RESTClient) CreateVirtualMachine(ctx context.Context, node string, vmid int, options api.VirtualMachineCreateOptions) (*string, error) {
3233
options.VMID = vmid
3334
path := fmt.Sprintf("/nodes/%s/qemu", node)
3435
var upid *string
35-
if err := c.Post(path, options, &upid); err != nil {
36+
if err := c.Post(ctx, path, options, &upid); err != nil {
3637
return nil, err
3738
}
3839
return upid, nil
3940
}
4041

41-
func (c *RESTClient) DeleteVirtualMachine(node string, vmid int) (*string, error) {
42+
func (c *RESTClient) DeleteVirtualMachine(ctx context.Context, node string, vmid int) (*string, error) {
4243
path := fmt.Sprintf("/nodes/%s/qemu/%d", node, vmid)
4344
var upid *string
44-
if err := c.Delete(path, nil, upid); err != nil {
45+
if err := c.Delete(ctx, path, nil, upid); err != nil {
4546
return nil, err
4647
}
4748
return upid, nil
4849
}
4950

50-
func (c *RESTClient) GetVirtualMachineConfig(node string, vmid int) (*api.VirtualMachineConfig, error) {
51+
func (c *RESTClient) GetVirtualMachineConfig(ctx context.Context, node string, vmid int) (*api.VirtualMachineConfig, error) {
5152
path := fmt.Sprintf("/nodes/%s/qemu/%d/config", node, vmid)
5253
var config *api.VirtualMachineConfig
53-
if err := c.Get(path, &config); err != nil {
54+
if err := c.Get(ctx, path, &config); err != nil {
5455
return nil, err
5556
}
5657
return config, nil
5758
}
59+
60+
func (c *RESTClient) GetVirtualMachineStatus(ctx context.Context, node string, vmid int) (*api.ProcessStatus, error) {
61+
path := fmt.Sprintf("/nodes/%s/qemu/%d/status", node, vmid)
62+
var status *api.ProcessStatus
63+
if err := c.Get(ctx, path, &status); err != nil {
64+
return nil, err
65+
}
66+
return status, nil
67+
}

rest/qemu_test.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package rest
22

3-
import "github.com/sp-yduck/proxmox-go/api"
3+
import (
4+
"context"
5+
6+
"github.com/sp-yduck/proxmox-go/api"
7+
)
48

59
func (s *TestSuite) TestGetVirtualMachines() {
610
nodeName := s.GetTestNode().Node
7-
vms, err := s.restclient.GetVirtualMachines(nodeName)
11+
vms, err := s.restclient.GetVirtualMachines(context.TODO(), nodeName)
812
if err != nil {
913
s.T().Fatalf("failed to get vms: %v", err)
1014
}
@@ -13,7 +17,7 @@ func (s *TestSuite) TestGetVirtualMachines() {
1317

1418
func (s *TestSuite) GetTestVM() *api.VirtualMachine {
1519
nodeName := s.GetTestNode().Node
16-
vms, err := s.restclient.GetVirtualMachines(nodeName)
20+
vms, err := s.restclient.GetVirtualMachines(context.TODO(), nodeName)
1721
if err != nil {
1822
s.T().Fatalf("failed to get vms: %v", err)
1923
}
@@ -23,7 +27,7 @@ func (s *TestSuite) GetTestVM() *api.VirtualMachine {
2327
func (s *TestSuite) TestGetVirtualMachine() {
2428
nodeName := s.GetTestNode().Node
2529
vmid := s.GetTestVM().VMID
26-
vm, err := s.restclient.GetVirtualMachine(nodeName, vmid)
30+
vm, err := s.restclient.GetVirtualMachine(context.TODO(), nodeName, vmid)
2731
if err != nil {
2832
s.T().Fatalf("failed to get vm: %v", err)
2933
}
@@ -33,7 +37,7 @@ func (s *TestSuite) TestGetVirtualMachine() {
3337
func (s *TestSuite) TestGetVirtualMachineConfig() {
3438
nodeName := s.GetTestNode().Node
3539
vmid := s.GetTestVM().VMID
36-
config, err := s.restclient.GetVirtualMachineConfig(nodeName, vmid)
40+
config, err := s.restclient.GetVirtualMachineConfig(context.TODO(), nodeName, vmid)
3741
if err != nil {
3842
s.T().Fatalf("failed to get vm: %v", err)
3943
}

rest/storage.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
package rest
22

33
import (
4+
"context"
45
"fmt"
56

67
"github.com/sp-yduck/proxmox-go/api"
78
)
89

9-
func (c *RESTClient) GetStorages() ([]*api.Storage, error) {
10+
func (c *RESTClient) GetStorages(ctx context.Context) ([]*api.Storage, error) {
1011
var storages []*api.Storage
11-
if err := c.Get("/storage", &storages); err != nil {
12+
if err := c.Get(ctx, "/storage", &storages); err != nil {
1213
return nil, err
1314
}
1415
return storages, nil
1516
}
1617

17-
func (c *RESTClient) GetStorage(name string) (*api.Storage, error) {
18-
storages, err := c.GetStorages()
18+
func (c *RESTClient) GetStorage(ctx context.Context, name string) (*api.Storage, error) {
19+
storages, err := c.GetStorages(ctx)
1920
if err != nil {
2021
return nil, err
2122
}
@@ -27,19 +28,19 @@ func (c *RESTClient) GetStorage(name string) (*api.Storage, error) {
2728
return nil, NotFoundErr
2829
}
2930

30-
func (c *RESTClient) CreateStorage(name, storageType string, options api.StorageCreateOptions) (*api.Storage, error) {
31+
func (c *RESTClient) CreateStorage(ctx context.Context, name, storageType string, options api.StorageCreateOptions) (*api.Storage, error) {
3132
options.Storage = name
3233
options.StorageType = storageType
3334
var storage *api.Storage
34-
if err := c.Post("/storage", options, &storage); err != nil {
35+
if err := c.Post(ctx, "/storage", options, &storage); err != nil {
3536
return nil, err
3637
}
3738
return storage, nil
3839
}
3940

40-
func (c *RESTClient) DeleteStorage(name string) error {
41+
func (c *RESTClient) DeleteStorage(ctx context.Context, name string) error {
4142
path := fmt.Sprintf("/storage/%s", name)
42-
if err := c.Delete(path, nil, nil); err != nil {
43+
if err := c.Delete(ctx, path, nil, nil); err != nil {
4344
return err
4445
}
4546
return nil

0 commit comments

Comments
 (0)