0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-06-30 18:42:58 +00:00

frat(vm): Add custom CPU models support (#254)

* chore: add regex for custom cpu model validation

* update logic to use TF validators & add tests

---------

Co-authored-by: Kai Kahllund <kai.kahllund@akra.de>
Co-authored-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
This commit is contained in:
Kai Kahllund 2023-03-13 01:34:02 +01:00 committed by GitHub
parent 4d447390e6
commit 82016fc8ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 2 deletions

View File

@ -98,7 +98,7 @@ func getCPUFlagsValidator() schema.SchemaValidateDiagFunc {
}
func getCPUTypeValidator() schema.SchemaValidateDiagFunc {
return validation.ToDiagFunc(validation.StringInSlice([]string{
standardTypes := []string{
"486",
"Broadwell",
"Broadwell-IBRS",
@ -144,7 +144,12 @@ func getCPUTypeValidator() schema.SchemaValidateDiagFunc {
"phenom",
"qemu32",
"qemu64",
}, false))
}
return validation.ToDiagFunc(validation.Any(
validation.StringInSlice(standardTypes, false),
validation.StringMatch(regexp.MustCompile(`^custom-.+$`), "must be a valid custom CPU type"),
))
}
func getFileFormatValidator() schema.SchemaValidateDiagFunc {

37
proxmoxtf/utils_test.go Normal file
View File

@ -0,0 +1,37 @@
/*
* 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 proxmoxtf
import (
"testing"
)
func Test_getCPUTypeValidator(t *testing.T) {
t.Parallel()
tests := []struct {
name string
value string
valid bool
}{
{"empty", "", false},
{"invalid", "invalid", false},
{"valid", "host", true},
{"valid", "qemu64", true},
{"valid", "custom-abc", true},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
f := getCPUTypeValidator()
res := f(tt.value, nil)
if !res.HasError() != tt.valid {
t.Errorf("validate: '%s', want %v got %v", tt.value, tt.valid, res)
}
})
}
}