2016-05-14 20:18:51 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"sort"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func dataSourceAwsAvailabilityZones() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Read: dataSourceAwsAvailabilityZonesRead,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2016-06-15 15:17:12 +02:00
|
|
|
"names": &schema.Schema{
|
2016-05-14 20:18:51 +02:00
|
|
|
Type: schema.TypeList,
|
|
|
|
Computed: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
},
|
2016-08-05 02:14:05 +02:00
|
|
|
"state": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ValidateFunc: validateStateType,
|
|
|
|
},
|
2016-05-14 20:18:51 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func dataSourceAwsAvailabilityZonesRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).ec2conn
|
|
|
|
|
2016-08-05 02:14:05 +02:00
|
|
|
log.Printf("[DEBUG] Reading Availability Zones.")
|
2016-05-14 20:18:51 +02:00
|
|
|
d.SetId(time.Now().UTC().String())
|
|
|
|
|
2016-08-05 02:14:05 +02:00
|
|
|
request := &ec2.DescribeAvailabilityZonesInput{}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("state"); ok {
|
|
|
|
request.Filters = []*ec2.Filter{
|
|
|
|
&ec2.Filter{
|
|
|
|
Name: aws.String("state"),
|
|
|
|
Values: []*string{aws.String(v.(string))},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Availability Zones request options: %#v", *request)
|
|
|
|
|
|
|
|
resp, err := conn.DescribeAvailabilityZones(request)
|
2016-05-14 20:18:51 +02:00
|
|
|
if err != nil {
|
2016-08-05 02:14:05 +02:00
|
|
|
return fmt.Errorf("Error fetching Availability Zones: %s", err)
|
2016-05-14 20:18:51 +02:00
|
|
|
}
|
|
|
|
|
2016-08-05 02:14:05 +02:00
|
|
|
raw := make([]string, len(resp.AvailabilityZones))
|
|
|
|
for i, v := range resp.AvailabilityZones {
|
2016-05-14 20:18:51 +02:00
|
|
|
raw[i] = *v.ZoneName
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(raw)
|
|
|
|
|
2016-06-15 15:17:12 +02:00
|
|
|
if err := d.Set("names", raw); err != nil {
|
2016-08-05 02:14:05 +02:00
|
|
|
return fmt.Errorf("[WARN] Error setting Availability Zones: %s", err)
|
2016-05-14 20:18:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-08-05 02:14:05 +02:00
|
|
|
|
|
|
|
func validateStateType(v interface{}, k string) (ws []string, errors []error) {
|
|
|
|
value := v.(string)
|
|
|
|
|
|
|
|
validState := map[string]bool{
|
|
|
|
"available": true,
|
|
|
|
"information": true,
|
|
|
|
"impaired": true,
|
|
|
|
"unavailable": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
if !validState[value] {
|
|
|
|
errors = append(errors, fmt.Errorf(
|
|
|
|
"%q contains an invalid Availability Zone state %q. Valid states are: %q, %q, %q and %q.",
|
|
|
|
k, value, "available", "information", "impaired", "unavailable"))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|