forked from huaweicloud/terraform-provider-huaweicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(RDS): add rds slow log files data source (huaweicloud#5024)
- Loading branch information
Showing
4 changed files
with
211 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,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. |
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
41 changes: 41 additions & 0 deletions
41
huaweicloud/services/acceptance/rds/data_source_huaweicloud_rds_slow_log_files_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,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
122
huaweicloud/services/rds/data_source_huaweicloud_rds_slow_log_files.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,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() | ||
} |