Skip to content

Commit

Permalink
fix lints
Browse files Browse the repository at this point in the history
  • Loading branch information
ForsakenHarmony committed Jun 26, 2023
1 parent 5d0d48e commit 31ccb36
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 47 deletions.
8 changes: 2 additions & 6 deletions proxmox/types/disk_size.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ func (r DiskSize) InGigabytes() int {
return int(int64(r) / 1024 / 1024 / 1024)
}

// DiskSizeFromMegabytes creates a DiskSize from megabytes.
func DiskSizeFromMegabytes(size int) DiskSize {
return DiskSize(size * 1024 * 1024)
}

// DiskSizeFromGigabytes creates a DiskSize from gigabytes.
func DiskSizeFromGigabytes(size int) DiskSize {
return DiskSize(size * 1024 * 1024 * 1024)
Expand Down Expand Up @@ -71,7 +66,7 @@ func (r *DiskSize) UnmarshalJSON(b []byte) error {
return nil
}

// parseDiskSize parses a disk size string into a number of bytes.
// ParseDiskSize parses a disk size string into a number of bytes.
func ParseDiskSize(size string) (DiskSize, error) {
matches := sizeRegex.FindStringSubmatch(size)
if len(matches) > 0 {
Expand All @@ -97,6 +92,7 @@ func ParseDiskSize(size string) (DiskSize, error) {
return -1, fmt.Errorf("cannot parse disk size \"%s\"", size)
}

// FormatDiskSize turns a number of bytes into a disk size string.
func FormatDiskSize(size DiskSize) string {
if size < 0 {
return ""
Expand Down
39 changes: 19 additions & 20 deletions proxmox/types/disk_size_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,36 @@ func TestParseDiskSize(t *testing.T) {

tests := []struct {
name string
size *string
size string
want int64
wantErr bool
}{
{"handle null size", nil, 0, false},
{"parse TB", StrPtr("2TB"), 2199023255552, false},
{"parse T", StrPtr("2T"), 2199023255552, false},
{"parse fraction T", StrPtr("2.2T"), 2418925581108, false},
{"parse GB", StrPtr("2GB"), 2147483648, false},
{"parse G", StrPtr("2G"), 2147483648, false},
{"parse M", StrPtr("2048M"), 2147483648, false},
{"parse MB", StrPtr("2048MB"), 2147483648, false},
{"parse MiB", StrPtr("2048MiB"), 2147483648, false},
{"parse K", StrPtr("1K"), 1024, false},
{"parse KB", StrPtr("2KB"), 2048, false},
{"parse KiB", StrPtr("4KiB"), 4096, false},
{"parse no units as bytes", StrPtr("12345"), 12345, false},
{"error on bad format string", StrPtr("20l8G"), -1, true},
{"error on unknown unit string", StrPtr("2048W"), -1, true},
{"error on arbitrary string", StrPtr("something"), -1, true},
{"parse TB", "2TB", 2199023255552, false},
{"parse T", "2T", 2199023255552, false},
{"parse fraction T", "2.2T", 2418925581108, false},
{"parse GB", "2GB", 2147483648, false},
{"parse G", "2G", 2147483648, false},
{"parse M", "2048M", 2147483648, false},
{"parse MB", "2048MB", 2147483648, false},
{"parse MiB", "2048MiB", 2147483648, false},
{"parse K", "1K", 1024, false},
{"parse KB", "2KB", 2048, false},
{"parse KiB", "4KiB", 4096, false},
{"parse no units as bytes", "12345", 12345, false},
{"error on bad format string", "20l8G", -1, true},
{"error on unknown unit string", "2048W", -1, true},
{"error on arbitrary string", "something", -1, true},
}
for _, test := range tests {
tt := test
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := parseDiskSize(tt.size)
got, err := ParseDiskSize(tt.size)
if (err != nil) != tt.wantErr {
t.Errorf("parseDiskSize() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
if int64(got) != tt.want {
t.Errorf("parseDiskSize() got = %v, want %v", got, tt.want)
}
})
Expand All @@ -73,7 +72,7 @@ func TestFormatDiskSize(t *testing.T) {
tt := test
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := formatDiskSize(tt.size); got != tt.want {
if got := FormatDiskSize(DiskSize(tt.size)); got != tt.want {
t.Errorf("formatDiskSize() = %v, want %v", got, tt.want)
}
})
Expand Down
10 changes: 5 additions & 5 deletions proxmoxtf/resource/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,24 +188,24 @@ func getFileIDValidator() schema.SchemaValidateDiagFunc {
}

func getFileSizeValidator() schema.SchemaValidateDiagFunc {
return validation.ToDiagFunc(func(i interface{}, k string) (ws []string, es []error) {
return validation.ToDiagFunc(func(i interface{}, k string) ([]string, []error) {
v, ok := i.(string)
var es []error

if !ok {
es = append(es, fmt.Errorf("expected type of %s to be string", k))
return
return nil, es
}

if v != "" {
_, err := types.ParseDiskSize(v)

if err != nil {
es = append(es, fmt.Errorf("expected %s to be a valid file size (100, 1M, 1G), got %s", k, v))
return
return nil, es
}
}

return
return []string{}, es
})
}

Expand Down
33 changes: 17 additions & 16 deletions proxmoxtf/resource/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -1851,14 +1851,14 @@ func vmCreateClone(ctx context.Context, d *schema.ResourceData, m interface{}) d
if moveDisk {
moveDiskTimeout := d.Get(mkResourceVirtualEnvironmentVMTimeoutMoveDisk).(int)

e := vmAPI.MoveVMDisk(ctx, diskMoveBody, moveDiskTimeout)
e = vmAPI.MoveVMDisk(ctx, diskMoveBody, moveDiskTimeout)
if e != nil {
return diag.FromErr(e)
}
}

if diskSize > currentDiskInfo.Size.InGigabytes() {
e := vmAPI.ResizeVMDisk(ctx, diskResizeBody)
e = vmAPI.ResizeVMDisk(ctx, diskResizeBody)
if e != nil {
return diag.FromErr(e)
}
Expand Down Expand Up @@ -2016,8 +2016,9 @@ func vmCreateCustom(ctx context.Context, d *schema.ResourceData, m interface{})
return diag.FromErr(err)
}

efiDiskBlock := d.Get(mkResourceVirtualEnvironmentVMEfiDisk).([]interface{})
var efiDisk *vms.CustomEFIDisk

efiDiskBlock := d.Get(mkResourceVirtualEnvironmentVMEfiDisk).([]interface{})
if len(efiDiskBlock) > 0 {
block := efiDiskBlock[0].(map[string]interface{})

Expand All @@ -2029,6 +2030,7 @@ func vmCreateCustom(ctx context.Context, d *schema.ResourceData, m interface{})
fileFormat = dvResourceVirtualEnvironmentVMEfiDiskFileFormat
}

//nolint:govet
diskSize, err := types.ParseDiskSize(size)
if err != nil {
return diag.FromErr(err)
Expand Down Expand Up @@ -2790,12 +2792,12 @@ func vmGetEfiDisk(d *schema.ResourceData, disk []interface{}) (*vms.CustomEFIDis
fileFormat, _ := block[mkResourceVirtualEnvironmentVMEfiDiskFileFormat].(string)
size, _ := block[mkResourceVirtualEnvironmentVMEfiDiskSize].(string)

efiDiskConfig.FileVolume = fmt.Sprintf("%s:%d", datastoreID, size)
efiDiskConfig.FileVolume = fmt.Sprintf("%s:%s", datastoreID, size)
efiDiskConfig.Format = &fileFormat

diskSize, err := types.ParseDiskSize(size)
if err != nil {
return nil, err
return nil, fmt.Errorf("parsing efi disk size: %w", err)
}

efiDiskConfig.Size = &diskSize
Expand Down Expand Up @@ -3435,6 +3437,7 @@ func vmReadCustom(
diags = append(diags, diag.FromErr(err)...)
}

//nolint:nestif
if vmConfig.EFIDisk != nil {
efiDisk := map[string]interface{}{}

Expand Down Expand Up @@ -3470,11 +3473,9 @@ func vmReadCustom(

currentEfiDisk := d.Get(mkResourceVirtualEnvironmentVMEfiDisk).([]interface{})

if len(clone) > 0 {
if len(currentEfiDisk) > 0 {
err := d.Set(mkResourceVirtualEnvironmentVMEfiDisk, []interface{}{efiDisk})
diags = append(diags, diag.FromErr(err)...)
}
if len(clone) > 0 && len(currentEfiDisk) > 0 {
err := d.Set(mkResourceVirtualEnvironmentVMEfiDisk, []interface{}{efiDisk})
diags = append(diags, diag.FromErr(err)...)
} else if len(currentEfiDisk) > 0 ||
efiDisk[mkResourceVirtualEnvironmentVMEfiDiskDatastoreID] != dvResourceVirtualEnvironmentVMEfiDiskDatastoreID ||
efiDisk[mkResourceVirtualEnvironmentVMEfiDiskSize] != dvResourceVirtualEnvironmentVMEfiDiskSize ||
Expand Down Expand Up @@ -4616,15 +4617,14 @@ func vmUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.D
Media: &cdromMedia,
}

if vmConfig.IDEDevice2 != nil {
if strings.Contains(
if vmConfig.IDEDevice2 != nil &&
strings.Contains(
vmConfig.IDEDevice2.FileVolume,
fmt.Sprintf("vm-%d-cloudinit", vmID),
) {
tmp := updateBody.IDEDevices["ide2"]
tmp.Enabled = true
updateBody.IDEDevices["ide2"] = tmp
}
tmp := updateBody.IDEDevices["ide2"]
tmp.Enabled = true
updateBody.IDEDevices["ide2"] = tmp
}
}

Expand Down Expand Up @@ -4825,6 +4825,7 @@ func vmUpdateDiskLocationAndSize(
if d.HasChange(mkResourceVirtualEnvironmentVMEfiDisk) {
diskOld, diskNew := d.GetChange(mkResourceVirtualEnvironmentVMEfiDisk)

//nolint:govet
oldEfiDisk, err := vmGetEfiDiskAsStorageDevice(d, diskOld.([]interface{}))
if err != nil {
return diag.FromErr(err)
Expand Down

0 comments on commit 31ccb36

Please sign in to comment.