diff --git a/cluster.go b/cluster.go index b61b1a2..f780c3e 100644 --- a/cluster.go +++ b/cluster.go @@ -51,32 +51,32 @@ type GetClusterResourcesResponse struct { // 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"` + 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 *IntOrString `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 diff --git a/cluster_test.go b/cluster_test.go index b50c70c..b733a4b 100644 --- a/cluster_test.go +++ b/cluster_test.go @@ -20,6 +20,10 @@ func testFloat64(f float64) *float64 { return &f } +func testIntOrString(is IntOrString) *IntOrString { + return &is +} + func TestGetClusterResources(t *testing.T) { mux, server, client := setup(t) defer teardown(server) @@ -53,7 +57,7 @@ func TestGetClusterResources(t *testing.T) { Template: testInt(0), Type: "qemu", Uptime: testInt(234806), - VMID: testInt(101), + VMID: testIntOrString("101"), }, { CgroupMode: testInt(2), diff --git a/nodes.go b/nodes.go index 9a589d2..ddd4b1f 100644 --- a/nodes.go +++ b/nodes.go @@ -26,7 +26,7 @@ type GetNodesData struct { MaxMem int `json:"maxmem"` Mem int `json:"mem"` Node string `json:"node"` - SslFingerprint string `json:"ssl_fingerprint"` + SSLFingerprint string `json:"ssl_fingerprint"` Status string `json:"status"` Type string `json:"type"` Uptime int `json:"uptime"` @@ -62,12 +62,12 @@ type GetNodeStatusData struct { CPUInfo CPUInfo `json:"cpuinfo"` CurrentKernel CurrentKernel `json:"current-kernel"` Idle int `json:"idle"` - Ksm Ksm `json:"ksm"` + KSM KSM `json:"ksm"` Kversion string `json:"kversion"` LoadAvg []string `json:"loadavg"` Memory Memory `json:"memory"` PveVersion string `json:"pveversion"` - RootFs RootFs `json:"rootfs"` + RootFs RootFS `json:"rootfs"` Swap Swap `json:"swap"` Uptime int `json:"uptime"` Wait float64 `json:"wait"` @@ -129,21 +129,21 @@ type GetNodeQemuResponse struct { // GetNodeQemuData contains data of one VM from a GetNodeQemu response type GetNodeQemuData struct { - CPU float64 `json:"cpu"` - Cpus int `json:"cpus"` - Disk int `json:"disk"` - DiskRead int `json:"diskread"` - DiskWrite int `json:"diskwrite"` - MaxDisk int `json:"maxdisk"` - MaxMem int `json:"maxmem"` - Mem int `json:"mem"` - Name string `json:"name"` - NetIn int `json:"netin"` - NetOut int `json:"netout"` - Pid int `json:"pid"` - Status string `json:"status"` - Uptime int `json:"uptime"` - VMID int `json:"vmid"` + CPU float64 `json:"cpu"` + CPUs int `json:"cpus"` + Disk int `json:"disk"` + DiskRead int `json:"diskread"` + DiskWrite int `json:"diskwrite"` + MaxDisk int `json:"maxdisk"` + MaxMem int `json:"maxmem"` + Mem int `json:"mem"` + Name string `json:"name"` + NetIn int `json:"netin"` + NetOut int `json:"netout"` + PID int `json:"pid"` + Status string `json:"status"` + Uptime int `json:"uptime"` + VMID IntOrString `json:"vmid"` } // GetNodeQemu makes a GET request to the /nodes/{node}/qemu endpoint @@ -171,22 +171,22 @@ type GetNodeLxcResponse struct { // GetNodeLxcData contains data of one VM from a GetNodeLxc response type GetNodeLxcData struct { - CPU float64 `json:"cpu"` - Cpus int `json:"cpus"` - Disk int `json:"disk"` - DiskRead int `json:"diskread"` - DiskWrite int `json:"diskwrite"` - MaxDisk int `json:"maxdisk"` - MaxMem int `json:"maxmem"` - MaxSwap int `json:"maxswap"` - Mem int `json:"mem"` - Name string `json:"name"` - NetIn int `json:"netin"` - NetOut int `json:"netout"` - Status string `json:"status"` - Type string `json:"type"` - Uptime int `json:"uptime"` - VMID string `json:"vmid"` + CPU float64 `json:"cpu"` + CPUs int `json:"cpus"` + Disk int `json:"disk"` + DiskRead int `json:"diskread"` + DiskWrite int `json:"diskwrite"` + MaxDisk int `json:"maxdisk"` + MaxMem int `json:"maxmem"` + MaxSwap int `json:"maxswap"` + Mem int `json:"mem"` + Name string `json:"name"` + NetIn int `json:"netin"` + NetOut int `json:"netout"` + Status string `json:"status"` + Type string `json:"type"` + Uptime int `json:"uptime"` + VMID IntOrString `json:"vmid"` } // GetNodeLxc makes a GET request to the /nodes/{node}/lxc endpoint @@ -260,7 +260,7 @@ type GetNodeCertificatesInfoData struct { Issuer string `json:"issuer"` NotAfter int `json:"notafter"` NotBefore int `json:"notbefore"` - Pem string `json:"pem"` + PEM string `json:"pem"` PublicKeyBits int `json:"public-key-bits"` PublicKeyType string `json:"public-key-type"` San []string `json:"san"` diff --git a/nodes_test.go b/nodes_test.go index 9ea7a42..bb0c897 100644 --- a/nodes_test.go +++ b/nodes_test.go @@ -23,9 +23,9 @@ func TestGetNodes(t *testing.T) { want := GetNodesResponse{ Data: []GetNodesData{ - {CPU: 0.0522061746389294, Disk: 5513633792, ID: "node/srv3", Level: "", MaxCPU: 8, MaxDisk: 100861726720, MaxMem: 16367738880, Mem: 14766223360, Node: "srv3", SslFingerprint: "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00", Status: "online", Type: "node", Uptime: 418333}, - {CPU: 0.0220677146311971, Disk: 5727686656, ID: "node/srv1", Level: "", MaxCPU: 16, MaxDisk: 100861726720, MaxMem: 134850498560, Mem: 45189853184, Node: "srv1", SslFingerprint: "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00", Status: "online", Type: "node", Uptime: 418326}, - {CPU: 0.0673548074849297, Disk: 5488590848, ID: "node/srv2", Level: "", MaxCPU: 8, MaxDisk: 100861726720, MaxMem: 16367742976, Mem: 13080690688, Node: "srv2", SslFingerprint: "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00", Status: "online", Type: "node", Uptime: 418322}, + {CPU: 0.0522061746389294, Disk: 5513633792, ID: "node/srv3", Level: "", MaxCPU: 8, MaxDisk: 100861726720, MaxMem: 16367738880, Mem: 14766223360, Node: "srv3", SSLFingerprint: "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00", Status: "online", Type: "node", Uptime: 418333}, + {CPU: 0.0220677146311971, Disk: 5727686656, ID: "node/srv1", Level: "", MaxCPU: 16, MaxDisk: 100861726720, MaxMem: 134850498560, Mem: 45189853184, Node: "srv1", SSLFingerprint: "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00", Status: "online", Type: "node", Uptime: 418326}, + {CPU: 0.0673548074849297, Disk: 5488590848, ID: "node/srv2", Level: "", MaxCPU: 8, MaxDisk: 100861726720, MaxMem: 16367742976, Mem: 13080690688, Node: "srv2", SSLFingerprint: "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00", Status: "online", Type: "node", Uptime: 418322}, }, } @@ -52,15 +52,15 @@ func TestGetNode(t *testing.T) { Data: GetNodeStatusData{ BootInfo: BootInfo{Mode: "efi", SecureBoot: 0}, CPU: 0.0282238002623366, - CPUInfo: CPUInfo{Cores: 50, Cpus: 200, Flags: "fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx avx512f avx512dq rdseed adx smap avx512ifma clflushopt intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req vnmi avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq rdpid fsrm md_clear flush_l1d arch_capabilities", Hvm: "1", Mhz: "4886.225", Model: "99th Gen Intel(R) Core(TM) i19-9000 @ 6.50GHz", Sockets: 1, UserHz: 100}, - CurrentKernel: CurrentKernel{Machine: "x86_64", Release: "6.5.11-8-pve", Sysname: "Linux", Version: "#1 SMP PREEMPT_DYNAMIC PMX 6.5.11-8 (2024-01-30T12:27Z)"}, + CPUInfo: CPUInfo{Cores: 50, CPUs: 200, Flags: "fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx avx512f avx512dq rdseed adx smap avx512ifma clflushopt intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req vnmi avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq rdpid fsrm md_clear flush_l1d arch_capabilities", HVM: "1", MHz: "4886.225", Model: "99th Gen Intel(R) Core(TM) i19-9000 @ 6.50GHz", Sockets: 1, UserHz: 100}, + CurrentKernel: CurrentKernel{Machine: "x86_64", Release: "6.5.11-8-pve", SysName: "Linux", Version: "#1 SMP PREEMPT_DYNAMIC PMX 6.5.11-8 (2024-01-30T12:27Z)"}, Idle: 0, - Ksm: Ksm{Shared: 0}, + KSM: KSM{Shared: 0}, Kversion: "Linux 6.5.11-8-pve #1 SMP PREEMPT_DYNAMIC PMX 6.5.11-8 (2024-01-30T12:27Z)", LoadAvg: []string{"0.53", "0.46", "0.43"}, Memory: Memory{Free: 89574653952, Total: 134850498560, Used: 45275844608}, PveVersion: "pve-manager/0.0.0/0000000000000000", - RootFs: RootFs{Avail: 89962983424, Free: 95133720576, Total: 100861726720, Used: 5728006144}, + RootFs: RootFS{Avail: 89962983424, Free: 95133720576, Total: 100861726720, Used: 5728006144}, Swap: Swap{Free: 8589930496, Total: 8589930496, Used: 0}, Uptime: 419090, Wait: 0.00150768163794533, @@ -72,3 +72,86 @@ func TestGetNode(t *testing.T) { require.NotNil(t, resp) require.Equal(t, want, *r) } + +func TestGetNodeLxc(t *testing.T) { + mux, server, client := setup(t) + defer teardown(server) + + mux.HandleFunc("/api2/json/nodes/srv1/lxc", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + _, err := fmt.Fprint(w, fixture("nodes/get_node_lxc.json")) + if err != nil { + return + } + }) + + want := GetNodeLxcResponse{ + Data: []GetNodeLxcData{ + { + CPU: 0, + CPUs: 1, + Disk: 0, + DiskRead: 0, + DiskWrite: 0, + MaxDisk: 8589934592, + MaxMem: 536870912, + MaxSwap: 536870912, + Mem: 0, + Name: "CT103", + NetIn: 0, + NetOut: 0, + Status: "stopped", + Type: "lxc", + Uptime: 0, + VMID: IntOrString("103"), + }, + }, + } + + r, resp, err := client.Nodes.GetNodeLxc("srv1") + require.NoError(t, err) + require.NotNil(t, resp) + require.Equal(t, want, *r) +} + +func TestGetNodeQemu(t *testing.T) { + mux, server, client := setup(t) + defer teardown(server) + + mux.HandleFunc("/api2/json/nodes/srv1/qemu", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + _, err := fmt.Fprint(w, fixture("nodes/get_node_qemu.json")) + if err != nil { + return + } + }) + + want := GetNodeQemuResponse{ + Data: []GetNodeQemuData{ + { + CPU: 0.0156071608339279, + CPUs: 5, + Disk: 0, + DiskRead: 0, + DiskWrite: 0, + MaxDisk: 274877906944, + MaxMem: 8589934592, + Mem: 3072665958, + PID: 1551, + Name: "test", + NetIn: 294680188, + NetOut: 200064110, + Status: "running", + Uptime: 28661, + VMID: IntOrString("104"), + }, + }, + } + + r, resp, err := client.Nodes.GetNodeQemu("srv1") + require.NoError(t, err) + require.NotNil(t, resp) + require.Equal(t, want, *r) +} diff --git a/testdata/nodes/get_node_lxc.json b/testdata/nodes/get_node_lxc.json new file mode 100644 index 0000000..91f92cd --- /dev/null +++ b/testdata/nodes/get_node_lxc.json @@ -0,0 +1,23 @@ +{ + "data":[ + { + "status":"stopped", + "maxdisk":8589934592, + "maxswap":536870912, + "swap":0, + "netin":0, + "cpu":0, + "cpus":1, + "type":"lxc", + "maxmem":536870912, + "name":"CT103", + "disk":0, + "diskwrite":0, + "vmid":103, + "uptime":0, + "diskread":0, + "netout":0, + "mem":0 + } + ] +} diff --git a/testdata/nodes/get_node_qemu.json b/testdata/nodes/get_node_qemu.json new file mode 100644 index 0000000..8d913b4 --- /dev/null +++ b/testdata/nodes/get_node_qemu.json @@ -0,0 +1,21 @@ +{ + "data":[ + { + "status":"running", + "cpus":5, + "netout":200064110, + "name":"test", + "vmid":104, + "netin":294680188, + "mem":3072665958, + "pid":1551, + "uptime":28661, + "maxdisk":274877906944, + "diskwrite":0, + "diskread":0, + "disk":0, + "cpu":0.0156071608339279, + "maxmem":8589934592 + } + ] +} diff --git a/types.go b/types.go index cafe206..585a8e5 100644 --- a/types.go +++ b/types.go @@ -1,5 +1,11 @@ package proxmox +import ( + "encoding/json" + "fmt" + "strconv" +) + // BootInfo info about host boot type BootInfo struct { Mode string `json:"mode"` @@ -9,10 +15,10 @@ type BootInfo struct { // CPUInfo info about host CPU type CPUInfo struct { Cores int `json:"cores"` - Cpus int `json:"cpus"` + CPUs int `json:"cpus"` Flags string `json:"flags"` - Hvm string `json:"hvm"` - Mhz string `json:"mhz"` + HVM string `json:"hvm"` + MHz string `json:"mhz"` Model string `json:"model"` Sockets int `json:"sockets"` UserHz int `json:"user_hz"` @@ -22,12 +28,12 @@ type CPUInfo struct { type CurrentKernel struct { Machine string `json:"machine"` Release string `json:"release"` - Sysname string `json:"sysname"` + SysName string `json:"sysname"` Version string `json:"version"` } -// Ksm info about Kernel same-page merging -type Ksm struct { +// KSM info about Kernel same-page merging +type KSM struct { Shared int `json:"shared"` } @@ -38,8 +44,8 @@ type Memory struct { Used int `json:"used"` } -// RootFs info about the host root filesystem -type RootFs struct { +// RootFS info about the host root filesystem +type RootFS struct { Avail int `json:"avail"` Free int `json:"free"` Total int `json:"total"` @@ -52,3 +58,29 @@ type Swap struct { Total int `json:"total"` Used int `json:"used"` } + +// IntOrString is an alias for some returns from the Proxmox API where we've identified that some versions return a string, and others return an integer +// For example, LXC VMIDs were a string return in PVE 8.1.x, and are an integer in 8.2.x +// Since it can be either depending on the version of Proxmox queried, we're going to return the looser type. String in this case. +type IntOrString string + +// UnmarshalJSON implements the json.Unmarshaler interface for IntOrString types +func (is *IntOrString) UnmarshalJSON(data []byte) error { + // attempt to unmarshal into an integer + var i int + var intErr error + if intErr = json.Unmarshal(data, &i); intErr == nil && i != 0 { + *is = IntOrString(strconv.Itoa(i)) + return nil + } + + // attempt to unmarshal into a string + var str string + var strErr error + if strErr = json.Unmarshal(data, &str); strErr == nil && str != "" { + *is = IntOrString(str) + return nil + } + + return fmt.Errorf("failed to unmarshal as either int or string: int err: %s | string err: %s", intErr.Error(), strErr.Error()) +} diff --git a/types_test.go b/types_test.go new file mode 100644 index 0000000..f364c79 --- /dev/null +++ b/types_test.go @@ -0,0 +1,43 @@ +package proxmox + +import ( + "encoding/json" + "github.com/stretchr/testify/require" + "testing" +) + +type testIntOrStringExample struct { + ID IntOrString `json:"id"` +} + +// TestUnmarshalIntOrString_Positive tests the unmarshalling of valid IntOrString values. +func TestUnmarshalIntOrString_Positive(t *testing.T) { + tests := []struct { + input string + expected string + }{ + {`{"id": 12345}`, "12345"}, + {`{"id": "abc123"}`, "abc123"}, + } + + for _, test := range tests { + var example testIntOrStringExample + err := json.Unmarshal([]byte(test.input), &example) + require.NoError(t, err) + require.Equal(t, IntOrString(test.expected), example.ID) + } +} + +// TestUnmarshalIntOrString_Negative tests the unmarshalling of invalid IntOrString values. +func TestUnmarshalIntOrString_Negative(t *testing.T) { + tests := []string{ + `{"id": {"nested": "object"}}`, + `{"id": [12345]}`, + } + + for _, test := range tests { + var example testIntOrStringExample + err := json.Unmarshal([]byte(test), &example) + require.Error(t, err) + } +}