diff --git a/cloudstack/resource_cloudstack_host.go b/cloudstack/resource_cloudstack_host.go index 5f656392..61e81946 100644 --- a/cloudstack/resource_cloudstack_host.go +++ b/cloudstack/resource_cloudstack_host.go @@ -23,12 +23,12 @@ import ( "errors" "fmt" "log" - "sort" "strings" "time" "github.com/apache/cloudstack-go/v2/cloudstack" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" ) func resourceCloudStackHost() *schema.Resource { @@ -48,19 +48,10 @@ func resourceCloudStackHost() *schema.Resource { }, Schema: map[string]*schema.Schema{ "hypervisor": { - Type: schema.TypeString, - Required: true, - ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { - validHypervisors := []string{"xenserver", "kvm", "vmware", "baremetal", "simulator"} - - sort.Strings(validHypervisors) - - if sort.SearchStrings(validHypervisors, v.(string)) >= len(validHypervisors) { - errors = append(errors, fmt.Errorf("%q must be one of %v", k, validHypervisors)) - } - return - }, - ForceNew: true, + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringInSlice([]string{"XenServer", "KVM", "VMware", "Hyperv", "BareMetal", "Simulator", "Ovm3"}, true), + ForceNew: true, }, "pod_id": { Type: schema.TypeString, diff --git a/cloudstack/resource_cloudstack_host_test.go b/cloudstack/resource_cloudstack_host_test.go index 25b53652..c0774d82 100644 --- a/cloudstack/resource_cloudstack_host_test.go +++ b/cloudstack/resource_cloudstack_host_test.go @@ -29,6 +29,26 @@ import ( "github.com/hashicorp/terraform-plugin-testing/terraform" ) +func TestResourceCloudStackHostHypervisorValidation(t *testing.T) { + validate := resourceCloudStackHost().Schema["hypervisor"].ValidateFunc + + // CloudStack's HypervisorType.getType() lowercases the input before lookup, so matching + // is case-insensitive server-side; the validator must accept any casing accordingly. + valid := []string{"XenServer", "KVM", "VMware", "Hyperv", "BareMetal", "Simulator", "Ovm3", "kvm", "simulator", "XENSERVER"} + for _, v := range valid { + if _, errs := validate(v, "hypervisor"); len(errs) != 0 { + t.Errorf("supported hypervisor %q should be accepted, got errors: %v", v, errs) + } + } + + invalid := []string{"foo", "docker", "esxi", "kvm2", ""} + for _, v := range invalid { + if _, errs := validate(v, "hypervisor"); len(errs) == 0 { + t.Errorf("unsupported hypervisor %q should be rejected, but validation accepted it", v) + } + } +} + func TestAccCloudStackHost_basic(t *testing.T) { var h cloudstack.Host resource.Test(t, resource.TestCase{