Skip to content

Commit

Permalink
Added data source 'aws_eips'.
Browse files Browse the repository at this point in the history
  • Loading branch information
David Chamard committed May 6, 2019
1 parent 9aebcd6 commit d41b93f
Show file tree
Hide file tree
Showing 4 changed files with 328 additions and 0 deletions.
87 changes: 87 additions & 0 deletions aws/data_source_aws_eips.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package aws

import (
"fmt"
"log"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceAwsEips() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsEipsRead,

Schema: map[string]*schema.Schema{

"ids": {
Type: schema.TypeList,
Computed: true,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},

"public_ips": {
Type: schema.TypeList,
Computed: true,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"filter": ec2CustomFiltersSchema(),
"tags": tagsSchemaComputed(),
},
}
}

func dataSourceAwsEipsRead(d *schema.ResourceData, meta interface{}) error {

conn := meta.(*AWSClient).ec2conn
d.SetId(time.Now().UTC().String())

var rawIds []string
var rawPublicIps []string

req := &ec2.DescribeAddressesInput{}

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 len(req.Filters) == 0 {
return fmt.Errorf("No Filters were found")
}

log.Printf("[DEBUG] Reading EIP: %s", req)
resp, err := conn.DescribeAddresses(req)
if err != nil {
return fmt.Errorf("error describing EC2 Address: %s", err)
}

if resp == nil || len(resp.Addresses) == 0 {
return fmt.Errorf("no matching Elastic IP found")
}

for _, eip := range resp.Addresses {
log.Printf("[DEBUG] Reading EIP TEST: %s", eip)
log.Printf("[DEBUG] Reading EIP TEST: %s", *eip.PublicIp)
if aws.StringValue(eip.Domain) == ec2.DomainTypeVpc {
rawIds = append(rawIds, 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)
rawIds = append(rawIds, aws.StringValue(eip.PublicIp))
}
rawPublicIps = append(rawPublicIps, aws.StringValue(eip.PublicIp))
}
d.Set("public_ips", rawPublicIps)
d.Set("ids", rawIds)

return nil
}
196 changes: 196 additions & 0 deletions aws/data_source_aws_eips_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
package aws

import (
"fmt"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"testing"
)

func TestAccDataSourceAwsEips_Filter(t *testing.T) {
var conf ec2.Address

dataSourceName := "data.aws_eips.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEIPDestroy,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsEipsConfigFilter(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEIPExists("aws_eip.test", &conf),
testAccCheckAWSEIPExists("aws_eip.test2", &conf),
resource.TestCheckResourceAttr(dataSourceName, "ids.#", "2"),
resource.TestCheckResourceAttr(dataSourceName, "public_ips.#", "2"),
),
},
},
})
}

func TestAccDataSourceAwsEipsClassic_Filter(t *testing.T) {
var conf ec2.Address

dataSourceName := "data.aws_eips.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEIPDestroy,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsEipsClassicConfigFilter(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEIPExists("aws_eip.test", &conf),
testAccCheckAWSEIPExists("aws_eip.test2", &conf),
resource.TestCheckResourceAttr(dataSourceName, "ids.#", "2"),
resource.TestCheckResourceAttr(dataSourceName, "public_ips.#", "2"),
),
},
},
})
}

func TestAccDataSourceAwsEips_Tags(t *testing.T) {
var conf ec2.Address

dataSourceName := "data.aws_eips.test"
resourceName := "aws_eip.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEIPDestroy,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsEipsConfigTags(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEIPExists("aws_eip.test", &conf),
resource.TestCheckResourceAttrPair(dataSourceName, "ids.0", resourceName, "id"),
resource.TestCheckResourceAttrPair(dataSourceName, "public_ips.0", resourceName, "public_ip"),
resource.TestCheckResourceAttr(dataSourceName, "ids.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "public_ips.#", "1"),
),
},
},
})
}

func TestAccDataSourceAwsEipsClassic_Tags(t *testing.T) {
var conf ec2.Address

dataSourceName := "data.aws_eips.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEIPDestroy,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsEipsClassicConfigTags(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEIPExists("aws_eip.test", &conf),
resource.TestCheckResourceAttrPair(dataSourceName, "ids.0", "aws_eips.test", "public_ip"),
resource.TestCheckResourceAttrPair(dataSourceName, "public_ips.0", "aws_eips.test", "public_ip"),
resource.TestCheckResourceAttr(dataSourceName, "ids.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "public_ips.#", "1"),
),
},
},
})
}

func testAccDataSourceAwsEipsConfigFilter(rName string) string {
return fmt.Sprintf(`
resource "aws_eip" "test" {
vpc = true
tags = {
Name = %q
}
}
resource "aws_eip" "test2" {
vpc = true
tags = {
Name = %q
}
}
data "aws_eips" "test" {
filter {
name = "tag:Name"
values = ["${aws_eip.test.tags.Name}"]
}
}
`, rName, rName)
}

func testAccDataSourceAwsEipsConfigTags(rName string) string {
return fmt.Sprintf(`
resource "aws_eip" "test" {
vpc = true
tags = {
Name = %q
}
}
data "aws_eips" "test" {
tags = {
Name = "${aws_eip.test.tags["Name"]}"
}
}
`, rName)
}

func testAccDataSourceAwsEipsClassicConfigFilter(rName string) string {
return fmt.Sprintf(`
resource "aws_eip" "test" {
vpc = false
tags = {
Name = %q
}
}
resource "aws_eip" "test2" {
vpc = true
tags = {
Name = %q
}
}
data "aws_eips" "test" {
filter {
name = "tag:Name"
values = ["${aws_eip.test.tags.Name}"]
}
}
`, rName, rName)
}

func testAccDataSourceAwsEipsClassicConfigTags(rName string) string {
return fmt.Sprintf(`
resource "aws_eip" "test" {
vpc = false
tags = {
Name = %q
}
}
data "aws_eips" "test" {
tags = {
Name = "${aws_eip.test.tags["Name"]}"
}
}
`, rName)
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ func Provider() terraform.ResourceProvider {
"aws_efs_file_system": dataSourceAwsEfsFileSystem(),
"aws_efs_mount_target": dataSourceAwsEfsMountTarget(),
"aws_eip": dataSourceAwsEip(),
"aws_eips": dataSourceAwsEips(),
"aws_eks_cluster": dataSourceAwsEksCluster(),
"aws_eks_cluster_auth": dataSourceAwsEksClusterAuth(),
"aws_elastic_beanstalk_application": dataSourceAwsElasticBeanstalkApplication(),
Expand Down
44 changes: 44 additions & 0 deletions website/docs/d/eips.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
layout: "aws"
page_title: "AWS: aws_eips"
sidebar_current: "docs-aws-datasource-eips"
description: |-
Use this data source to get ALLOCATION_IDs or PUBLIC_IPs of Amazon EIPs to be referenced elsewhere.
---

# Data Source: aws_eips

`aws_eips`Use this data source to get ALLOCATION_IDs or PUBLIC_IPS of Amazon EIPs to be referenced elsewhere.

## Example Usage
### Search By Filters (EC2-Classic or VPC)

```hcl
data "aws_eips" "by_filter" {
filter {
name = "tag:Name"
values = ["exampleNameTagValue"]
}
}
```
### Search By Tags (EC2-Classic or VPC)

```hcl
data "aws_eips" "by_tags" {
tags = {
Name = "exampleNameTagValue"
}
}
```

## Argument Reference

* `filter` - (Optional) One or more name/value pairs to use as filters. There are several valid keys, for a full reference, check out the [EC2 API Reference](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeAddresses.html).
* `tags` - (Optional) A mapping of tags, each pair of which must exactly match a pair on the desired Elastic IPs

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `ids` - If VPC Elastic IP, the allocations identifiers. If EC2-Classic Elastic IPs, the public IPs addresses.
* `public_ips` - Public IP addresess of Elastic IPs.

0 comments on commit d41b93f

Please sign in to comment.