2017-01-19 15:08:56 +01:00
|
|
|
package alicloud
|
|
|
|
|
|
|
|
import (
|
2017-03-24 12:04:56 +01:00
|
|
|
"fmt"
|
2017-01-19 15:08:56 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2017-03-24 12:04:56 +01:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"strconv"
|
2017-01-19 15:08:56 +01:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccAlicloudZonesDataSource_basic(t *testing.T) {
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() {
|
|
|
|
testAccPreCheck(t)
|
|
|
|
},
|
|
|
|
Providers: testAccProviders,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
Config: testAccCheckAlicloudZonesDataSourceBasicConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAlicloudDataSourceID("data.alicloud_zones.foo"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAlicloudZonesDataSource_filter(t *testing.T) {
|
2017-03-24 12:04:56 +01:00
|
|
|
|
2017-01-19 15:08:56 +01:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() {
|
|
|
|
testAccPreCheck(t)
|
|
|
|
},
|
|
|
|
Providers: testAccProviders,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
Config: testAccCheckAlicloudZonesDataSourceFilter,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAlicloudDataSourceID("data.alicloud_zones.foo"),
|
2017-03-24 12:04:56 +01:00
|
|
|
testCheckZoneLength("data.alicloud_zones.foo"),
|
2017-01-19 15:08:56 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccCheckAlicloudZonesDataSourceFilterIoOptimized,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAlicloudDataSourceID("data.alicloud_zones.foo"),
|
2017-03-24 12:04:56 +01:00
|
|
|
testCheckZoneLength("data.alicloud_zones.foo"),
|
2017-01-19 15:08:56 +01:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-03-24 12:04:56 +01:00
|
|
|
func TestAccAlicloudZonesDataSource_unitRegion(t *testing.T) {
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() {
|
|
|
|
testAccPreCheck(t)
|
|
|
|
},
|
|
|
|
Providers: testAccProviders,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
Config: testAccCheckAlicloudZonesDataSource_unitRegion,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAlicloudDataSourceID("data.alicloud_zones.foo"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// the zone length changed occasionally
|
|
|
|
// check by range to avoid test case failure
|
|
|
|
func testCheckZoneLength(name string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
ms := s.RootModule()
|
|
|
|
rs, ok := ms.Resources[name]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
is := rs.Primary
|
|
|
|
if is == nil {
|
|
|
|
return fmt.Errorf("No primary instance: %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
i, err := strconv.Atoi(is.Attributes["zones.#"])
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("convert zone length err: %#v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if i <= 0 {
|
|
|
|
return fmt.Errorf("zone length expected greater than 0 got err: %d", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-19 15:08:56 +01:00
|
|
|
const testAccCheckAlicloudZonesDataSourceBasicConfig = `
|
|
|
|
data "alicloud_zones" "foo" {
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const testAccCheckAlicloudZonesDataSourceFilter = `
|
|
|
|
data "alicloud_zones" "foo" {
|
2017-03-24 12:04:56 +01:00
|
|
|
available_instance_type= "ecs.c2.xlarge"
|
|
|
|
available_resource_creation= "VSwitch"
|
|
|
|
available_disk_category= "cloud_efficiency"
|
2017-01-19 15:08:56 +01:00
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const testAccCheckAlicloudZonesDataSourceFilterIoOptimized = `
|
|
|
|
data "alicloud_zones" "foo" {
|
2017-03-24 12:04:56 +01:00
|
|
|
available_instance_type= "ecs.c2.xlarge"
|
|
|
|
available_resource_creation= "IoOptimized"
|
|
|
|
available_disk_category= "cloud"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const testAccCheckAlicloudZonesDataSource_unitRegion = `
|
|
|
|
provider "alicloud" {
|
|
|
|
alias = "northeast"
|
|
|
|
region = "ap-northeast-1"
|
|
|
|
}
|
|
|
|
|
|
|
|
data "alicloud_zones" "foo" {
|
|
|
|
provider = "alicloud.northeast"
|
|
|
|
available_resource_creation= "VSwitch"
|
2017-01-19 15:08:56 +01:00
|
|
|
}
|
|
|
|
`
|