Skip to content

Commit c51bebf

Browse files
authored
Merge pull request #6 from Starttoaster/typefix
Use IntOrString type for disk RPM representation
2 parents 8689574 + a1c6cad commit c51bebf

File tree

6 files changed

+250
-28
lines changed

6 files changed

+250
-28
lines changed

nodes.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -214,20 +214,21 @@ type GetNodeDisksListResponse struct {
214214

215215
// GetNodeDisksListData contains data of disks from a GetNodeDisksList response
216216
type GetNodeDisksListData struct {
217-
ByIDLink string `json:"by_id_link"`
218-
DevPath string `json:"devpath"`
219-
GPT int `json:"gpt"`
220-
Health string `json:"health"`
221-
Model string `json:"model"`
222-
RPM int `json:"rpm"`
223-
Serial string `json:"serial"`
224-
Size int `json:"size"`
225-
Type string `json:"type"`
226-
Used string `json:"used"`
227-
Vendor string `json:"vendor"`
228-
WWN string `json:"wwn"`
229-
Bluestore int `json:"bluestore,omitempty"`
230-
OSDEncrypted int `json:"osdencrypted,omitempty"`
217+
ByIDLink string `json:"by_id_link"`
218+
DevPath string `json:"devpath"`
219+
GPT int `json:"gpt"`
220+
Health string `json:"health"`
221+
Model string `json:"model"`
222+
RPM IntOrString `json:"rpm"`
223+
Serial string `json:"serial"`
224+
Size int `json:"size"`
225+
Type string `json:"type"`
226+
Used string `json:"used"`
227+
Vendor string `json:"vendor"`
228+
WWN string `json:"wwn"`
229+
Wearout IntOrString `json:"wearout"`
230+
Bluestore int `json:"bluestore,omitempty"`
231+
OSDEncrypted int `json:"osdencrypted,omitempty"`
231232
}
232233

233234
// GetNodeDisksList makes a GET request to the /nodes/{node}/disks/list endpoint

nodes_test.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,24 @@ func TestGetNodeLxc(t *testing.T) {
106106
Uptime: 0,
107107
VMID: IntOrString("103"),
108108
},
109+
{
110+
CPU: 0,
111+
CPUs: 1,
112+
Disk: 0,
113+
DiskRead: 0,
114+
DiskWrite: 0,
115+
MaxDisk: 8589934592,
116+
MaxMem: 536870912,
117+
MaxSwap: 536870912,
118+
Mem: 0,
119+
Name: "CT104",
120+
NetIn: 0,
121+
NetOut: 0,
122+
Status: "stopped",
123+
Type: "lxc",
124+
Uptime: 0,
125+
VMID: IntOrString("104"),
126+
},
109127
},
110128
}
111129

@@ -155,3 +173,104 @@ func TestGetNodeQemu(t *testing.T) {
155173
require.NotNil(t, resp)
156174
require.Equal(t, want, *r)
157175
}
176+
177+
func TestGetNodeDisksList(t *testing.T) {
178+
mux, server, client := setup(t)
179+
defer teardown(server)
180+
181+
mux.HandleFunc("/api2/json/nodes/srv1/disks/list", func(w http.ResponseWriter, r *http.Request) {
182+
w.Header().Set("Content-Type", "application/json")
183+
w.WriteHeader(http.StatusOK)
184+
_, err := fmt.Fprint(w, fixture("nodes/get_node_disks_list.json"))
185+
if err != nil {
186+
return
187+
}
188+
})
189+
190+
want := GetNodeDisksListResponse{
191+
Data: []GetNodeDisksListData{
192+
{
193+
DevPath: "/dev/nvme0n1",
194+
WWN: "testwwn",
195+
Serial: "testser",
196+
ByIDLink: "/dev/disk/by-id/nvme-drive",
197+
Size: 512110190592,
198+
Used: "BIOS boot",
199+
RPM: IntOrString("0"),
200+
Wearout: IntOrString("99"),
201+
GPT: 1,
202+
Health: "PASSED",
203+
Model: "Test Model",
204+
Vendor: "unknown",
205+
Type: "nvme",
206+
},
207+
{
208+
Serial: "testser",
209+
ByIDLink: "/dev/disk/by-id/ata-TOSHIBA_THNS",
210+
Bluestore: 1,
211+
DevPath: "/dev/sda",
212+
OSDEncrypted: 0,
213+
Type: "ssd",
214+
Health: "PASSED",
215+
RPM: IntOrString("0"),
216+
Wearout: IntOrString("N/A"),
217+
GPT: 0,
218+
Vendor: "unknown",
219+
Size: 1920383410176,
220+
Model: "Test Model",
221+
WWN: "testwwn",
222+
Used: "LVM",
223+
},
224+
{
225+
DevPath: "/dev/sda",
226+
WWN: "testwwn",
227+
Serial: "testser",
228+
ByIDLink: "/dev/disk/by-id/ata-ST2000",
229+
Size: 2000398934016,
230+
Used: "LVM",
231+
RPM: IntOrString("5400"),
232+
Wearout: IntOrString("N/A"),
233+
GPT: 0,
234+
Health: "PASSED",
235+
Model: "Test Model",
236+
Vendor: "unknown",
237+
Type: "hdd",
238+
},
239+
{
240+
DevPath: "/dev/sdc",
241+
WWN: "testwwn",
242+
Serial: "testser",
243+
ByIDLink: "/dev/disk/by-id/ata-WDC_DRIVE2",
244+
Size: 5000947302400,
245+
Used: "ext4",
246+
RPM: IntOrString("4800"),
247+
Wearout: IntOrString("N/A"),
248+
GPT: 0,
249+
Health: "PASSED",
250+
Model: "Test Model",
251+
Vendor: "unknown",
252+
Type: "hdd",
253+
},
254+
{
255+
DevPath: "/dev/sde",
256+
WWN: "testwwn",
257+
Serial: "testser",
258+
ByIDLink: "/dev/disk/by-id/ata-WDC_DRIVE1",
259+
Size: 5000947302400,
260+
Used: "ZFS",
261+
RPM: IntOrString("4800"),
262+
Wearout: IntOrString("N/A"),
263+
GPT: 0,
264+
Health: "PASSED",
265+
Model: "Test Model",
266+
Vendor: "unknown",
267+
Type: "hdd",
268+
},
269+
},
270+
}
271+
272+
r, resp, err := client.Nodes.GetNodeDisksList("srv1")
273+
require.NoError(t, err)
274+
require.NotNil(t, resp)
275+
require.Equal(t, want, *r)
276+
}

request.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package proxmox
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
"io"
7-
"log"
88
"net/http"
99
"net/url"
1010

@@ -61,16 +61,6 @@ func (c *Client) Do(req *http.Request, v interface{}) (*http.Response, error) {
6161
if err != nil {
6262
return nil, err
6363
}
64-
defer func() {
65-
if err := resp.Body.Close(); err != nil {
66-
log.Printf("proxmox client couldn't close the response body: %v", err)
67-
}
68-
}()
69-
defer func() {
70-
if _, err := io.Copy(io.Discard, resp.Body); err != nil {
71-
log.Printf("proxmox client couldn't discard the response body: %v", err)
72-
}
73-
}()
7464

7565
// Check for error API response and capture it as an error
7666
// 3xx codes get treated as errors, unclear if there's a valid reason for redirection here
@@ -80,7 +70,7 @@ func (c *Client) Do(req *http.Request, v interface{}) (*http.Response, error) {
8070
return nil, fmt.Errorf("error reading Proxmox response body: %v", err)
8171
}
8272

83-
return resp, fmt.Errorf(string(body))
73+
return resp, errors.New(string(body))
8474
}
8575

8676
// Copy body into v
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
{
2+
"data": [
3+
{
4+
"devpath": "/dev/nvme0n1",
5+
"wwn": "testwwn",
6+
"serial": "testser",
7+
"by_id_link": "/dev/disk/by-id/nvme-drive",
8+
"size": 512110190592,
9+
"used": "BIOS boot",
10+
"osdid": -1,
11+
"rpm": 0,
12+
"wearout": 99,
13+
"gpt": 1,
14+
"osdid-list": null,
15+
"health": "PASSED",
16+
"model": "Test Model",
17+
"vendor": "unknown",
18+
"type": "nvme"
19+
},
20+
{
21+
"serial": "testser",
22+
"by_id_link": "/dev/disk/by-id/ata-TOSHIBA_THNS",
23+
"bluestore": 1,
24+
"devpath": "/dev/sda",
25+
"osdencrypted": 0,
26+
"type": "ssd",
27+
"osdid-list":[
28+
"1"
29+
],
30+
"osdid": "1",
31+
"health": "PASSED",
32+
"rpm": 0,
33+
"wearout": "N/A",
34+
"gpt": 0,
35+
"vendor": "unknown",
36+
"size": 1920383410176,
37+
"model": "Test Model",
38+
"wwn": "testwwn",
39+
"used": "LVM"
40+
},
41+
{
42+
"osdid": -1,
43+
"rpm": "5400",
44+
"serial": "testser",
45+
"by_id_link": "/dev/disk/by-id/ata-ST2000",
46+
"size": 2000398934016,
47+
"used": "LVM",
48+
"devpath": "/dev/sda",
49+
"wwn": "testwwn",
50+
"type": "hdd",
51+
"vendor": "unknown",
52+
"health": "PASSED",
53+
"model": "Test Model",
54+
"gpt": 0,
55+
"osdid-list": null,
56+
"wearout": "N/A"
57+
},
58+
{
59+
"rpm": "4800",
60+
"osdid": -1,
61+
"by_id_link": "/dev/disk/by-id/ata-WDC_DRIVE2",
62+
"size": 5000947302400,
63+
"used": "ext4",
64+
"serial": "testser",
65+
"wwn": "testwwn",
66+
"devpath": "/dev/sdc",
67+
"type": "hdd",
68+
"vendor": "unknown",
69+
"model": "Test Model",
70+
"health": "PASSED",
71+
"osdid-list": null,
72+
"gpt": 0,
73+
"wearout": "N/A"
74+
},
75+
{
76+
"size": 5000947302400,
77+
"used": "ZFS",
78+
"by_id_link": "/dev/disk/by-id/ata-WDC_DRIVE1",
79+
"serial": "testser",
80+
"rpm": "4800",
81+
"osdid": -1,
82+
"wwn": "testwwn",
83+
"devpath": "/dev/sde",
84+
"model": "Test Model",
85+
"health": "PASSED",
86+
"type": "hdd",
87+
"vendor": "unknown",
88+
"wearout": "N/A",
89+
"osdid-list": null,
90+
"gpt": 0
91+
}
92+
]
93+
}

testdata/nodes/get_node_lxc.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,25 @@
1818
"diskread":0,
1919
"netout":0,
2020
"mem":0
21+
},
22+
{
23+
"status":"stopped",
24+
"maxdisk":8589934592,
25+
"maxswap":536870912,
26+
"swap":0,
27+
"netin":0,
28+
"cpu":0,
29+
"cpus":1,
30+
"type":"lxc",
31+
"maxmem":536870912,
32+
"name":"CT104",
33+
"disk":0,
34+
"diskwrite":0,
35+
"vmid":"104",
36+
"uptime":0,
37+
"diskread":0,
38+
"netout":0,
39+
"mem":0
2140
}
2241
]
2342
}

types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ func (is *IntOrString) UnmarshalJSON(data []byte) error {
6969
// attempt to unmarshal into an integer
7070
var i int
7171
var intErr error
72-
if intErr = json.Unmarshal(data, &i); intErr == nil && i != 0 {
72+
if intErr = json.Unmarshal(data, &i); intErr == nil {
7373
*is = IntOrString(strconv.Itoa(i))
7474
return nil
7575
}
7676

7777
// attempt to unmarshal into a string
7878
var str string
7979
var strErr error
80-
if strErr = json.Unmarshal(data, &str); strErr == nil && str != "" {
80+
if strErr = json.Unmarshal(data, &str); strErr == nil {
8181
*is = IntOrString(str)
8282
return nil
8383
}

0 commit comments

Comments
 (0)