Skip to content

Commit

Permalink
data-source/aws_eip: Refactoring after merging #3505, #3522, and #3525
Browse files Browse the repository at this point in the history
```
--- PASS: TestAccDataSourceAwsEip_PublicIP_EC2Classic (8.56s)
--- PASS: TestAccDataSourceAwsEip_PublicIP_VPC (11.76s)
--- PASS: TestAccDataSourceAwsEip_Id (11.79s)
--- PASS: TestAccDataSourceAwsEip_Tags (12.45s)
--- PASS: TestAccDataSourceAwsEip_Filter (12.58s)
```
  • Loading branch information
bflad committed Nov 13, 2018
1 parent 1556c1b commit 180fcee
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 173 deletions.
46 changes: 21 additions & 25 deletions aws/data_source_aws_eip.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func dataSourceAwsEip() *schema.Resource {
Read: dataSourceAwsEipRead,

Schema: map[string]*schema.Schema{
"filter": dataSourceFiltersSchema(),
"filter": ec2CustomFiltersSchema(),
"id": {
Type: schema.TypeString,
Optional: true,
Expand All @@ -33,39 +33,35 @@ 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{}
req.Filters = []*ec2.Filter{}

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

if publicIPOk {
req.PublicIps = []*string{aws.String(publicIP.(string))}
if v, ok := d.GetOk("public_ip"); ok {
req.PublicIps = []*string{aws.String(v.(string))}
}

if filtersOk {
req.Filters = buildAwsDataSourceFilters(filters.(*schema.Set))
}
req.Filters = []*ec2.Filter{}

req.Filters = append(req.Filters, buildEC2CustomFilterList(
d.Get("filter").(*schema.Set),
)...)

req.Filters = append(req.Filters, buildEC2TagFilterList(
tagsFromMap(d.Get("tags").(map[string]interface{})),
)...)

if tags, ok := d.GetOk("tags"); ok {
req.Filters = append(req.Filters, buildEC2TagFilterList(
tagsFromMap(tags.(map[string]interface{})),
)...)
if len(req.Filters) == 0 {
// Don't send an empty filters list; the EC2 API won't accept it.
req.Filters = nil
}

log.Printf("[DEBUG] Reading EIP: %s", req)
resp, err := conn.DescribeAddresses(req)
if err != nil {
return err
return fmt.Errorf("error describing EC2 Address: %s", err)
}
if resp == nil || len(resp.Addresses) == 0 {
return fmt.Errorf("no matching Elastic IP found")
Expand All @@ -76,11 +72,11 @@ func dataSourceAwsEipRead(d *schema.ResourceData, meta interface{}) error {

eip := resp.Addresses[0]

if *eip.Domain == "vpc" {
d.SetId(*eip.AllocationId)
if aws.StringValue(eip.Domain) == ec2.DomainTypeVpc {
d.SetId(aws.StringValue(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.SetId(aws.StringValue(eip.PublicIp))
}

d.Set("public_ip", eip.PublicIp)
Expand Down
203 changes: 99 additions & 104 deletions aws/data_source_aws_eip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,177 +6,172 @@ import (

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccDataSourceAwsEip_classic(t *testing.T) {
resource.Test(t, resource.TestCase{
func TestAccDataSourceAwsEip_Filter(t *testing.T) {
dataSourceName := "data.aws_eip.test"
resourceName := "aws_eip.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDataSourceAwsEipClassicConfig,
{
Config: testAccDataSourceAwsEipConfigFilter(rName),
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsEipCheck("data.aws_eip.test_classic", "aws_eip.test_classic"),
resource.TestCheckResourceAttrPair(dataSourceName, "id", resourceName, "id"),
resource.TestCheckResourceAttrPair(dataSourceName, "public_ip", resourceName, "public_ip"),
),
},
},
})
}

func TestAccDataSourceAwsEip_vpc(t *testing.T) {
resource.Test(t, resource.TestCase{
func TestAccDataSourceAwsEip_Id(t *testing.T) {
dataSourceName := "data.aws_eip.test"
resourceName := "aws_eip.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDataSourceAwsEipVPCConfig,
{
Config: testAccDataSourceAwsEipConfigId,
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"),
resource.TestCheckResourceAttrPair(dataSourceName, "id", resourceName, "id"),
resource.TestCheckResourceAttrPair(dataSourceName, "public_ip", resourceName, "public_ip"),
),
},
},
})
}

func TestAccDataSourceAwsEip_filter(t *testing.T) {
func TestAccDataSourceAwsEip_PublicIP_EC2Classic(t *testing.T) {
dataSourceName := "data.aws_eip.test"
resourceName := "aws_eip.test"

// Do not parallelize this test until the provider testing framework
// has a stable us-east-1 alias
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDataSourceAwsEipFilterConfig,
{
Config: testAccDataSourceAwsEipConfigPublicIpEc2Classic,
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsEipCheck("data.aws_eip.by_filter", "aws_eip.test"),
resource.TestCheckResourceAttrPair(dataSourceName, "id", resourceName, "id"),
resource.TestCheckResourceAttrPair(dataSourceName, "public_ip", resourceName, "public_ip"),
),
},
},
})
}

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

eipRs, ok := s.RootModule().Resources[resource_path]
if !ok {
return fmt.Errorf("can't find %s in state", resource_path)
}
func TestAccDataSourceAwsEip_PublicIP_VPC(t *testing.T) {
dataSourceName := "data.aws_eip.test"
resourceName := "aws_eip.test"

attr := rs.Primary.Attributes

if attr["id"] != eipRs.Primary.Attributes["id"] {
return fmt.Errorf(
"id is %s; want %s",
attr["id"],
eipRs.Primary.Attributes["id"],
)
}

if attr["public_ip"] != eipRs.Primary.Attributes["public_ip"] {
return fmt.Errorf(
"public_ip is %s; want %s",
attr["public_ip"],
eipRs.Primary.Attributes["public_ip"],
)
}

return nil
}
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsEipConfigPublicIpVpc,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSourceName, "id", resourceName, "id"),
resource.TestCheckResourceAttrPair(dataSourceName, "public_ip", resourceName, "public_ip"),
),
},
},
})
}

func TestAccDataSourceAwsEip_tags(t *testing.T) {
rInt := acctest.RandInt()
resource.Test(t, resource.TestCase{
func TestAccDataSourceAwsEip_Tags(t *testing.T) {
dataSourceName := "data.aws_eip.test"
resourceName := "aws_eip.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDataSourceAwsEip_tags_config(rInt),
{
Config: testAccDataSourceAwsEipConfigTags(rName),
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsEipCheck("data.aws_eip.by_tag", "aws_eip.test"),
resource.TestCheckResourceAttrPair(dataSourceName, "id", resourceName, "id"),
resource.TestCheckResourceAttrPair(dataSourceName, "public_ip", resourceName, "public_ip"),
),
},
},
})
}

const testAccDataSourceAwsEipClassicConfig = `
provider "aws" {
region = "us-west-2"
func testAccDataSourceAwsEipConfigFilter(rName string) string {
return fmt.Sprintf(`
resource "aws_eip" "test" {
vpc = true
tags {
Name = %q
}
}
resource "aws_eip" "test_classic" {}
data "aws_eip" "test" {
filter {
name = "tag:Name"
values = ["${aws_eip.test.tags.Name}"]
}
}
`, rName)
}

data "aws_eip" "test_classic" {
public_ip = "${aws_eip.test_classic.public_ip}"
const testAccDataSourceAwsEipConfigId = `
resource "aws_eip" "test" {
vpc = true
}
data "aws_eip" "test" {
id = "${aws_eip.test.id}"
}
`

const testAccDataSourceAwsEipVPCConfig = `
const testAccDataSourceAwsEipConfigPublicIpEc2Classic = `
provider "aws" {
region = "us-west-2"
region = "us-east-1"
}
resource "aws_eip" "test_vpc" {
vpc = true
}
resource "aws_eip" "test" {}
data "aws_eip" "test_vpc_by_id" {
id = "${aws_eip.test_vpc.id}"
data "aws_eip" "test" {
public_ip = "${aws_eip.test.public_ip}"
}
`

data "aws_eip" "test_vpc_by_public_ip" {
public_ip = "${aws_eip.test_vpc.public_ip}"
const testAccDataSourceAwsEipConfigPublicIpVpc = `
resource "aws_eip" "test" {
vpc = true
}
`
const testAccDataSourceAwsEipFilterConfig = `
provider "aws" {
region = "us-west-2"
data "aws_eip" "test" {
public_ip = "${aws_eip.test.public_ip}"
}
`

func testAccDataSourceAwsEipConfigTags(rName string) string {
return fmt.Sprintf(`
resource "aws_eip" "test" {
vpc = true
tags {
Name = "testeip"
}
}
vpc = true
data "aws_eip" "by_filter" {
filter {
name = "tag:Name"
values = ["${aws_eip.test.tags.Name}"]
tags {
Name = %q
}
}
`
func testAccDataSourceAwsEip_tags_config(rInt int) string {
return fmt.Sprintf(
`
provider "aws" {
region = "us-west-2"
data "aws_eip" "test" {
tags {
Name = "${aws_eip.test.tags["Name"]}"
}
}
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)
`, rName)
}
Loading

0 comments on commit 180fcee

Please sign in to comment.