Skip to content

Commit

Permalink
fixes #3423 adds filter to data source aws_eip
Browse files Browse the repository at this point in the history
this addes the ability to search for EIP’s via tags or other attribute that EIP’s can use in a filter.

This did highlight a known issue with regrads to how terraform eval’s data sources before they should if be the resourse is a computed value. This is only an issue if the data block is referencing a resource that is created at the same time. If referencing a pre exiting resource this does not happen.

the test config testAccDataSourceAwsEipFilterConfig makes a interpol ref to force terraform to eval the data block after the resource has either been created or read in `values = ["${aws_eip.test.tags.Name}”]` see the following links

hashicorp/terraform#10603
hashicorp/terraform#17173
  • Loading branch information
richardbowden committed Feb 25, 2018
1 parent aa8b83f commit ec0a43c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
19 changes: 16 additions & 3 deletions aws/data_source_aws_eip.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func dataSourceAwsEip() *schema.Resource {
Read: dataSourceAwsEipRead,

Schema: map[string]*schema.Schema{
"filter": dataSourceFiltersSchema(),
"id": {
Type: schema.TypeString,
Optional: true,
Expand All @@ -31,14 +32,26 @@ func dataSourceAwsEip() *schema.Resource {
func dataSourceAwsEipRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

filters, filtersOk := d.GetOk("filter")
id, idOk := d.GetOk("id")
publicIP, publicIPOk := d.GetOk("public_ip")

if (idOk || publicIPOk) && filtersOk {
return fmt.Errorf("filter cannot be used when id or public_ip is set")
}

req := &ec2.DescribeAddressesInput{}

if id, ok := d.GetOk("id"); ok {
if idOk {
req.AllocationIds = []*string{aws.String(id.(string))}
}

if public_ip := d.Get("public_ip"); public_ip != "" {
req.PublicIps = []*string{aws.String(public_ip.(string))}
if publicIPOk {
req.PublicIps = []*string{aws.String(publicIP.(string))}
}

if filtersOk {
req.Filters = buildAwsDataSourceFilters(filters.(*schema.Set))
}

log.Printf("[DEBUG] Reading EIP: %s", req)
Expand Down
36 changes: 36 additions & 0 deletions aws/data_source_aws_eip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ func TestAccDataSourceAwsEip_vpc(t *testing.T) {
})
}

func TestAccDataSourceAwsEip_filter(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDataSourceAwsEipFilterConfig,
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsEipCheck("data.aws_eip.by_filter", "aws_eip.test"),
),
},
},
})
}

func testAccDataSourceAwsEipCheck(data_path string, resource_path string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[data_path]
Expand Down Expand Up @@ -103,3 +118,24 @@ data "aws_eip" "test_vpc_by_public_ip" {
public_ip = "${aws_eip.test_vpc.public_ip}"
}
`

const testAccDataSourceAwsEipFilterConfig = `
provider "aws" {
region = "us-west-2"
}
resource "aws_eip" "test" {
vpc = true
tags {
Name = "testeip"
}
}
data "aws_eip" "by_filter" {
filter {
name = "tag:Name"
values = ["${aws_eip.test.tags.Name}"]
}
}
`

0 comments on commit ec0a43c

Please sign in to comment.