Skip to content

Commit 16af38a

Browse files
authored
Merge pull request #4 from Starttoaster/clusterresources
Add GET cluster/resources API Method
2 parents 2417173 + 21d4dc8 commit 16af38a

File tree

4 files changed

+211
-0
lines changed

4 files changed

+211
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea/

cluster.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,56 @@ func (s *ClusterService) GetClusterStatus() (*GetClusterStatusResponse, *http.Re
4343

4444
return d, resp, nil
4545
}
46+
47+
// GetClusterResourcesResponse contains the response for the /cluster/resources endpoint
48+
type GetClusterResourcesResponse struct {
49+
Data []GetClusterResourcesData `json:"data"`
50+
}
51+
52+
// GetClusterResourcesData contains data of a cluster's resources from GetClusterResources
53+
type GetClusterResourcesData struct {
54+
ID string `json:"id"`
55+
Node string `json:"node"`
56+
Status string `json:"status"`
57+
Type string `json:"type"`
58+
CPU *float64 `json:"cpu"`
59+
Disk *int `json:"disk"`
60+
DiskRead *int `json:"diskread"`
61+
DiskWrite *int `json:"diskwrite"`
62+
MaxCPU *int `json:"maxcpu"`
63+
MaxDisk *int `json:"maxdisk"`
64+
MaxMem *int `json:"maxmem"`
65+
Mem *int `json:"mem"`
66+
Name *string `json:"name"`
67+
NetIn *int `json:"netin"`
68+
NetOut *int `json:"netout"`
69+
Template *int `json:"template"`
70+
Uptime *int `json:"uptime"`
71+
VMID *int `json:"vmid"`
72+
HAState *string `json:"hastate"`
73+
CgroupMode *int `json:"cgroup-mode"`
74+
Level *string `json:"level"`
75+
Content *string `json:"content"`
76+
PluginType *string `json:"plugintype"`
77+
Shared *int `json:"shared"`
78+
Storage *string `json:"storage"`
79+
SDN *string `json:"sdn"`
80+
}
81+
82+
// GetClusterResources makes a GET request to the /cluster/resources endpoint
83+
// https://pve.proxmox.com/pve-docs/api-viewer/index.html#/cluster/resources
84+
func (s *ClusterService) GetClusterResources() (*GetClusterResourcesResponse, *http.Response, error) {
85+
u := "cluster/resources"
86+
req, err := s.client.NewRequest(http.MethodGet, u, nil)
87+
if err != nil {
88+
return nil, nil, err
89+
}
90+
91+
d := new(GetClusterResourcesResponse)
92+
resp, err := s.client.Do(req, d)
93+
if err != nil {
94+
return nil, resp, err
95+
}
96+
97+
return d, resp, nil
98+
}

cluster_test.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package proxmox
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func testStr(s string) *string {
12+
return &s
13+
}
14+
15+
func testInt(i int) *int {
16+
return &i
17+
}
18+
19+
func testFloat64(f float64) *float64 {
20+
return &f
21+
}
22+
23+
func TestGetClusterResources(t *testing.T) {
24+
mux, server, client := setup(t)
25+
defer teardown(server)
26+
27+
mux.HandleFunc("/api2/json/cluster/resources", func(w http.ResponseWriter, r *http.Request) {
28+
w.Header().Set("Content-Type", "application/json")
29+
w.WriteHeader(http.StatusOK)
30+
_, err := fmt.Fprint(w, fixture("clusters/get_cluster_resources.json"))
31+
if err != nil {
32+
return
33+
}
34+
})
35+
36+
want := GetClusterResourcesResponse{
37+
Data: []GetClusterResourcesData{
38+
{
39+
CPU: testFloat64(.00215395696684676),
40+
Disk: testInt(0),
41+
DiskRead: testInt(1899047936),
42+
DiskWrite: testInt(2697581568),
43+
ID: "qemu/101",
44+
MaxCPU: testInt(4),
45+
MaxDisk: testInt(549755813888),
46+
MaxMem: testInt(17179869184),
47+
Mem: testInt(3865654169),
48+
Name: testStr("my-vm"),
49+
NetIn: testInt(554461212),
50+
NetOut: testInt(13830445),
51+
Node: "node1",
52+
Status: "running",
53+
Template: testInt(0),
54+
Type: "qemu",
55+
Uptime: testInt(234806),
56+
VMID: testInt(101),
57+
},
58+
{
59+
CgroupMode: testInt(2),
60+
CPU: testFloat64(0.0496424063946151),
61+
Disk: testInt(5996113920),
62+
ID: "node/node1",
63+
Level: testStr(""),
64+
MaxCPU: testInt(16),
65+
MaxDisk: testInt(100861726720),
66+
MaxMem: testInt(134850514944),
67+
Mem: testInt(64139268096),
68+
Node: "node1",
69+
Status: "online",
70+
Type: "node",
71+
Uptime: testInt(234832),
72+
},
73+
{
74+
Content: testStr("iso,backup,vztmpl"),
75+
Disk: testInt(5996113920),
76+
ID: "storage/node1/local",
77+
MaxDisk: testInt(100861726720),
78+
Node: "node1",
79+
PluginType: testStr("dir"),
80+
Shared: testInt(0),
81+
Status: "available",
82+
Storage: testStr("local"),
83+
Type: "storage",
84+
},
85+
{
86+
ID: "sdn/node1/localnetwork",
87+
Node: "node1",
88+
SDN: testStr("localnetwork"),
89+
Status: "ok",
90+
Type: "sdn",
91+
},
92+
},
93+
}
94+
95+
r, resp, err := client.Cluster.GetClusterResources()
96+
require.NoError(t, err)
97+
require.NotNil(t, resp)
98+
require.Equal(t, want, *r)
99+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"data": [
3+
{
4+
"cpu": 0.00215395696684676,
5+
"disk": 0,
6+
"diskread": 1899047936,
7+
"diskwrite": 2697581568,
8+
"id": "qemu/101",
9+
"maxcpu": 4,
10+
"maxdisk": 549755813888,
11+
"maxmem": 17179869184,
12+
"mem": 3865654169,
13+
"name": "my-vm",
14+
"netin": 554461212,
15+
"netout": 13830445,
16+
"node": "node1",
17+
"status": "running",
18+
"template": 0,
19+
"type": "qemu",
20+
"uptime": 234806,
21+
"vmid": 101
22+
},
23+
{
24+
"cgroup-mode": 2,
25+
"cpu": 0.0496424063946151,
26+
"disk": 5996113920,
27+
"id": "node/node1",
28+
"level": "",
29+
"maxcpu": 16,
30+
"maxdisk": 100861726720,
31+
"maxmem": 134850514944,
32+
"mem": 64139268096,
33+
"node": "node1",
34+
"status": "online",
35+
"type": "node",
36+
"uptime": 234832
37+
},
38+
{
39+
"content": "iso,backup,vztmpl",
40+
"disk": 5996113920,
41+
"id": "storage/node1/local",
42+
"maxdisk": 100861726720,
43+
"node": "node1",
44+
"plugintype": "dir",
45+
"shared": 0,
46+
"status": "available",
47+
"storage": "local",
48+
"type": "storage"
49+
},
50+
{
51+
"id": "sdn/node1/localnetwork",
52+
"node": "node1",
53+
"sdn": "localnetwork",
54+
"status": "ok",
55+
"type": "sdn"
56+
}
57+
]
58+
}

0 commit comments

Comments
 (0)