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

Add Datasource for COIP Pool(s) (Outpost) #12852

Merged
merged 3 commits into from
May 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
103 changes: 103 additions & 0 deletions aws/data_source_aws_coip_pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package aws

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func dataSourceAwsCoipPool() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsCoipPoolRead,

Schema: map[string]*schema.Schema{
"local_gateway_route_table_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},

"pool_cidrs": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
Set: schema.HashString,
},

"pool_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},

"tags": tagsSchemaComputed(),

"filter": ec2CustomFiltersSchema(),
},
}
}

func dataSourceAwsCoipPoolRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

req := &ec2.DescribeCoipPoolsInput{}

var id string
if cid, ok := d.GetOk("pool_id"); ok {
id = cid.(string)
}

if id != "" {
req.PoolIds = []*string{aws.String(id)}
}

filters := map[string]string{}

if v, ok := d.GetOk("local_gateway_route_table_id"); ok {
filters["coip-pool.local-gateway-route-table-id"] = v.(string)
}

req.Filters = buildEC2AttributeFilterList(filters)

if tags, tagsOk := d.GetOk("tags"); tagsOk {
req.Filters = append(req.Filters, buildEC2TagFilterList(
keyvaluetags.New(tags.(map[string]interface{})).Ec2Tags(),
)...)
}

req.Filters = append(req.Filters, buildEC2CustomFilterList(
d.Get("filter").(*schema.Set),
)...)
if len(req.Filters) == 0 {
// Don't send an empty filters list; the EC2 API won't accept it.
req.Filters = nil
}

log.Printf("[DEBUG] Reading AWS COIP Pool: %s", req)
resp, err := conn.DescribeCoipPools(req)
if err != nil {
return err
}
if resp == nil || len(resp.CoipPools) == 0 {
return fmt.Errorf("no matching COIP Pool found")
}
if len(resp.CoipPools) > 1 {
return fmt.Errorf("multiple Coip Pools matched; use additional constraints to reduce matches to a single COIP Pool")
}

coip := resp.CoipPools[0]

d.SetId(aws.StringValue(coip.PoolId))
d.Set("pool_cidrs", coip.PoolCidrs)
d.Set("local_gateway_route_table_id", coip.LocalGatewayRouteTableId)

if err := d.Set("tags", keyvaluetags.Ec2KeyValueTags(coip.Tags).IgnoreAws().Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

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

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccDataSourceAwsCoipPool_basic(t *testing.T) {
rPoolId := os.Getenv("AWS_COIP_POOL_ID")
if rPoolId == "" {
t.Skip(
"Environment variable AWS_COIP_POOL_ID is not set. " +
"This environment variable must be set to the ID of " +
"a deployed Coip Pool to enable this test.")
}

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsLocalGatewayConfig(rPoolId),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.aws_coip_pool.by_id", "pool_id", rPoolId),
resource.TestCheckResourceAttrSet("data.aws_coip_pool.by_id", "local_gateway_route_table_id"),
testCheckResourceAttrGreaterThanValue("data.aws_coip_pool.by_id", "pool_cidrs.#", "0"),
),
},
},
})
}

func testAccDataSourceAwsLocalGatewayConfig(rPoolId string) string {
return fmt.Sprintf(`
data "aws_coip_pool" "by_id" {
pool_id = "%s"
}
`, rPoolId)
}
75 changes: 75 additions & 0 deletions aws/data_source_aws_coip_pools.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func dataSourceAwsCoipPools() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsCoipPoolsRead,
Schema: map[string]*schema.Schema{
"filter": ec2CustomFiltersSchema(),

"tags": tagsSchemaComputed(),

"pool_ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
},
}
}

func dataSourceAwsCoipPoolsRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

req := &ec2.DescribeCoipPoolsInput{}

if tags, tagsOk := d.GetOk("tags"); tagsOk {
req.Filters = append(req.Filters, buildEC2TagFilterList(
keyvaluetags.New(tags.(map[string]interface{})).Ec2Tags(),
)...)
}

if filters, filtersOk := d.GetOk("filter"); filtersOk {
req.Filters = append(req.Filters, buildEC2CustomFilterList(
filters.(*schema.Set),
)...)
}
if len(req.Filters) == 0 {
// Don't send an empty filters list; the EC2 API won't accept it.
req.Filters = nil
}

log.Printf("[DEBUG] DescribeCoipPools %s\n", req)
resp, err := conn.DescribeCoipPools(req)
if err != nil {
return err
}

if resp == nil || len(resp.CoipPools) == 0 {
return fmt.Errorf("no matching Coip Pool found")
}

coippools := make([]string, 0)

for _, coippool := range resp.CoipPools {
coippools = append(coippools, aws.StringValue(coippool.PoolId))
}

d.SetId(time.Now().UTC().String())
if err := d.Set("pool_ids", coippools); err != nil {
return fmt.Errorf("Error setting coip pool ids: %s", err)
}

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

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

func TestAccDataSourceAwsCoipPools_basic(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsCoipPoolsConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsCoipPoolDataSourceExists("data.aws_coip_pools.all"),
),
},
},
})
}

func TestAccDataSourceAwsCoipPools_filters(t *testing.T) {
rPoolId := os.Getenv("AWS_COIP_POOL_ID")
if rPoolId == "" {
t.Skip(
"Environment variable AWS_COIP_POOL_ID is not set. " +
"This environment variable must be set to the ID of " +
"a deployed Coip Pool to enable this test.")
}

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsCoipPoolsConfig_filters(rPoolId),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsCoipPoolDataSourceExists("data.aws_coip_pools.selected"),
testCheckResourceAttrGreaterThanValue("data.aws_coip_pools.selected", "pool_ids.#", "0"),
),
},
},
})
}

func testAccCheckAwsCoipPoolDataSourceExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("can't find aws_coip_pools data source: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("aws_coip_pools data source ID not set")
}
return nil
}
}

const testAccDataSourceAwsCoipPoolsConfig = `data "aws_coip_pools" "all" {}`

func testAccDataSourceAwsCoipPoolsConfig_filters(rPoolId string) string {
return fmt.Sprintf(`
data "aws_coip_pools" "selected" {
filter {
name = "coip-pool.pool-id"
values = ["%s"]
}
}
`, rPoolId)
}
2 changes: 2 additions & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ func Provider() terraform.ResourceProvider {
"aws_cloudtrail_service_account": dataSourceAwsCloudTrailServiceAccount(),
"aws_cloudwatch_log_group": dataSourceAwsCloudwatchLogGroup(),
"aws_cognito_user_pools": dataSourceAwsCognitoUserPools(),
"aws_coip_pool": dataSourceAwsCoipPool(),
"aws_coip_pools": dataSourceAwsCoipPools(),
"aws_codecommit_repository": dataSourceAwsCodeCommitRepository(),
"aws_cur_report_definition": dataSourceAwsCurReportDefinition(),
"aws_db_cluster_snapshot": dataSourceAwsDbClusterSnapshot(),
Expand Down
6 changes: 6 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3153,6 +3153,12 @@
<li>
<a href="#">Data Sources</a>
<ul class="nav nav-auto-expand">
<li>
<a href="/docs/providers/aws/d/coip_pool.html">aws_coip_pool</a>
</li>
<li>
<a href="/docs/providers/aws/d/coip_pools.html">aws_coip_pools</a>
</li>
<li>
<a href="/docs/providers/aws/d/customer_gateway.html">aws_customer_gateway</a>
</li>
Expand Down
60 changes: 60 additions & 0 deletions website/docs/d/coip_pool.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
subcategory: "VPC"
layout: "aws"
page_title: "AWS: aws_coip_pool"
description: |-
Provides details about a specific COIP Pool
---

# Data Source: aws_coip_pool

`aws_coip_pool` provides details about a specific COIP Pool.

This resource can prove useful when a module accepts a coip pool id as
an input variable and needs to, for example, determine the CIDR block of that
COIP Pool.

## Example Usage

The following example returns a specific coip pool ID

```hcl
variable "coip_pool_id" {}

data "aws_coip_pool" "selected" {
id = "${var.coip_pool_id}"
}
```

## Argument Reference

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

* `local_gateway_route_table_id` - (Optional) Local Gateway Route Table Id assigned to desired COIP Pool

* `pool_id` - (Optional) The id of the specific COIP Pool to retrieve.

* `tags` - (Optional) A mapping of tags, each pair of which must exactly match
a pair on the desired COIP Pool.

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_DescribeCoipPools.html).

* `values` - (Required) Set of values that are accepted for the given field.
A COIP Pool will be selected if any one of the given values matches.

## 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 COIP Pool.

The following attribute is additionally exported:

* `pool_cidrs` - List of CIDR blocks in pool
Loading