diff --git a/cluster.go b/cluster.go index 88fde9b..31fbdd4 100644 --- a/cluster.go +++ b/cluster.go @@ -1,7 +1,7 @@ package proxmox import ( - "fmt" + "net/url" "strconv" "strings" ) @@ -26,13 +26,15 @@ func (cl *Cluster) NextID() (int, error) { // to filter searched values. // It returns a list of ClusterResources. func (cl *Cluster) Resources(filters ...string) (rs ClusterResources, err error) { - url := "/cluster/resources" + u := url.URL{Path: "/cluster/resources"} // filters are variadic because they're optional, munging everything passed into one big string to make // a good request and the api will error out if there's an issue if f := strings.Replace(strings.Join(filters, ""), " ", "", -1); f != "" { - url = fmt.Sprintf("%s?type=%s", url, f) + params := url.Values{} + params.Add("type", f) + u.RawQuery = params.Encode() } - return rs, cl.client.Get(url, &rs) + return rs, cl.client.Get(u.String(), &rs) } diff --git a/cluster_test.go b/cluster_test.go index 5f2e379..1f2c97a 100644 --- a/cluster_test.go +++ b/cluster_test.go @@ -33,3 +33,20 @@ func TestNextID(t *testing.T) { assert.Nil(t, err) assert.Equal(t, 100, nextid) } + +func TestCluster_Resources(t *testing.T) { + mocks.On(mockConfig) + defer mocks.Off() + client := mockClient() + + cluster, err := client.Cluster() + assert.Nil(t, err) + + // json unmarshall tests + rs, err := cluster.Resources() + assert.Equal(t, 20, len(rs)) + + // type param test + rs, err = cluster.Resources("node") + assert.Equal(t, 1, len(rs)) +} diff --git a/tests/mocks/pve7x/cluster.go b/tests/mocks/pve7x/cluster.go index 8ea0906..52542ef 100644 --- a/tests/mocks/pve7x/cluster.go +++ b/tests/mocks/pve7x/cluster.go @@ -6,7 +6,6 @@ import ( ) func cluster() { - gock.New(config.C.URI). Get("/cluster/nextid"). Reply(200). @@ -67,4 +66,312 @@ func cluster() { } ] }`) + + gock.New(config.C.URI). + Get("^/cluster/resources$"). + MatchParams(map[string]string{ + "type": "node", + }). + Reply(200). + JSON(`{ + "data": [ + { + "type": "node", + "id": "node1" + } + ] +}`) + + gock.New(config.C.URI). + Get("^/cluster/resources$"). + Reply(200). + JSON(`{ + "data": [ + { + "netout": 545248946, + "type": "qemu", + "name": "server1", + "maxcpu": 1, + "mem": 842551296, + "netin": 2456116121, + "maxmem": 1073741824, + "disk": 0, + "node": "node2", + "cpu": 0.0249060195469461, + "maxdisk": 34359738368, + "vmid": 100, + "diskwrite": 6059209728, + "diskread": 4510777856, + "status": "running", + "template": 0, + "id": "qemu/100", + "uptime": 874350 + }, + { + "id": "qemu/101", + "diskwrite": 0, + "status": "stopped", + "diskread": 0, + "template": 1, + "uptime": 0, + "name": "leap154", + "maxcpu": 4, + "mem": 0, + "netin": 0, + "maxmem": 16777216000, + "netout": 0, + "type": "qemu", + "maxdisk": 68719476736, + "vmid": 101, + "disk": 0, + "node": "node1", + "cpu": 0 + }, + { + "netout": 0, + "type": "qemu", + "maxcpu": 4, + "name": "machine-test", + "maxmem": 8388608000, + "netin": 0, + "mem": 0, + "node": "node1", + "disk": 0, + "cpu": 0, + "maxdisk": 53901000704, + "vmid": 102, + "status": "stopped", + "diskwrite": 0, + "diskread": 0, + "template": 0, + "id": "qemu/102", + "uptime": 0, + "tags": "go-proxmox+cloud-init" + }, + { + "type": "qemu", + "netout": 0, + "netin": 0, + "mem": 0, + "maxmem": 8388608000, + "maxcpu": 4, + "name": "VM 200", + "cpu": 0, + "node": "node1", + "disk": 0, + "vmid": 200, + "maxdisk": 53901000704, + "template": 0, + "diskwrite": 0, + "diskread": 0, + "status": "stopped", + "id": "qemu/200", + "uptime": 0 + }, + { + "maxdisk": 482713534464, + "cpu": 0.0054917623564653, + "node": "node3", + "disk": 2983723008, + "maxmem": 16668827648, + "mem": 1681965056, + "maxcpu": 4, + "type": "node", + "uptime": 872961, + "level": "", + "id": "node/node3", + "cgroup-mode": 2, + "status": "online" + }, + { + "cgroup-mode": 2, + "status": "online", + "id": "node/node2", + "level": "", + "uptime": 874373, + "type": "node", + "mem": 8127873024, + "maxmem": 33567911936, + "maxcpu": 12, + "cpu": 0.00365387809333998, + "node": "node2", + "disk": 2797338624, + "maxdisk": 940166742016 + }, + { + "status": "online", + "cgroup-mode": 2, + "id": "node/node1", + "level": "", + "uptime": 872854, + "type": "node", + "maxcpu": 8, + "mem": 2113265664, + "maxmem": 65919459328, + "disk": 10486546432, + "node": "node1", + "cpu": 0.00336910406788121, + "maxdisk": 951055941632 + }, + { + "cgroup-mode": 2, + "status": "online", + "id": "node/node4", + "level": "", + "uptime": 872920, + "type": "node", + "mem": 1698938880, + "maxmem": 16651702272, + "maxcpu": 4, + "cpu": 0.00724094881398252, + "disk": 2789867520, + "node": "node4", + "maxdisk": 482719825920 + }, + { + "shared": 0, + "type": "storage", + "status": "available", + "plugintype": "zfspool", + "id": "storage/node3/local-zfs", + "storage": "local-zfs", + "node": "node3", + "disk": 98304, + "content": "images,rootdir", + "maxdisk": 479730032640 + }, + { + "shared": 0, + "type": "storage", + "status": "available", + "id": "storage/node2/local-zfs", + "plugintype": "zfspool", + "content": "images,rootdir", + "disk": 25294921728, + "storage": "local-zfs", + "node": "node2", + "maxdisk": 962664386560 + }, + { + "maxdisk": 955016175616, + "node": "node1", + "storage": "local-zfs", + "content": "images,rootdir", + "disk": 14446702592, + "id": "storage/node1/local-zfs", + "plugintype": "zfspool", + "status": "available", + "shared": 0, + "type": "storage" + }, + { + "type": "storage", + "shared": 0, + "status": "available", + "plugintype": "zfspool", + "id": "storage/node4/local-zfs", + "content": "images,rootdir", + "disk": 98304, + "storage": "local-zfs", + "node": "node4", + "maxdisk": 479930105856 + }, + { + "maxdisk": 482713534464, + "content": "backup,vztmpl,iso", + "disk": 2983723008, + "storage": "local", + "node": "node3", + "plugintype": "dir", + "id": "storage/node3/local", + "status": "available", + "type": "storage", + "shared": 0 + }, + { + "maxdisk": 940166742016, + "node": "node2", + "storage": "local", + "disk": 2797338624, + "content": "backup,vztmpl,iso", + "id": "storage/node2/local", + "plugintype": "dir", + "shared": 0, + "type": "storage", + "status": "available" + }, + { + "maxdisk": 951055941632, + "disk": 10486546432, + "content": "backup,vztmpl,iso", + "storage": "local", + "node": "node1", + "id": "storage/node1/local", + "plugintype": "dir", + "status": "available", + "shared": 0, + "type": "storage" + }, + { + "plugintype": "dir", + "id": "storage/node4/local", + "status": "available", + "shared": 0, + "type": "storage", + "maxdisk": 482719825920, + "storage": "local", + "node": "node4", + "content": "backup,vztmpl,iso", + "disk": 2789867520 + }, + { + "plugintype": "dir", + "id": "storage/node3/cloud-init", + "status": "available", + "type": "storage", + "shared": 0, + "maxdisk": 482713534464, + "content": "snippets", + "disk": 2983723008, + "storage": "cloud-init", + "node": "node3" + }, + { + "plugintype": "dir", + "id": "storage/node2/cloud-init", + "status": "available", + "type": "storage", + "shared": 0, + "maxdisk": 940166742016, + "disk": 2797338624, + "content": "snippets", + "node": "node2", + "storage": "cloud-init" + }, + { + "disk": 10486546432, + "content": "snippets", + "node": "node1", + "storage": "cloud-init", + "maxdisk": 951055941632, + "status": "available", + "type": "storage", + "shared": 0, + "id": "storage/node1/cloud-init", + "plugintype": "dir" + }, + { + "id": "storage/node4/cloud-init", + "plugintype": "dir", + "type": "storage", + "shared": 0, + "status": "available", + "maxdisk": 482719825920, + "content": "snippets", + "disk": 2789867520, + "storage": "cloud-init", + "node": "node4" + } + ] +}`) }