Skip to content

Commit 45228fb

Browse files
committed
blockstorage: add helper to return available AZs
A similar utility exists for the compute API. Introduce one for the block storage API. Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
1 parent 8e78007 commit 45228fb

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

  • openstack/blockstorage/v3/availabilityzones
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package availabilityzones
2+
3+
import (
4+
"github.com/gophercloud/gophercloud"
5+
"github.com/gophercloud/gophercloud/openstack/blockstorage/extensions/availabilityzones"
6+
)
7+
8+
// ListAvailableAvailabilityZones is a convenience function that return a slice of available Availability Zones.
9+
func ListAvailableAvailabilityZones(client *gophercloud.ServiceClient) ([]string, error) {
10+
var zones []string
11+
12+
allPages, err := availabilityzones.List(client).AllPages()
13+
if err != nil {
14+
return nil, err
15+
}
16+
17+
availabilityZoneInfo, err := availabilityzones.ExtractAvailabilityZones(allPages)
18+
if err != nil {
19+
return nil, err
20+
}
21+
22+
// This should always return at at least two AZs. By default, Nova will
23+
// return an AZ for internal services (typically called 'internal') and AZ
24+
// for (typically called 'nova'). We can obviously configure additional AZs
25+
// and you can also configure the names of these default AZs with
26+
// '[DEFAULT] internal_service_availability_zone' and
27+
// '[DEFAULT] default_availability_zone', respectively.
28+
for _, zone := range availabilityZoneInfo {
29+
if zone.ZoneState.Available {
30+
zones = append(zones, zone.ZoneName)
31+
}
32+
}
33+
34+
return zones, nil
35+
}

0 commit comments

Comments
 (0)