alicloud: simplify validators (#12982)

This commit is contained in:
Joseph Anthony Pasquale Holsten 2017-03-23 01:57:11 -07:00 committed by Paul Stack
parent 8c04991e4a
commit 855adb47ed
2 changed files with 128 additions and 257 deletions

View File

@ -2,26 +2,17 @@ package alicloud
import ( import (
"fmt" "fmt"
"net" "regexp"
"strconv"
"strings" "strings"
"github.com/denverdino/aliyungo/common" "github.com/denverdino/aliyungo/common"
"github.com/denverdino/aliyungo/ecs" "github.com/denverdino/aliyungo/ecs"
"github.com/denverdino/aliyungo/slb" "github.com/hashicorp/terraform/helper/validation"
"regexp"
) )
// common // common
func validateInstancePort(v interface{}, k string) (ws []string, errors []error) { func validateInstancePort(v interface{}, k string) (ws []string, errors []error) {
value := v.(int) return validation.IntBetween(1, 65535)(v, k)
if value < 1 || value > 65535 {
errors = append(errors, fmt.Errorf(
"%q must be a valid instance port between 1 and 65535",
k))
return
}
return
} }
func validateInstanceProtocol(v interface{}, k string) (ws []string, errors []error) { func validateInstanceProtocol(v interface{}, k string) (ws []string, errors []error) {
@ -37,12 +28,11 @@ func validateInstanceProtocol(v interface{}, k string) (ws []string, errors []er
// ecs // ecs
func validateDiskCategory(v interface{}, k string) (ws []string, errors []error) { func validateDiskCategory(v interface{}, k string) (ws []string, errors []error) {
category := ecs.DiskCategory(v.(string)) return validation.StringInSlice([]string{
if category != ecs.DiskCategoryCloud && category != ecs.DiskCategoryCloudEfficiency && category != ecs.DiskCategoryCloudSSD { string(ecs.DiskCategoryCloud),
errors = append(errors, fmt.Errorf("%s must be one of %s %s %s", k, ecs.DiskCategoryCloud, ecs.DiskCategoryCloudEfficiency, ecs.DiskCategoryCloudSSD)) string(ecs.DiskCategoryCloudEfficiency),
} string(ecs.DiskCategoryCloudSSD),
}, false)(v, k)
return
} }
func validateInstanceName(v interface{}, k string) (ws []string, errors []error) { func validateInstanceName(v interface{}, k string) (ws []string, errors []error) {
@ -59,12 +49,7 @@ func validateInstanceName(v interface{}, k string) (ws []string, errors []error)
} }
func validateInstanceDescription(v interface{}, k string) (ws []string, errors []error) { func validateInstanceDescription(v interface{}, k string) (ws []string, errors []error) {
value := v.(string) return validation.StringLenBetween(2, 256)(v, k)
if len(value) < 2 || len(value) > 256 {
errors = append(errors, fmt.Errorf("%q cannot be longer than 256 characters", k))
}
return
} }
func validateDiskName(v interface{}, k string) (ws []string, errors []error) { func validateDiskName(v interface{}, k string) (ws []string, errors []error) {
@ -86,12 +71,7 @@ func validateDiskName(v interface{}, k string) (ws []string, errors []error) {
} }
func validateDiskDescription(v interface{}, k string) (ws []string, errors []error) { func validateDiskDescription(v interface{}, k string) (ws []string, errors []error) {
value := v.(string) return validation.StringLenBetween(2, 128)(v, k)
if len(value) < 2 || len(value) > 256 {
errors = append(errors, fmt.Errorf("%q cannot be longer than 256 characters", k))
}
return
} }
//security group //security group
@ -109,225 +89,114 @@ func validateSecurityGroupName(v interface{}, k string) (ws []string, errors []e
} }
func validateSecurityGroupDescription(v interface{}, k string) (ws []string, errors []error) { func validateSecurityGroupDescription(v interface{}, k string) (ws []string, errors []error) {
value := v.(string) return validation.StringLenBetween(2, 256)(v, k)
if len(value) < 2 || len(value) > 256 {
errors = append(errors, fmt.Errorf("%q cannot be longer than 256 characters", k))
}
return
} }
func validateSecurityRuleType(v interface{}, k string) (ws []string, errors []error) { func validateSecurityRuleType(v interface{}, k string) (ws []string, errors []error) {
rt := GroupRuleDirection(v.(string)) return validation.StringInSlice([]string{
if rt != GroupRuleIngress && rt != GroupRuleEgress { string(GroupRuleIngress),
errors = append(errors, fmt.Errorf("%s must be one of %s %s", k, GroupRuleIngress, GroupRuleEgress)) string(GroupRuleEgress),
} }, false)(v, k)
return
} }
func validateSecurityRuleIpProtocol(v interface{}, k string) (ws []string, errors []error) { func validateSecurityRuleIpProtocol(v interface{}, k string) (ws []string, errors []error) {
pt := GroupRuleIpProtocol(v.(string)) return validation.StringInSlice([]string{
if pt != GroupRuleTcp && pt != GroupRuleUdp && pt != GroupRuleIcmp && pt != GroupRuleGre && pt != GroupRuleAll { string(GroupRuleTcp),
errors = append(errors, fmt.Errorf("%s must be one of %s %s %s %s %s", k, string(GroupRuleUdp),
GroupRuleTcp, GroupRuleUdp, GroupRuleIcmp, GroupRuleGre, GroupRuleAll)) string(GroupRuleIcmp),
} string(GroupRuleGre),
string(GroupRuleAll),
return }, false)(v, k)
} }
func validateSecurityRuleNicType(v interface{}, k string) (ws []string, errors []error) { func validateSecurityRuleNicType(v interface{}, k string) (ws []string, errors []error) {
pt := GroupRuleNicType(v.(string)) return validation.StringInSlice([]string{
if pt != GroupRuleInternet && pt != GroupRuleIntranet { string(GroupRuleInternet),
errors = append(errors, fmt.Errorf("%s must be one of %s %s", k, GroupRuleInternet, GroupRuleIntranet)) string(GroupRuleIntranet),
} }, false)(v, k)
return
} }
func validateSecurityRulePolicy(v interface{}, k string) (ws []string, errors []error) { func validateSecurityRulePolicy(v interface{}, k string) (ws []string, errors []error) {
pt := GroupRulePolicy(v.(string)) return validation.StringInSlice([]string{
if pt != GroupRulePolicyAccept && pt != GroupRulePolicyDrop { string(GroupRulePolicyAccept),
errors = append(errors, fmt.Errorf("%s must be one of %s %s", k, GroupRulePolicyAccept, GroupRulePolicyDrop)) string(GroupRulePolicyDrop),
} }, false)(v, k)
return
} }
func validateSecurityPriority(v interface{}, k string) (ws []string, errors []error) { func validateSecurityPriority(v interface{}, k string) (ws []string, errors []error) {
value := v.(int) return validation.IntBetween(1, 100)(v, k)
if value < 1 || value > 100 {
errors = append(errors, fmt.Errorf(
"%q must be a valid authorization policy priority between 1 and 100",
k))
return
}
return
} }
// validateCIDRNetworkAddress ensures that the string value is a valid CIDR that // validateCIDRNetworkAddress ensures that the string value is a valid CIDR that
// represents a network address - it adds an error otherwise // represents a network address - it adds an error otherwise
func validateCIDRNetworkAddress(v interface{}, k string) (ws []string, errors []error) { func validateCIDRNetworkAddress(v interface{}, k string) (ws []string, errors []error) {
value := v.(string) return validation.CIDRNetwork(0, 32)(v, k)
_, ipnet, err := net.ParseCIDR(value)
if err != nil {
errors = append(errors, fmt.Errorf(
"%q must contain a valid CIDR, got error parsing: %s", k, err))
return
}
if ipnet == nil || value != ipnet.String() {
errors = append(errors, fmt.Errorf(
"%q must contain a valid network CIDR, expected %q, got %q",
k, ipnet, value))
}
return
} }
func validateRouteEntryNextHopType(v interface{}, k string) (ws []string, errors []error) { func validateRouteEntryNextHopType(v interface{}, k string) (ws []string, errors []error) {
nht := ecs.NextHopType(v.(string)) return validation.StringInSlice([]string{
if nht != ecs.NextHopIntance && nht != ecs.NextHopTunnel { string(ecs.NextHopIntance),
errors = append(errors, fmt.Errorf("%s must be one of %s %s", k, string(ecs.NextHopTunnel),
ecs.NextHopIntance, ecs.NextHopTunnel)) }, false)(v, k)
}
return
} }
func validateSwitchCIDRNetworkAddress(v interface{}, k string) (ws []string, errors []error) { func validateSwitchCIDRNetworkAddress(v interface{}, k string) (ws []string, errors []error) {
value := v.(string) return validation.CIDRNetwork(16, 29)(v, k)
_, ipnet, err := net.ParseCIDR(value)
if err != nil {
errors = append(errors, fmt.Errorf(
"%q must contain a valid CIDR, got error parsing: %s", k, err))
return
}
if ipnet == nil || value != ipnet.String() {
errors = append(errors, fmt.Errorf(
"%q must contain a valid network CIDR, expected %q, got %q",
k, ipnet, value))
return
}
mark, _ := strconv.Atoi(strings.Split(ipnet.String(), "/")[1])
if mark < 16 || mark > 29 {
errors = append(errors, fmt.Errorf(
"%q must contain a network CIDR which mark between 16 and 29",
k))
}
return
} }
// validateIoOptimized ensures that the string value is a valid IoOptimized that // validateIoOptimized ensures that the string value is a valid IoOptimized that
// represents a IoOptimized - it adds an error otherwise // represents a IoOptimized - it adds an error otherwise
func validateIoOptimized(v interface{}, k string) (ws []string, errors []error) { func validateIoOptimized(v interface{}, k string) (ws []string, errors []error) {
if value := v.(string); value != "" { return validation.StringInSlice([]string{
ioOptimized := ecs.IoOptimized(value) "",
if ioOptimized != ecs.IoOptimizedNone && string(ecs.IoOptimizedNone),
ioOptimized != ecs.IoOptimizedOptimized { string(ecs.IoOptimizedOptimized),
errors = append(errors, fmt.Errorf( }, false)(v, k)
"%q must contain a valid IoOptimized, expected %s or %s, got %q",
k, ecs.IoOptimizedNone, ecs.IoOptimizedOptimized, ioOptimized))
}
}
return
} }
// validateInstanceNetworkType ensures that the string value is a classic or vpc // validateInstanceNetworkType ensures that the string value is a classic or vpc
func validateInstanceNetworkType(v interface{}, k string) (ws []string, errors []error) { func validateInstanceNetworkType(v interface{}, k string) (ws []string, errors []error) {
if value := v.(string); value != "" { return validation.StringInSlice([]string{
network := InstanceNetWork(value) "",
if network != ClassicNet && string(ClassicNet),
network != VpcNet { string(VpcNet),
errors = append(errors, fmt.Errorf( }, false)(v, k)
"%q must contain a valid InstanceNetworkType, expected %s or %s, go %q",
k, ClassicNet, VpcNet, network))
}
}
return
} }
func validateInstanceChargeType(v interface{}, k string) (ws []string, errors []error) { func validateInstanceChargeType(v interface{}, k string) (ws []string, errors []error) {
if value := v.(string); value != "" { return validation.StringInSlice([]string{
chargeType := common.InstanceChargeType(value) "",
if chargeType != common.PrePaid && string(common.PrePaid),
chargeType != common.PostPaid { string(common.PostPaid),
errors = append(errors, fmt.Errorf( }, false)(v, k)
"%q must contain a valid InstanceChargeType, expected %s or %s, got %q",
k, common.PrePaid, common.PostPaid, chargeType))
}
}
return
} }
func validateInternetChargeType(v interface{}, k string) (ws []string, errors []error) { func validateInternetChargeType(v interface{}, k string) (ws []string, errors []error) {
if value := v.(string); value != "" { return validation.StringInSlice([]string{
chargeType := common.InternetChargeType(value) "",
if chargeType != common.PayByBandwidth && string(common.PayByBandwidth),
chargeType != common.PayByTraffic { string(common.PayByTraffic),
errors = append(errors, fmt.Errorf( }, false)(v, k)
"%q must contain a valid InstanceChargeType, expected %s or %s, got %q",
k, common.PayByBandwidth, common.PayByTraffic, chargeType))
}
}
return
} }
func validateInternetMaxBandWidthOut(v interface{}, k string) (ws []string, errors []error) { func validateInternetMaxBandWidthOut(v interface{}, k string) (ws []string, errors []error) {
value := v.(int) return validation.IntBetween(1, 100)(v, k)
if value < 1 || value > 100 {
errors = append(errors, fmt.Errorf(
"%q must be a valid internet bandwidth out between 1 and 1000",
k))
return
}
return
} }
// SLB // SLB
func validateSlbName(v interface{}, k string) (ws []string, errors []error) { func validateSlbName(v interface{}, k string) (ws []string, errors []error) {
if value := v.(string); value != "" { return validation.StringLenBetween(0, 80)(v, k)
if len(value) < 1 || len(value) > 80 {
errors = append(errors, fmt.Errorf(
"%q must be a valid load balancer name characters between 1 and 80",
k))
return
}
}
return
} }
func validateSlbInternetChargeType(v interface{}, k string) (ws []string, errors []error) { func validateSlbInternetChargeType(v interface{}, k string) (ws []string, errors []error) {
if value := v.(string); value != "" { return validation.StringInSlice([]string{
chargeType := common.InternetChargeType(value) "paybybandwidth",
"paybytraffic",
if chargeType != "paybybandwidth" && }, false)(v, k)
chargeType != "paybytraffic" {
errors = append(errors, fmt.Errorf(
"%q must contain a valid InstanceChargeType, expected %s or %s, got %q",
k, "paybybandwidth", "paybytraffic", value))
}
}
return
} }
func validateSlbBandwidth(v interface{}, k string) (ws []string, errors []error) { func validateSlbBandwidth(v interface{}, k string) (ws []string, errors []error) {
value := v.(int) return validation.IntBetween(1, 1000)(v, k)
if value < 1 || value > 1000 {
errors = append(errors, fmt.Errorf(
"%q must be a valid load balancer bandwidth between 1 and 1000",
k))
return
}
return
} }
func validateSlbListenerBandwidth(v interface{}, k string) (ws []string, errors []error) { func validateSlbListenerBandwidth(v interface{}, k string) (ws []string, errors []error) {
@ -342,67 +211,23 @@ func validateSlbListenerBandwidth(v interface{}, k string) (ws []string, errors
} }
func validateSlbListenerScheduler(v interface{}, k string) (ws []string, errors []error) { func validateSlbListenerScheduler(v interface{}, k string) (ws []string, errors []error) {
if value := v.(string); value != "" { return validation.StringInSlice([]string{"wrr", "wlc"}, false)(v, k)
scheduler := slb.SchedulerType(value)
if scheduler != "wrr" && scheduler != "wlc" {
errors = append(errors, fmt.Errorf(
"%q must contain a valid SchedulerType, expected %s or %s, got %q",
k, "wrr", "wlc", value))
}
}
return
} }
func validateSlbListenerStickySession(v interface{}, k string) (ws []string, errors []error) { func validateSlbListenerStickySession(v interface{}, k string) (ws []string, errors []error) {
if value := v.(string); value != "" { return validation.StringInSlice([]string{"", "on", "off"}, false)(v, k)
flag := slb.FlagType(value)
if flag != "on" && flag != "off" {
errors = append(errors, fmt.Errorf(
"%q must contain a valid StickySession, expected %s or %s, got %q",
k, "on", "off", value))
}
}
return
} }
func validateSlbListenerStickySessionType(v interface{}, k string) (ws []string, errors []error) { func validateSlbListenerStickySessionType(v interface{}, k string) (ws []string, errors []error) {
if value := v.(string); value != "" { return validation.StringInSlice([]string{"", "insert", "server"}, false)(v, k)
flag := slb.StickySessionType(value)
if flag != "insert" && flag != "server" {
errors = append(errors, fmt.Errorf(
"%q must contain a valid StickySessionType, expected %s or %s, got %q",
k, "insert", "server", value))
}
}
return
} }
func validateSlbListenerCookie(v interface{}, k string) (ws []string, errors []error) { func validateSlbListenerCookie(v interface{}, k string) (ws []string, errors []error) {
if value := v.(string); value != "" { return validation.StringInSlice([]string{"", "insert", "server"}, false)(v, k)
flag := slb.StickySessionType(value)
if flag != "insert" && flag != "server" {
errors = append(errors, fmt.Errorf(
"%q must contain a valid StickySessionType, expected %s or %s, got %q",
k, "insert", "server", value))
}
}
return
} }
func validateSlbListenerPersistenceTimeout(v interface{}, k string) (ws []string, errors []error) { func validateSlbListenerPersistenceTimeout(v interface{}, k string) (ws []string, errors []error) {
value := v.(int) return validation.IntBetween(0, 86400)(v, k)
if value < 0 || value > 86400 {
errors = append(errors, fmt.Errorf(
"%q must be a valid load balancer persistence timeout between 0 and 86400",
k))
return
}
return
} }
//data source validate func //data source validate func
@ -419,19 +244,14 @@ func validateNameRegex(v interface{}, k string) (ws []string, errors []error) {
} }
func validateImageOwners(v interface{}, k string) (ws []string, errors []error) { func validateImageOwners(v interface{}, k string) (ws []string, errors []error) {
if value := v.(string); value != "" { return validation.StringInSlice([]string{
owners := ecs.ImageOwnerAlias(value) "",
if owners != ecs.ImageOwnerSystem && string(ecs.ImageOwnerSystem),
owners != ecs.ImageOwnerSelf && string(ecs.ImageOwnerSelf),
owners != ecs.ImageOwnerOthers && string(ecs.ImageOwnerOthers),
owners != ecs.ImageOwnerMarketplace && string(ecs.ImageOwnerMarketplace),
owners != ecs.ImageOwnerDefault { string(ecs.ImageOwnerDefault),
errors = append(errors, fmt.Errorf( }, false)(v, k)
"%q must contain a valid Image owner , expected %s, %s, %s, %s or %s, got %q",
k, ecs.ImageOwnerSystem, ecs.ImageOwnerSelf, ecs.ImageOwnerOthers, ecs.ImageOwnerMarketplace, ecs.ImageOwnerDefault, owners))
}
}
return
} }
func validateRegion(v interface{}, k string) (ws []string, errors []error) { func validateRegion(v interface{}, k string) (ws []string, errors []error) {

View File

@ -2,6 +2,7 @@ package validation
import ( import (
"fmt" "fmt"
"net"
"strings" "strings"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
@ -47,3 +48,53 @@ func StringInSlice(valid []string, ignoreCase bool) schema.SchemaValidateFunc {
return return
} }
} }
// StringLenBetween returns a SchemaValidateFunc which tests if the provided value
// is of type string and has length between min and max (inclusive)
func StringLenBetween(min, max int) schema.SchemaValidateFunc {
return func(i interface{}, k string) (s []string, es []error) {
v, ok := i.(string)
if !ok {
es = append(es, fmt.Errorf("expected type of %s to be string", k))
return
}
if len(v) < min || len(v) > max {
es = append(es, fmt.Errorf("expected length of %s to be in the range (%d - %d), got %s", k, min, max, v))
}
return
}
}
// CIDRNetwork returns a SchemaValidateFunc which tests if the provided value
// is of type string, is in valid CIDR network notation, and has significant bits between min and max (inclusive)
func CIDRNetwork(min, max int) schema.SchemaValidateFunc {
return func(i interface{}, k string) (s []string, es []error) {
v, ok := i.(string)
if !ok {
es = append(es, fmt.Errorf("expected type of %s to be string", k))
return
}
_, ipnet, err := net.ParseCIDR(v)
if err != nil {
es = append(es, fmt.Errorf(
"expected %s to contain a valid CIDR, got: %s with err: %s", k, v, err))
return
}
if ipnet == nil || v != ipnet.String() {
es = append(es, fmt.Errorf(
"expected %s to contain a valid network CIDR, expected %s, got %s",
k, ipnet, v))
}
sigbits, _ := ipnet.Mask.Size()
if sigbits < min || sigbits > max {
es = append(es, fmt.Errorf(
"expected %q to contain a network CIDR with between %d and %d significant bits, got: %d",
k, min, max, sigbits))
}
return
}
}