-
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(vm): Add parser for CustomEFIDisk (#208)
* feat: add support for "args" flag for VM * switch from args to kvmarguments, update type * cosmetics: `kvmarguments` -> `kvm_arguments` also update doc to match description from the official PVE documentation. * fix(vm): Add parser for CustomEFIDisk * use parseDiskSize(&string) from utils.go for CustomEFIDisk * readd the remove space by github space * address linter errors, remove duplicated code, add unit test Co-authored-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
- Loading branch information
Showing
6 changed files
with
118 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
package proxmox | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
func ParseDiskSize(size *string) (int, error) { | ||
if size == nil { | ||
return 0, nil | ||
} | ||
|
||
if strings.HasSuffix(*size, "T") { | ||
diskSize, err := strconv.Atoi(strings.TrimSuffix(*size, "T")) | ||
if err != nil { | ||
return -1, fmt.Errorf("failed to parse disk size: %w", err) | ||
} | ||
return int(math.Ceil(float64(diskSize) * 1024)), nil | ||
} | ||
|
||
if strings.HasSuffix(*size, "G") { | ||
diskSize, err := strconv.Atoi(strings.TrimSuffix(*size, "G")) | ||
if err != nil { | ||
return -1, fmt.Errorf("failed to parse disk size: %w", err) | ||
} | ||
return diskSize, nil | ||
} | ||
|
||
if strings.HasSuffix(*size, "M") { | ||
diskSize, err := strconv.Atoi(strings.TrimSuffix(*size, "M")) | ||
if err != nil { | ||
return -1, fmt.Errorf("failed to parse disk size: %w", err) | ||
} | ||
return int(math.Ceil(float64(diskSize) / 1024)), nil | ||
} | ||
|
||
return -1, fmt.Errorf("cannot parse disk size \"%s\"", *size) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package proxmox | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestParseDiskSize(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
size *string | ||
want int | ||
wantErr bool | ||
}{ | ||
{"handle null size", nil, 0, false}, | ||
{"parse terabytes", strPtr("2T"), 2048, false}, | ||
{"parse gigabytes", strPtr("2G"), 2, false}, | ||
{"parse megabytes", strPtr("2048M"), 2, false}, | ||
{"error on arbitrary string", strPtr("something"), -1, true}, | ||
{"error on missing unit", strPtr("12345"), -1, true}, | ||
} | ||
for _, test := range tests { | ||
tt := test | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
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 { | ||
t.Errorf("ParseDiskSize() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters