You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
248 lines
6.4 KiB
248 lines
6.4 KiB
4 months ago
|
package validators
|
||
|
|
||
|
import (
|
||
|
"net/url"
|
||
|
"reflect"
|
||
|
|
||
|
"github.com/go-playground/validator/v10"
|
||
|
)
|
||
|
|
||
|
// virtTypeValidator validates the virtualization type
|
||
|
func virtTypeValidator(fl validator.FieldLevel) bool {
|
||
|
fieldValue := fl.Field().String()
|
||
|
|
||
|
return IsInSlice(fieldValue, virtTypeValues)
|
||
|
}
|
||
|
|
||
|
// vmStatusValidator validates the VM status
|
||
|
func vmStatusValidator(fl validator.FieldLevel) bool {
|
||
|
fieldValue := fl.Field().String()
|
||
|
|
||
|
return IsInSlice(fieldValue, vmStatusValues)
|
||
|
}
|
||
|
|
||
|
// cpuUnitsValidator validates the CPU units (2-262144)
|
||
|
func cpuUnitsValidator(fl validator.FieldLevel) bool {
|
||
|
cpuUnits := fl.Field().Int()
|
||
|
return IsInRange(cpuUnits, cpuUnitsRange)
|
||
|
}
|
||
|
|
||
|
// cpuLimitValidator validates the CPU limit (0-100)
|
||
|
func cpuLimitValidator(fl validator.FieldLevel) bool {
|
||
|
cpuLimit := fl.Field().Int()
|
||
|
return IsInRange(cpuLimit, cpuLimitRange)
|
||
|
}
|
||
|
|
||
|
// ioPriorityValidator validates the IO priority (0-7)
|
||
|
func ioPriorityValidator(fl validator.FieldLevel) bool {
|
||
|
ioPriority := fl.Field().Int()
|
||
|
return IsInRange(ioPriority, ioPriorityRange)
|
||
|
}
|
||
|
|
||
|
// videoRamValidator validates the video RAM limit (1-1024)
|
||
|
func videoRamValidator(fl validator.FieldLevel) bool {
|
||
|
videoRam := fl.Field().Int()
|
||
|
return IsInRange(videoRam, videoRamRange)
|
||
|
}
|
||
|
|
||
|
// memGuaranteeSizeValidator validates the memory guarantee size (-1, 100)
|
||
|
func memGuaranteeSizeValidator(fl validator.FieldLevel) bool {
|
||
|
memGuarantee := fl.Field().Int()
|
||
|
return IsInRange(memGuarantee, memGuaranteeSizeRange)
|
||
|
}
|
||
|
|
||
|
// haStatusValidator validates the HA status
|
||
|
func haStatusValidator(fl validator.FieldLevel) bool {
|
||
|
fieldValue := fl.Field().String()
|
||
|
|
||
|
return IsInSlice(fieldValue, haStatusValues)
|
||
|
}
|
||
|
|
||
|
// visibilityValidator validates the visibility
|
||
|
func visibilityValidator(fl validator.FieldLevel) bool {
|
||
|
fieldValue := fl.Field().String()
|
||
|
|
||
|
return IsInSlice(fieldValue, visibilityValues)
|
||
|
}
|
||
|
|
||
|
// metricsPeriodValidator validates the metrics
|
||
|
func metricsPeriodValidator(fl validator.FieldLevel) bool {
|
||
|
fieldValue := fl.Field().String()
|
||
|
|
||
|
return IsInSlice(fieldValue, metricPeriodValues)
|
||
|
}
|
||
|
|
||
|
// networkTypeValidator validates the network type
|
||
|
func networkTypeValidator(fl validator.FieldLevel) bool {
|
||
|
fieldValue := fl.Field().String()
|
||
|
|
||
|
return IsInSlice(fieldValue, networkTypeValues)
|
||
|
}
|
||
|
|
||
|
// imageTypeValidator validates the image type
|
||
|
func imageTypeValidator(fl validator.FieldLevel) bool {
|
||
|
fieldValue := fl.Field().String()
|
||
|
|
||
|
return IsInSlice(fieldValue, imageTypeValues)
|
||
|
}
|
||
|
|
||
|
// imageStatusValidator validates the image status
|
||
|
func imageStatusValidator(fl validator.FieldLevel) bool {
|
||
|
fieldValue := fl.Field().String()
|
||
|
|
||
|
return IsInSlice(fieldValue, imageStatusValues)
|
||
|
}
|
||
|
|
||
|
// isBoolTypeValidator validates that field value == bool
|
||
|
func isBoolTypeValidator(fe validator.FieldLevel) bool {
|
||
|
return fe.Field().CanConvert(reflect.TypeOf(true))
|
||
|
}
|
||
|
|
||
|
// extStorageStatusValidator validates the external storage status
|
||
|
func extStorageStatusValidator(fl validator.FieldLevel) bool {
|
||
|
fieldValue := fl.Field().String()
|
||
|
|
||
|
return IsInSlice(fieldValue, extStorageStatusValues)
|
||
|
}
|
||
|
|
||
|
// extStorageTypeValidator validates the external storage type
|
||
|
func extStorageTypeValidator(fl validator.FieldLevel) bool {
|
||
|
fieldValue := fl.Field().String()
|
||
|
|
||
|
return IsInSlice(fieldValue, extStorageTypeValues)
|
||
|
}
|
||
|
|
||
|
// nodeMaintenanceStatusValidator validates the node maintenance status
|
||
|
func nodeMaintenanceStatusValidator(fl validator.FieldLevel) bool {
|
||
|
fieldSlice, ok := fl.Field().Interface().([]string)
|
||
|
if !ok {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
for _, value := range fieldSlice {
|
||
|
if !IsInSlice(value, nodeMaintenanceStatusValues) {
|
||
|
return false
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
// nodeAgentStatusValidator validates the node agent status
|
||
|
func nodeAgentStatusValidator(fl validator.FieldLevel) bool {
|
||
|
fieldSlice, ok := fl.Field().Interface().([]string)
|
||
|
if !ok {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
for _, value := range fieldSlice {
|
||
|
if !IsInSlice(value, nodeAgentStatusValues) {
|
||
|
return false
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
// haMultiStatusValidator validates the HA status
|
||
|
func haMultiStatusValidator(fl validator.FieldLevel) bool {
|
||
|
fieldSlice, ok := fl.Field().Interface().([]string)
|
||
|
if !ok {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
for _, value := range fieldSlice {
|
||
|
if !IsInSlice(value, haStatusValues) {
|
||
|
return false
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
// nodeStatusInHaClusterValidator validates the metrics
|
||
|
func nodeStatusInHaClusterValidator(fl validator.FieldLevel) bool {
|
||
|
fieldSlice, ok := fl.Field().Interface().([]string)
|
||
|
if !ok {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
for _, value := range fieldSlice {
|
||
|
if !IsInSlice(value, nodeStatusInHaClusterValues) {
|
||
|
return false
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
// affinityRuleValidator validates the affinity rule value
|
||
|
func affinityRuleValidator(fl validator.FieldLevel) bool {
|
||
|
fieldValue := fl.Field().String()
|
||
|
|
||
|
return IsInSlice(fieldValue, affinityRuleValues)
|
||
|
}
|
||
|
|
||
|
// guestOSValidator validates the guest os value
|
||
|
func guestOSValidator(fl validator.FieldLevel) bool {
|
||
|
fieldValue := fl.Field().String()
|
||
|
|
||
|
return IsInSlice(fieldValue, guestOSValues)
|
||
|
}
|
||
|
|
||
|
// guestOSVersionValidator validates the guest os version value
|
||
|
func guestOSVersionValidator(fl validator.FieldLevel) bool {
|
||
|
fieldValue := fl.Field().String()
|
||
|
|
||
|
return IsInSlice(fieldValue, guestOSVersionValues)
|
||
|
}
|
||
|
|
||
|
// autoStartValidator validates the virtual machine startup options value
|
||
|
func autoStartValidator(fl validator.FieldLevel) bool {
|
||
|
fieldValue := fl.Field().String()
|
||
|
|
||
|
return IsInSlice(fieldValue, autoStartValues)
|
||
|
}
|
||
|
|
||
|
// autoStopValidator validates the virtual machine shutdown mode value
|
||
|
func autoStopValidator(fl validator.FieldLevel) bool {
|
||
|
fieldValue := fl.Field().String()
|
||
|
|
||
|
return IsInSlice(fieldValue, autoStopValues)
|
||
|
}
|
||
|
|
||
|
// vncModeValidator validates the vnc mode value
|
||
|
func vncModeValidator(fl validator.FieldLevel) bool {
|
||
|
fieldValue := fl.Field().String()
|
||
|
|
||
|
return IsInSlice(fieldValue, vncModeValues)
|
||
|
}
|
||
|
|
||
|
// chipsetValidator validates the chipset value
|
||
|
func chipsetValidator(fl validator.FieldLevel) bool {
|
||
|
fieldValue := fl.Field().String()
|
||
|
|
||
|
return IsInSlice(fieldValue, chipsetValues)
|
||
|
}
|
||
|
|
||
|
// clockOffsetValidator validates the VM clock offset value
|
||
|
func clockOffsetValidator(fl validator.FieldLevel) bool {
|
||
|
fieldValue := fl.Field().String()
|
||
|
|
||
|
return IsInSlice(fieldValue, clockOffsetValues)
|
||
|
}
|
||
|
|
||
|
// templateStatusValidator validates the template status value
|
||
|
func templateStatusValidator(fl validator.FieldLevel) bool {
|
||
|
fieldValue := fl.Field().String()
|
||
|
|
||
|
return IsInSlice(fieldValue, templateStatusValues)
|
||
|
}
|
||
|
|
||
|
func urlValidartor(fl validator.FieldLevel) bool {
|
||
|
fieldValues := fl.Field().String()
|
||
|
|
||
|
_, err := url.ParseRequestURI(fieldValues)
|
||
|
return err == nil
|
||
|
}
|