github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/opc/validators.go (about) 1 package opc 2 3 import ( 4 "fmt" 5 "net" 6 7 "github.com/hashicorp/go-oracle-terraform/compute" 8 ) 9 10 // Validate whether an IP Prefix CIDR is correct or not 11 func validateIPPrefixCIDR(v interface{}, k string) (ws []string, errors []error) { 12 value := v.(string) 13 14 _, ipnet, err := net.ParseCIDR(value) 15 if err != nil { 16 errors = append(errors, fmt.Errorf( 17 "%q must contain a valid CIDR, got error while parsing: %s", k, err)) 18 return 19 } 20 21 if ipnet == nil || value != ipnet.String() { 22 errors = append(errors, fmt.Errorf( 23 "%q must contain a valid network CIDR, expected %q, got %q", k, ipnet, value)) 24 return 25 } 26 return 27 } 28 29 // Admin distance can either be a 0, 1, or a 2. Defaults to 0. 30 func validateAdminDistance(v interface{}, k string) (ws []string, errors []error) { 31 value := v.(int) 32 33 if value < 0 || value > 2 { 34 errors = append(errors, fmt.Errorf( 35 "%q can only be an interger between 0-2. Got: %d", k, value)) 36 } 37 return 38 } 39 40 // Admin distance can either be a 0, 1, or a 2. Defaults to 0. 41 func validateIPProtocol(v interface{}, k string) (ws []string, errors []error) { 42 validProtocols := map[string]struct{}{ 43 string(compute.All): {}, 44 string(compute.AH): {}, 45 string(compute.ESP): {}, 46 string(compute.ICMP): {}, 47 string(compute.ICMPV6): {}, 48 string(compute.IGMP): {}, 49 string(compute.IPIP): {}, 50 string(compute.GRE): {}, 51 string(compute.MPLSIP): {}, 52 string(compute.OSPF): {}, 53 string(compute.PIM): {}, 54 string(compute.RDP): {}, 55 string(compute.SCTP): {}, 56 string(compute.TCP): {}, 57 string(compute.UDP): {}, 58 } 59 60 value := v.(string) 61 if _, ok := validProtocols[value]; !ok { 62 errors = append(errors, fmt.Errorf( 63 `%q must contain a valid Image owner , expected ["all", "ah", "esp", "icmp", "icmpv6", "igmp", "ipip", "gre", "mplsip", "ospf", "pim", "rdp", "sctp", "tcp", "udp"] got %q`, 64 k, value)) 65 } 66 return 67 }