diff --git a/internal/pve/storage.go b/internal/pve/storage.go index 2bdb46b..2b291c5 100644 --- a/internal/pve/storage.go +++ b/internal/pve/storage.go @@ -2,6 +2,7 @@ package pve import ( "context" + "encoding/json" "io" "net/url" ) @@ -61,6 +62,29 @@ type Volume struct { Notes string `json:"notes,omitempty"` } +// UnmarshalJSON accepts PVE's numbers as JSON numbers or numeric strings. +// On 2026-10-28, `pvecli storage content local-lvm` failed on the whole +// listing with "json: cannot unmarshal string into Go struct field .0.ctime +// of type int64": the API typed ctime as a string for one volume, the same +// drift flexInt already absorbs for access tokens. size, used and vmid get the +// same treatment, since they come from the same untyped source. +func (v *Volume) UnmarshalJSON(raw []byte) error { + type plain Volume + var aux struct { + plain + Size flexInt `json:"size,omitempty"` + Used flexInt `json:"used,omitempty"` + VMID flexInt `json:"vmid,omitempty"` + CTime flexInt `json:"ctime,omitempty"` + } + if err := json.Unmarshal(raw, &aux); err != nil { + return err + } + *v = Volume(aux.plain) + v.Size, v.Used, v.VMID, v.CTime = aux.Size.Int(), aux.Used.Int(), int(aux.VMID.Int()), aux.CTime.Int() + return nil +} + // StorageContent lists what a storage holds, optionally filtered by content // type (iso, vztmpl, backup, images, rootdir, snippets). // diff --git a/internal/pve/storage_volume_test.go b/internal/pve/storage_volume_test.go new file mode 100644 index 0000000..7ad3944 --- /dev/null +++ b/internal/pve/storage_volume_test.go @@ -0,0 +1,26 @@ +package pve + +import ( + "encoding/json" + "testing" +) + +// PVE returned ctime as a string for one volume and a number for another in +// the same listing (local-lvm, 2026-10-28); both must decode. +func TestVolumeDecodesNumbersAndNumericStrings(t *testing.T) { + raw := `[{"volid":"local-lvm:vm-242-disk-0","content":"images","size":25769803776,"vmid":242,"ctime":1730000000}, + {"volid":"local:iso/seed.iso","content":"iso","size":"374784","used":"0","vmid":"0","ctime":"1730000001"}]` + var volumes []Volume + if err := json.Unmarshal([]byte(raw), &volumes); err != nil { + t.Fatalf("decode: %v", err) + } + if volumes[0].CTime != 1730000000 || volumes[0].VMID != 242 || volumes[0].Size != 25769803776 { + t.Fatalf("numeric volume = %+v", volumes[0]) + } + if volumes[1].CTime != 1730000001 || volumes[1].Size != 374784 || volumes[1].VolID != "local:iso/seed.iso" { + t.Fatalf("string-typed volume = %+v", volumes[1]) + } + if err := json.Unmarshal([]byte(`[{"volid":"x","ctime":"soon"}]`), &volumes); err == nil { + t.Fatal("a non-numeric ctime must still be refused") + } +}