Skip to content

Commit 9fdab94

Browse files
committed
add delete storage
1 parent 22302ca commit 9fdab94

File tree

5 files changed

+137
-12
lines changed

5 files changed

+137
-12
lines changed

api/task_type.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package api
33
type Task struct {
44
Endtime int `json:"endtime"`
55
// Id string `json:"id,omitempty"`
6-
Node string `json:"node"`
76
PID int `json:"pid"`
87
PStart int `json:"pstart"`
98
StartTime int `json:"starttime"`

rest/client.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,21 @@ func withLogin() ClientOption {
8383
}
8484

8585
func (c *RESTClient) Do(httpMethod, urlPath string, req, v interface{}) error {
86-
jsonReq, err := json.Marshal(req)
86+
url, err := url.JoinPath(c.endpoint, urlPath)
8787
if err != nil {
8888
return err
8989
}
9090

91-
url, err := url.JoinPath(c.endpoint, urlPath)
92-
if err != nil {
93-
return err
91+
var body io.Reader
92+
if req != nil {
93+
jsonReq, err := json.Marshal(req)
94+
if err != nil {
95+
return err
96+
}
97+
body = bytes.NewReader(jsonReq)
9498
}
95-
httpReq, err := http.NewRequest(httpMethod, url, bytes.NewReader(jsonReq))
99+
100+
httpReq, err := http.NewRequest(httpMethod, url, body)
96101
if err != nil {
97102
return err
98103
}
@@ -168,18 +173,19 @@ func checkResponse(res *http.Response) error {
168173
if res.StatusCode >= 200 && res.StatusCode <= 299 {
169174
return nil
170175
}
171-
if res.StatusCode == http.StatusInternalServerError || res.StatusCode == http.StatusNotImplemented {
172-
return &Error{code: res.StatusCode, returnMessage: NotFound}
173-
}
174-
if res.StatusCode == http.StatusUnauthorized || res.StatusCode == http.StatusForbidden {
175-
return &Error{code: res.StatusCode, returnMessage: NotAuthorized}
176-
}
177176

178177
body, err := io.ReadAll(res.Body)
179178
if err != nil {
180179
return errors.Errorf("failed to read body while handling http response of status %d : %v", res.StatusCode, err)
181180
}
182181

182+
if res.StatusCode == http.StatusInternalServerError || res.StatusCode == http.StatusNotImplemented {
183+
return &Error{code: res.StatusCode, returnMessage: res.Status}
184+
}
185+
if res.StatusCode == http.StatusUnauthorized || res.StatusCode == http.StatusForbidden {
186+
return &Error{code: res.StatusCode, returnMessage: NotAuthorized}
187+
}
188+
183189
if res.StatusCode == http.StatusBadRequest {
184190
var errorskey map[string]json.RawMessage
185191
if err := json.Unmarshal(body, &errorskey); err != nil {

rest/storage.go

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

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

@@ -34,3 +36,11 @@ func (c *RESTClient) CreateStorage(name, storageType string, options api.Storage
3436
}
3537
return storage, nil
3638
}
39+
40+
func (c *RESTClient) DeleteStorage(name string) error {
41+
path := fmt.Sprintf("/storage/%s", name)
42+
if err := c.Delete(path, nil, nil); err != nil {
43+
return err
44+
}
45+
return nil
46+
}

rest/storage_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package rest
2+
3+
import (
4+
"time"
5+
6+
"github.com/sp-yduck/proxmox-go/api"
7+
)
8+
9+
func (s *TestSuite) TestGetStorages() {
10+
storages, err := s.restclient.GetStorages()
11+
if err != nil {
12+
s.T().Fatalf("failed to get storages: %v", err)
13+
}
14+
s.T().Logf("get storages: %v", storages)
15+
}
16+
17+
func (s *TestSuite) GetTestStorage() *api.Storage {
18+
storages, err := s.restclient.GetStorages()
19+
if err != nil {
20+
s.T().Fatalf("failed to get storages: %v", err)
21+
}
22+
return storages[0]
23+
}
24+
25+
func (s *TestSuite) TestGetStorage() {
26+
testStorageName := s.GetTestStorage().Storage
27+
28+
storage, err := s.restclient.GetStorage(testStorageName)
29+
if err != nil {
30+
s.T().Fatalf("failed to get storage(name=%s): %v", testStorageName, err)
31+
}
32+
s.T().Logf("get storage: %v", *storage)
33+
}
34+
35+
func (s *TestSuite) EnsureNoStorage(name string) {
36+
storage, err := s.restclient.GetStorage(name)
37+
if err == nil {
38+
s.T().Logf("error: %v", err)
39+
if err := s.restclient.DeleteStorage(storage.Storage); err != nil {
40+
s.T().Fatalf("failed to ensure no storage (name=%s): %v", storage.Storage, err)
41+
}
42+
time.Sleep(2 * time.Second)
43+
}
44+
if err != nil && !IsNotFound(err) {
45+
s.T().Logf("failed to get storage(name=%s): %v", name, err)
46+
}
47+
}
48+
49+
func (s *TestSuite) TestCreateDeleteStorage() {
50+
testStorageName := "test-proxmox-go"
51+
s.EnsureNoStorage(testStorageName)
52+
53+
// create
54+
testOptions := api.StorageCreateOptions{
55+
Content: "images",
56+
Mkdir: true,
57+
Path: "/var/lib/vz/test",
58+
}
59+
storage, err := s.restclient.CreateStorage(testStorageName, "dir", testOptions)
60+
if err != nil {
61+
s.T().Fatalf("failed to create storage(name=%s): %v", testStorageName, err)
62+
}
63+
s.T().Logf("create storage: %v", *storage)
64+
time.Sleep(2 * time.Second)
65+
66+
// delete
67+
err = s.restclient.DeleteStorage(testStorageName)
68+
if err != nil {
69+
s.T().Fatalf("failed to delete storage(name=%s): %v", testStorageName, err)
70+
}
71+
}

rest/task_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package rest
2+
3+
import (
4+
"github.com/sp-yduck/proxmox-go/api"
5+
)
6+
7+
func (s *TestSuite) GetTestNode() *api.Node {
8+
nodes, err := s.restclient.GetNodes()
9+
if err != nil {
10+
s.T().Errorf("failed to get nodes: %v", err)
11+
}
12+
return nodes[0]
13+
}
14+
15+
func (s *TestSuite) TestGetTasks() {
16+
testNodeName := s.GetTestNode().Node
17+
18+
tasks, err := s.restclient.GetTasks(testNodeName)
19+
if err != nil {
20+
s.T().Errorf("failed to get tasks on a node=%s: %v", testNodeName, err)
21+
}
22+
s.T().Logf("get tasks: %v", tasks)
23+
}
24+
25+
func (s *TestSuite) TestGetTask() {
26+
testNodeName := s.GetTestNode().Node
27+
28+
tasks, err := s.restclient.GetTasks(testNodeName)
29+
if err != nil {
30+
s.T().Errorf("failed to get tasks on a node=%s: %v", testNodeName, err)
31+
}
32+
33+
testTaskUPID := tasks[0].UPID
34+
task, err := s.restclient.GetTask(testNodeName, testTaskUPID)
35+
if err != nil {
36+
s.T().Errorf("failed to get task(upid=%s) on a node=%s: %v", testTaskUPID, testNodeName, err)
37+
}
38+
s.T().Logf("get task: %v", *task)
39+
}

0 commit comments

Comments
 (0)