Skip to content

Commit

Permalink
feat(GaussDBforMySQL): add gaussdb mysql slow logs data source (#5777)
Browse files Browse the repository at this point in the history
  • Loading branch information
houpeng80 authored Oct 30, 2024
1 parent 3995e5e commit d1857c5
Show file tree
Hide file tree
Showing 5 changed files with 427 additions and 0 deletions.
84 changes: 84 additions & 0 deletions docs/data-sources/gaussdb_mysql_slow_logs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
---
subcategory: "GaussDB(for MySQL)"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_gaussdb_mysql_slow_logs"
description: |-
Use this data source to get the list of GaussDB MySQL slow logs.
---

# huaweicloud_gaussdb_mysql_slow_logs

Use this data source to get the list of GaussDB MySQL slow logs.

## Example Usage

```hcl
variable "instance_id" {}
variable "node_id" {}
variable "start_time" {}
variable "end_time" {}
data "huaweicloud_gaussdb_mysql_slow_logs" "test" {
instance_id = var.instance_id
node_id = var.node_id
start_time = var.start_time
end_time = var.end_time
}
```

## 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 GaussDB MySQL instance.

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

* `start_time` - (Required, String) Specifies the start time in the **yyyy-mm-ddThh:mm:ssZ** format.

* `end_time` - (Required, String) Specifies the end time in the **yyyy-mm-ddThh:mm:ssZ** format.

* `operate_type` - (Optional, String) Specifies the SQL statement type. Value options: **INSERT**, **UPDATE**, **SELECT**,
**DELETE**, **CREATE**, **ALTER**, **DROP**.

* `database` - (Optional, String) Specifies the name of the database.

## Attribute Reference

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

* `id` - The data source ID.

* `slow_log_list` - Indicates the list of the slow logs.

The [slow_log_list](#slow_log_list_struct) structure is documented below.

<a name="slow_log_list_struct"></a>
The `slow_log_list` block supports:

* `count` - Indicates the ID of the instance node.

* `count` - Indicates the number of executions.

* `time` - Indicates the execution time.

* `lock_time` - Indicates the lock wait time.

* `rows_sent` - Indicates the number of sent rows.

* `rows_examined` - Indicates the number of scanned rows.

* `database` - Indicates the database that slow query logs belong to.

* `users` - Indicates the name of the account.

* `query_sample` - Indicates the execution syntax.

* `type` - Indicates the statement type.

* `start_time` - Indicates the start time in the **yyyy-mm-ddThh:mm:ssZ** format.

* `client_ip` - Indicates the IP address of the client.
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,7 @@ func Provider() *schema.Provider {
"huaweicloud_gaussdb_mysql_restored_tables": gaussdb.DataSourceGaussdbMysqlRestoredTables(),
"huaweicloud_gaussdb_mysql_project_quotas": gaussdb.DataSourceGaussdbMysqlProjectQuotas(),
"huaweicloud_gaussdb_mysql_scheduled_tasks": gaussdb.DataSourceGaussDBMysqlScheduledTasks(),
"huaweicloud_gaussdb_mysql_slow_logs": gaussdb.DataSourceGaussDBMysqlSlowLogs(),

"huaweicloud_gaussdb_influx_instances": gaussdb.DataSourceGaussDBInfluxInstances(),

Expand Down
17 changes: 17 additions & 0 deletions huaweicloud/services/acceptance/acceptance.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,15 @@ var (
HW_BUILD_IMAGE_URL_UPDATED = os.Getenv("HW_BUILD_IMAGE_URL_UPDATED") // SWR Image URL for component deployment update

HW_GAUSSDB_MYSQL_INSTANCE_ID = os.Getenv("HW_GAUSSDB_MYSQL_INSTANCE_ID")
HW_GAUSSDB_MYSQL_NODE_ID = os.Getenv("HW_GAUSSDB_MYSQL_NODE_ID")
HW_GAUSSDB_MYSQL_DATABASE_NAME = os.Getenv("HW_GAUSSDB_MYSQL_DATABASE_NAME")
HW_GAUSSDB_MYSQL_TABLE_NAME = os.Getenv("HW_GAUSSDB_MYSQL_TABLE_NAME")
HW_GAUSSDB_MYSQL_INSTANCE_CONFIGURATION_ID = os.Getenv("HW_GAUSSDB_MYSQL_INSTANCE_CONFIGURATION_ID")
HW_GAUSSDB_MYSQL_BACKUP_BEGIN_TIME = os.Getenv("HW_GAUSSDB_MYSQL_BACKUP_BEGIN_TIME")
HW_GAUSSDB_MYSQL_BACKUP_END_TIME = os.Getenv("HW_GAUSSDB_MYSQL_BACKUP_END_TIME")
HW_GAUSSDB_MYSQL_JOB_ID = os.Getenv("HW_GAUSSDB_MYSQL_JOB_ID")
HW_GAUSSDB_MYSQL_START_TIME = os.Getenv("HW_GAUSSDB_MYSQL_START_TIME")
HW_GAUSSDB_MYSQL_END_TIME = os.Getenv("HW_GAUSSDB_MYSQL_END_TIME")

HW_VOD_WATERMARK_FILE = os.Getenv("HW_VOD_WATERMARK_FILE")
HW_VOD_MEDIA_ASSET_FILE = os.Getenv("HW_VOD_MEDIA_ASSET_FILE")
Expand Down Expand Up @@ -1205,6 +1208,13 @@ func TestAccPreCheckGaussDBMysqlInstanceId(t *testing.T) {
}
}

// lintignore:AT003
func TestAccPreCheckGaussDBMysqlNodeId(t *testing.T) {
if HW_GAUSSDB_MYSQL_NODE_ID == "" {
t.Skip("HW_GAUSSDB_MYSQL_NODE_ID must be set for GaussDB MySQL acceptance tests.")
}
}

// lintignore:AT003
func TestAccPreCheckGaussDBMysqlDatabaseName(t *testing.T) {
if HW_GAUSSDB_MYSQL_DATABASE_NAME == "" {
Expand Down Expand Up @@ -1247,6 +1257,13 @@ func TestAccPreCheckGaussDBMysqlJobId(t *testing.T) {
}
}

// lintignore:AT003
func TestAccPreCheckGaussDBMysqlTimeRange(t *testing.T) {
if HW_GAUSSDB_MYSQL_START_TIME == "" || HW_GAUSSDB_MYSQL_END_TIME == "" {
t.Skip("HW_GAUSSDB_MYSQL_START_TIME and HW_GAUSSDB_MYSQL_END_TIME must be set for GaussDB MySQL acceptance tests")
}
}

// lintignore:AT003
func TestAccPreCheckVODWatermark(t *testing.T) {
if HW_VOD_WATERMARK_FILE == "" {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package gaussdb

import (
"fmt"
"testing"

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

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

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

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
acceptance.TestAccPreCheckGaussDBMysqlInstanceId(t)
acceptance.TestAccPreCheckGaussDBMysqlNodeId(t)
acceptance.TestAccPreCheckGaussDBMysqlTimeRange(t)
},
ProviderFactories: acceptance.TestAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testDataSourceGaussDBMysqlSlowLogs_basic(),
Check: resource.ComposeTestCheckFunc(
dc.CheckResourceExists(),
resource.TestCheckResourceAttrSet(dataSource, "slow_log_list.#"),
resource.TestCheckResourceAttrSet(dataSource, "slow_log_list.0.node_id"),
resource.TestCheckResourceAttrSet(dataSource, "slow_log_list.0.count"),
resource.TestCheckResourceAttrSet(dataSource, "slow_log_list.0.time"),
resource.TestCheckResourceAttrSet(dataSource, "slow_log_list.0.lock_time"),
resource.TestCheckResourceAttrSet(dataSource, "slow_log_list.0.rows_sent"),
resource.TestCheckResourceAttrSet(dataSource, "slow_log_list.0.rows_examined"),
resource.TestCheckResourceAttrSet(dataSource, "slow_log_list.0.database"),
resource.TestCheckResourceAttrSet(dataSource, "slow_log_list.0.users"),
resource.TestCheckResourceAttrSet(dataSource, "slow_log_list.0.query_sample"),
resource.TestCheckResourceAttrSet(dataSource, "slow_log_list.0.type"),
resource.TestCheckResourceAttrSet(dataSource, "slow_log_list.0.start_time"),
resource.TestCheckResourceAttrSet(dataSource, "slow_log_list.0.client_ip"),

resource.TestCheckOutput("operate_type_filter_is_useful", "true"),
resource.TestCheckOutput("database_filter_is_useful", "true"),
),
},
},
})
}

func testDataSourceGaussDBMysqlSlowLogs_basic() string {
return fmt.Sprintf(`
data "huaweicloud_gaussdb_mysql_slow_logs" "test" {
instance_id = "%[1]s"
node_id = "%[2]s"
start_time = "%[3]s"
end_time = "%[4]s"
}
locals {
operate_type = "UPDATE"
}
data "huaweicloud_gaussdb_mysql_slow_logs" "operate_type_filter" {
instance_id = "%[1]s"
node_id = "%[2]s"
start_time = "%[3]s"
end_time = "%[4]s"
operate_type = "UPDATE"
}
output "operate_type_filter_is_useful" {
value = length(data.huaweicloud_gaussdb_mysql_slow_logs.operate_type_filter.slow_log_list) > 0 && alltrue(
[for v in data.huaweicloud_gaussdb_mysql_slow_logs.operate_type_filter.slow_log_list[*].type : v == local.operate_type]
)
}
locals {
database = "test_db_1"
}
data "huaweicloud_gaussdb_mysql_slow_logs" "database_filter" {
instance_id = "%[1]s"
node_id = "%[2]s"
start_time = "%[3]s"
end_time = "%[4]s"
database = "test_db_1"
}
output "database_filter_is_useful" {
value = length(data.huaweicloud_gaussdb_mysql_slow_logs.database_filter.slow_log_list) > 0 && alltrue(
[for v in data.huaweicloud_gaussdb_mysql_slow_logs.database_filter.slow_log_list[*].database : v == local.database]
)
}
`, acceptance.HW_GAUSSDB_MYSQL_INSTANCE_ID, acceptance.HW_GAUSSDB_MYSQL_NODE_ID, acceptance.HW_GAUSSDB_MYSQL_START_TIME,
acceptance.HW_GAUSSDB_MYSQL_END_TIME)
}
Loading

0 comments on commit d1857c5

Please sign in to comment.