Skip to content

Commit

Permalink
feat: type QemuDiskSize
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinyblargon committed Feb 1, 2024
1 parent f32d302 commit 917baef
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 58 deletions.
47 changes: 40 additions & 7 deletions proxmox/config_qemu_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ type qemuDisk struct {
ReadOnly bool // Only set for scsi,virtio
Replicate bool
Serial QemuDiskSerial
SizeInKibibytes uint
SizeInKibibytes QemuDiskSize
// TODO custom type
Storage string // Only set for Disk
Type qemuDiskType
Expand All @@ -210,7 +210,6 @@ type qemuDisk struct {
const (
Error_QemuDisk_File string = "file may not be empty"
Error_QemuDisk_MutuallyExclusive string = "settings cdrom,cloudinit,disk,passthrough are mutually exclusive"
Error_QemuDisk_Size string = "size must be greater then 0"
Error_QemuDisk_Storage string = "storage may not be empty"
)

Expand Down Expand Up @@ -425,7 +424,7 @@ func (qemuDisk) mapToStruct(diskData string, settings map[string]interface{}, li
disk.Serial = QemuDiskSerial(value.(string))
}
if value, isSet := settings["size"]; isSet {
disk.SizeInKibibytes = kibibyteParse(value.(string))
disk.SizeInKibibytes = QemuDiskSize(0).parse(value.(string))
}
if value, isSet := settings["ssd"]; isSet {
disk.EmulateSSD, _ = strconv.ParseBool(value.(string))
Expand Down Expand Up @@ -527,8 +526,8 @@ func (disk *qemuDisk) validate() (err error) {
if err = disk.Format.Validate(); err != nil {
return
}
if disk.SizeInKibibytes == 0 {
return errors.New(Error_QemuDisk_Size)
if err = disk.SizeInKibibytes.Validate(); err != nil {
return
}
if disk.Storage == "" {
return errors.New(Error_QemuDisk_Storage)
Expand Down Expand Up @@ -790,7 +789,7 @@ func (id QemuDiskId) Validate() error {
type qemuDiskMark struct {
Format QemuDiskFormat
Id QemuDiskId
Size uint
Size QemuDiskSize
Storage string
Type qemuDiskType
}
Expand Down Expand Up @@ -843,9 +842,43 @@ func (serial QemuDiskSerial) Validate() error {
return nil
}

// Amount of Kibibytes the disk should be.
// Disk size must be greater then 4096.
type QemuDiskSize uint

const (
QemuDiskSize_Error_Minimum string = "disk size must be greater then 4096"
qemuDiskSize_Minimum QemuDiskSize = 4097
mebibyte QemuDiskSize = 1024
gibibyte QemuDiskSize = 1048576
tebibyte QemuDiskSize = 1073741824
)

func (QemuDiskSize) parse(rawSize string) (size QemuDiskSize) {
tmpSize, _ := strconv.ParseInt(rawSize[:len(rawSize)-1], 10, 0)
switch rawSize[len(rawSize)-1:] {
case "T":
size = QemuDiskSize(tmpSize) * tebibyte
case "G":
size = QemuDiskSize(tmpSize) * gibibyte
case "M":
size = QemuDiskSize(tmpSize) * mebibyte
case "K":
size = QemuDiskSize(tmpSize)
}
return
}

func (size QemuDiskSize) Validate() error {
if size < qemuDiskSize_Minimum {
return errors.New(QemuDiskSize_Error_Minimum)
}
return nil
}

type qemuDiskResize struct {
Id QemuDiskId
SizeInKibibytes uint
SizeInKibibytes QemuDiskSize
}

// Increase the disk size to the specified amount in gigabytes
Expand Down
4 changes: 2 additions & 2 deletions proxmox/config_qemu_disk_ide.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type QemuIdeDisk struct {
LinkedDiskId *uint `json:"linked"` //LinkedClone is only returned and setting it has no effect
Replicate bool `json:"replicate"`
Serial QemuDiskSerial `json:"serial,omitempty"`
SizeInKibibytes uint `json:"size"`
SizeInKibibytes QemuDiskSize `json:"size"`
Storage string `json:"storage"`
syntax diskSyntaxEnum
WorldWideName QemuWorldWideName `json:"wwn"`
Expand Down Expand Up @@ -178,7 +178,7 @@ type QemuIdePassthrough struct {
File string `json:"file"`
Replicate bool `json:"replicate"`
Serial QemuDiskSerial `json:"serial,omitempty"`
SizeInKibibytes uint `json:"size"` //size is only returned and setting it has no effect
SizeInKibibytes QemuDiskSize `json:"size"` //size is only returned and setting it has no effect
WorldWideName QemuWorldWideName `json:"wwn"`
}

Expand Down
4 changes: 2 additions & 2 deletions proxmox/config_qemu_disk_sata.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type QemuSataDisk struct {
LinkedDiskId *uint `json:"linked"` //LinkedClone is only returned and setting it has no effect
Replicate bool `json:"replicate"`
Serial QemuDiskSerial `json:"serial,omitempty"`
SizeInKibibytes uint `json:"size"`
SizeInKibibytes QemuDiskSize `json:"size"`
Storage string `json:"storage"`
syntax diskSyntaxEnum
WorldWideName QemuWorldWideName `json:"wwn"`
Expand Down Expand Up @@ -188,7 +188,7 @@ type QemuSataPassthrough struct {
File string `json:"file"`
Replicate bool `json:"replicate"`
Serial QemuDiskSerial `json:"serial,omitempty"`
SizeInKibibytes uint `json:"size"` //size is only returned and setting it has no effect
SizeInKibibytes QemuDiskSize `json:"size"` //size is only returned and setting it has no effect
WorldWideName QemuWorldWideName `json:"wwn"`
}

Expand Down
4 changes: 2 additions & 2 deletions proxmox/config_qemu_disk_scsi.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type QemuScsiDisk struct {
ReadOnly bool `json:"readonly"`
Replicate bool `json:"replicate"`
Serial QemuDiskSerial `json:"serial,omitempty"`
SizeInKibibytes uint `json:"size"`
SizeInKibibytes QemuDiskSize `json:"size"`
Storage string `json:"storage"`
syntax diskSyntaxEnum
WorldWideName QemuWorldWideName `json:"wwn"`
Expand Down Expand Up @@ -344,7 +344,7 @@ type QemuScsiPassthrough struct {
ReadOnly bool `json:"readonly"`
Replicate bool `json:"replicate"`
Serial QemuDiskSerial `json:"serial,omitempty"`
SizeInKibibytes uint `json:"size"` //size is only returned and setting it has no effect
SizeInKibibytes QemuDiskSize `json:"size"` //size is only returned and setting it has no effect
WorldWideName QemuWorldWideName `json:"wwn"`
}

Expand Down
4 changes: 2 additions & 2 deletions proxmox/config_qemu_disk_virtio.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type QemuVirtIODisk struct {
ReadOnly bool `json:"readonly"`
Replicate bool `json:"replicate"`
Serial QemuDiskSerial `json:"serial,omitempty"`
SizeInKibibytes uint `json:"size"`
SizeInKibibytes QemuDiskSize `json:"size"`
Storage string `json:"storage"`
syntax diskSyntaxEnum
WorldWideName QemuWorldWideName `json:"wwn"`
Expand Down Expand Up @@ -251,7 +251,7 @@ type QemuVirtIOPassthrough struct {
ReadOnly bool `json:"readonly"`
Replicate bool `json:"replicate"`
Serial QemuDiskSerial `json:"serial,omitempty"`
SizeInKibibytes uint `json:"size"` //size is only returned and setting it has no effect
SizeInKibibytes QemuDiskSize `json:"size"` //size is only returned and setting it has no effect
WorldWideName QemuWorldWideName `json:"wwn"`
}

Expand Down
38 changes: 19 additions & 19 deletions proxmox/config_qemu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5949,7 +5949,7 @@ func Test_ConfigQemu_Validate(t *testing.T) {
Bandwidth: BandwidthValid0,
Cache: QemuDiskCache_DirectSync,
Format: QemuDiskFormat_Raw,
SizeInKibibytes: 32,
SizeInKibibytes: 5748543,
Storage: "test",
WorldWideName: "0x500A1B2C3D4E5F60",
}}},
Expand All @@ -5958,7 +5958,7 @@ func Test_ConfigQemu_Validate(t *testing.T) {
Bandwidth: BandwidthValid1,
Cache: QemuDiskCache_None,
Format: QemuDiskFormat_Cow,
SizeInKibibytes: 1,
SizeInKibibytes: 4097,
Storage: "test",
WorldWideName: "0x500F123456789ABC",
}}},
Expand All @@ -5967,7 +5967,7 @@ func Test_ConfigQemu_Validate(t *testing.T) {
Bandwidth: BandwidthValid2,
Cache: QemuDiskCache_WriteBack,
Format: QemuDiskFormat_Qcow2,
SizeInKibibytes: 10,
SizeInKibibytes: 9475478,
Storage: "test",
WorldWideName: "0x5009876543210DEF",
}}},
Expand All @@ -5976,7 +5976,7 @@ func Test_ConfigQemu_Validate(t *testing.T) {
Bandwidth: BandwidthValid3,
Cache: "",
Format: QemuDiskFormat_Vmdk,
SizeInKibibytes: 1024,
SizeInKibibytes: 18742,
Storage: "test",
WorldWideName: "0x500C0D0E0F101112",
}}},
Expand Down Expand Up @@ -6581,17 +6581,17 @@ func Test_ConfigQemu_Validate(t *testing.T) {
}}}}},
err: errors.New(Error_QemuDiskSerial_IllegalLength),
},
{name: `Invalid Disks Disk Ide errors.New(Error_QemuDisk_Size)`,
{name: `Invalid Disks Disk Ide errors.New(QemuDiskSize_Error_Minimum)`,
input: ConfigQemu{Disks: &QemuStorages{Ide: &QemuIdeDisks{Disk_2: &QemuIdeStorage{Disk: &QemuIdeDisk{
Format: QemuDiskFormat_Raw,
SizeInKibibytes: 0,
SizeInKibibytes: 4096,
}}}}},
err: errors.New(Error_QemuDisk_Size),
err: errors.New(QemuDiskSize_Error_Minimum),
},
{name: `Invalid Disks Disk Ide errors.New(Error_QemuDisk_Storage)`,
input: ConfigQemu{Disks: &QemuStorages{Ide: &QemuIdeDisks{Disk_3: &QemuIdeStorage{Disk: &QemuIdeDisk{
Format: QemuDiskFormat_Raw,
SizeInKibibytes: 32,
SizeInKibibytes: qemuDiskSize_Minimum,
Storage: "",
}}}}},
err: errors.New(Error_QemuDisk_Storage),
Expand Down Expand Up @@ -6668,17 +6668,17 @@ func Test_ConfigQemu_Validate(t *testing.T) {
}}}}},
err: errors.New(Error_QemuDiskSerial_IllegalLength),
},
{name: `Invalid Disks Disk Sata errors.New(Error_QemuDisk_Size)`,
{name: `Invalid Disks Disk Sata errors.New(QemuDiskSize_Error_Minimum)`,
input: ConfigQemu{Disks: &QemuStorages{Sata: &QemuSataDisks{Disk_2: &QemuSataStorage{Disk: &QemuSataDisk{
Format: QemuDiskFormat_Raw,
SizeInKibibytes: 0,
SizeInKibibytes: 4096,
}}}}},
err: errors.New(Error_QemuDisk_Size),
err: errors.New(QemuDiskSize_Error_Minimum),
},
{name: `Invalid Disks Disk Sata errors.New(Error_QemuDisk_Storage)`,
input: ConfigQemu{Disks: &QemuStorages{Sata: &QemuSataDisks{Disk_3: &QemuSataStorage{Disk: &QemuSataDisk{
Format: QemuDiskFormat_Raw,
SizeInKibibytes: 32,
SizeInKibibytes: qemuDiskSize_Minimum,
Storage: "",
}}}}},
err: errors.New(Error_QemuDisk_Storage),
Expand Down Expand Up @@ -6755,17 +6755,17 @@ func Test_ConfigQemu_Validate(t *testing.T) {
}}}}},
err: errors.New(Error_QemuDiskSerial_IllegalLength),
},
{name: `Invalid Disks Disk Scsi errors.New(Error_QemuDisk_Size)`,
{name: `Invalid Disks Disk Scsi errors.New(QemuDiskSize_Error_Minimum)`,
input: ConfigQemu{Disks: &QemuStorages{Scsi: &QemuScsiDisks{Disk_14: &QemuScsiStorage{Disk: &QemuScsiDisk{
Format: QemuDiskFormat_Raw,
SizeInKibibytes: 0,
}}}}},
err: errors.New(Error_QemuDisk_Size),
err: errors.New(QemuDiskSize_Error_Minimum),
},
{name: `Invalid Disks Disk Scsi errors.New(Error_QemuDisk_Storage)`,
input: ConfigQemu{Disks: &QemuStorages{Scsi: &QemuScsiDisks{Disk_15: &QemuScsiStorage{Disk: &QemuScsiDisk{
Format: QemuDiskFormat_Raw,
SizeInKibibytes: 32,
SizeInKibibytes: qemuDiskSize_Minimum,
Storage: "",
}}}}},
err: errors.New(Error_QemuDisk_Storage),
Expand Down Expand Up @@ -6842,17 +6842,17 @@ func Test_ConfigQemu_Validate(t *testing.T) {
}}}}},
err: errors.New(Error_QemuDiskSerial_IllegalLength),
},
{name: `Invalid Disks Disk VirtIO errors.New(Error_QemuDisk_Size)`,
{name: `Invalid Disks Disk VirtIO errors.New(QemuDiskSize_Error_Minimum)`,
input: ConfigQemu{Disks: &QemuStorages{VirtIO: &QemuVirtIODisks{Disk_14: &QemuVirtIOStorage{Disk: &QemuVirtIODisk{
Format: QemuDiskFormat_Raw,
SizeInKibibytes: 0,
SizeInKibibytes: 1024,
}}}}},
err: errors.New(Error_QemuDisk_Size),
err: errors.New(QemuDiskSize_Error_Minimum),
},
{name: `Invalid Disks Disk VirtIO errors.New(Error_QemuDisk_Storage)`,
input: ConfigQemu{Disks: &QemuStorages{VirtIO: &QemuVirtIODisks{Disk_15: &QemuVirtIOStorage{Disk: &QemuVirtIODisk{
Format: QemuDiskFormat_Raw,
SizeInKibibytes: 32,
SizeInKibibytes: qemuDiskSize_Minimum,
Storage: "",
}}}}},
err: errors.New(Error_QemuDisk_Storage),
Expand Down
9 changes: 0 additions & 9 deletions proxmox/constants.go

This file was deleted.

15 changes: 0 additions & 15 deletions proxmox/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,21 +234,6 @@ func floatToTrimmedString(f float64, maxDecimals uint8) (s string) {
return
}

func kibibyteParse(rawSize string) (size uint) {
tmpSize, _ := strconv.ParseInt(rawSize[:len(rawSize)-1], 10, 0)
switch rawSize[len(rawSize)-1:] {
case "T":
size = uint(tmpSize) * tebibyte
case "G":
size = uint(tmpSize) * gibibyte
case "M":
size = uint(tmpSize) * mebibyte
case "K":
size = uint(tmpSize)
}
return
}

func splitStringOfSettings(settings string) map[string]interface{} {
settingValuePairs := strings.Split(settings, ",")
settingMap := map[string]interface{}{}
Expand Down

0 comments on commit 917baef

Please sign in to comment.