Skip to content

Commit 94c45c6

Browse files
provider/aws: aws_region data source
The primary purpose of this data source is to ask the question "what is my current region?", but it can also be used to retrieve the endpoint hostname for a particular (possibly non-current) region, should that be useful for some esoteric case.
1 parent fca9216 commit 94c45c6

5 files changed

Lines changed: 209 additions & 3 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package aws
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/aws/aws-sdk-go/aws"
8+
"github.com/aws/aws-sdk-go/service/ec2"
9+
"github.com/hashicorp/terraform/helper/schema"
10+
)
11+
12+
func dataSourceAwsRegion() *schema.Resource {
13+
return &schema.Resource{
14+
Read: dataSourceAwsRegionRead,
15+
16+
Schema: map[string]*schema.Schema{
17+
"name": &schema.Schema{
18+
Type: schema.TypeString,
19+
Optional: true,
20+
Computed: true,
21+
},
22+
23+
"current": &schema.Schema{
24+
Type: schema.TypeBool,
25+
Optional: true,
26+
Computed: true,
27+
},
28+
29+
"endpoint": &schema.Schema{
30+
Type: schema.TypeString,
31+
Optional: true,
32+
Computed: true,
33+
},
34+
},
35+
}
36+
}
37+
38+
func dataSourceAwsRegionRead(d *schema.ResourceData, meta interface{}) error {
39+
conn := meta.(*AWSClient).ec2conn
40+
currentRegion := meta.(*AWSClient).region
41+
42+
req := &ec2.DescribeRegionsInput{}
43+
44+
req.RegionNames = make([]*string, 0, 2)
45+
if name := d.Get("name").(string); name != "" {
46+
req.RegionNames = append(req.RegionNames, aws.String(name))
47+
}
48+
49+
if d.Get("current").(bool) {
50+
req.RegionNames = append(req.RegionNames, aws.String(currentRegion))
51+
}
52+
53+
req.Filters = buildEC2AttributeFilterList(
54+
map[string]string{
55+
"endpoint": d.Get("endpoint").(string),
56+
},
57+
)
58+
if len(req.Filters) == 0 {
59+
// Don't send an empty filters list; the EC2 API won't accept it.
60+
req.Filters = nil
61+
}
62+
63+
log.Printf("[DEBUG] DescribeRegions %s\n", req)
64+
resp, err := conn.DescribeRegions(req)
65+
if err != nil {
66+
return err
67+
}
68+
if resp == nil || len(resp.Regions) == 0 {
69+
return fmt.Errorf("no matching regions found")
70+
}
71+
if len(resp.Regions) > 1 {
72+
return fmt.Errorf("multiple regions matched; use additional constraints to reduce matches to a single region")
73+
}
74+
75+
region := resp.Regions[0]
76+
77+
d.SetId(*region.RegionName)
78+
d.Set("id", region.RegionName)
79+
d.Set("name", region.RegionName)
80+
d.Set("endpoint", region.Endpoint)
81+
d.Set("current", *region.RegionName == currentRegion)
82+
83+
return nil
84+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package aws
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform/helper/resource"
8+
"github.com/hashicorp/terraform/terraform"
9+
)
10+
11+
func TestAccDataSourceAwsRegion(t *testing.T) {
12+
resource.Test(t, resource.TestCase{
13+
PreCheck: func() { testAccPreCheck(t) },
14+
Providers: testAccProviders,
15+
Steps: []resource.TestStep{
16+
resource.TestStep{
17+
Config: testAccDataSourceAwsRegionConfig,
18+
Check: resource.ComposeTestCheckFunc(
19+
testAccDataSourceAwsRegionCheck("data.aws_region.by_name_current", "us-west-2", "true"),
20+
testAccDataSourceAwsRegionCheck("data.aws_region.by_name_other", "us-west-1", "false"),
21+
testAccDataSourceAwsRegionCheck("data.aws_region.by_current", "us-west-2", "true"),
22+
),
23+
},
24+
},
25+
})
26+
}
27+
28+
func testAccDataSourceAwsRegionCheck(name, region, current string) resource.TestCheckFunc {
29+
return func(s *terraform.State) error {
30+
rs, ok := s.RootModule().Resources[name]
31+
if !ok {
32+
return fmt.Errorf("root module has no resource called %s", name)
33+
}
34+
35+
attr := rs.Primary.Attributes
36+
37+
if attr["name"] != region {
38+
return fmt.Errorf("bad name %s", attr["name"])
39+
}
40+
if attr["current"] != current {
41+
return fmt.Errorf("bad current %s; want %s", attr["current"], current)
42+
}
43+
44+
return nil
45+
}
46+
}
47+
48+
const testAccDataSourceAwsRegionConfig = `
49+
provider "aws" {
50+
region = "us-west-2"
51+
}
52+
53+
data "aws_region" "by_name_current" {
54+
name = "us-west-2"
55+
}
56+
57+
data "aws_region" "by_name_other" {
58+
name = "us-west-1"
59+
}
60+
61+
data "aws_region" "by_current" {
62+
current = true
63+
}
64+
`

builtin/providers/aws/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ func Provider() terraform.ResourceProvider {
153153
"aws_iam_policy_document": dataSourceAwsIamPolicyDocument(),
154154
"aws_ip_ranges": dataSourceAwsIPRanges(),
155155
"aws_redshift_service_account": dataSourceAwsRedshiftServiceAccount(),
156+
"aws_region": dataSourceAwsRegion(),
156157
"aws_s3_bucket_object": dataSourceAwsS3BucketObject(),
157158
"aws_subnet": dataSourceAwsSubnet(),
158159
"aws_vpc": dataSourceAwsVpc(),
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
layout: "aws"
3+
page_title: "AWS: aws_region"
4+
sidebar_current: "docs-aws-datasource-region"
5+
description: |-
6+
Provides details about a specific service region
7+
---
8+
9+
# aws\_region
10+
11+
`aws_region` provides details about a specific AWS region.
12+
13+
As well as validating a given region name (and optionally obtaining its
14+
endpoint) this resource can be used to discover the name of the region
15+
configured within the provider. The latter can be useful in a child module
16+
which is inheriting an AWS provider configuration from its parent module.
17+
18+
## Example Usage
19+
20+
The following example shows how the resource might be used to obtain
21+
the name of the AWS region configured on the provider.
22+
23+
```
24+
data "aws_region" "current" {
25+
current = true
26+
}
27+
```
28+
29+
## Argument Reference
30+
31+
The arguments of this data source act as filters for querying the available
32+
regions. The given filters must match exactly one region whose data will be
33+
exported as attributes.
34+
35+
* `name` - (Optional) The full name of the region to select.
36+
37+
* `current` - (Optional) Set to `true` to match only the region configured
38+
in the provider. (It is not meaningful to set this to `false`.)
39+
40+
* `endpoint` - (Optional) The endpoint of the region to select.
41+
42+
At least one of the above attributes should be provided to ensure that only
43+
one region is matched.
44+
45+
## Attributes Reference
46+
47+
The following attributes are exported:
48+
49+
* `name` - The name of the selected region.
50+
51+
* `current` - `true` if the selected region is the one configured on the
52+
provider, or `false` otherwise.
53+
54+
* `endpoint` - The endpoint for the selected region.

website/source/layouts/aws.erb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@
4141
<li<%= sidebar_current("docs-aws-datasource-ip_ranges") %>>
4242
<a href="/docs/providers/aws/d/ip_ranges.html">aws_ip_ranges</a>
4343
</li>
44-
<li<%= sidebar_current("docs-aws-datasource-redshift-service-account") %>>
45-
<a href="/docs/providers/aws/d/redshift_service_account.html">aws_redshift_service_account</a>
46-
</li>
44+
<li<%= sidebar_current("docs-aws-datasource-redshift-service-account") %>>
45+
<a href="/docs/providers/aws/d/redshift_service_account.html">aws_redshift_service_account</a>
46+
</li>
47+
<li<%= sidebar_current("docs-aws-datasource-region") %>>
48+
<a href="/docs/providers/aws/d/region.html">aws_region</a>
49+
</li>
4750
<li<%= sidebar_current("docs-aws-datasource-s3-bucket-object") %>>
4851
<a href="/docs/providers/aws/d/s3_bucket_object.html">aws_s3_bucket_object</a>
4952
</li>

0 commit comments

Comments
 (0)