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

r/aws_customer_gateway: Add device_name attribute #14786

Merged
merged 2 commits into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion aws/data_source_aws_customer_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ func dataSourceAwsCustomerGateway() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},

"bgp_asn": {
Type: schema.TypeInt,
Computed: true,
},
"device_name": {
Type: schema.TypeString,
Computed: true,
},
"ip_address": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -80,6 +83,7 @@ func dataSourceAwsCustomerGatewayRead(d *schema.ResourceData, meta interface{})

d.Set("ip_address", cg.IpAddress)
d.Set("type", cg.Type)
d.Set("device_name", cg.DeviceName)
d.SetId(aws.StringValue(cg.CustomerGatewayId))

if v := aws.StringValue(cg.BgpAsn); v != "" {
Expand Down
8 changes: 5 additions & 3 deletions aws/data_source_aws_customer_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func TestAccAWSCustomerGatewayDataSource_ID(t *testing.T) {
resource.TestCheckResourceAttrPair(resourceName, "ip_address", dataSourceName, "ip_address"),
resource.TestCheckResourceAttrPair(resourceName, "tags.%", dataSourceName, "tags.%"),
resource.TestCheckResourceAttrPair(resourceName, "type", dataSourceName, "type"),
resource.TestCheckResourceAttrPair(resourceName, "device_name", dataSourceName, "device_name"),
),
},
},
Expand Down Expand Up @@ -88,9 +89,10 @@ data "aws_customer_gateway" "test" {
func testAccAWSCustomerGatewayDataSourceConfigID(asn, hostOctet int) string {
return fmt.Sprintf(`
resource "aws_customer_gateway" "test" {
bgp_asn = %d
ip_address = "50.0.0.%d"
type = "ipsec.1"
bgp_asn = %d
ip_address = "50.0.0.%d"
device_name = "test"
type = "ipsec.1"
}

data "aws_customer_gateway" "test" {
Expand Down
50 changes: 36 additions & 14 deletions aws/resource_aws_customer_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ func resourceAwsCustomerGateway() *schema.Resource {
ValidateFunc: validate4ByteAsn,
},

"device_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 255),
},

"ip_address": {
Type: schema.TypeString,
Required: true,
Expand All @@ -53,6 +60,7 @@ func resourceAwsCustomerGateway() *schema.Resource {
},

"tags": tagsSchema(),

"arn": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -67,8 +75,9 @@ func resourceAwsCustomerGatewayCreate(d *schema.ResourceData, meta interface{})
ipAddress := d.Get("ip_address").(string)
vpnType := d.Get("type").(string)
bgpAsn := d.Get("bgp_asn").(string)
deviceName := d.Get("device_name").(string)

alreadyExists, err := resourceAwsCustomerGatewayExists(vpnType, ipAddress, bgpAsn, conn)
alreadyExists, err := resourceAwsCustomerGatewayExists(vpnType, ipAddress, bgpAsn, deviceName, conn)
if err != nil {
return err
}
Expand All @@ -89,6 +98,10 @@ func resourceAwsCustomerGatewayCreate(d *schema.ResourceData, meta interface{})
TagSpecifications: ec2TagSpecificationsFromMap(d.Get("tags").(map[string]interface{}), ec2.ResourceTypeCustomerGateway),
}

if len(deviceName) != 0 {
createOpts.DeviceName = aws.String(deviceName)
}

// Create the Customer Gateway.
log.Printf("[DEBUG] Creating customer gateway")
resp, err := conn.CreateCustomerGateway(createOpts)
Expand All @@ -113,6 +126,7 @@ func resourceAwsCustomerGatewayCreate(d *schema.ResourceData, meta interface{})
}

_, stateErr := stateConf.WaitForState()

if stateErr != nil {
return fmt.Errorf(
"Error waiting for customer gateway (%s) to become ready: %s", cgId, err)
Expand Down Expand Up @@ -150,24 +164,31 @@ func customerGatewayRefreshFunc(conn *ec2.EC2, gatewayId string) resource.StateR
}
}

func resourceAwsCustomerGatewayExists(vpnType, ipAddress, bgpAsn string, conn *ec2.EC2) (bool, error) {
ipAddressFilter := &ec2.Filter{
Name: aws.String("ip-address"),
Values: []*string{aws.String(ipAddress)},
}

typeFilter := &ec2.Filter{
Name: aws.String("type"),
Values: []*string{aws.String(vpnType)},
func resourceAwsCustomerGatewayExists(vpnType, ipAddress, bgpAsn, deviceName string, conn *ec2.EC2) (bool, error) {
filters := []*ec2.Filter{
{
Name: aws.String("ip-address"),
Values: []*string{aws.String(ipAddress)},
},
{
Name: aws.String("type"),
Values: []*string{aws.String(vpnType)},
},
{
Name: aws.String("bgp-asn"),
Values: []*string{aws.String(bgpAsn)},
},
}

bgpAsnFilter := &ec2.Filter{
Name: aws.String("bgp-asn"),
Values: []*string{aws.String(bgpAsn)},
if len(deviceName) != 0 {
filters = append(filters, &ec2.Filter{
Name: aws.String("device-name"),
Values: []*string{aws.String(deviceName)},
})
}

resp, err := conn.DescribeCustomerGateways(&ec2.DescribeCustomerGatewaysInput{
Filters: []*ec2.Filter{ipAddressFilter, typeFilter, bgpAsnFilter},
Filters: filters,
})
if err != nil {
return false, err
Expand Down Expand Up @@ -217,6 +238,7 @@ func resourceAwsCustomerGatewayRead(d *schema.ResourceData, meta interface{}) er
d.Set("bgp_asn", customerGateway.BgpAsn)
d.Set("ip_address", customerGateway.IpAddress)
d.Set("type", customerGateway.Type)
d.Set("device_name", customerGateway.DeviceName)

if err := d.Set("tags", keyvaluetags.Ec2KeyValueTags(customerGateway.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
Expand Down
41 changes: 41 additions & 0 deletions aws/resource_aws_customer_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,36 @@ func TestAccAWSCustomerGateway_similarAlreadyExists(t *testing.T) {
})
}

func TestAccAWSCustomerGateway_deviceName(t *testing.T) {
var gateway ec2.CustomerGateway
rBgpAsn := acctest.RandIntRange(64512, 65534)
resourceName := "aws_customer_gateway.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: resourceName,
Providers: testAccProviders,
CheckDestroy: testAccCheckCustomerGatewayDestroy,
Steps: []resource.TestStep{
{
Config: testAccCustomerGatewayConfigDeviceName(rBgpAsn),
Check: resource.ComposeTestCheckFunc(
testAccCheckCustomerGateway(resourceName, &gateway),
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "ec2", regexp.MustCompile(`customer-gateway/cgw-.+`)),
resource.TestCheckResourceAttr(resourceName, "bgp_asn", strconv.Itoa(rBgpAsn)),
resource.TestCheckResourceAttr(resourceName, "device_name", "test"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAWSCustomerGateway_disappears(t *testing.T) {
rBgpAsn := acctest.RandIntRange(64512, 65534)
var gateway ec2.CustomerGateway
Expand Down Expand Up @@ -300,6 +330,17 @@ resource "aws_customer_gateway" "identical" {
`, rBgpAsn)
}

func testAccCustomerGatewayConfigDeviceName(rBgpAsn int) string {
return fmt.Sprintf(`
resource "aws_customer_gateway" "test" {
bgp_asn = %[1]d
ip_address = "172.0.0.1"
type = "ipsec.1"
device_name = "test"
}
`, rBgpAsn)
}

// Change the ip_address.
func testAccCustomerGatewayConfigForceReplace(rBgpAsn int) string {
return fmt.Sprintf(`
Expand Down
1 change: 1 addition & 0 deletions website/docs/d/customer_gateway.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ In addition to the arguments above, the following attributes are exported:

* `arn` - The ARN of the customer gateway.
* `bgp_asn` - (Optional) The gateway's Border Gateway Protocol (BGP) Autonomous System Number (ASN).
* `device_name` - (Optional) A name for the customer gateway device.
* `ip_address` - (Optional) The IP address of the gateway's Internet-routable external interface.
* `tags` - Map of key-value pairs assigned to the gateway.
* `type` - (Optional) The type of customer gateway. The only type AWS supports at this time is "ipsec.1".
2 changes: 2 additions & 0 deletions website/docs/r/customer_gateway.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ resource "aws_customer_gateway" "main" {
The following arguments are supported:

* `bgp_asn` - (Required) The gateway's Border Gateway Protocol (BGP) Autonomous System Number (ASN).
* `device_name` - (Optional) A name for the customer gateway device.
* `ip_address` - (Required) The IP address of the gateway's Internet-routable external interface.
* `type` - (Required) The type of customer gateway. The only type AWS
supports at this time is "ipsec.1".
Expand All @@ -43,6 +44,7 @@ In addition to all arguments above, the following attributes are exported:
* `id` - The amazon-assigned ID of the gateway.
* `arn` - The ARN of the customer gateway.
* `bgp_asn` - The gateway's Border Gateway Protocol (BGP) Autonomous System Number (ASN).
* `device_name` - A name for the customer gateway device.
* `ip_address` - The IP address of the gateway's Internet-routable external interface.
* `type` - The type of customer gateway.
* `tags` - Tags applied to the gateway.
Expand Down