Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions cloudstack/resource_cloudstack_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
20 changes: 20 additions & 0 deletions cloudstack/resource_cloudstack_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
Loading