Skip to content

Commit

Permalink
Add availability zones data source (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
niuzhenguo authored Dec 2, 2019
1 parent 11e7ce3 commit c159e0e
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 38 deletions.
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
}
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" {}
`
77 changes: 39 additions & 38 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,44 +190,45 @@ func Provider() terraform.ResourceProvider {
},

DataSourcesMap: map[string]*schema.Resource{
"huaweicloud_images_image_v2": dataSourceImagesImageV2(),
"huaweicloud_networking_network_v2": dataSourceNetworkingNetworkV2(),
"huaweicloud_networking_subnet_v2": dataSourceNetworkingSubnetV2(),
"huaweicloud_networking_port_v2": dataSourceNetworkingPortV2(),
"huaweicloud_networking_secgroup_v2": dataSourceNetworkingSecGroupV2(),
"huaweicloud_s3_bucket_object": dataSourceS3BucketObject(),
"huaweicloud_kms_key_v1": dataSourceKmsKeyV1(),
"huaweicloud_kms_data_key_v1": dataSourceKmsDataKeyV1(),
"huaweicloud_rds_flavors_v1": dataSourceRdsFlavorV1(),
"huaweicloud_sfs_file_system_v2": dataSourceSFSFileSystemV2(),
"huaweicloud_rts_stack_v1": dataSourceRTSStackV1(),
"huaweicloud_rts_stack_resource_v1": dataSourceRTSStackResourcesV1(),
"huaweicloud_iam_role_v3": dataSourceIAMRoleV3(),
"huaweicloud_vpc_v1": dataSourceVirtualPrivateCloudVpcV1(),
"huaweicloud_vpc_ids_v1": dataSourceVirtualPrivateCloudVpcIdsV1(),
"huaweicloud_vpc_peering_connection_v2": dataSourceVpcPeeringConnectionV2(),
"huaweicloud_vpc_route_v2": dataSourceVPCRouteV2(),
"huaweicloud_vpc_route_ids_v2": dataSourceVPCRouteIdsV2(),
"huaweicloud_vpc_subnet_v1": dataSourceVpcSubnetV1(),
"huaweicloud_vpc_subnet_ids_v1": dataSourceVpcSubnetIdsV1(),
"huaweicloud_cce_cluster_v3": dataSourceCCEClusterV3(),
"huaweicloud_cce_node_v3": dataSourceCceNodesV3(),
"huaweicloud_rts_software_config_v1": dataSourceRtsSoftwareConfigV1(),
"huaweicloud_csbs_backup_v1": dataSourceCSBSBackupV1(),
"huaweicloud_csbs_backup_policy_v1": dataSourceCSBSBackupPolicyV1(),
"huaweicloud_dms_az_v1": dataSourceDmsAZV1(),
"huaweicloud_dms_product_v1": dataSourceDmsProductV1(),
"huaweicloud_dms_maintainwindow_v1": dataSourceDmsMaintainWindowV1(),
"huaweicloud_vbs_backup_policy_v2": dataSourceVBSBackupPolicyV2(),
"huaweicloud_vbs_backup_v2": dataSourceVBSBackupV2(),
"huaweicloud_cts_tracker_v1": dataSourceCTSTrackerV1(),
"huaweicloud_antiddos_v1": dataSourceAntiDdosV1(),
"huaweicloud_dcs_az_v1": dataSourceDcsAZV1(),
"huaweicloud_dcs_maintainwindow_v1": dataSourceDcsMaintainWindowV1(),
"huaweicloud_dcs_product_v1": dataSourceDcsProductV1(),
"huaweicloud_identity_role_v3": dataSourceIdentityRoleV3(),
"huaweicloud_cdm_flavors_v1": dataSourceCdmFlavorV1(),
"huaweicloud_dis_partition_v2": dataSourceDisPartitionV2(),
"huaweicloud_compute_availability_zones_v2": dataSourceComputeAvailabilityZonesV2(),
"huaweicloud_images_image_v2": dataSourceImagesImageV2(),
"huaweicloud_networking_network_v2": dataSourceNetworkingNetworkV2(),
"huaweicloud_networking_subnet_v2": dataSourceNetworkingSubnetV2(),
"huaweicloud_networking_port_v2": dataSourceNetworkingPortV2(),
"huaweicloud_networking_secgroup_v2": dataSourceNetworkingSecGroupV2(),
"huaweicloud_s3_bucket_object": dataSourceS3BucketObject(),
"huaweicloud_kms_key_v1": dataSourceKmsKeyV1(),
"huaweicloud_kms_data_key_v1": dataSourceKmsDataKeyV1(),
"huaweicloud_rds_flavors_v1": dataSourceRdsFlavorV1(),
"huaweicloud_sfs_file_system_v2": dataSourceSFSFileSystemV2(),
"huaweicloud_rts_stack_v1": dataSourceRTSStackV1(),
"huaweicloud_rts_stack_resource_v1": dataSourceRTSStackResourcesV1(),
"huaweicloud_iam_role_v3": dataSourceIAMRoleV3(),
"huaweicloud_vpc_v1": dataSourceVirtualPrivateCloudVpcV1(),
"huaweicloud_vpc_ids_v1": dataSourceVirtualPrivateCloudVpcIdsV1(),
"huaweicloud_vpc_peering_connection_v2": dataSourceVpcPeeringConnectionV2(),
"huaweicloud_vpc_route_v2": dataSourceVPCRouteV2(),
"huaweicloud_vpc_route_ids_v2": dataSourceVPCRouteIdsV2(),
"huaweicloud_vpc_subnet_v1": dataSourceVpcSubnetV1(),
"huaweicloud_vpc_subnet_ids_v1": dataSourceVpcSubnetIdsV1(),
"huaweicloud_cce_cluster_v3": dataSourceCCEClusterV3(),
"huaweicloud_cce_node_v3": dataSourceCceNodesV3(),
"huaweicloud_rts_software_config_v1": dataSourceRtsSoftwareConfigV1(),
"huaweicloud_csbs_backup_v1": dataSourceCSBSBackupV1(),
"huaweicloud_csbs_backup_policy_v1": dataSourceCSBSBackupPolicyV1(),
"huaweicloud_dms_az_v1": dataSourceDmsAZV1(),
"huaweicloud_dms_product_v1": dataSourceDmsProductV1(),
"huaweicloud_dms_maintainwindow_v1": dataSourceDmsMaintainWindowV1(),
"huaweicloud_vbs_backup_policy_v2": dataSourceVBSBackupPolicyV2(),
"huaweicloud_vbs_backup_v2": dataSourceVBSBackupV2(),
"huaweicloud_cts_tracker_v1": dataSourceCTSTrackerV1(),
"huaweicloud_antiddos_v1": dataSourceAntiDdosV1(),
"huaweicloud_dcs_az_v1": dataSourceDcsAZV1(),
"huaweicloud_dcs_maintainwindow_v1": dataSourceDcsMaintainWindowV1(),
"huaweicloud_dcs_product_v1": dataSourceDcsProductV1(),
"huaweicloud_identity_role_v3": dataSourceIdentityRoleV3(),
"huaweicloud_cdm_flavors_v1": dataSourceCdmFlavorV1(),
"huaweicloud_dis_partition_v2": dataSourceDisPartitionV2(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down
29 changes: 29 additions & 0 deletions website/docs/d/compute_availability_zones_v2.html.markdown
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`
3 changes: 3 additions & 0 deletions website/huaweicloud.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
<li<%= sidebar_current("docs-huaweicloud-datasource-cdm-flavors-v1") %>>
<a href="/docs/providers/huaweicloud/d/cdm_flavors_v1.html">huaweicloud_cdm_flavors_v1</a>
</li>
<li<%= sidebar_current("docs-huaweicloud-datasource-compute-availability-zones-v2") %>>
<a href="/docs/providers/huaweicloud/d/compute_availability_zones_v2.html">huaweicloud_compute_availability_zones_v2</a>
</li>
<li<%= sidebar_current("docs-huaweicloud-datasource-csbs-backup-policy-v1") %>>
<a href="/docs/providers/huaweicloud/d/csbs_backup_policy_v1.html">huaweicloud_csbs_backup_policy_v1</a>
</li>
Expand Down

0 comments on commit c159e0e

Please sign in to comment.