Skip to content

Commit

Permalink
Merge pull request #27051 from drewmullen/f-ds_aws_vpc_ipam_pool_cidrs
Browse files Browse the repository at this point in the history
[New Data Source] d/aws_vpc_ipam_pool_cidrs
  • Loading branch information
ewbankkit authored Oct 3, 2022
2 parents d9db01d + fafbcdc commit 17e4705
Show file tree
Hide file tree
Showing 6 changed files with 325 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/27051.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_vpc_ipam_pool_cidrs
```
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,7 @@ func New(_ context.Context) (*schema.Provider, error) {
"aws_vpc_endpoint_service": ec2.DataSourceVPCEndpointService(),
"aws_vpc_endpoint": ec2.DataSourceVPCEndpoint(),
"aws_vpc_ipam_pool": ec2.DataSourceIPAMPool(),
"aws_vpc_ipam_pool_cidrs": ec2.DataSourceIPAMPoolCIDRs(),
"aws_vpc_ipam_preview_next_cidr": ec2.DataSourceIPAMPreviewNextCIDR(),
"aws_vpc_peering_connection": ec2.DataSourceVPCPeeringConnection(),
"aws_vpc_peering_connections": ec2.DataSourceVPCPeeringConnections(),
Expand Down
31 changes: 31 additions & 0 deletions internal/service/ec2/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -4937,6 +4937,37 @@ func FindInternetGatewayAttachment(conn *ec2.EC2, internetGatewayID, vpcID strin
return attachment, nil
}

func FindIPAMPoolCIDRs(conn *ec2.EC2, input *ec2.GetIpamPoolCidrsInput) ([]*ec2.IpamPoolCidr, error) {
var output []*ec2.IpamPoolCidr

err := conn.GetIpamPoolCidrsPages(input, func(page *ec2.GetIpamPoolCidrsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

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

return !lastPage
})

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

if err != nil {
return nil, err
}

return output, nil
}

func FindKeyPair(conn *ec2.EC2, input *ec2.DescribeKeyPairsInput) (*ec2.KeyPairInfo, error) {
output, err := FindKeyPairs(conn, input)

Expand Down
90 changes: 90 additions & 0 deletions internal/service/ec2/ipam_pool_cidrs_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package ec2

import (
"time"

"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"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
)

func DataSourceIPAMPoolCIDRs() *schema.Resource {
return &schema.Resource{
Read: dataSourceIPAMPoolCIDRsRead,

Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(1 * time.Minute),
},

Schema: map[string]*schema.Schema{
"filter": DataSourceFiltersSchema(),
"ipam_pool_id": {
Type: schema.TypeString,
Required: true,
},
"ipam_pool_cidrs": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cidr": {
Type: schema.TypeString,
Computed: true,
},
"state": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}

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

input := &ec2.GetIpamPoolCidrsInput{}

if v, ok := d.GetOk("ipam_pool_id"); ok {
input.IpamPoolId = aws.String(v.(string))
}

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

output, err := FindIPAMPoolCIDRs(conn, input)

if err != nil {
return err
}

if len(output) == 0 || output[0] == nil {
return tfresource.SingularDataSourceFindError("CIDRS IN EC2 VPC IPAM POOL", tfresource.NewEmptyResultError(input))
}

d.SetId(d.Get("ipam_pool_id").(string))
d.Set("ipam_pool_cidrs", flattenIPAMPoolCIDRs(output))

return nil
}

func flattenIPAMPoolCIDRs(c []*ec2.IpamPoolCidr) []interface{} {
cidrs := []interface{}{}
for _, cidr := range c {
cidrs = append(cidrs, flattenIPAMPoolCIDR(cidr))
}
return cidrs
}

func flattenIPAMPoolCIDR(c *ec2.IpamPoolCidr) map[string]interface{} {
cidr := make(map[string]interface{})
cidr["cidr"] = aws.StringValue(c.Cidr)
cidr["state"] = aws.StringValue(c.State)
return cidr
}
102 changes: 102 additions & 0 deletions internal/service/ec2/ipam_pool_cidrs_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package ec2_test

import (
"testing"

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

func TestAccIPAMPoolCIDRsDataSource_basic(t *testing.T) {
dataSourceName := "data.aws_vpc_ipam_pool_cidrs.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t); testAccIPAMPreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, ec2.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccIPAMPoolCIDRsDataSourceConfig_basicOneCIDRs,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "ipam_pool_cidrs.#", "1"),
),
},
{
Config: testAccIPAMPoolCIDRsDataSourceConfig_basicTwoCIDRs,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "ipam_pool_cidrs.#", "2"),
),
},
{
Config: testAccIPAMPoolCIDRsDataSourceConfig_basicTwoCIDRsFiltered,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "ipam_pool_cidrs.#", "1"),
),
},
},
})
}

var testAccIPAMPoolCIDRsDataSourceConfig_basicOneCIDRs = acctest.ConfigCompose(
testAccIPAMPoolConfig_basic, `
resource "aws_vpc_ipam_pool_cidr" "test" {
ipam_pool_id = aws_vpc_ipam_pool.test.id
cidr = "172.2.0.0/16"
}
data "aws_vpc_ipam_pool_cidrs" "test" {
ipam_pool_id = aws_vpc_ipam_pool.test.id
depends_on = [
aws_vpc_ipam_pool_cidr.test
]
}
`)

var testAccIPAMPoolCIDRsDataSourceConfig_basicTwoCIDRs = acctest.ConfigCompose(
testAccIPAMPoolConfig_basic, `
resource "aws_vpc_ipam_pool_cidr" "test" {
ipam_pool_id = aws_vpc_ipam_pool.test.id
cidr = "172.2.0.0/16"
}
resource "aws_vpc_ipam_pool_cidr" "testtwo" {
ipam_pool_id = aws_vpc_ipam_pool.test.id
cidr = "10.2.0.0/16"
}
data "aws_vpc_ipam_pool_cidrs" "test" {
ipam_pool_id = aws_vpc_ipam_pool.test.id
depends_on = [
aws_vpc_ipam_pool_cidr.test,
aws_vpc_ipam_pool_cidr.testtwo,
]
}
`)

var testAccIPAMPoolCIDRsDataSourceConfig_basicTwoCIDRsFiltered = acctest.ConfigCompose(
testAccIPAMPoolConfig_basic, `
resource "aws_vpc_ipam_pool_cidr" "test" {
ipam_pool_id = aws_vpc_ipam_pool.test.id
cidr = "172.2.0.0/16"
}
resource "aws_vpc_ipam_pool_cidr" "testtwo" {
ipam_pool_id = aws_vpc_ipam_pool.test.id
cidr = "10.2.0.0/16"
}
data "aws_vpc_ipam_pool_cidrs" "test" {
ipam_pool_id = aws_vpc_ipam_pool.test.id
filter {
name = "cidr"
values = ["10.*"]
}
depends_on = [
aws_vpc_ipam_pool_cidr.test,
aws_vpc_ipam_pool_cidr.testtwo,
]
}
`)
98 changes: 98 additions & 0 deletions website/docs/d/vpc_ipam_pool_cidrs.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
---
subcategory: "VPC IPAM (IP Address Manager)"
layout: "aws"
page_title: "AWS: aws_vpc_ipam_pool_cidrs"
description: |-
Returns cidrs provisioned into an IPAM pool.
---

# Data Source: aws_vpc_ipam_pool_cidrs

`aws_vpc_ipam_pool_cidrs` provides details about an IPAM pool.

This resource can prove useful when an ipam pool was shared to your account and you want to know all (or a filtered list) of the CIDRs that are provisioned into the pool.

## Example Usage

Basic usage:

```terraform
data "aws_vpc_ipam_pool_cidrs" "c" {
ipam_pool_id = data.aws_vpc_ipam_pool.p.id
}
data "aws_vpc_ipam_pool" "p" {
filter {
name = "description"
values = ["*mypool*"]
}
filter {
name = "address-family"
values = ["ipv4"]
}
}
```

Filtering:

```terraform
data "aws_vpc_ipam_pool_cidrs" "c" {
ipam_pool_id = "ipam-pool-123"
filter {
name = "cidr"
values = ["10.*"]
}
}
locals {
mycidrs = [for cidr in data.aws_vpc_ipam_pool_cidrs.c.ipam_pool_cidrs :
cidr.cidr if
cidr.state == "provisioned"]
}
resource "aws_ec2_managed_prefix_list" "pls" {
name = "IPAM Pool (${aws_vpc_ipam_pool.test.id}) Cidrs"
address_family = "IPv4"
max_entries = length(local.mycidrs)
dynamic "entry" {
for_each = local.mycidrs
content {
cidr = entry.value
description = entry.value
}
}
}
```

## Argument Reference

The arguments of this data source act as filters for querying the available
VPCs in the current region. The given filters must match exactly one
VPC whose data will be exported as attributes.

* `ipam_pool_id` - ID of the IPAM pool you would like the list of provisioned CIDRs.
* `filter` - Custom filter block as described below.

## Attributes Reference

All of the argument attributes except `filter` blocks are also exported as
result attributes. This data source will complete the data by populating
any fields that are not included in the configuration with the data for
the selected IPAM Pool CIDRs.

The following attribute is additionally exported:

* `ipam_pool_cidrs` - The CIDRs provisioned into the IPAM pool, described below.

### ipam_pool_cidrs

* `cidr` - A network CIDR.
* `state` - The provisioning state of that CIDR.

## Timeouts

[Configuration options](https://www.terraform.io/docs/configuration/blocks/resources/syntax.html#operation-timeouts):

- `read` - (Default `1m`)

0 comments on commit 17e4705

Please sign in to comment.