This repository has been archived by the owner on Nov 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
data_source_openstack_compute_availability_zones_v2.go
74 lines (63 loc) · 1.91 KB
/
data_source_openstack_compute_availability_zones_v2.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
64
65
66
67
68
69
70
71
72
73
74
package openstack
import (
"fmt"
"sort"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/availabilityzones"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)
func dataSourceComputeAvailabilityZonesV2() *schema.Resource {
return &schema.Resource{
Read: dataSourceComputeAvailabilityZonesV2Read,
Schema: map[string]*schema.Schema{
"names": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"region": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"state": {
Type: schema.TypeString,
Default: "available",
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"available", "unavailable"}, true),
},
},
}
}
func dataSourceComputeAvailabilityZonesV2Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
region := GetRegion(d, config)
computeClient, err := config.computeV2Client(region)
if err != nil {
return fmt.Errorf("Error creating OpenStack compute client: %s", err)
}
allPages, err := availabilityzones.List(computeClient).AllPages()
if err != nil {
return fmt.Errorf("Error retrieving openstack_compute_availability_zones_v2: %s", err)
}
zoneInfo, err := availabilityzones.ExtractAvailabilityZones(allPages)
if err != nil {
return fmt.Errorf("Error extracting openstack_compute_availability_zones_v2 from response: %s", err)
}
stateBool := d.Get("state").(string) == "available"
zones := make([]string, 0, len(zoneInfo))
for _, z := range zoneInfo {
if z.ZoneState.Available == stateBool {
zones = append(zones, z.ZoneName)
}
}
// sort.Strings sorts in place, returns nothing
sort.Strings(zones)
d.SetId(hashcode.Strings(zones))
d.Set("names", zones)
d.Set("region", region)
return nil
}