Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

datasource/alicloud_adb_db_clusters: Adds three internal parameters current_pagepage_size total_count to support paging; datasource/alicloud_db_instances: Adds three internal parameters current_pagepage_size total_count to support paging; datasource/alicloud_route_tables: Adds three internal parameters current_pagepage_size total_count to support paging; #4542

Merged
merged 1 commit into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 34 additions & 5 deletions alicloud/data_source_alicloud_adb_db_clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ func dataSourceAlicloudAdbDbClusters() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"page_number": {
Type: schema.TypeInt,
Optional: true,
},
"page_size": {
Type: schema.TypeInt,
Optional: true,
Default: 100,
},
"clusters": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -241,6 +250,10 @@ func dataSourceAlicloudAdbDbClusters() *schema.Resource {
Optional: true,
Default: false,
},
"total_count": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -270,9 +283,17 @@ func dataSourceAlicloudAdbDbClustersRead(d *schema.ResourceData, meta interface{
}
request["Tag.*"] = tags
}
request["PageSize"] = PageSizeLarge
request["PageNumber"] = 1
var objects []map[string]interface{}
if v, ok := d.GetOk("page_number"); ok && v.(int) > 0 {
request["PageNumber"] = v.(int)
} else {
request["PageNumber"] = 1
}
if v, ok := d.GetOk("page_size"); ok && v.(int) > 0 {
request["PageSize"] = v.(int)
} else {
request["PageSize"] = PageSizeLarge
}
var objects []interface{}
var descriptionRegex *regexp.Regexp
if v, ok := d.GetOk("description_regex"); ok {
r, err := regexp.Compile(v.(string))
Expand Down Expand Up @@ -310,6 +331,10 @@ func dataSourceAlicloudAdbDbClustersRead(d *schema.ResourceData, meta interface{
return WrapErrorf(err, FailedGetAttributeMsg, action, "$.Items.DBCluster", response)
}
result, _ := resp.([]interface{})
if isPagingRequest(d) {
objects = result
break
}
for _, v := range result {
item := v.(map[string]interface{})
if descriptionRegex != nil {
Expand All @@ -324,15 +349,16 @@ func dataSourceAlicloudAdbDbClustersRead(d *schema.ResourceData, meta interface{
}
objects = append(objects, item)
}
if len(result) < PageSizeLarge {
if len(result) < request["PageSize"].(int) {
break
}
request["PageNumber"] = request["PageNumber"].(int) + 1
}
descriptions := make([]string, 0)
ids := make([]string, 0)
s := make([]map[string]interface{}, 0)
for _, object := range objects {
for _, v := range objects {
object := v.(map[string]interface{})
mapping := map[string]interface{}{
"commodity_code": object["CommodityCode"],
"compute_resource": object["ComputeResource"],
Expand Down Expand Up @@ -431,6 +457,9 @@ func dataSourceAlicloudAdbDbClustersRead(d *schema.ResourceData, meta interface{
if err := d.Set("clusters", s); err != nil {
return WrapError(err)
}
if err := d.Set("total_count", formatInt(response["TotalCount"])); err != nil {
return WrapError(err)
}
if output, ok := d.GetOk("output_file"); ok && output.(string) != "" {
writeToFile(output.(string), s)
}
Expand Down
1 change: 1 addition & 0 deletions alicloud/data_source_alicloud_adb_db_clusters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func TestAccAlicloudAdbDbClustersDataSource(t *testing.T) {
"ids.#": "1",
"descriptions.#": "1",
"clusters.#": "1",
"total_count": CHECKSET,
"clusters.0.id": CHECKSET,
"clusters.0.description": CHECKSET,
"clusters.0.payment_type": "PayAsYouGo",
Expand Down
49 changes: 38 additions & 11 deletions alicloud/data_source_alicloud_db_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,15 @@ func dataSourceAlicloudDBInstances() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},

"page_number": {
Type: schema.TypeInt,
Optional: true,
},
"page_size": {
Type: schema.TypeInt,
Optional: true,
Default: 100,
},
// Computed values
"names": {
Type: schema.TypeList,
Expand Down Expand Up @@ -339,6 +347,10 @@ func dataSourceAlicloudDBInstances() *schema.Resource {
},
},
},
"total_count": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}
Expand All @@ -347,10 +359,18 @@ func dataSourceAlicloudDBInstancesRead(d *schema.ResourceData, meta interface{})
client := meta.(*connectivity.AliyunClient)
action := "DescribeDBInstances"
request := map[string]interface{}{
"RegionId": client.RegionId,
"SourceIp": client.SourceIp,
"PageSize": PageSizeLarge,
"PageNumber": 1,
"RegionId": client.RegionId,
"SourceIp": client.SourceIp,
}
if v, ok := d.GetOk("page_number"); ok && v.(int) > 0 {
request["PageNumber"] = v.(int)
} else {
request["PageNumber"] = 1
}
if v, ok := d.GetOk("page_size"); ok && v.(int) > 0 {
request["PageSize"] = v.(int)
} else {
request["PageSize"] = PageSizeLarge
}
if v, ok := d.GetOk("engine"); ok && v.(string) != "" {
request["Engine"] = v.(string)
Expand Down Expand Up @@ -385,7 +405,7 @@ func dataSourceAlicloudDBInstancesRead(d *schema.ResourceData, meta interface{})
return WrapError(err)
}

var objects []map[string]interface{}
var objects []interface{}

var nameRegex *regexp.Regexp
if v, ok := d.GetOk("name_regex"); ok {
Expand Down Expand Up @@ -431,6 +451,10 @@ func dataSourceAlicloudDBInstancesRead(d *schema.ResourceData, meta interface{})
return WrapErrorf(err, FailedGetAttributeMsg, action, "$.Items.DBInstance", response)
}
result, _ := resp.([]interface{})
if isPagingRequest(d) {
objects = result
break
}
for _, v := range result {
item := v.(map[string]interface{})
if nameRegex != nil {
Expand All @@ -445,23 +469,24 @@ func dataSourceAlicloudDBInstancesRead(d *schema.ResourceData, meta interface{})
}
objects = append(objects, item)
}
if len(result) < PageSizeLarge {
if len(result) < request["PageSize"].(int) {
break
}
request["PageNumber"] = request["PageNumber"].(int) + 1
}
return rdsInstancesDescription(d, meta, objects)
return rdsInstancesDescription(d, meta, objects, formatInt(response["TotalRecordCount"]))
}

func rdsInstancesDescription(d *schema.ResourceData, meta interface{}, objects []map[string]interface{}) error {
func rdsInstancesDescription(d *schema.ResourceData, meta interface{}, objects []interface{}, totalCount int) error {
client := meta.(*connectivity.AliyunClient)
rdsService := RdsService{client}

var ids []string
var names []string
var s []map[string]interface{}

for _, item := range objects {
for _, v := range objects {
item := v.(map[string]interface{})
readOnlyInstanceIDs := []string{}
for _, id := range item["ReadOnlyDBInstanceIds"].(map[string]interface{})["ReadOnlyDBInstanceId"].([]interface{}) {
readOnlyInstanceIDs = append(readOnlyInstanceIDs, fmt.Sprint(id.(map[string]interface{})["DBInstanceId"]))
Expand Down Expand Up @@ -623,7 +648,9 @@ func rdsInstancesDescription(d *schema.ResourceData, meta interface{}, objects [
if err := d.Set("names", names); err != nil {
return WrapError(err)
}

if err := d.Set("total_count", totalCount); err != nil {
return WrapError(err)
}
// create a json file in current directory and write data source to it
if output, ok := d.GetOk("output_file"); ok && output.(string) != "" {
writeToFile(output.(string), s)
Expand Down
2 changes: 2 additions & 0 deletions alicloud/data_source_alicloud_db_instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func TestAccAlicloudRdsDBInstancesDataSource(t *testing.T) {
"ids.#": "1",
"names.#": "1",
"instances.#": "1",
"total_count": CHECKSET,
"instances.0.id": CHECKSET,
"instances.0.name": fmt.Sprintf("tf-testAccDBInstanceConfig_%d", rand),
"instances.0.db_type": CHECKSET,
Expand Down Expand Up @@ -269,6 +270,7 @@ func TestAccAlicloudRdsDBInstancesDataSourcePostgreSQLSSL(t *testing.T) {
"ids.#": "1",
"names.#": "1",
"instances.#": "1",
"total_count": CHECKSET,
"instances.0.id": CHECKSET,
"instances.0.name": fmt.Sprintf("tf-testAccDBInstanceConfig_%d", rand),
"instances.0.db_type": CHECKSET,
Expand Down
39 changes: 34 additions & 5 deletions alicloud/data_source_alicloud_route_tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ func dataSourceAlicloudRouteTables() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"page_number": {
Type: schema.TypeInt,
Optional: true,
},
"page_size": {
Type: schema.TypeInt,
Optional: true,
Default: 50,
},
"tables": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -131,6 +140,10 @@ func dataSourceAlicloudRouteTables() *schema.Resource {
},
},
},
"total_count": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -166,9 +179,17 @@ func dataSourceAlicloudRouteTablesRead(d *schema.ResourceData, meta interface{})
if v, ok := d.GetOk("vpc_id"); ok {
request["VpcId"] = v
}
request["PageSize"] = PageSizeLarge
request["PageNumber"] = 1
var objects []map[string]interface{}
if v, ok := d.GetOk("page_number"); ok && v.(int) > 0 {
request["PageNumber"] = v.(int)
} else {
request["PageNumber"] = 1
}
if v, ok := d.GetOk("page_size"); ok && v.(int) > 0 {
request["PageSize"] = v.(int)
} else {
request["PageSize"] = PageSizeLarge
}
var objects []interface{}
var routeTableNameRegex *regexp.Regexp
if v, ok := d.GetOk("name_regex"); ok {
r, err := regexp.Compile(v.(string))
Expand Down Expand Up @@ -207,6 +228,10 @@ func dataSourceAlicloudRouteTablesRead(d *schema.ResourceData, meta interface{})
return WrapErrorf(err, FailedGetAttributeMsg, action, "$.RouterTableList.RouterTableListType", response)
}
result, _ := resp.([]interface{})
if isPagingRequest(d) {
objects = result
break
}
for _, v := range result {
item := v.(map[string]interface{})
if routeTableNameRegex != nil {
Expand All @@ -224,15 +249,16 @@ func dataSourceAlicloudRouteTablesRead(d *schema.ResourceData, meta interface{})
}
objects = append(objects, item)
}
if len(result) < PageSizeLarge {
if len(result) < request["PageSize"].(int) {
break
}
request["PageNumber"] = request["PageNumber"].(int) + 1
}
ids := make([]string, 0)
names := make([]interface{}, 0)
s := make([]map[string]interface{}, 0)
for _, object := range objects {
for _, v := range objects {
object := v.(map[string]interface{})
mapping := map[string]interface{}{
"description": object["Description"],
"resource_group_id": object["ResourceGroupId"],
Expand Down Expand Up @@ -277,6 +303,9 @@ func dataSourceAlicloudRouteTablesRead(d *schema.ResourceData, meta interface{})
if err := d.Set("tables", s); err != nil {
return WrapError(err)
}
if err := d.Set("total_count", formatInt(response["TotalCount"])); err != nil {
return WrapError(err)
}
if output, ok := d.GetOk("output_file"); ok && output.(string) != "" {
writeToFile(output.(string), s)
}
Expand Down
18 changes: 17 additions & 1 deletion alicloud/data_source_alicloud_route_tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,24 +86,39 @@ func TestAccAlicloudVpcRouteTablesDataSourceBasic(t *testing.T) {
}),
}

pagingConf := dataSourceTestAccConfig{
existConfig: testAccCheckAlicloudRouteTablesDataSourceConfigBaisc(rand, map[string]string{
"route_table_name": `"${alicloud_route_table.default.name}"`,
"page_number": `1`,
}),
fakeConfig: testAccCheckAlicloudRouteTablesDataSourceConfigBaisc(rand, map[string]string{
"route_table_name": `"${alicloud_route_table.default.name}"`,
"page_number": `2`,
}),
}

allConfig := dataSourceTestAccConfig{
existConfig: testAccCheckAlicloudRouteTablesDataSourceConfigBaisc(rand, map[string]string{
"name_regex": `"${alicloud_route_table.default.name}"`,
"vpc_id": `"${alicloud_vpc.default.id}"`,
"ids": `[ "${alicloud_route_table.default.id}" ]`,
"resource_group_id": `""`,
"status": `"Available"`,
"route_table_name": `"${alicloud_route_table.default.name}"`,
"page_number": `1`,
}),
fakeConfig: testAccCheckAlicloudRouteTablesDataSourceConfigBaisc(rand, map[string]string{
"name_regex": `"${alicloud_route_table.default.name}_fake"`,
"vpc_id": `"${alicloud_vpc.default.id}"`,
"ids": `[ "${alicloud_route_table.default.id}" ]`,
"resource_group_id": `""`,
"status": `"Pending"`,
"route_table_name": `"${alicloud_route_table.default.name}_fake"`,
"page_number": `2`,
}),
}

routeTablesCheckInfo.dataSourceTestCheckWithPreCheck(t, rand, preCheck, nameRegexConfig, statusConfig, vpcIdConfig, idsConfig, tagsConfig, resourceGroupIdConfig, allConfig)
routeTablesCheckInfo.dataSourceTestCheckWithPreCheck(t, rand, preCheck, nameRegexConfig, statusConfig, vpcIdConfig, idsConfig, tagsConfig, resourceGroupIdConfig, pagingConf, allConfig)
}

func testAccCheckAlicloudRouteTablesDataSourceConfigBaisc(rand int, attrMap map[string]string) string {
Expand Down Expand Up @@ -144,6 +159,7 @@ var existRouteTablesMapFunc = func(rand int) map[string]string {
"ids.#": "1",
"names.#": "1",
"tables.#": "1",
"total_count": CHECKSET,
"tables.0.id": CHECKSET,
"tables.0.route_table_type": CHECKSET,
"tables.0.router_id": CHECKSET,
Expand Down