-
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.
Merge pull request #27051 from drewmullen/f-ds_aws_vpc_ipam_pool_cidrs
[New Data Source] d/aws_vpc_ipam_pool_cidrs
- Loading branch information
Showing
6 changed files
with
325 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,3 @@ | ||
```release-note:new-data-source | ||
aws_vpc_ipam_pool_cidrs | ||
``` |
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
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,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
102
internal/service/ec2/ipam_pool_cidrs_data_source_test.go
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,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, | ||
] | ||
} | ||
`) |
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,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`) |