Skip to content

Commit

Permalink
support for BYOIP: allow to define public_ipv4_pool name when EIP get…
Browse files Browse the repository at this point in the history
…s allocated
  • Loading branch information
mhorbul committed Nov 19, 2018
1 parent b6416b3 commit e68471e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
12 changes: 12 additions & 0 deletions aws/resource_aws_eip.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ func resourceAwsEip() *schema.Resource {
Optional: true,
},

"public_ipv4_pool": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},

"tags": tagsSchema(),
},
}
Expand All @@ -98,6 +105,10 @@ func resourceAwsEipCreate(d *schema.ResourceData, meta interface{}) error {
Domain: aws.String(domainOpt),
}

if v := d.Get("public_ipv4_pool"); v != nil && v.(string) != "" {
allocOpts.PublicIpv4Pool = aws.String(d.Get("public_ipv4_pool").(string))
}

log.Printf("[DEBUG] EIP create configuration: %#v", allocOpts)
allocResp, err := ec2conn.AllocateAddress(allocOpts)
if err != nil {
Expand Down Expand Up @@ -211,6 +222,7 @@ func resourceAwsEipRead(d *schema.ResourceData, meta interface{}) error {
}
d.Set("private_ip", address.PrivateIpAddress)
d.Set("public_ip", address.PublicIp)
d.Set("public_ipv4_pool", address.PublicIpv4Pool)

// On import (domain never set, which it must've been if we created),
// set the 'vpc' attribute depending on if we're in a VPC.
Expand Down
42 changes: 40 additions & 2 deletions aws/resource_aws_eip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,35 @@ func TestAccAWSEIP_tags(t *testing.T) {
})
}

func TestAccAWSEIP_PublicIpv4Pool(t *testing.T) {
var conf ec2.Address
resourceName := "aws_eip.bar"
envPoolName := fmt.Sprintf("%s", os.Getenv("AWS_EC2_EIP_PUBLIC_IPV4_POOL"))
poolName := ""
if envPoolName == "" {
poolName = "amazon"
} else {
poolName = envPoolName
}

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: "aws_eip.bar",
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEIPDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEIPConfig_PublicIpv4Pool(poolName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEIPExists(resourceName, &conf),
testAccCheckAWSEIPAttributes(&conf),
resource.TestCheckResourceAttr(resourceName, "public_ipv4_pool", poolName),
),
},
},
})
}

func testAccCheckAWSEIPDisappears(v *ec2.Address) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ec2conn
Expand Down Expand Up @@ -454,6 +483,15 @@ resource "aws_eip" "bar" {
`, rName, testName)
}

func testAccAWSEIPConfig_PublicIpv4Pool(poolName string) string {
return fmt.Sprintf(`
resource "aws_eip" "bar" {
vpc = true
public_ipv4_pool = "%s"
}
`, poolName)
}

const testAccAWSEIPInstanceEc2Classic = `
provider "aws" {
region = "us-east-1"
Expand Down Expand Up @@ -716,7 +754,7 @@ resource "aws_subnet" "bar" {
availability_zone = "us-west-2a"
cidr_block = "10.0.0.0/24"
tags {
Name = "tf-acc-eip-network-interface"
Name = "tf-acc-eip-network-interface"
}
}
Expand Down Expand Up @@ -750,7 +788,7 @@ resource "aws_subnet" "bar" {
availability_zone = "us-west-2a"
cidr_block = "10.0.0.0/24"
tags {
Name = "tf-acc-eip-multi-network-interface"
Name = "tf-acc-eip-multi-network-interface"
}
}
Expand Down
11 changes: 11 additions & 0 deletions website/docs/r/eip.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ resource "aws_eip" "bar" {
}
```

Allocating EIP from the BYOIP pool:

```hcl
resource "aws_eip" "byoip-ip" {
vpc = true
public_ipv4_pool = "ipv4pool-ec2-012345"
}
```

## Argument Reference

The following arguments are supported:
Expand All @@ -95,6 +104,7 @@ The following arguments are supported:
associate with the Elastic IP address. If no private IP address is specified,
the Elastic IP address is associated with the primary private IP address.
* `tags` - (Optional) A mapping of tags to assign to the resource.
* `public_ipv4_pool` - (Optional) IPv4 address pool

~> **NOTE:** You can specify either the `instance` ID or the `network_interface` ID,
but not both. Including both will **not** return an error from the AWS API, but will
Expand All @@ -112,6 +122,7 @@ In addition to all arguments above, the following attributes are exported:
* `public_ip` - Contains the public IP address.
* `instance` - Contains the ID of the attached instance.
* `network_interface` - Contains the ID of the attached network interface.
* `public_ipv4_pool` - IPv4 address pool.

## Timeouts
`aws_eip` provides the following [Timeouts](/docs/configuration/resources.html#timeouts) configuration options:
Expand Down

0 comments on commit e68471e

Please sign in to comment.