This is an automated email from the ASF dual-hosted git repository.
sureshanaparti pushed a commit to branch main
in repository
https://gitbox.apache.org/repos/asf/cloudstack-terraform-provider.git
The following commit(s) were added to refs/heads/main by this push:
new 2f8ac44 fix: reject unsupported hypervisor values in host resource
validation (#337)
2f8ac44 is described below
commit 2f8ac44a9dea457e3c0c51cbe0461016fcd7300e
Author: Ramgopal Nagaboina <[email protected]>
AuthorDate: Mon Sep 7 02:37:05 2026 -0400
fix: reject unsupported hypervisor values in host resource validation (#337)
* fix: reject unsupported hypervisor values in host resource validation
The hypervisor field accepted unsupported values at plan time because the
ValidateFunc used sort.SearchStrings as a found-check when it returns an
insertion index. Replace it with validation.StringInSlice, which rejects any
value not in the supported list. Keeps the unit test covering accepted and
rejected values.
* fix hypervisor casing to match CloudStack enum; merge unit test into
acceptance test file
* make hypervisor validation case-insensitive to match CloudStack
server-side matching
Co-authored-by: Manoj Kumar <[email protected]>
---
cloudstack/resource_cloudstack_host.go | 19 +++++--------------
cloudstack/resource_cloudstack_host_test.go | 20 ++++++++++++++++++++
2 files changed, 25 insertions(+), 14 deletions(-)
diff --git a/cloudstack/resource_cloudstack_host.go
b/cloudstack/resource_cloudstack_host.go
index 5f65639..61e8194 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 25b5365..c0774d8 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{