Skip to content

Commit

Permalink
feat(dds): support to get recycle instances
Browse files Browse the repository at this point in the history
  • Loading branch information
saf3dfsa committed Oct 31, 2024
1 parent 453ca67 commit ca94ea4
Show file tree
Hide file tree
Showing 5 changed files with 320 additions and 0 deletions.
66 changes: 66 additions & 0 deletions docs/data-sources/dds_recycle_instances.md
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.
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,7 @@ func Provider() *schema.Provider {
"huaweicloud_dds_database_users": dds.DateSourceDDSDatabaseUser(),
"huaweicloud_dds_storage_types": dds.DataSourceDdsStorageTypes(),
"huaweicloud_dds_restore_time_ranges": dds.DataSourceDdsRestoreTimeRanges(),
"huaweicloud_dds_recycle_instances": dds.DataSourceDdsRecycleInstances(),
"huaweicloud_dds_backups": dds.DataSourceDDSBackups(),
"huaweicloud_dds_database_roles": dds.DateSourceDDSDatabaseRoles(),
"huaweicloud_dds_error_logs": dds.DataSourceDDSErrorLogs(),
Expand Down
8 changes: 8 additions & 0 deletions huaweicloud/services/acceptance/acceptance.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ var (
HW_DDS_INSTANCE_ID = os.Getenv("HW_DDS_INSTANCE_ID")
HW_DDS_START_TIME = os.Getenv("HW_DDS_START_TIME")
HW_DDS_END_TIME = os.Getenv("HW_DDS_END_TIME")
HW_DDS_RECYCLE_INSTANCES_ENABLED = os.Getenv("HW_DDS_RECYCLE_INSTANCES_ENABLED")

HW_RDS_CROSS_REGION_BACKUP_INSTANCE_ID = os.Getenv("HW_RDS_CROSS_REGION_BACKUP_INSTANCE_ID")
HW_RDS_INSTANCE_ID = os.Getenv("HW_RDS_INSTANCE_ID")
Expand Down Expand Up @@ -2319,6 +2320,13 @@ func TestAccPreCheckDDSTimeRange(t *testing.T) {
}
}

// lintignore:AT003
func TestAccPreCheckDDSRecycleInstancesEnabled(t *testing.T) {
if HW_DDS_RECYCLE_INSTANCES_ENABLED == "" {
t.Skip("HW_DDS_RECYCLE_INSTANCES_ENABLED must be set for the acceptance test")
}
}

// lintignore:AT003
func TestAccPreCheckRdsCrossRegionBackupInstanceId(t *testing.T) {
if HW_RDS_CROSS_REGION_BACKUP_INSTANCE_ID == "" {
Expand Down
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" {}`
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
package dds

import (
"context"
"fmt"
"strings"

"github.com/chnsz/golangsdk"

Check failure on line 8 in huaweicloud/services/dds/data_source_huaweicloud_dds_recycle_instances.go

View workflow job for this annotation

GitHub Actions / golangci

File is not `gci`-ed with -s standard -s default -s prefix(github.com/chnsz/golangsdk) -s prefix(github.com/huaweicloud/huaweicloud-sdk-go-v3) -s prefix(github.com/huaweicloud/terraform-provider-huaweicloud) -s blank -s dot --custom-order (gci)
"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"

Check failure on line 13 in huaweicloud/services/dds/data_source_huaweicloud_dds_recycle_instances.go

View workflow job for this annotation

GitHub Actions / golangci

File is not `gci`-ed with -s standard -s default -s prefix(github.com/chnsz/golangsdk) -s prefix(github.com/huaweicloud/huaweicloud-sdk-go-v3) -s prefix(github.com/huaweicloud/terraform-provider-huaweicloud) -s blank -s dot --custom-order (gci)
"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
}
}

0 comments on commit ca94ea4

Please sign in to comment.