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

Added data source 'aws_eips'. #7537

Merged
merged 109 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
109 commits
Select commit Hold shift + click to select a range
d41b93f
Added data source 'aws_eips'.
Feb 13, 2019
49ad0a5
Added link to main page for eips
Feb 13, 2019
e52ee8d
Fixed link based on new format
May 6, 2019
dde952e
Support tags filter only
May 6, 2019
1df2832
Added back filter
May 6, 2019
68ca312
Doc fixes
May 9, 2019
4ff915e
aws_vpn_connection resource's customer_gateway_configuration should b…
b-dean Oct 22, 2020
ac6fec9
Add CHANGELOG entry.
ewbankkit Jan 24, 2022
4f2950c
Fix data source aws_vpc_peering_connections
Feb 1, 2021
85e8830
Prevent potential panics
borancar Feb 11, 2021
82867a1
Re-add fmt
borancar Feb 11, 2021
929adb3
r/route and r/route_table: deprecate 'instance_id'
anGie44 Jan 19, 2022
d1116cc
Update CHANGELOG for #22664
anGie44 Jan 19, 2022
4cb8d32
Update CHANGELOG.md
anGie44 Jan 19, 2022
06d4dd1
add acceptance test coverage
anGie44 Jan 20, 2022
014c543
Add resource aws_s3_bucket_versioning
devonbleak Jul 9, 2018
cd485f7
CR updates: align with API and update overall provider design patterns
anGie44 Jan 21, 2022
fa892a0
Update CHANGELOG for #5132
anGie44 Jan 21, 2022
4b4ba07
first draft
dirk39 Dec 14, 2021
89ef24b
ok
dirk39 Dec 14, 2021
a7e4283
test ok. manage destroy
dirk39 Dec 15, 2021
efeeafa
test ok. destroy ok
dirk39 Dec 15, 2021
4ead8c4
test ok. destroy ok
dirk39 Dec 15, 2021
7081296
updated doc
dirk39 Dec 16, 2021
9efb223
Revert "updated doc"
ewbankkit Jan 18, 2022
cf3aa75
Revert "test ok. destroy ok"
ewbankkit Jan 18, 2022
6aad5c8
Revert "test ok. destroy ok"
ewbankkit Jan 18, 2022
dfa49c7
Revert "test ok. manage destroy"
ewbankkit Jan 18, 2022
41d6293
Revert "ok"
ewbankkit Jan 18, 2022
23b4ad2
Revert "first draft"
ewbankkit Jan 18, 2022
ec7a894
r/aws_default_subnet: Full resource life cycle. First baby steps.
ewbankkit Jan 11, 2022
8a47012
r/aws_default_subnet: Full resource life cycle. Playing with some ide…
ewbankkit Jan 11, 2022
1a7e808
r/aws_default_subnet: 'map_public_ip_on_launch' has a Default of true.
ewbankkit Jan 12, 2022
bfdc5f5
Additional 'modifySubnetAttriute' functions.
ewbankkit Jan 12, 2022
a430617
r/aws_default_subnet: Complete 'modifySubnetAttributesOnCreate'.
ewbankkit Jan 12, 2022
e8c1ebb
r/aws_default_subnet: Set tags on resource Create.
ewbankkit Jan 13, 2022
032d221
r/aws_default_subnet: Corrections after some testing.
ewbankkit Jan 13, 2022
f5b3c9f
'setting' -> 'modifying' in error messages.
ewbankkit Jan 14, 2022
a488c43
r/aws_default_vpc: Don't reuse ResourceVPC's schema.
ewbankkit Jan 19, 2022
6392c5e
Tidy up subnet and VPC sweepers.
ewbankkit Jan 19, 2022
8ad66f3
r/aws_default_vpc: IPv6 changes.
ewbankkit Jan 19, 2022
4a753d1
r/aws_default_vpc: Test IPv6 changes.
ewbankkit Jan 19, 2022
f6b8080
r/aws_default_vpc and r/aws_default_subnet: Serialize tests.
ewbankkit Jan 19, 2022
8607515
Store any new IPv6 CIDR block association ID to state.
ewbankkit Jan 19, 2022
e25e316
Add 'testAccPreCheckDefaultVPCAvailable'.
ewbankkit Jan 20, 2022
4f71d54
Add 'testAccPreCheckDefaultSubnetAvailable'.
ewbankkit Jan 20, 2022
148faa8
r/aws_default_vpc and r/aws_default_subnet: Check existence in TestCa…
ewbankkit Jan 20, 2022
7c2672b
Add CHANGELOG entry.
ewbankkit Jan 20, 2022
76a4ef2
d/aws_vpcs: Return empty list when no VPCs match.
ewbankkit Jan 20, 2022
8364e13
EC2: 'testCheckResourceAttrGreaterThanValue' -> 'acctest.CheckResourc…
ewbankkit Jan 20, 2022
560608c
EKS: 'testCheckResourceAttrGreaterThanValue' -> 'acctest.CheckResourc…
ewbankkit Jan 20, 2022
40c026b
r/aws_default_subnet: Add 'testAccEC2DefaultSubnet_privateDnsNameOpti…
ewbankkit Jan 20, 2022
1c98f56
r/aws_default_vpc: Add `force_destroy` and create new tests.
ewbankkit Jan 20, 2022
681707e
r/aws_default_subnet: Additional tests.
ewbankkit Jan 20, 2022
14272a4
r/aws_default_subnet: Ensure default subnets are recreated.
ewbankkit Jan 21, 2022
02574a0
r/aws_default_subnet & r/aws_default_vpc: Don't run acceptance tests …
ewbankkit Jan 21, 2022
dfd36ba
Documentation updates.
ewbankkit Jan 21, 2022
e7f6a69
Merge pull request #15806 from b-dean/b-aws_vpn_connection.customer_g…
ewbankkit Jan 24, 2022
aed088f
Add new data source
kamilturek Jan 21, 2022
d7fb925
Add tests
kamilturek Jan 21, 2022
df5afa4
Add changelog
kamilturek Jan 21, 2022
71c9415
Fix terrafmt errors
kamilturek Jan 21, 2022
551e3df
Add documentation page
kamilturek Jan 21, 2022
0651098
Add a blank line between imports
kamilturek Jan 21, 2022
675f977
Missing entry after rebase.
ewbankkit Jan 24, 2022
c96aa26
dataexchange dataset
DrFaust92 Jan 20, 2022
c35ccde
changelog
DrFaust92 Jan 20, 2022
ce948b7
docs cat
DrFaust92 Jan 20, 2022
1484d06
docs fix
DrFaust92 Jan 21, 2022
d2b402d
sweep
DrFaust92 Jan 22, 2022
9b52c82
Add 'acctest.PreCheckPartitionHasService' for GovCloud.
ewbankkit Jan 24, 2022
0df1da0
Add new data source
kamilturek Jan 22, 2022
a869564
Add data source to the provider
kamilturek Jan 22, 2022
10b40bd
Add an acceptance test
kamilturek Jan 22, 2022
6cca5f5
Add documentation page
kamilturek Jan 22, 2022
96c6427
Add changelog
kamilturek Jan 22, 2022
2cf08bb
Fix terrafmt errors
kamilturek Jan 22, 2022
1e31213
Remove redundant whitespaces
kamilturek Jan 22, 2022
cf5e89b
Remove hardcoded region
kamilturek Jan 22, 2022
231dc9c
Add missing prefix
kamilturek Jan 22, 2022
290520e
Correct name
kamilturek Jan 23, 2022
d4f6dfb
apigw export
DrFaust92 Jan 22, 2022
ed5dc48
fmt
DrFaust92 Jan 22, 2022
2cd250d
fmt
DrFaust92 Jan 22, 2022
984f26a
changelog
DrFaust92 Jan 22, 2022
fcee144
apigw datasource
DrFaust92 Jan 22, 2022
4181701
changelog
DrFaust92 Jan 22, 2022
594d29f
sdk id
DrFaust92 Jan 22, 2022
06965b6
fmt
DrFaust92 Jan 22, 2022
68bd4c4
fmt
DrFaust92 Jan 22, 2022
b2e22b7
fmt
DrFaust92 Jan 22, 2022
14eacd5
Update api_gateway_sdk.html.markdown
ewbankkit Jan 24, 2022
3f71bab
Update api_gateway_sdk.html.markdown
ewbankkit Jan 24, 2022
35c9327
apigw v2 export api
DrFaust92 Jan 22, 2022
97f9edd
changelog
DrFaust92 Jan 22, 2022
ea81dff
Update 22732.txt
ewbankkit Jan 24, 2022
deda445
Update apigatewayv2_export.html.markdown
ewbankkit Jan 24, 2022
a461715
d/aws_apigatewayv2_export: Fix 'Error: error exporting Gateway v2 API…
ewbankkit Jan 24, 2022
173f784
Revert "Doc fixes"
ewbankkit Jan 24, 2022
51fef24
Revert "Added back filter"
ewbankkit Jan 24, 2022
28ef323
Revert "Support tags filter only"
ewbankkit Jan 24, 2022
ecb483c
Revert "Fixed link based on new format"
ewbankkit Jan 24, 2022
0f3c29c
Revert "Added link to main page for eips"
ewbankkit Jan 24, 2022
5106224
Revert "Added data source 'aws_eips'."
ewbankkit Jan 24, 2022
cc6b477
Merge branch 'release/4.x' into HEAD
ewbankkit Jan 24, 2022
f2ff73a
Add 'FindEIPs'.
ewbankkit Jan 25, 2022
2d26fb9
Add 'aws_eips' data source.
ewbankkit Jan 25, 2022
45c805b
Add CHANGELOG entry.
ewbankkit Jan 25, 2022
6632ffa
Merge branch 'release/4.x' into HEAD
ewbankkit Jan 25, 2022
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
3 changes: 3 additions & 0 deletions .changelog/7537.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_eips
```
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ func Provider() *schema.Provider {
"aws_ec2_transit_gateway_vpc_attachment": ec2.DataSourceTransitGatewayVPCAttachment(),
"aws_ec2_transit_gateway_vpn_attachment": ec2.DataSourceTransitGatewayVPNAttachment(),
"aws_eip": ec2.DataSourceEIP(),
"aws_eips": ec2.DataSourceEIPs(),
"aws_instance": ec2.DataSourceInstance(),
"aws_instances": ec2.DataSourceInstances(),
"aws_internet_gateway": ec2.DataSourceInternetGateway(),
Expand Down
76 changes: 76 additions & 0 deletions internal/service/ec2/eips_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package ec2

import (
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
)

func DataSourceEIPs() *schema.Resource {
return &schema.Resource{
Read: dataSourceEIPsRead,

Schema: map[string]*schema.Schema{
"allocation_ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"filter": DataSourceFiltersSchema(),
"public_ips": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"tags": tftags.TagsSchemaComputed(),
},
}
}

func dataSourceEIPsRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).EC2Conn

input := &ec2.DescribeAddressesInput{}

if tags, tagsOk := d.GetOk("tags"); tagsOk {
input.Filters = append(input.Filters, BuildTagFilterList(
Tags(tftags.New(tags.(map[string]interface{}))),
)...)
}

if filters, filtersOk := d.GetOk("filter"); filtersOk {
input.Filters = append(input.Filters,
BuildFiltersDataSource(filters.(*schema.Set))...)
}

if len(input.Filters) == 0 {
input.Filters = nil
}

output, err := FindEIPs(conn, input)

if err != nil {
return fmt.Errorf("error reading EC2 EIPs: %w", err)
}

var allocationIDs []string
var publicIPs []string

for _, v := range output {
if aws.StringValue(v.Domain) == ec2.DomainTypeVpc {
allocationIDs = append(allocationIDs, aws.StringValue(v.AllocationId))
} else {
publicIPs = append(publicIPs, aws.StringValue(v.PublicIp))
}
}

d.SetId(meta.(*conns.AWSClient).Region)
d.Set("allocation_ids", allocationIDs)
d.Set("public_ips", publicIPs)

return nil
}
100 changes: 100 additions & 0 deletions internal/service/ec2/eips_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package ec2_test

import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/service/ec2"
sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
)

func TestAccEC2EIPsDataSource_vpcDomain(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID),
Providers: acctest.Providers,
Steps: []resource.TestStep{
{
Config: testAccEIPsVPCDomainDataSourceConfig(rName),
Check: resource.ComposeTestCheckFunc(
acctest.CheckResourceAttrGreaterThanValue("data.aws_eips.all", "allocation_ids.#", "1"),
resource.TestCheckResourceAttr("data.aws_eips.by_tags", "allocation_ids.#", "1"),
resource.TestCheckResourceAttr("data.aws_eips.by_tags", "public_ips.#", "0"),
resource.TestCheckResourceAttr("data.aws_eips.none", "allocation_ids.#", "0"),
resource.TestCheckResourceAttr("data.aws_eips.none", "public_ips.#", "0"),
),
},
},
})
}

func TestAccEC2EIPsDataSource_standardDomain(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t); acctest.PreCheckEC2Classic(t) },
ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID),
ProviderFactories: acctest.ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccEIPsStandardDomainDataSourceConfig(),
Check: resource.ComposeTestCheckFunc(
acctest.CheckResourceAttrGreaterThanValue("data.aws_eips.all", "public_ips.#", "0"),
),
},
},
})
}

func testAccEIPsVPCDomainDataSourceConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_eip" "test1" {
vpc = true

tags = {
Name = "%[1]s-1"
}
}

resource "aws_eip" "test2" {
vpc = true

tags = {
Name = "%[1]s-2"
}
}

data "aws_eips" "all" {
depends_on = [aws_eip.test1, aws_eip.test2]
}

data "aws_eips" "by_tags" {
tags = {
Name = "%[1]s-1"
}

depends_on = [aws_eip.test1, aws_eip.test2]
}

data "aws_eips" "none" {
filter {
name = "tag-key"
values = ["%[1]s-3"]
}

depends_on = [aws_eip.test1, aws_eip.test2]
}
`, rName)
}

func testAccEIPsStandardDomainDataSourceConfig() string {
return acctest.ConfigCompose(acctest.ConfigEC2ClassicRegionProvider(), `
resource "aws_eip" "test" {}

data "aws_eips" "all" {
depends_on = [aws_eip.test]
}
`)
}
2 changes: 2 additions & 0 deletions internal/service/ec2/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const (
ErrCodeDependencyViolation = "DependencyViolation"
ErrCodeGatewayNotAttached = "Gateway.NotAttached"
ErrCodeIncorrectState = "IncorrectState"
ErrCodeInvalidAddressNotFound = "InvalidAddress.NotFound"
ErrCodeInvalidAllocationIDNotFound = "InvalidAllocationID.NotFound"
ErrCodeInvalidAssociationIDNotFound = "InvalidAssociationID.NotFound"
ErrCodeInvalidAttachmentIDNotFound = "InvalidAttachmentID.NotFound"
ErrCodeInvalidCarrierGatewayIDNotFound = "InvalidCarrierGatewayID.NotFound"
Expand Down
25 changes: 25 additions & 0 deletions internal/service/ec2/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,31 @@ func FindClientVPNRouteByID(conn *ec2.EC2, routeID string) (*ec2.DescribeClientV
return FindClientVPNRoute(conn, endpointID, targetSubnetID, destinationCidr)
}

func FindEIPs(conn *ec2.EC2, input *ec2.DescribeAddressesInput) ([]*ec2.Address, error) {
var addresses []*ec2.Address

output, err := conn.DescribeAddresses(input)

if tfawserr.ErrCodeEquals(err, ErrCodeInvalidAddressNotFound, ErrCodeInvalidAllocationIDNotFound) {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

for _, v := range output.Addresses {
if v != nil {
addresses = append(addresses, v)
}
}

return addresses, nil
}

func FindHostByID(conn *ec2.EC2, id string) (*ec2.Host, error) {
input := &ec2.DescribeHostsInput{
HostIds: aws.StringSlice([]string{id}),
Expand Down
50 changes: 50 additions & 0 deletions website/docs/d/eips.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
subcategory: "VPC"
layout: "aws"
page_title: "AWS: aws_eips"
description: |-
Provides a list of Elastic IPs in a region
---

# Data Source: aws_eips

Provides a list of Elastic IPs in a region.

## Example Usage

The following shows outputing all Elastic IPs with the a specific tag value.

```terraform
data "aws_eips" "example" {
tags = {
Env = "dev"
}
}

# VPC EIPs.
output "allocation_ids" {
value = data.aws_eips.example.allocation_ids
}

# EC2-Classic EIPs.
output "public_ips" {
value = data.aws_eips.example.public_ips
}
```

## Argument Reference

* `filter` - (Optional) Custom filter block as described below.
* `tags` - (Optional) A map of tags, each pair of which must exactly match a pair on the desired Elastic IPs.

More complex filters can be expressed using one or more `filter` sub-blocks, which take the following arguments:

* `name` - (Required) The name of the field to filter by, as defined by
[the underlying AWS API](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeAddresses.html).
* `values` - (Required) Set of values that are accepted for the given field. An Elastic IP will be selected if any one of the given values matches.

## Attributes Reference

* `id` - AWS Region.
* `allocation_ids` - A list of all the allocation IDs for address for use with EC2-VPC.
* `public_ips` - A list of all the Elastic IP addresses for use with EC2-Classic.