diff --git a/aws/data_source_aws_eip.go b/aws/data_source_aws_eip.go index f461a374e8b..70ab4f22fe7 100644 --- a/aws/data_source_aws_eip.go +++ b/aws/data_source_aws_eip.go @@ -24,6 +24,7 @@ func dataSourceAwsEip() *schema.Resource { Optional: true, Computed: true, }, + "tags": tagsSchemaComputed(), }, } } @@ -32,6 +33,7 @@ func dataSourceAwsEipRead(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).ec2conn req := &ec2.DescribeAddressesInput{} + req.Filters = []*ec2.Filter{} if id, ok := d.GetOk("id"); ok { req.AllocationIds = []*string{aws.String(id.(string))} @@ -41,6 +43,12 @@ func dataSourceAwsEipRead(d *schema.ResourceData, meta interface{}) error { req.PublicIps = []*string{aws.String(public_ip.(string))} } + if tags, ok := d.GetOk("tags"); ok { + req.Filters = append(req.Filters, buildEC2TagFilterList( + tagsFromMap(tags.(map[string]interface{})), + )...) + } + log.Printf("[DEBUG] Reading EIP: %s", req) resp, err := conn.DescribeAddresses(req) if err != nil { @@ -57,6 +65,7 @@ func dataSourceAwsEipRead(d *schema.ResourceData, meta interface{}) error { d.SetId(*eip.AllocationId) d.Set("public_ip", eip.PublicIp) + d.Set("tags", tagsToMap(eip.Tags)) return nil } diff --git a/aws/data_source_aws_eip_test.go b/aws/data_source_aws_eip_test.go index d5d0c6ac708..718effd36ed 100644 --- a/aws/data_source_aws_eip_test.go +++ b/aws/data_source_aws_eip_test.go @@ -4,6 +4,7 @@ import ( "fmt" "testing" + "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" ) @@ -58,6 +59,22 @@ func testAccDataSourceAwsEipCheck(name string) resource.TestCheckFunc { } } +func TestAccDataSourceAwsEip_tags(t *testing.T) { + rInt := acctest.RandInt() + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccDataSourceAwsEip_tags_config(rInt), + Check: resource.ComposeTestCheckFunc( + testAccDataSourceAwsEipCheck("data.aws_eip.by_tag"), + ), + }, + }, + }) +} + const testAccDataSourceAwsEipConfig = ` provider "aws" { region = "us-west-2" @@ -75,3 +92,27 @@ data "aws_eip" "by_public_ip" { public_ip = "${aws_eip.test.public_ip}" } ` + +func testAccDataSourceAwsEip_tags_config(rInt int) string { + return fmt.Sprintf( + ` +provider "aws" { + region = "us-west-2" +} + +resource "aws_eip" "test" { + tags { + test_tag = "hello tag" + random_tag = "%d" + } +} + +data "aws_eip" "by_tag" { + tags { + test_tag = "${aws_eip.test.tags["test_tag"]}" + random_tag = "%d" + } + public_ip = "${aws_eip.test.public_ip}" +} +`, rInt, rInt) +} diff --git a/website/docs/d/eip.html.markdown b/website/docs/d/eip.html.markdown index 9a6535eedb4..a4d12591368 100644 --- a/website/docs/d/eip.html.markdown +++ b/website/docs/d/eip.html.markdown @@ -42,6 +42,8 @@ Elastic IP whose data will be exported as attributes. * `public_ip` - (Optional) The public IP of the specific EIP to retrieve. +* `tags` - (Optional) A mapping of tags, each pair of which must exactly match a pair on the desired Elastic IP + ## Attributes Reference All of the argument attributes are also exported as result attributes. This