Skip to content

Commit

Permalink
Add support for Amazon side private ASN for aws_vpn_gateway (#1888)
Browse files Browse the repository at this point in the history
* Add support for Amazon side private ASN for aws_vpn_gateway.

* Use 'TypeFloat' (golang float64) instead of 'TypeString' for Amazon side ASN.

* Additional filter on available VGWs in acceptance test.

* Revert "Use 'TypeFloat' (golang float64) instead of 'TypeString' for Amazon side ASN."

This reverts commit 1c7e0aa0c977e9b7edb2b348fd356b590e1d976c.
  • Loading branch information
ewbankkit authored and radeksimko committed Feb 8, 2018
1 parent fc732e5 commit 41b957f
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 103 deletions.
14 changes: 14 additions & 0 deletions aws/data_source_aws_vpn_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"log"
"strconv"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
Expand Down Expand Up @@ -34,6 +35,11 @@ func dataSourceAwsVpnGateway() *schema.Resource {
Optional: true,
Computed: true,
},
"amazon_side_asn": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"filter": ec2CustomFiltersSchema(),
"tags": tagsSchemaComputed(),
},
Expand All @@ -55,6 +61,13 @@ func dataSourceAwsVpnGatewayRead(d *schema.ResourceData, meta interface{}) error
"availability-zone": d.Get("availability_zone").(string),
},
)
if asn, ok := d.GetOk("amazon_side_asn"); ok {
req.Filters = append(req.Filters, buildEC2AttributeFilterList(
map[string]string{
"amazon-side-asn": asn.(string),
},
)...)
}
if id, ok := d.GetOk("attached_vpc_id"); ok {
req.Filters = append(req.Filters, buildEC2AttributeFilterList(
map[string]string{
Expand Down Expand Up @@ -91,6 +104,7 @@ func dataSourceAwsVpnGatewayRead(d *schema.ResourceData, meta interface{}) error
d.SetId(aws.StringValue(vgw.VpnGatewayId))
d.Set("state", vgw.State)
d.Set("availability_zone", vgw.AvailabilityZone)
d.Set("amazon_side_asn", strconv.FormatInt(aws.Int64Value(vgw.AmazonSideAsn), 10))
d.Set("tags", tagsToMap(vgw.Tags))

for _, attachment := range vgw.VpcAttachments {
Expand Down
48 changes: 25 additions & 23 deletions aws/data_source_aws_vpn_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ func TestAccDataSourceAwsVpnGateway_unattached(t *testing.T) {
resource.TestCheckResourceAttrPair(
"data.aws_vpn_gateway.test_by_tags", "id",
"aws_vpn_gateway.unattached", "id"),
resource.TestCheckResourceAttrPair(
"data.aws_vpn_gateway.test_by_amazon_side_asn", "id",
"aws_vpn_gateway.unattached", "id"),
resource.TestCheckResourceAttrSet("data.aws_vpn_gateway.test_by_id", "state"),
resource.TestCheckResourceAttr("data.aws_vpn_gateway.test_by_tags", "tags.%", "3"),
resource.TestCheckNoResourceAttr("data.aws_vpn_gateway.test_by_id", "attached_vpc_id"),
resource.TestCheckResourceAttr("data.aws_vpn_gateway.test_by_amazon_side_asn", "amazon_side_asn", "4294967293"),
),
},
},
Expand Down Expand Up @@ -59,46 +63,44 @@ func TestAccDataSourceAwsVpnGateway_attached(t *testing.T) {

func testAccDataSourceAwsVpnGatewayUnattachedConfig(rInt int) string {
return fmt.Sprintf(`
provider "aws" {
region = "us-west-2"
}
resource "aws_vpn_gateway" "unattached" {
tags {
Name = "terraform-testacc-vpn-gateway-data-source-unattached-%d"
ABC = "testacc-%d"
XYZ = "testacc-%d"
}
tags {
Name = "terraform-testacc-vpn-gateway-data-source-unattached-%d"
ABC = "testacc-%d"
XYZ = "testacc-%d"
}
amazon_side_asn = 4294967293
}
data "aws_vpn_gateway" "test_by_id" {
id = "${aws_vpn_gateway.unattached.id}"
id = "${aws_vpn_gateway.unattached.id}"
}
data "aws_vpn_gateway" "test_by_tags" {
tags = "${aws_vpn_gateway.unattached.tags}"
tags = "${aws_vpn_gateway.unattached.tags}"
}
data "aws_vpn_gateway" "test_by_amazon_side_asn" {
amazon_side_asn = "${aws_vpn_gateway.unattached.amazon_side_asn}"
state = "available"
}
`, rInt, rInt+1, rInt-1)
}

func testAccDataSourceAwsVpnGatewayAttachedConfig(rInt int) string {
return fmt.Sprintf(`
provider "aws" {
region = "us-west-2"
}
resource "aws_vpc" "foo" {
cidr_block = "10.1.0.0/16"
cidr_block = "10.1.0.0/16"
tags {
Name = "terraform-testacc-vpn-gateway-data-source-foo-%d"
}
tags {
Name = "terraform-testacc-vpn-gateway-data-source-foo-%d"
}
}
resource "aws_vpn_gateway" "attached" {
tags {
Name = "terraform-testacc-vpn-gateway-data-source-attached-%d"
}
tags {
Name = "terraform-testacc-vpn-gateway-data-source-attached-%d"
}
}
resource "aws_vpn_gateway_attachment" "vpn_attachment" {
Expand All @@ -107,7 +109,7 @@ resource "aws_vpn_gateway_attachment" "vpn_attachment" {
}
data "aws_vpn_gateway" "test_by_attached_vpc_id" {
attached_vpc_id = "${aws_vpn_gateway_attachment.vpn_attachment.vpc_id}"
attached_vpc_id = "${aws_vpn_gateway_attachment.vpn_attachment.vpc_id}"
}
`, rInt, rInt)
}
21 changes: 19 additions & 2 deletions aws/resource_aws_vpn_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"log"
"strconv"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -23,13 +24,21 @@ func resourceAwsVpnGateway() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"availability_zone": &schema.Schema{
"availability_zone": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"vpc_id": &schema.Schema{
"amazon_side_asn": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
ValidateFunc: validateAmazonSideAsn,
},

"vpc_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Expand All @@ -47,6 +56,13 @@ func resourceAwsVpnGatewayCreate(d *schema.ResourceData, meta interface{}) error
AvailabilityZone: aws.String(d.Get("availability_zone").(string)),
Type: aws.String("ipsec.1"),
}
if asn, ok := d.GetOk("amazon_side_asn"); ok {
i, err := strconv.ParseInt(asn.(string), 10, 64)
if err != nil {
return err
}
createOpts.AmazonSideAsn = aws.Int64(i)
}

// Create the VPN gateway
log.Printf("[DEBUG] Creating VPN gateway")
Expand Down Expand Up @@ -98,6 +114,7 @@ func resourceAwsVpnGatewayRead(d *schema.ResourceData, meta interface{}) error {
if vpnGateway.AvailabilityZone != nil && *vpnGateway.AvailabilityZone != "" {
d.Set("availability_zone", vpnGateway.AvailabilityZone)
}
d.Set("amazon_side_asn", strconv.FormatInt(aws.Int64Value(vpnGateway.AmazonSideAsn), 10))
d.Set("tags", tagsToMap(vpnGateway.Tags))

return nil
Expand Down
Loading

0 comments on commit 41b957f

Please sign in to comment.