diff --git a/proxmoxtf/utils.go b/proxmoxtf/utils.go index dfba77d1..bf8f9298 100644 --- a/proxmoxtf/utils.go +++ b/proxmoxtf/utils.go @@ -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 { diff --git a/proxmoxtf/utils_test.go b/proxmoxtf/utils_test.go new file mode 100644 index 00000000..48b25d98 --- /dev/null +++ b/proxmoxtf/utils_test.go @@ -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) + } + }) + } +}