github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/alicloud/validators.go (about)

     1  package alicloud
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"strconv"
     7  	"strings"
     8  
     9  	"github.com/denverdino/aliyungo/common"
    10  	"github.com/denverdino/aliyungo/ecs"
    11  	"github.com/denverdino/aliyungo/slb"
    12  	"regexp"
    13  )
    14  
    15  // common
    16  func validateInstancePort(v interface{}, k string) (ws []string, errors []error) {
    17  	value := v.(int)
    18  	if value < 1 || value > 65535 {
    19  		errors = append(errors, fmt.Errorf(
    20  			"%q must be a valid instance port between 1 and 65535",
    21  			k))
    22  		return
    23  	}
    24  	return
    25  }
    26  
    27  func validateInstanceProtocol(v interface{}, k string) (ws []string, errors []error) {
    28  	protocal := v.(string)
    29  	if !isProtocalValid(protocal) {
    30  		errors = append(errors, fmt.Errorf(
    31  			"%q is an invalid value. Valid values are either http, https, tcp or udp",
    32  			k))
    33  		return
    34  	}
    35  	return
    36  }
    37  
    38  // ecs
    39  func validateDiskCategory(v interface{}, k string) (ws []string, errors []error) {
    40  	category := ecs.DiskCategory(v.(string))
    41  	if category != ecs.DiskCategoryCloud && category != ecs.DiskCategoryCloudEfficiency && category != ecs.DiskCategoryCloudSSD {
    42  		errors = append(errors, fmt.Errorf("%s must be one of %s %s %s", k, ecs.DiskCategoryCloud, ecs.DiskCategoryCloudEfficiency, ecs.DiskCategoryCloudSSD))
    43  	}
    44  
    45  	return
    46  }
    47  
    48  func validateInstanceName(v interface{}, k string) (ws []string, errors []error) {
    49  	value := v.(string)
    50  	if len(value) < 2 || len(value) > 128 {
    51  		errors = append(errors, fmt.Errorf("%q cannot be longer than 128 characters", k))
    52  	}
    53  
    54  	if strings.HasPrefix(value, "http://") || strings.HasPrefix(value, "https://") {
    55  		errors = append(errors, fmt.Errorf("%s cannot starts with http:// or https://", k))
    56  	}
    57  
    58  	return
    59  }
    60  
    61  func validateInstanceDescription(v interface{}, k string) (ws []string, errors []error) {
    62  	value := v.(string)
    63  	if len(value) < 2 || len(value) > 256 {
    64  		errors = append(errors, fmt.Errorf("%q cannot be longer than 256 characters", k))
    65  
    66  	}
    67  	return
    68  }
    69  
    70  func validateDiskName(v interface{}, k string) (ws []string, errors []error) {
    71  	value := v.(string)
    72  
    73  	if value == "" {
    74  		return
    75  	}
    76  
    77  	if len(value) < 2 || len(value) > 128 {
    78  		errors = append(errors, fmt.Errorf("%q cannot be longer than 128 characters", k))
    79  	}
    80  
    81  	if strings.HasPrefix(value, "http://") || strings.HasPrefix(value, "https://") {
    82  		errors = append(errors, fmt.Errorf("%s cannot starts with http:// or https://", k))
    83  	}
    84  
    85  	return
    86  }
    87  
    88  func validateDiskDescription(v interface{}, k string) (ws []string, errors []error) {
    89  	value := v.(string)
    90  	if len(value) < 2 || len(value) > 256 {
    91  		errors = append(errors, fmt.Errorf("%q cannot be longer than 256 characters", k))
    92  
    93  	}
    94  	return
    95  }
    96  
    97  //security group
    98  func validateSecurityGroupName(v interface{}, k string) (ws []string, errors []error) {
    99  	value := v.(string)
   100  	if len(value) < 2 || len(value) > 128 {
   101  		errors = append(errors, fmt.Errorf("%q cannot be longer than 128 characters", k))
   102  	}
   103  
   104  	if strings.HasPrefix(value, "http://") || strings.HasPrefix(value, "https://") {
   105  		errors = append(errors, fmt.Errorf("%s cannot starts with http:// or https://", k))
   106  	}
   107  
   108  	return
   109  }
   110  
   111  func validateSecurityGroupDescription(v interface{}, k string) (ws []string, errors []error) {
   112  	value := v.(string)
   113  	if len(value) < 2 || len(value) > 256 {
   114  		errors = append(errors, fmt.Errorf("%q cannot be longer than 256 characters", k))
   115  
   116  	}
   117  	return
   118  }
   119  
   120  func validateSecurityRuleType(v interface{}, k string) (ws []string, errors []error) {
   121  	rt := GroupRuleDirection(v.(string))
   122  	if rt != GroupRuleIngress && rt != GroupRuleEgress {
   123  		errors = append(errors, fmt.Errorf("%s must be one of %s %s", k, GroupRuleIngress, GroupRuleEgress))
   124  	}
   125  
   126  	return
   127  }
   128  
   129  func validateSecurityRuleIpProtocol(v interface{}, k string) (ws []string, errors []error) {
   130  	pt := GroupRuleIpProtocol(v.(string))
   131  	if pt != GroupRuleTcp && pt != GroupRuleUdp && pt != GroupRuleIcmp && pt != GroupRuleGre && pt != GroupRuleAll {
   132  		errors = append(errors, fmt.Errorf("%s must be one of %s %s %s %s %s", k,
   133  			GroupRuleTcp, GroupRuleUdp, GroupRuleIcmp, GroupRuleGre, GroupRuleAll))
   134  	}
   135  
   136  	return
   137  }
   138  
   139  func validateSecurityRuleNicType(v interface{}, k string) (ws []string, errors []error) {
   140  	pt := GroupRuleNicType(v.(string))
   141  	if pt != GroupRuleInternet && pt != GroupRuleIntranet {
   142  		errors = append(errors, fmt.Errorf("%s must be one of %s %s", k, GroupRuleInternet, GroupRuleIntranet))
   143  	}
   144  
   145  	return
   146  }
   147  
   148  func validateSecurityRulePolicy(v interface{}, k string) (ws []string, errors []error) {
   149  	pt := GroupRulePolicy(v.(string))
   150  	if pt != GroupRulePolicyAccept && pt != GroupRulePolicyDrop {
   151  		errors = append(errors, fmt.Errorf("%s must be one of %s %s", k, GroupRulePolicyAccept, GroupRulePolicyDrop))
   152  	}
   153  
   154  	return
   155  }
   156  
   157  func validateSecurityPriority(v interface{}, k string) (ws []string, errors []error) {
   158  	value := v.(int)
   159  	if value < 1 || value > 100 {
   160  		errors = append(errors, fmt.Errorf(
   161  			"%q must be a valid authorization policy priority between 1 and 100",
   162  			k))
   163  		return
   164  	}
   165  	return
   166  }
   167  
   168  // validateCIDRNetworkAddress ensures that the string value is a valid CIDR that
   169  // represents a network address - it adds an error otherwise
   170  func validateCIDRNetworkAddress(v interface{}, k string) (ws []string, errors []error) {
   171  	value := v.(string)
   172  	_, ipnet, err := net.ParseCIDR(value)
   173  	if err != nil {
   174  		errors = append(errors, fmt.Errorf(
   175  			"%q must contain a valid CIDR, got error parsing: %s", k, err))
   176  		return
   177  	}
   178  
   179  	if ipnet == nil || value != ipnet.String() {
   180  		errors = append(errors, fmt.Errorf(
   181  			"%q must contain a valid network CIDR, expected %q, got %q",
   182  			k, ipnet, value))
   183  	}
   184  
   185  	return
   186  }
   187  
   188  func validateRouteEntryNextHopType(v interface{}, k string) (ws []string, errors []error) {
   189  	nht := ecs.NextHopType(v.(string))
   190  	if nht != ecs.NextHopIntance && nht != ecs.NextHopTunnel {
   191  		errors = append(errors, fmt.Errorf("%s must be one of %s %s", k,
   192  			ecs.NextHopIntance, ecs.NextHopTunnel))
   193  	}
   194  
   195  	return
   196  }
   197  
   198  func validateSwitchCIDRNetworkAddress(v interface{}, k string) (ws []string, errors []error) {
   199  	value := v.(string)
   200  	_, ipnet, err := net.ParseCIDR(value)
   201  	if err != nil {
   202  		errors = append(errors, fmt.Errorf(
   203  			"%q must contain a valid CIDR, got error parsing: %s", k, err))
   204  		return
   205  	}
   206  
   207  	if ipnet == nil || value != ipnet.String() {
   208  		errors = append(errors, fmt.Errorf(
   209  			"%q must contain a valid network CIDR, expected %q, got %q",
   210  			k, ipnet, value))
   211  		return
   212  	}
   213  
   214  	mark, _ := strconv.Atoi(strings.Split(ipnet.String(), "/")[1])
   215  	if mark < 16 || mark > 29 {
   216  		errors = append(errors, fmt.Errorf(
   217  			"%q must contain a network CIDR which mark between 16 and 29",
   218  			k))
   219  	}
   220  
   221  	return
   222  }
   223  
   224  // validateIoOptimized ensures that the string value is a valid IoOptimized that
   225  // represents a IoOptimized - it adds an error otherwise
   226  func validateIoOptimized(v interface{}, k string) (ws []string, errors []error) {
   227  	if value := v.(string); value != "" {
   228  		ioOptimized := ecs.IoOptimized(value)
   229  		if ioOptimized != ecs.IoOptimizedNone &&
   230  			ioOptimized != ecs.IoOptimizedOptimized {
   231  			errors = append(errors, fmt.Errorf(
   232  				"%q must contain a valid IoOptimized, expected %s or %s, got %q",
   233  				k, ecs.IoOptimizedNone, ecs.IoOptimizedOptimized, ioOptimized))
   234  		}
   235  	}
   236  
   237  	return
   238  }
   239  
   240  // validateInstanceNetworkType ensures that the string value is a classic or vpc
   241  func validateInstanceNetworkType(v interface{}, k string) (ws []string, errors []error) {
   242  	if value := v.(string); value != "" {
   243  		network := InstanceNetWork(value)
   244  		if network != ClassicNet &&
   245  			network != VpcNet {
   246  			errors = append(errors, fmt.Errorf(
   247  				"%q must contain a valid InstanceNetworkType, expected %s or %s, go %q",
   248  				k, ClassicNet, VpcNet, network))
   249  		}
   250  	}
   251  	return
   252  }
   253  
   254  func validateInstanceChargeType(v interface{}, k string) (ws []string, errors []error) {
   255  	if value := v.(string); value != "" {
   256  		chargeType := common.InstanceChargeType(value)
   257  		if chargeType != common.PrePaid &&
   258  			chargeType != common.PostPaid {
   259  			errors = append(errors, fmt.Errorf(
   260  				"%q must contain a valid InstanceChargeType, expected %s or %s, got %q",
   261  				k, common.PrePaid, common.PostPaid, chargeType))
   262  		}
   263  	}
   264  
   265  	return
   266  }
   267  
   268  func validateInternetChargeType(v interface{}, k string) (ws []string, errors []error) {
   269  	if value := v.(string); value != "" {
   270  		chargeType := common.InternetChargeType(value)
   271  		if chargeType != common.PayByBandwidth &&
   272  			chargeType != common.PayByTraffic {
   273  			errors = append(errors, fmt.Errorf(
   274  				"%q must contain a valid InstanceChargeType, expected %s or %s, got %q",
   275  				k, common.PayByBandwidth, common.PayByTraffic, chargeType))
   276  		}
   277  	}
   278  
   279  	return
   280  }
   281  
   282  func validateInternetMaxBandWidthOut(v interface{}, k string) (ws []string, errors []error) {
   283  	value := v.(int)
   284  	if value < 1 || value > 100 {
   285  		errors = append(errors, fmt.Errorf(
   286  			"%q must be a valid internet bandwidth out between 1 and 1000",
   287  			k))
   288  		return
   289  	}
   290  	return
   291  }
   292  
   293  // SLB
   294  func validateSlbName(v interface{}, k string) (ws []string, errors []error) {
   295  	if value := v.(string); value != "" {
   296  		if len(value) < 1 || len(value) > 80 {
   297  			errors = append(errors, fmt.Errorf(
   298  				"%q must be a valid load balancer name characters between 1 and 80",
   299  				k))
   300  			return
   301  		}
   302  	}
   303  
   304  	return
   305  }
   306  
   307  func validateSlbInternetChargeType(v interface{}, k string) (ws []string, errors []error) {
   308  	if value := v.(string); value != "" {
   309  		chargeType := common.InternetChargeType(value)
   310  
   311  		if chargeType != "paybybandwidth" &&
   312  			chargeType != "paybytraffic" {
   313  			errors = append(errors, fmt.Errorf(
   314  				"%q must contain a valid InstanceChargeType, expected %s or %s, got %q",
   315  				k, "paybybandwidth", "paybytraffic", value))
   316  		}
   317  	}
   318  
   319  	return
   320  }
   321  
   322  func validateSlbBandwidth(v interface{}, k string) (ws []string, errors []error) {
   323  	value := v.(int)
   324  	if value < 1 || value > 1000 {
   325  		errors = append(errors, fmt.Errorf(
   326  			"%q must be a valid load balancer bandwidth between 1 and 1000",
   327  			k))
   328  		return
   329  	}
   330  	return
   331  }
   332  
   333  func validateSlbListenerBandwidth(v interface{}, k string) (ws []string, errors []error) {
   334  	value := v.(int)
   335  	if (value < 1 || value > 1000) && value != -1 {
   336  		errors = append(errors, fmt.Errorf(
   337  			"%q must be a valid load balancer bandwidth between 1 and 1000 or -1",
   338  			k))
   339  		return
   340  	}
   341  	return
   342  }
   343  
   344  func validateSlbListenerScheduler(v interface{}, k string) (ws []string, errors []error) {
   345  	if value := v.(string); value != "" {
   346  		scheduler := slb.SchedulerType(value)
   347  
   348  		if scheduler != "wrr" && scheduler != "wlc" {
   349  			errors = append(errors, fmt.Errorf(
   350  				"%q must contain a valid SchedulerType, expected %s or %s, got %q",
   351  				k, "wrr", "wlc", value))
   352  		}
   353  	}
   354  
   355  	return
   356  }
   357  
   358  func validateSlbListenerStickySession(v interface{}, k string) (ws []string, errors []error) {
   359  	if value := v.(string); value != "" {
   360  		flag := slb.FlagType(value)
   361  
   362  		if flag != "on" && flag != "off" {
   363  			errors = append(errors, fmt.Errorf(
   364  				"%q must contain a valid StickySession, expected %s or %s, got %q",
   365  				k, "on", "off", value))
   366  		}
   367  	}
   368  	return
   369  }
   370  
   371  func validateSlbListenerStickySessionType(v interface{}, k string) (ws []string, errors []error) {
   372  	if value := v.(string); value != "" {
   373  		flag := slb.StickySessionType(value)
   374  
   375  		if flag != "insert" && flag != "server" {
   376  			errors = append(errors, fmt.Errorf(
   377  				"%q must contain a valid StickySessionType, expected %s or %s, got %q",
   378  				k, "insert", "server", value))
   379  		}
   380  	}
   381  	return
   382  }
   383  
   384  func validateSlbListenerCookie(v interface{}, k string) (ws []string, errors []error) {
   385  	if value := v.(string); value != "" {
   386  		flag := slb.StickySessionType(value)
   387  
   388  		if flag != "insert" && flag != "server" {
   389  			errors = append(errors, fmt.Errorf(
   390  				"%q must contain a valid StickySessionType, expected %s or %s, got %q",
   391  				k, "insert", "server", value))
   392  		}
   393  	}
   394  	return
   395  }
   396  
   397  func validateSlbListenerPersistenceTimeout(v interface{}, k string) (ws []string, errors []error) {
   398  	value := v.(int)
   399  	if value < 0 || value > 86400 {
   400  		errors = append(errors, fmt.Errorf(
   401  			"%q must be a valid load balancer persistence timeout between 0 and 86400",
   402  			k))
   403  		return
   404  	}
   405  	return
   406  }
   407  
   408  //data source validate func
   409  //data_source_alicloud_image
   410  func validateNameRegex(v interface{}, k string) (ws []string, errors []error) {
   411  	value := v.(string)
   412  
   413  	if _, err := regexp.Compile(value); err != nil {
   414  		errors = append(errors, fmt.Errorf(
   415  			"%q contains an invalid regular expression: %s",
   416  			k, err))
   417  	}
   418  	return
   419  }
   420  
   421  func validateImageOwners(v interface{}, k string) (ws []string, errors []error) {
   422  	if value := v.(string); value != "" {
   423  		owners := ecs.ImageOwnerAlias(value)
   424  		if owners != ecs.ImageOwnerSystem &&
   425  			owners != ecs.ImageOwnerSelf &&
   426  			owners != ecs.ImageOwnerOthers &&
   427  			owners != ecs.ImageOwnerMarketplace &&
   428  			owners != ecs.ImageOwnerDefault {
   429  			errors = append(errors, fmt.Errorf(
   430  				"%q must contain a valid Image owner , expected %s, %s, %s, %s or %s, got %q",
   431  				k, ecs.ImageOwnerSystem, ecs.ImageOwnerSelf, ecs.ImageOwnerOthers, ecs.ImageOwnerMarketplace, ecs.ImageOwnerDefault, owners))
   432  		}
   433  	}
   434  	return
   435  }
   436  
   437  func validateRegion(v interface{}, k string) (ws []string, errors []error) {
   438  	if value := v.(string); value != "" {
   439  		region := common.Region(value)
   440  		var valid string
   441  		for _, re := range common.ValidRegions {
   442  			if region == re {
   443  				return
   444  			}
   445  			valid = valid + ", " + string(re)
   446  		}
   447  		errors = append(errors, fmt.Errorf(
   448  			"%q must contain a valid Region ID , expected %#v, got %q",
   449  			k, valid, value))
   450  
   451  	}
   452  	return
   453  }