-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add availability zones data source (#240)
- Loading branch information
1 parent
11e7ce3
commit c159e0e
Showing
5 changed files
with
164 additions
and
38 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
huaweicloud/data_source_huaweicloud_compute_availability_zones_v2.go
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,66 @@ | ||
package huaweicloud | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/validation" | ||
"github.com/huaweicloud/golangsdk/openstack/compute/v2/extensions/availabilityzones" | ||
) | ||
|
||
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, | ||
}, | ||
}, | ||
"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 HuaweiCloud compute client: %s", err) | ||
} | ||
|
||
allPages, err := availabilityzones.List(computeClient).AllPages() | ||
if err != nil { | ||
return fmt.Errorf("Error retrieving huaweicloud_compute_availability_zones_v2: %s", err) | ||
} | ||
zoneInfo, err := availabilityzones.ExtractAvailabilityZones(allPages) | ||
if err != nil { | ||
return fmt.Errorf("Error extracting huaweicloud_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) | ||
|
||
return nil | ||
} |
27 changes: 27 additions & 0 deletions
27
huaweicloud/data_source_huaweicloud_compute_availability_zones_v2_test.go
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,27 @@ | ||
package huaweicloud | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
) | ||
|
||
func TestAccAvailabilityZonesV2_basic(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAvailabilityZonesConfig, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestMatchResourceAttr("data.huaweicloud_compute_availability_zones_v2.zones", "names.#", regexp.MustCompile("[1-9]\\d*")), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
const testAccAvailabilityZonesConfig = ` | ||
data "huaweicloud_compute_availability_zones_v2" "zones" {} | ||
` |
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
29 changes: 29 additions & 0 deletions
29
website/docs/d/compute_availability_zones_v2.html.markdown
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,29 @@ | ||
--- | ||
layout: "huaweicloud" | ||
page_title: "HuaweiCloud: huaweicloud_compute_availability_zones_v2" | ||
sidebar_current: "docs-huaweicloud-datasource-compute-availability-zones-v2" | ||
description: |- | ||
Get a list of availability zones from HuaweiCloud | ||
--- | ||
|
||
# huaweicloud\_compute\_availability\_zones\_v2 | ||
|
||
Use this data source to get a list of availability zones from HuaweiCloud | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "huaweicloud_compute_availability_zones_v2" "zones" {} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
* `state` - (Optional) The `state` of the availability zones to match, default ("available"). | ||
|
||
|
||
## Attributes Reference | ||
|
||
`id` is set to hash of the returned zone list. In addition, the following attributes | ||
are exported: | ||
|
||
* `names` - The names of the availability zones, ordered alphanumerically, that match the queried `state` |
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