-
Notifications
You must be signed in to change notification settings - Fork 565
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add alicloud_adb_db_cluster_classes datasource to search ADB availabl…
…e classes.
- Loading branch information
Showing
11 changed files
with
240 additions
and
16 deletions.
There are no files selected for viewing
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
127 changes: 127 additions & 0 deletions
127
alicloud/data_source_alicloud_adb_db_cluster_classes.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,127 @@ | ||
package alicloud | ||
|
||
import ( | ||
"github.com/PaesslerAG/jsonpath" | ||
util "github.com/alibabacloud-go/tea-utils/service" | ||
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/validation" | ||
) | ||
|
||
func dataSourceAlicloudAdbDbClusterClasses() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceAlicloudAdbDbClusterClassesRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"zone_id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"payment_type": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Default: "PayAsYouGo", | ||
ValidateFunc: validation.StringInSlice([]string{"PayAsYouGo", "Subscription"}, false), | ||
}, | ||
"output_file": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"available_zone_list": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"zone_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"classes": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceAlicloudAdbDbClusterClassesRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*connectivity.AliyunClient) | ||
|
||
action := "DescribeAvailableResource" | ||
request := make(map[string]interface{}) | ||
request["RegionId"] = client.RegionId | ||
|
||
if v, ok := d.GetOk("zone_id"); ok { | ||
request["ZoneId"] = v.(string) | ||
} | ||
if v, ok := d.GetOk("payment_type"); ok { | ||
request["payment_type"] = convertAdbDbClusterDBClusterPaymentTypeRequest(v.(string)) | ||
} | ||
|
||
response := make(map[string]interface{}) | ||
conn, err := client.NewAdsClient() | ||
runtime := util.RuntimeOptions{} | ||
runtime.SetAutoretry(true) | ||
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2019-03-15"), StringPointer("AK"), nil, request, &runtime) | ||
if err != nil { | ||
return WrapErrorf(err, DataDefaultErrorMsg, "alicloud_adb_db_cluster_classes", action, AlibabaCloudSdkGoERROR) | ||
} | ||
addDebug(action, response, request) | ||
|
||
resp, err := jsonpath.Get("$.AvailableZoneList", response) | ||
if err != nil { | ||
return WrapErrorf(err, FailedGetAttributeMsg, action, "$.AvailableZoneList", response) | ||
} | ||
|
||
result := resp.([]interface{}) | ||
objects := make([]map[string]interface{}, 0) | ||
zoneIds := make([]string, 0) | ||
s := make([]map[string]interface{}, 0) | ||
for _, v := range result { | ||
item := v.(map[string]interface{}) | ||
objects = append(objects, item) | ||
} | ||
|
||
for _, object := range objects { | ||
classes := make([]string, 0) | ||
mapping := map[string]interface{}{ | ||
"zone_id": object["ZoneId"], | ||
} | ||
zoneIds = append(zoneIds, object["ZoneId"].(string)) | ||
if supportedMode, ok := object["SupportedMode"]; ok { | ||
if supportedModes, ok := supportedMode.([]interface{}); ok { | ||
for _, supportedMode := range supportedModes { | ||
if supportedSerialList, ok := supportedMode.(map[string]interface{})["SupportedSerialList"]; ok { | ||
if supportedSerials, ok := supportedSerialList.([]interface{}); ok { | ||
for _, supportedSerial := range supportedSerials { | ||
if supportedInstanceClassList, ok := supportedSerial.(map[string]interface{})["SupportedInstanceClassList"]; ok { | ||
if supportedInstanceClasses, ok := supportedInstanceClassList.([]interface{}); ok { | ||
for _, supportedInstanceClass := range supportedInstanceClasses { | ||
classes = append(classes, supportedInstanceClass.(map[string]interface{})["InstanceClass"].(string)) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
mapping["classes"] = classes | ||
s = append(s, mapping) | ||
} | ||
|
||
d.SetId(dataResourceIdHash(zoneIds)) | ||
if err := d.Set("available_zone_list", s); err != nil { | ||
return WrapError(err) | ||
} | ||
|
||
if output, ok := d.GetOk("output_file"); ok && output.(string) != "" { | ||
writeToFile(output.(string), s) | ||
} | ||
return nil | ||
} |
44 changes: 44 additions & 0 deletions
44
alicloud/data_source_alicloud_adb_db_cluster_classes_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,44 @@ | ||
package alicloud | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest" | ||
) | ||
|
||
func TestAccAlicloudAdbDbClusterClassesDataSource_basic(t *testing.T) { | ||
rand := acctest.RandInt() | ||
resourceId := "data.alicloud_adb_db_cluster_classes.default" | ||
|
||
testAccConfig := dataSourceTestAccConfigFunc(resourceId, "", dataSourceAdbDbClusterClassesConfigDependence) | ||
|
||
paymentTypeConfig := dataSourceTestAccConfig{ | ||
existConfig: testAccConfig(map[string]interface{}{ | ||
"payment_type": "PayAsYouGo", | ||
}), | ||
} | ||
|
||
var existAdbZonesMapFunc = func(rand int) map[string]string { | ||
return map[string]string{ | ||
"available_zone_list.#": CHECKSET, | ||
} | ||
} | ||
|
||
var fakeAdbZonesMapFunc = func(rand int) map[string]string { | ||
return map[string]string{ | ||
"available_zone_list.#": "0", | ||
} | ||
} | ||
|
||
var adbZonesCheckInfo = dataSourceAttr{ | ||
resourceId: resourceId, | ||
existMapFunc: existAdbZonesMapFunc, | ||
fakeMapFunc: fakeAdbZonesMapFunc, | ||
} | ||
|
||
adbZonesCheckInfo.dataSourceTestCheck(t, rand, paymentTypeConfig) | ||
} | ||
|
||
func dataSourceAdbDbClusterClassesConfigDependence(name string) string { | ||
return "" | ||
} |
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
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
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
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
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
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
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
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,41 @@ | ||
--- | ||
subcategory: "AnalyticDB for MySQL (ADB)" | ||
layout: "alicloud" | ||
page_title: "Alicloud: alicloud_adb_db_cluster_classes" | ||
sidebar_current: "docs-alicloud-datasource-adb-db-cluster-classes" | ||
description: |- | ||
Provides a list of available cluster classes for ADB that can be used by an Alibaba Cloud account. | ||
--- | ||
|
||
# alicloud\_adb\_db\_cluster\_classes | ||
|
||
This data source provides available cluster classes for ADB that can be accessed by an Alibaba Cloud account within the region configured in the provider. | ||
|
||
-> **NOTE:** Available in v1.122.0+. | ||
|
||
## Example Usage | ||
|
||
``` | ||
# Declare the data source | ||
data "alicloud_adb_db_cluster_classes" "availabile_classes" {} | ||
output "classes" { | ||
value = alicloud_adb_db_cluster_classes.availabile_classes.available_zone_list[0].classes[0] | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `zone_id` - (Optional) The zone ID of the resource. | ||
* `payment_type` - (Optional) The payment type of the resource. Valid values are `PayAsYouGo` and `Subscription`. Default to `PayAsYouGo`. | ||
* `output_file` - (Optional) File name where to save data source results (after running `terraform plan`). | ||
|
||
## Attributes Reference | ||
|
||
The following attributes are exported in addition to the arguments listed above: | ||
|
||
* `available_zone_list` - A list of availability zones. Each element contains the following attributes: | ||
* `zone_id` - ID of the zone. | ||
* `classes` - The available classes of zone. |