Skip to content

Commit

Permalink
feat(RDS): add rds slow log files data source (huaweicloud#5024)
Browse files Browse the repository at this point in the history
  • Loading branch information
houpeng80 authored Jun 18, 2024
1 parent e13f4fa commit c209935
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 0 deletions.
47 changes: 47 additions & 0 deletions docs/data-sources/rds_slow_log_files.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
subcategory: "Relational Database Service (RDS)"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_rds_slow_log_files"
description: |-
Use this data source to get the list of RDS slow log files.
---

# huaweicloud_rds_slow_log_files

Use this data source to get the list of RDS slow log files.

## Example Usage

```hcl
variable "instance_id" {}
data "huaweicloud_rds_slow_log_files" "test" {
instance_id = var.instance_id
}
```

## 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.

* `instance_id` - (Required, String) Specifies the ID of the instance.

## Attribute Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The data source ID.

* `files` - Indicates the list of slow log files.

The [files](#files_struct) structure is documented below.

<a name="files_struct"></a>
The `files` block supports:

* `file_name` - Indicates the file name.

* `file_size` - Indicates the file size in bytes.
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,7 @@ func Provider() *schema.Provider {
"huaweicloud_rds_pg_plugin_parameter_values": rds.DataSourceRdsPgPluginParameterValues(),
"huaweicloud_rds_restore_time_ranges": rds.DataSourceRdsRestoreTimeRanges(),
"huaweicloud_rds_extend_log_files": rds.DataSourceRdsExtendLogFiles(),
"huaweicloud_rds_slow_log_files": rds.DataSourceRdsSlowLogFiles(),

"huaweicloud_rms_policy_definitions": rms.DataSourcePolicyDefinitions(),
"huaweicloud_rms_assignment_package_templates": rms.DataSourceTemplates(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package rds

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
)

func TestAccDataSourceRdsSlowLogFiles_basic(t *testing.T) {
dataSource := "data.huaweicloud_rds_slow_log_files.test"
dc := acceptance.InitDataSourceCheck(dataSource)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
},
ProviderFactories: acceptance.TestAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testDataSourceDataSourceRdsSlowLogFiles_basic(),
Check: resource.ComposeTestCheckFunc(
dc.CheckResourceExists(),
resource.TestCheckResourceAttrSet(dataSource, "files.#"),
resource.TestCheckResourceAttrSet(dataSource, "files.0.file_name"),
resource.TestCheckResourceAttrSet(dataSource, "files.0.file_size"),
),
},
},
})
}

func testDataSourceDataSourceRdsSlowLogFiles_basic() string {
return fmt.Sprintf(`
data "huaweicloud_rds_slow_log_files" "test" {
instance_id = "%s"
}
`, acceptance.HW_RDS_INSTANCE_ID)
}
122 changes: 122 additions & 0 deletions huaweicloud/services/rds/data_source_huaweicloud_rds_slow_log_files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Generated by PMS #222
package rds

import (
"context"
"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/tidwall/gjson"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/httphelper"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/schemas"
)

func DataSourceRdsSlowLogFiles() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceRdsSlowLogFilesRead,

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.`,
},
"instance_id": {
Type: schema.TypeString,
Required: true,
Description: `Specifies the ID of the instance.`,
},
"files": {
Type: schema.TypeList,
Computed: true,
Description: `Indicates the list of slow log files.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"file_name": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the file name.`,
},
"file_size": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the file size in bytes.`,
},
},
},
},
},
}
}

type SlowLogFilesDSWrapper struct {
*schemas.ResourceDataWrapper
Config *config.Config
}

func newSlowLogFilesDSWrapper(d *schema.ResourceData, meta interface{}) *SlowLogFilesDSWrapper {
return &SlowLogFilesDSWrapper{
ResourceDataWrapper: schemas.NewSchemaWrapper(d),
Config: meta.(*config.Config),
}
}

func dataSourceRdsSlowLogFilesRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
wrapper := newSlowLogFilesDSWrapper(d, meta)
listSlowLogFileRst, err := wrapper.ListSlowLogFile()
if err != nil {
return diag.FromErr(err)
}

id, err := uuid.GenerateUUID()
if err != nil {
return diag.FromErr(err)
}
d.SetId(id)

err = wrapper.listSlowLogFileToSchema(listSlowLogFileRst)
if err != nil {
return diag.FromErr(err)
}

return nil
}

// @API RDS GET /v3/{project_id}/instances/{instance_id}/slowlog-files
func (w *SlowLogFilesDSWrapper) ListSlowLogFile() (*gjson.Result, error) {
client, err := w.NewClient(w.Config, "rds")
if err != nil {
return nil, err
}

uri := "/v3/{project_id}/instances/{instance_id}/slowlog-files"
uri = strings.ReplaceAll(uri, "{instance_id}", w.Get("instance_id").(string))
return httphelper.New(client).
Method("GET").
URI(uri).
OffsetPager("list", "offset", "limit", 0).
Request().
Result()
}

func (w *SlowLogFilesDSWrapper) listSlowLogFileToSchema(body *gjson.Result) error {
d := w.ResourceData
mErr := multierror.Append(nil,
d.Set("region", w.Config.GetRegion(w.ResourceData)),
d.Set("files", schemas.SliceToList(body.Get("list"),
func(files gjson.Result) any {
return map[string]any{
"file_name": files.Get("file_name").Value(),
"file_size": files.Get("file_size").Value(),
}
},
)),
)
return mErr.ErrorOrNil()
}

0 comments on commit c209935

Please sign in to comment.