diff --git a/aws/data_source_aws_eip.go b/aws/data_source_aws_eip.go index d08a6cb7de6b..c2df4602be22 100644 --- a/aws/data_source_aws_eip.go +++ b/aws/data_source_aws_eip.go @@ -14,6 +14,7 @@ func dataSourceAwsEip() *schema.Resource { Read: dataSourceAwsEipRead, Schema: map[string]*schema.Schema{ + "filter": dataSourceFiltersSchema(), "id": { Type: schema.TypeString, Optional: true, @@ -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) diff --git a/aws/data_source_aws_eip_test.go b/aws/data_source_aws_eip_test.go index 45cf5f258323..ceae6c83317a 100644 --- a/aws/data_source_aws_eip_test.go +++ b/aws/data_source_aws_eip_test.go @@ -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] @@ -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}"] + } +} +`