-
Notifications
You must be signed in to change notification settings - Fork 9.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
David Chamard
committed
May 6, 2019
1 parent
9aebcd6
commit d41b93f
Showing
4 changed files
with
328 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |