Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relocate Valid4ByteAsn function to common lib, add to CloudWAN Network Policy Doc Data Source #27305

Merged
merged 7 commits into from
Nov 3, 2022
3 changes: 3 additions & 0 deletions .changelog/27305.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
data-source/aws_networkmanager_core_network_policy_document: Add plan-time validation for `core_network_configuration.edge_locations.asn`
```
2 changes: 1 addition & 1 deletion internal/service/ec2/transitgateway_connect_peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func ResourceTransitGatewayConnectPeer() *schema.Resource {
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: valid4ByteASN,
ValidateFunc: verify.Valid4ByteASN,
},
"inside_cidr_blocks": {
Type: schema.TypeSet,
Expand Down
15 changes: 0 additions & 15 deletions internal/service/ec2/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,3 @@ func validAmazonSideASN(v interface{}, k string) (ws []string, errors []error) {
}
return
}

func valid4ByteASN(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)

asn, err := strconv.ParseInt(value, 10, 64)
if err != nil {
errors = append(errors, fmt.Errorf("%q (%q) must be a 64-bit integer", k, v))
return
}

if asn < 0 || asn > 4294967295 {
errors = append(errors, fmt.Errorf("%q (%q) must be in the range 0 to 4294967295", k, v))
}
return
}
31 changes: 0 additions & 31 deletions internal/service/ec2/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,34 +74,3 @@ func TestValidAmazonSideASN(t *testing.T) {
}
}
}

func TestValid4ByteASN(t *testing.T) {
validAsns := []string{
"0",
"1",
"65534",
"65535",
"4294967294",
"4294967295",
}
for _, v := range validAsns {
_, errors := valid4ByteASN(v, "bgp_asn")
if len(errors) != 0 {
t.Fatalf("%q should be a valid ASN: %q", v, errors)
}
}

invalidAsns := []string{
"-1",
"ABCDEFG",
"",
"4294967296",
"9999999999",
}
for _, v := range invalidAsns {
_, errors := valid4ByteASN(v, "bgp_asn")
if len(errors) == 0 {
t.Fatalf("%q should be an invalid ASN", v)
}
}
}
19 changes: 11 additions & 8 deletions internal/service/ec2/vpnsite_customer_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func ResourceCustomerGateway() *schema.Resource {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: valid4ByteASN,
ValidateFunc: verify.Valid4ByteASN,
},
"certificate_arn": {
Type: schema.TypeString,
Expand Down Expand Up @@ -76,19 +76,22 @@ func resourceCustomerGatewayCreate(d *schema.ResourceData, meta interface{}) err
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(d.Get("tags").(map[string]interface{})))

i64BgpAsn, err := strconv.ParseInt(d.Get("bgp_asn").(string), 10, 64)

if err != nil {
return err
}

input := &ec2.CreateCustomerGatewayInput{
BgpAsn: aws.Int64(i64BgpAsn),
IpAddress: aws.String(d.Get("ip_address").(string)),
TagSpecifications: tagSpecificationsFromKeyValueTags(tags, ec2.ResourceTypeCustomerGateway),
Type: aws.String(d.Get("type").(string)),
}

if v, ok := d.GetOk("bgp_asn"); ok {
v, err := strconv.ParseInt(v.(string), 10, 64)

if err != nil {
return err
}

input.BgpAsn = aws.Int64(v)
}

if v, ok := d.GetOk("certificate_arn"); ok {
input.CertificateArn = aws.String(v.(string))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,9 @@ func DataSourceCoreNetworkPolicyDocument() *schema.Resource {
ValidateFunc: verify.ValidRegionName,
},
"asn": {
Type: schema.TypeInt,
Optional: true,
Type: schema.TypeString,
Optional: true,
ValidateFunc: verify.Valid4ByteASN,
},
"inside_cidr_blocks": {
Type: schema.TypeList,
Expand Down Expand Up @@ -625,8 +626,14 @@ func expandDataCoreNetworkPolicyNetworkConfigurationEdgeLocations(tfList []inter
locMap[edgeLocation.Location] = struct{}{}
}

if v, ok := cfgEdgeLocation["asn"]; ok {
edgeLocation.Asn = v.(int)
if v, ok := cfgEdgeLocation["asn"].(string); ok && v != "" {
v, err := strconv.ParseInt(v, 10, 64)

if err != nil {
return nil, err
}

edgeLocation.Asn = v
}

if cidrs := cfgEdgeLocation["inside_cidr_blocks"].([]interface{}); len(cidrs) > 0 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type CoreNetworkPolicyCoreNetworkConfiguration struct {

type CoreNetworkEdgeLocation struct {
Location string `json:"location"`
Asn int `json:"asn,omitempty"`
Asn int64 `json:"asn,omitempty"`
InsideCidrBlocks interface{} `json:"inside-cidr-blocks,omitempty"`
}

Expand Down
15 changes: 15 additions & 0 deletions internal/verify/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ var accountIDRegexp = regexp.MustCompile(`^(aws|aws-managed|\d{12})$`)
var partitionRegexp = regexp.MustCompile(`^aws(-[a-z]+)*$`)
var regionRegexp = regexp.MustCompile(`^[a-z]{2}(-[a-z]+)+-\d$`)

func Valid4ByteASN(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)

asn, err := strconv.ParseInt(value, 10, 64)
if err != nil {
errors = append(errors, fmt.Errorf("%q (%q) must be a 64-bit integer", k, v))
return
}

if asn < 0 || asn > 4294967295 {
errors = append(errors, fmt.Errorf("%q (%q) must be in the range 0 to 4294967295", k, v))
}
return
}

func ValidARN(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)

Expand Down
31 changes: 31 additions & 0 deletions internal/verify/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,37 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func TestValid4ByteASNString(t *testing.T) {
validAsns := []string{
"0",
"1",
"65534",
"65535",
"4294967294",
"4294967295",
}
for _, v := range validAsns {
_, errors := Valid4ByteASN(v, "bgp_asn")
if len(errors) != 0 {
t.Fatalf("%q should be a valid ASN: %q", v, errors)
}
}

invalidAsns := []string{
"-1",
"ABCDEFG",
"",
"4294967296",
"9999999999",
}
for _, v := range invalidAsns {
_, errors := Valid4ByteASN(v, "bgp_asn")
if len(errors) == 0 {
t.Fatalf("%q should be an invalid ASN", v)
}
}
}

func TestValidTypeStringNullableBoolean(t *testing.T) {
testCases := []struct {
val interface{}
Expand Down