-
Notifications
You must be signed in to change notification settings - Fork 0
/
regions_data_source.go
63 lines (52 loc) · 1.53 KB
/
regions_data_source.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package meta
import (
"fmt"
"log"
"github.com/ATenderholt/rainbow-test/terraform/conns"
tfec2 "github.com/ATenderholt/rainbow-test/terraform/service/ec2"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func DataSourceRegions() *schema.Resource {
return &schema.Resource{
Read: dataSourceRegionsRead,
Schema: map[string]*schema.Schema{
"filter": tfec2.DataSourceFiltersSchema(),
"names": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"all_regions": {
Type: schema.TypeBool,
Optional: true,
},
},
}
}
func dataSourceRegionsRead(d *schema.ResourceData, meta interface{}) error {
connection := meta.(*conns.AWSClient).EC2Conn
log.Printf("[DEBUG] Reading regions.")
request := &ec2.DescribeRegionsInput{}
if v, ok := d.GetOk("filter"); ok {
request.Filters = tfec2.BuildFiltersDataSource(v.(*schema.Set))
}
if v, ok := d.GetOk("all_regions"); ok {
request.AllRegions = aws.Bool(v.(bool))
}
log.Printf("[DEBUG] Reading regions for request: %s", request)
response, err := connection.DescribeRegions(request)
if err != nil {
return fmt.Errorf("Error fetching Regions: %w", err)
}
names := []string{}
for _, v := range response.Regions {
names = append(names, aws.StringValue(v.RegionName))
}
d.SetId(meta.(*conns.AWSClient).Partition)
if err := d.Set("names", names); err != nil {
return fmt.Errorf("error setting names: %w", err)
}
return nil
}