Merge pull request #5255 from hashicorp/phinze/improve-vpc-cidr-errmsg
provider/aws: improve vpc cidr_block err message
This commit is contained in:
commit
5528abb51e
|
@ -3,7 +3,6 @@ package aws
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go/aws"
|
"github.com/aws/aws-sdk-go/aws"
|
||||||
|
@ -25,16 +24,7 @@ func resourceAwsVpc() *schema.Resource {
|
||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
Required: true,
|
Required: true,
|
||||||
ForceNew: true,
|
ForceNew: true,
|
||||||
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
|
ValidateFunc: validateCIDRNetworkAddress,
|
||||||
value := v.(string)
|
|
||||||
_, ipnet, err := net.ParseCIDR(value)
|
|
||||||
|
|
||||||
if err != nil || ipnet == nil || value != ipnet.String() {
|
|
||||||
errors = append(errors, fmt.Errorf(
|
|
||||||
"%q must contain a valid CIDR", k))
|
|
||||||
}
|
|
||||||
return
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
|
|
||||||
"instance_tenancy": &schema.Schema{
|
"instance_tenancy": &schema.Schema{
|
||||||
|
|
|
@ -2,6 +2,7 @@ package aws
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
"regexp"
|
"regexp"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -278,3 +279,23 @@ func validatePolicyStatementId(v interface{}, k string) (ws []string, errors []e
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// validateCIDRNetworkAddress ensures that the string value is a valid CIDR that
|
||||||
|
// represents a network address - it adds an error otherwise
|
||||||
|
func validateCIDRNetworkAddress(v interface{}, k string) (ws []string, errors []error) {
|
||||||
|
value := v.(string)
|
||||||
|
_, 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
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package aws
|
package aws
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -243,3 +244,33 @@ func TestValidatePolicyStatementId(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestValidateCIDRNetworkAddress(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
CIDR string
|
||||||
|
ExpectedErrSubstr string
|
||||||
|
}{
|
||||||
|
{"notacidr", `must contain a valid CIDR`},
|
||||||
|
{"10.0.1.0/16", `must contain a valid network CIDR`},
|
||||||
|
{"10.0.1.0/24", ``},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, tc := range cases {
|
||||||
|
_, errs := validateCIDRNetworkAddress(tc.CIDR, "foo")
|
||||||
|
if tc.ExpectedErrSubstr == "" {
|
||||||
|
if len(errs) != 0 {
|
||||||
|
t.Fatalf("%d/%d: Expected no error, got errs: %#v",
|
||||||
|
i+1, len(cases), errs)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if len(errs) != 1 {
|
||||||
|
t.Fatalf("%d/%d: Expected 1 err containing %q, got %d errs",
|
||||||
|
i+1, len(cases), tc.ExpectedErrSubstr, len(errs))
|
||||||
|
}
|
||||||
|
if !strings.Contains(errs[0].Error(), tc.ExpectedErrSubstr) {
|
||||||
|
t.Fatalf("%d/%d: Expected err: %q, to include %q",
|
||||||
|
i+1, len(cases), errs[0], tc.ExpectedErrSubstr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue