-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dds): support to get recycle instances
- Loading branch information
Showing
5 changed files
with
321 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
--- | ||
subcategory: "Document Database Service (DDS)" | ||
layout: "huaweicloud" | ||
page_title: "HuaweiCloud: huaweicloud_dds_recycle_instances" | ||
description: |- | ||
Use this data source to get the list of DDS recycle instances. | ||
--- | ||
|
||
# huaweicloud_dds_recycle_instances | ||
|
||
Use this data source to get the list of DDS recycle instances. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "huaweicloud_dds_recycle_instances" "test"{} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `region` - (Optional, String) Specifies the region in which to query the resource. | ||
If omitted, the provider-level region will be used. | ||
|
||
## Attribute Reference | ||
|
||
In addition to all arguments above, the following attributes are exported: | ||
|
||
* `id` - The data source ID. | ||
|
||
* `instances` - Indicates the instances. | ||
|
||
The [instances](#instances_struct) structure is documented below. | ||
|
||
<a name="instances_struct"></a> | ||
The `instances` block supports: | ||
|
||
* `id` - Indicates the instance ID. | ||
|
||
* `name` - Indicates the instance name. | ||
|
||
* `mode` - Indicates the instance mode. | ||
|
||
* `backup_id` - Indicates the backup ID. | ||
|
||
* `datastore` - Indicates the database information. | ||
|
||
The [datastore](#instances_datastore_struct) structure is documented below. | ||
|
||
* `charging_mode` - Indicates the charging mode. | ||
|
||
* `enterprise_project_id` - Indicates the enterprise project ID. | ||
|
||
* `created_at` - Indicates the creation time. | ||
|
||
* `deleted_at` - Indicates the deletion time. | ||
|
||
* `retained_until` - Indicates the retention end time. | ||
|
||
<a name="instances_datastore_struct"></a> | ||
The `datastore` block supports: | ||
|
||
* `version` - Indicates the database version. | ||
|
||
* `type` - Indicates the database type. |
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
44 changes: 44 additions & 0 deletions
44
huaweicloud/services/acceptance/dds/data_source_huaweicloud_dds_recycle_instances_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 dds | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
|
||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" | ||
) | ||
|
||
func TestAccDataSourceDdsRecycleInstances_basic(t *testing.T) { | ||
dataSource := "data.huaweicloud_dds_recycle_instances.test" | ||
dc := acceptance.InitDataSourceCheck(dataSource) | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { | ||
acceptance.TestAccPreCheck(t) | ||
acceptance.TestAccPreCheckDDSRecycleInstancesEnabled(t) | ||
}, | ||
ProviderFactories: acceptance.TestAccProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testDataSourceDdsRecycleInstances_basic, | ||
Check: resource.ComposeTestCheckFunc( | ||
dc.CheckResourceExists(), | ||
resource.TestCheckResourceAttrSet(dataSource, "instances.#"), | ||
resource.TestCheckResourceAttrSet(dataSource, "instances.0.id"), | ||
resource.TestCheckResourceAttrSet(dataSource, "instances.0.name"), | ||
resource.TestCheckResourceAttrSet(dataSource, "instances.0.mode"), | ||
resource.TestCheckResourceAttrSet(dataSource, "instances.0.backup_id"), | ||
resource.TestCheckResourceAttrSet(dataSource, "instances.0.datastore.0.version"), | ||
resource.TestCheckResourceAttrSet(dataSource, "instances.0.datastore.0.type"), | ||
resource.TestCheckResourceAttrSet(dataSource, "instances.0.charging_mode"), | ||
resource.TestCheckResourceAttrSet(dataSource, "instances.0.enterprise_project_id"), | ||
resource.TestCheckResourceAttrSet(dataSource, "instances.0.created_at"), | ||
resource.TestCheckResourceAttrSet(dataSource, "instances.0.deleted_at"), | ||
resource.TestCheckResourceAttrSet(dataSource, "instances.0.retained_until"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
const testDataSourceDdsRecycleInstances_basic = `data "huaweicloud_dds_recycle_instances" "test" {}` |
202 changes: 202 additions & 0 deletions
202
huaweicloud/services/dds/data_source_huaweicloud_dds_recycle_instances.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,202 @@ | ||
package dds | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/hashicorp/go-multierror" | ||
"github.com/hashicorp/go-uuid" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
||
"github.com/chnsz/golangsdk" | ||
|
||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" | ||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" | ||
) | ||
|
||
// @API DDS GET /v3/{project_id}/recycle-instances | ||
func DataSourceDdsRecycleInstances() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataSourceDdsRecycleInstancesRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"region": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
Description: `Specifies the region in which to query the resource. If omitted, the provider-level region will be used.`, | ||
}, | ||
"instances": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Description: `Indicates the instances.`, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `Indicates the instance ID.`, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `Indicates the instance name.`, | ||
}, | ||
"mode": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `Indicates the instance mode.`, | ||
}, | ||
"backup_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `Indicates the backup ID.`, | ||
}, | ||
"datastore": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Description: `Indicates the database information.`, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"version": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `Indicates the database version.`, | ||
}, | ||
"type": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `Indicates the database type.`, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"charging_mode": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `Indicates the charging mode.`, | ||
}, | ||
"enterprise_project_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `Indicates the enterprise project ID.`, | ||
}, | ||
"created_at": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `Indicates the creation time.`, | ||
}, | ||
"deleted_at": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `Indicates the deletion time.`, | ||
}, | ||
"retained_until": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `Indicates the retention end time.`, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceDdsRecycleInstancesRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
cfg := meta.(*config.Config) | ||
region := cfg.GetRegion(d) | ||
client, err := cfg.NewServiceClient("dds", region) | ||
if err != nil { | ||
return diag.Errorf("error creating DDS client: %s", err) | ||
} | ||
|
||
httpUrl := "v3/{project_id}/recycle-instances" | ||
getPath := client.Endpoint + httpUrl | ||
getPath = strings.ReplaceAll(getPath, "{project_id}", client.ProjectID) | ||
getOpt := golangsdk.RequestOpts{ | ||
KeepResponseBody: true, | ||
MoreHeaders: map[string]string{ | ||
"Content-Type": "application/json", | ||
}, | ||
} | ||
|
||
getPath += fmt.Sprintf("?limit=%d", pageLimit) | ||
currentTotal := 0 | ||
rst := make([]map[string]interface{}, 0) | ||
for { | ||
currentPath := getPath + fmt.Sprintf("&offset=%d", currentTotal) | ||
getResp, err := client.Request("GET", currentPath, &getOpt) | ||
if err != nil { | ||
return diag.Errorf("error retrieving DDS recycle insatnces: %s", err) | ||
} | ||
getRespBody, err := utils.FlattenResponse(getResp) | ||
if err != nil { | ||
return diag.Errorf("error flattening response: %s", err) | ||
} | ||
|
||
instances := utils.PathSearch("instances", getRespBody, make([]interface{}, 0)).([]interface{}) | ||
for _, instance := range instances { | ||
rst = append(rst, map[string]interface{}{ | ||
"id": utils.PathSearch("id", instance, nil), | ||
"name": utils.PathSearch("name", instance, nil), | ||
"mode": utils.PathSearch("mode", instance, nil), | ||
"backup_id": utils.PathSearch("backup_id", instance, nil), | ||
"datastore": flatteRecycleInstancesResponseDatastore(instance), | ||
"charging_mode": parseChargingMode(utils.PathSearch("pay_model", instance, "").(string)), | ||
"enterprise_project_id": utils.PathSearch("enterprise_project_id", instance, nil), | ||
"created_at": utils.PathSearch("create_at", instance, nil), | ||
"deleted_at": utils.PathSearch("deleted_at", instance, nil), | ||
"retained_until": utils.PathSearch("retained_until", instance, nil), | ||
}) | ||
} | ||
|
||
// `total_count` means the number of all `instances`, and type is float64. | ||
currentTotal += len(instances) | ||
totalCount := utils.PathSearch("total_count", getRespBody, float64(0)) | ||
if int(totalCount.(float64)) == currentTotal { | ||
break | ||
} | ||
} | ||
|
||
id, err := uuid.GenerateUUID() | ||
if err != nil { | ||
return diag.Errorf("unable to generate ID: %s", err) | ||
} | ||
d.SetId(id) | ||
|
||
mErr := multierror.Append(nil, | ||
d.Set("region", region), | ||
d.Set("instances", rst), | ||
) | ||
return diag.FromErr(mErr.ErrorOrNil()) | ||
} | ||
|
||
func flatteRecycleInstancesResponseDatastore(resp interface{}) []interface{} { | ||
var rst []interface{} | ||
curJson := utils.PathSearch("data_store", resp, nil) | ||
if curJson == nil { | ||
return rst | ||
} | ||
|
||
rst = []interface{}{ | ||
map[string]interface{}{ | ||
"type": utils.PathSearch("type", curJson, nil), | ||
"version": utils.PathSearch("version", curJson, nil), | ||
}, | ||
} | ||
return rst | ||
} | ||
|
||
func parseChargingMode(v string) string { | ||
switch v { | ||
case "0": | ||
return "postPaid" | ||
case "1": | ||
return "prePaid" | ||
default: | ||
return v | ||
} | ||
} |