-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
aa40644
commit d31fa9c
Showing
2 changed files
with
79 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,78 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func dataSourceAwsRegion() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceAwsRegionRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
|
||
"current": &schema.Schema{ | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
|
||
"endpoint": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceAwsRegionRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ec2conn | ||
currentRegion := meta.(*AWSClient).region | ||
|
||
req := &ec2.DescribeRegionsInput{} | ||
|
||
req.RegionNames = make([]*string, 0, 2) | ||
if name := d.Get("name").(string); name != "" { | ||
req.RegionNames = append(req.RegionNames, aws.String(name)) | ||
} | ||
|
||
if d.Get("current").(bool) { | ||
req.RegionNames = append(req.RegionNames, aws.String(currentRegion)) | ||
} | ||
|
||
req.Filters = buildEC2FilterList(d, map[string]string{ | ||
"endpoint": "endpoint", | ||
}) | ||
|
||
log.Printf("[DEBUG] DescribeRegions %#v\n", req) | ||
resp, err := conn.DescribeRegions(req) | ||
if err != nil { | ||
return err | ||
} | ||
if resp == nil || len(resp.Regions) == 0 { | ||
return fmt.Errorf("no matching regions found") | ||
} | ||
if len(resp.Regions) > 1 { | ||
return fmt.Errorf("multiple regions matched; use additional constraints to reduce matches to a single region") | ||
} | ||
|
||
region := resp.Regions[0] | ||
|
||
d.SetId(*region.RegionName) | ||
d.Set("id", region.RegionName) | ||
d.Set("name", region.RegionName) | ||
d.Set("endpoint", region.Endpoint) | ||
d.Set("current", *region.RegionName == currentRegion) | ||
|
||
return nil | ||
} |
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