Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding EIP Data Source support for Tags #3505

Merged
merged 7 commits into from
Nov 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions aws/data_source_aws_eip.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func dataSourceAwsEip() *schema.Resource {
Optional: true,
Computed: true,
},
"tags": tagsSchemaComputed(),
},
}
}
Expand All @@ -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))}
Expand All @@ -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 {
Expand All @@ -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
}
41 changes: 41 additions & 0 deletions aws/data_source_aws_eip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
Expand Down Expand Up @@ -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"
Expand All @@ -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)
}
2 changes: 2 additions & 0 deletions website/docs/d/eip.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down