Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GET cluster/resources API Method #4

Merged
merged 1 commit into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
53 changes: 53 additions & 0 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,56 @@ func (s *ClusterService) GetClusterStatus() (*GetClusterStatusResponse, *http.Re

return d, resp, nil
}

// GetClusterResourcesResponse contains the response for the /cluster/resources endpoint
type GetClusterResourcesResponse struct {
Data []GetClusterResourcesData `json:"data"`
}

// GetClusterResourcesData contains data of a cluster's resources from GetClusterResources
type GetClusterResourcesData struct {
ID string `json:"id"`
Node string `json:"node"`
Status string `json:"status"`
Type string `json:"type"`
CPU *float64 `json:"cpu"`
Disk *int `json:"disk"`
DiskRead *int `json:"diskread"`
DiskWrite *int `json:"diskwrite"`
MaxCPU *int `json:"maxcpu"`
MaxDisk *int `json:"maxdisk"`
MaxMem *int `json:"maxmem"`
Mem *int `json:"mem"`
Name *string `json:"name"`
NetIn *int `json:"netin"`
NetOut *int `json:"netout"`
Template *int `json:"template"`
Uptime *int `json:"uptime"`
VMID *int `json:"vmid"`
HAState *string `json:"hastate"`
CgroupMode *int `json:"cgroup-mode"`
Level *string `json:"level"`
Content *string `json:"content"`
PluginType *string `json:"plugintype"`
Shared *int `json:"shared"`
Storage *string `json:"storage"`
SDN *string `json:"sdn"`
}

// GetClusterResources makes a GET request to the /cluster/resources endpoint
// https://pve.proxmox.com/pve-docs/api-viewer/index.html#/cluster/resources
func (s *ClusterService) GetClusterResources() (*GetClusterResourcesResponse, *http.Response, error) {
u := "cluster/resources"
req, err := s.client.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, nil, err
}

d := new(GetClusterResourcesResponse)
resp, err := s.client.Do(req, d)
if err != nil {
return nil, resp, err
}

return d, resp, nil
}
99 changes: 99 additions & 0 deletions cluster_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package proxmox

import (
"fmt"
"net/http"
"testing"

"github.com/stretchr/testify/require"
)

func testStr(s string) *string {
return &s
}

func testInt(i int) *int {
return &i
}

func testFloat64(f float64) *float64 {
return &f
}

func TestGetClusterResources(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)

mux.HandleFunc("/api2/json/cluster/resources", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, err := fmt.Fprint(w, fixture("clusters/get_cluster_resources.json"))
if err != nil {
return
}
})

want := GetClusterResourcesResponse{
Data: []GetClusterResourcesData{
{
CPU: testFloat64(.00215395696684676),
Disk: testInt(0),
DiskRead: testInt(1899047936),
DiskWrite: testInt(2697581568),
ID: "qemu/101",
MaxCPU: testInt(4),
MaxDisk: testInt(549755813888),
MaxMem: testInt(17179869184),
Mem: testInt(3865654169),
Name: testStr("my-vm"),
NetIn: testInt(554461212),
NetOut: testInt(13830445),
Node: "node1",
Status: "running",
Template: testInt(0),
Type: "qemu",
Uptime: testInt(234806),
VMID: testInt(101),
},
{
CgroupMode: testInt(2),
CPU: testFloat64(0.0496424063946151),
Disk: testInt(5996113920),
ID: "node/node1",
Level: testStr(""),
MaxCPU: testInt(16),
MaxDisk: testInt(100861726720),
MaxMem: testInt(134850514944),
Mem: testInt(64139268096),
Node: "node1",
Status: "online",
Type: "node",
Uptime: testInt(234832),
},
{
Content: testStr("iso,backup,vztmpl"),
Disk: testInt(5996113920),
ID: "storage/node1/local",
MaxDisk: testInt(100861726720),
Node: "node1",
PluginType: testStr("dir"),
Shared: testInt(0),
Status: "available",
Storage: testStr("local"),
Type: "storage",
},
{
ID: "sdn/node1/localnetwork",
Node: "node1",
SDN: testStr("localnetwork"),
Status: "ok",
Type: "sdn",
},
},
}

r, resp, err := client.Cluster.GetClusterResources()
require.NoError(t, err)
require.NotNil(t, resp)
require.Equal(t, want, *r)
}
58 changes: 58 additions & 0 deletions testdata/clusters/get_cluster_resources.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"data": [
{
"cpu": 0.00215395696684676,
"disk": 0,
"diskread": 1899047936,
"diskwrite": 2697581568,
"id": "qemu/101",
"maxcpu": 4,
"maxdisk": 549755813888,
"maxmem": 17179869184,
"mem": 3865654169,
"name": "my-vm",
"netin": 554461212,
"netout": 13830445,
"node": "node1",
"status": "running",
"template": 0,
"type": "qemu",
"uptime": 234806,
"vmid": 101
},
{
"cgroup-mode": 2,
"cpu": 0.0496424063946151,
"disk": 5996113920,
"id": "node/node1",
"level": "",
"maxcpu": 16,
"maxdisk": 100861726720,
"maxmem": 134850514944,
"mem": 64139268096,
"node": "node1",
"status": "online",
"type": "node",
"uptime": 234832
},
{
"content": "iso,backup,vztmpl",
"disk": 5996113920,
"id": "storage/node1/local",
"maxdisk": 100861726720,
"node": "node1",
"plugintype": "dir",
"shared": 0,
"status": "available",
"storage": "local",
"type": "storage"
},
{
"id": "sdn/node1/localnetwork",
"node": "node1",
"sdn": "localnetwork",
"status": "ok",
"type": "sdn"
}
]
}