diff --git a/huaweicloud/data_source_huaweicloud_compute_availability_zones_v2.go b/huaweicloud/data_source_huaweicloud_compute_availability_zones_v2.go new file mode 100644 index 0000000000..ad18f567e8 --- /dev/null +++ b/huaweicloud/data_source_huaweicloud_compute_availability_zones_v2.go @@ -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 +} diff --git a/huaweicloud/data_source_huaweicloud_compute_availability_zones_v2_test.go b/huaweicloud/data_source_huaweicloud_compute_availability_zones_v2_test.go new file mode 100644 index 0000000000..3419431669 --- /dev/null +++ b/huaweicloud/data_source_huaweicloud_compute_availability_zones_v2_test.go @@ -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" {} +` diff --git a/huaweicloud/provider.go b/huaweicloud/provider.go index 027eedc565..2a3317fbf9 100644 --- a/huaweicloud/provider.go +++ b/huaweicloud/provider.go @@ -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{ diff --git a/website/docs/d/compute_availability_zones_v2.html.markdown b/website/docs/d/compute_availability_zones_v2.html.markdown new file mode 100644 index 0000000000..e4a9258a90 --- /dev/null +++ b/website/docs/d/compute_availability_zones_v2.html.markdown @@ -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` diff --git a/website/huaweicloud.erb b/website/huaweicloud.erb index 9ff5054f7c..cbb53ba78e 100644 --- a/website/huaweicloud.erb +++ b/website/huaweicloud.erb @@ -25,6 +25,9 @@ > huaweicloud_cdm_flavors_v1 + > + huaweicloud_compute_availability_zones_v2 + > huaweicloud_csbs_backup_policy_v1