fix: reject unsupported hypervisor values in host resource validation - #337
fix: reject unsupported hypervisor values in host resource validation#337nagaboinaramgopal wants to merge 4 commits into
Conversation
There was a problem hiding this comment.
🟢 Approval recommended
The validation fix is correct and low-risk, and the added unit test directly covers the previously broken behavior.
Pull request overview
This PR fixes plan-time validation for the hypervisor argument on the cloudstack_host Terraform resource by correcting the sort.SearchStrings usage so only exact matches against the supported hypervisor list are accepted. This prevents unsupported values from slipping through planning and failing later during apply with less actionable CloudStack API errors.
Changes:
- Corrected the
hypervisorValidateFuncto verify that the searched index is both in range and an exact string match. - Added a focused unit test to ensure supported hypervisors are accepted and unsupported values are rejected.
File summaries
| File | Description |
|---|---|
cloudstack/resource_cloudstack_host.go |
Fixes the validation logic to only accept exact matches from the supported hypervisor list. |
cloudstack/resource_cloudstack_host_unit_test.go |
Adds a unit test covering both accepted and rejected hypervisor values for plan-time validation. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
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. Signed-off-by: Ramgopal Nagaboina <ramgopal.nagaboina.dev@gmail.com>
eef658a to
5dc14fb
Compare
…acceptance test file
|
Fixed the |
Description
The
hypervisorValidateFuncon thecloudstack_hostresource usedsort.SearchStringsas if it returned a "found" flag, butsort.SearchStringsreturns the insertion index (0..len). The check>= len(validHypervisors)therefore only rejected a value that sorts after every supported entry, so unsupported values such asfoo,docker,esxi, orkvm2passed plan-time validation and failed later with an opaque server-side error at apply.Fixed by treating a value as valid only when the element at the returned index actually equals it, which is the standard
sort.SearchStringsidiom.Testing
Added a unit test
TestResourceCloudStackHostHypervisorValidationasserting the supported hypervisors are accepted and unsupported values are rejected. It needs no live CloudStack:The test fails against the current code (bad values are accepted) and passes with the fix.