Skip to content

Commit

Permalink
Switch to filter for more flexibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Grzegorz Szczudlik committed Mar 31, 2020
1 parent d192f0f commit 5da82fb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 46 deletions.
29 changes: 4 additions & 25 deletions aws/data_source_aws_regions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
"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/hashicorp/terraform-plugin-sdk/helper/validation"
)

func dataSourceAwsRegions() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsRegionsRead,

Schema: map[string]*schema.Schema{
"filter": dataSourceFiltersSchema(),
"names": {
Type: schema.TypeSet,
Computed: true,
Expand All @@ -27,18 +27,6 @@ func dataSourceAwsRegions() *schema.Resource {
Type: schema.TypeBool,
Optional: true,
},

"opt_in_status": {
Type: schema.TypeString,
Optional: true,
// There is no OptInStatus constants defined for Regions.
// Using those from AvailabilityZone definition.
ValidateFunc: validation.StringInSlice([]string{
ec2.AvailabilityZoneOptInStatusOptInNotRequired,
ec2.AvailabilityZoneOptInStatusOptedIn,
ec2.AvailabilityZoneOptInStatusNotOptedIn,
}, false),
},
},
}
}
Expand All @@ -47,23 +35,14 @@ func dataSourceAwsRegionsRead(d *schema.ResourceData, meta interface{}) error {
connection := meta.(*AWSClient).ec2conn

log.Printf("[DEBUG] Reading regions.")

request := &ec2.DescribeRegionsInput{}

if v, ok := d.GetOk("filter"); ok {
request.Filters = buildAwsDataSourceFilters(v.(*schema.Set))
}
if v, ok := d.GetOk("all_regions"); ok {
request.AllRegions = aws.Bool(v.(bool))
}

if v, ok := d.GetOk("opt_in_status"); ok {
log.Printf("[DEBUG] Adding region filters")
request.Filters = []*ec2.Filter{
{
Name: aws.String("opt-in-status"),
Values: []*string{aws.String(v.(string))},
},
}
}

log.Printf("[DEBUG] Reading regions for request: %s", request)
response, err := connection.DescribeRegions(request)
if err != nil {
Expand Down
24 changes: 9 additions & 15 deletions aws/data_source_aws_regions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package aws

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
Expand All @@ -21,7 +20,6 @@ func TestAccDataSourceAwsRegionsBasic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsRegionsCheck(resourceName),
resource.TestCheckNoResourceAttr(resourceName, "all_regions"),
resource.TestCheckNoResourceAttr(resourceName, "opt_in_status"),
),
},
},
Expand All @@ -41,31 +39,24 @@ func TestAccDataSourceAwsRegionsOptIn(t *testing.T) {
Steps: []resource.TestStep{
// This resource has to be at the very top of the test scenario due to bug in Terrafom Plugin SDK
{
Config: testAccDataSourceAwsRegionsConfigOptIn("invalid-opt-in-status"),
ExpectError: regexp.MustCompile(`expected opt_in_status to be one of .*, got invalid-opt-in-status`),
},
{
Config: testAccDataSourceAwsRegionsConfigOptIn(statusOptedIn),
Config: testAccDataSourceAwsRegionsConfigAllRegionsFiltered(statusOptedIn),
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsRegionsCheck(resourceName),
resource.TestCheckNoResourceAttr(resourceName, "all_regions"),
resource.TestCheckResourceAttr(resourceName, "opt_in_status", statusOptedIn),
),
},
{
Config: testAccDataSourceAwsRegionsConfigOptIn(statusOptInNotRequired),
Config: testAccDataSourceAwsRegionsConfigAllRegionsFiltered(statusOptInNotRequired),
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsRegionsCheck(resourceName),
resource.TestCheckNoResourceAttr(resourceName, "all_regions"),
resource.TestCheckResourceAttr(resourceName, "opt_in_status", statusOptInNotRequired),
),
},
{
Config: testAccDataSourceAwsRegionsConfigOptIn(statusNotOptedIn),
Config: testAccDataSourceAwsRegionsConfigAllRegionsFiltered(statusNotOptedIn),
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsRegionsCheck(resourceName),
resource.TestCheckNoResourceAttr(resourceName, "all_regions"),
resource.TestCheckResourceAttr(resourceName, "opt_in_status", statusNotOptedIn),
),
},
},
Expand Down Expand Up @@ -116,10 +107,13 @@ data "aws_regions" "all_regions" {
`
}

func testAccDataSourceAwsRegionsConfigOptIn(optInStatus string) string {
func testAccDataSourceAwsRegionsConfigAllRegionsFiltered(filter string) string {
return fmt.Sprintf(`
data "aws_regions" "opt_in_status" {
opt_in_status = "%s"
filter {
name = "opt-in-status"
values = ["%s"]
}
}
`, optInStatus)
`, filter)
}
19 changes: 13 additions & 6 deletions website/docs/d/regions.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ description: |-
`aws_regions` provides list of all enabled AWS regions.

The data source provides list of AWS regions available.
Can be used to filter regions by Opt-In status or list only regions enabled for current account.
Can be used to filter regions i.e. by Opt-In status or list only regions enabled for current account.
To get details like endpoint and description of each region the data source can be combined with `aws_region`.

## Example Usage

The following example shows how the resource might be used to obtain
the list of the AWS regions configured on the provider.

Regions enabled for the user
To list regions enabled for the user:

```hcl
data "aws_regions" "current" {}
Expand All @@ -33,13 +33,17 @@ data "aws_regions" "current" {
}
```

To see regions that are `"not-opted-in"` `"all_regions"` need to be set to true
or nothing will be displayed.
To see regions that are filtered by `"not-opted-in"` `"all_regions"` need to be set to true
or probably nothing will be displayed.

```hcl
data "aws_regions" "current" {
all_regions = true
opt_in_status = "not-opted-in"
filter {
name = "opt-in-status"
values = ["not-opted-in"]
}
}
```

Expand All @@ -51,11 +55,14 @@ exported as attributes.

* `all_regions` - (Optional) If true the source will query all regions regardless of availability.

* `opt_in_status` - (Optional) Filter the list of regions according to op-in-status filter. Can be one of: `"opt-in-not-required"`, `"opted-in"` or `"not-opted-in"`.
* `filter` - (Optional) One or more key/value pairs to use as filters. Full reference of valid keys
can be found [describe-regions in the AWS CLI reference][1].

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `names` - Names of regions that meets the criteria.

[1]: https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-regions.html

0 comments on commit 5da82fb

Please sign in to comment.