Skip to content

Commit

Permalink
fixes #2825 supports classic EIP’s in data.aws_eip
Browse files Browse the repository at this point in the history
  • Loading branch information
www committed Feb 25, 2018
1 parent 189fb80 commit aa8b83f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 20 deletions.
8 changes: 7 additions & 1 deletion aws/data_source_aws_eip.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ func dataSourceAwsEipRead(d *schema.ResourceData, meta interface{}) error {

eip := resp.Addresses[0]

d.SetId(*eip.AllocationId)
if *eip.Domain == "vpc" {
d.SetId(*eip.AllocationId)
} else {
log.Printf("[DEBUG] Reading EIP, has no AllocationId, this means we have a Classic EIP, the id will also be the public ip : %s", req)
d.SetId(*eip.PublicIp)
}

d.Set("public_ip", eip.PublicIp)

return nil
Expand Down
62 changes: 45 additions & 17 deletions aws/data_source_aws_eip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,47 @@ import (
"github.com/hashicorp/terraform/terraform"
)

func TestAccDataSourceAwsEip_basic(t *testing.T) {
func TestAccDataSourceAwsEip_classic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDataSourceAwsEipConfig,
Config: testAccDataSourceAwsEipClassicConfig,
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsEipCheck("data.aws_eip.by_id"),
testAccDataSourceAwsEipCheck("data.aws_eip.by_public_ip"),
testAccDataSourceAwsEipCheck("data.aws_eip.test_classic", "aws_eip.test_classic"),
),
},
},
})
}

func testAccDataSourceAwsEipCheck(name string) resource.TestCheckFunc {
func TestAccDataSourceAwsEip_vpc(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDataSourceAwsEipVPCConfig,
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsEipCheck("data.aws_eip.test_vpc_by_id", "aws_eip.test_vpc"),
testAccDataSourceAwsEipCheck("data.aws_eip.test_vpc_by_public_ip", "aws_eip.test_vpc"),
),
},
},
})
}

func testAccDataSourceAwsEipCheck(data_path string, resource_path string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
rs, ok := s.RootModule().Resources[data_path]
if !ok {
return fmt.Errorf("root module has no resource called %s", name)
return fmt.Errorf("root module has no resource called %s", data_path)
}

eipRs, ok := s.RootModule().Resources["aws_eip.test"]
eipRs, ok := s.RootModule().Resources[resource_path]
if !ok {
return fmt.Errorf("can't find aws_eip.test in state")
return fmt.Errorf("can't find %s in state", resource_path)
}

attr := rs.Primary.Attributes
Expand All @@ -58,20 +73,33 @@ func testAccDataSourceAwsEipCheck(name string) resource.TestCheckFunc {
}
}

const testAccDataSourceAwsEipConfig = `
const testAccDataSourceAwsEipClassicConfig = `
provider "aws" {
region = "us-west-2"
}
resource "aws_eip" "wrong1" {}
resource "aws_eip" "test" {}
resource "aws_eip" "wrong2" {}
resource "aws_eip" "test_classic" {}
data "aws_eip" "test_classic" {
public_ip = "${aws_eip.test_classic.public_ip}"
}
`

const testAccDataSourceAwsEipVPCConfig = `
provider "aws" {
region = "us-west-2"
}
resource "aws_eip" "test_vpc" {
vpc = true
}
data "aws_eip" "by_id" {
id = "${aws_eip.test.id}"
data "aws_eip" "test_vpc_by_id" {
id = "${aws_eip.test_vpc.id}"
}
data "aws_eip" "by_public_ip" {
public_ip = "${aws_eip.test.public_ip}"
data "aws_eip" "test_vpc_by_public_ip" {
public_ip = "${aws_eip.test_vpc.public_ip}"
}
`
6 changes: 4 additions & 2 deletions website/docs/d/eip.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public IP as an input variable and needs to determine the other.
## Example Usage

The following example shows how one might accept a public IP as a variable
and use this data source to obtain the allocation ID.
and use this data source to obtain the allocation ID when using an VPC EIP.

```hcl
variable "instance_id" {}
Expand All @@ -32,13 +32,15 @@ resource "aws_eip_association" "proxy_eip" {
}
```

Classic EIP's do not have an allocation_id, only use `public_ip` in the `data "aws_eip"` block.

## Argument Reference

The arguments of this data source act as filters for querying the available
Elastic IPs in the current region. The given filters must match exactly one
Elastic IP whose data will be exported as attributes.

* `id` - (Optional) The allocation id of the specific EIP to retrieve.
* `id` - (Optional) The allocation id of the specific VPC EIP to retrieve. If a classic EIP is required, do NOT set `id`, only set `public_ip`

* `public_ip` - (Optional) The public IP of the specific EIP to retrieve.

Expand Down

0 comments on commit aa8b83f

Please sign in to comment.