Skip to content

Commit

Permalink
feat(GaussDBforMySQL): add gaussdb mysql diagnosis instances data sou…
Browse files Browse the repository at this point in the history
…rce (#5796)
  • Loading branch information
houpeng80 authored Nov 1, 2024
1 parent b7ca13f commit 9653cf3
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 0 deletions.
47 changes: 47 additions & 0 deletions docs/data-sources/gaussdb_mysql_diagnosis_instances.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
subcategory: "GaussDB(for MySQL)"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_gaussdb_mysql_diagnosis_instances"
description: |-
Use this data source to get the abnormal instance information by a specific metric.
---

# huaweicloud_gaussdb_mysql_diagnosis_instances

Use this data source to get the abnormal instance information by a specific metric.

## Example Usage

```hcl
variable "metric_name" {}
data "huaweicloud_gaussdb_mysql_diagnosis_instances" "test" {
metric_name = var.metric_name
}
```

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

* `metric_name` - (Required, String) Specifies the metric name.

## Attribute Reference

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

* `id` - The data source ID.

* `instance_infos` - Indicates the information about the abnormal instances.

The [instance_infos](#instance_infos_struct) structure is documented below.

<a name="instance_infos_struct"></a>
The `instance_infos` block supports:

* `instance_id` - Indicates the instance ID.

* `master_node_id` - Indicates the primary node ID.
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,7 @@ func Provider() *schema.Provider {
"huaweicloud_gaussdb_mysql_slow_logs": gaussdb.DataSourceGaussDBMysqlSlowLogs(),
"huaweicloud_gaussdb_mysql_error_logs": gaussdb.DataSourceGaussDBMysqlErrorLogs(),
"huaweicloud_gaussdb_mysql_diagnosis_statistics": gaussdb.DataSourceGaussdbMysqlDiagnosisStatistics(),
"huaweicloud_gaussdb_mysql_diagnosis_instances": gaussdb.DataSourceGaussDBMysqlDiagnosisInstances(),

"huaweicloud_gaussdb_influx_instances": gaussdb.DataSourceGaussDBInfluxInstances(),

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package gaussdb

import (
"testing"

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

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

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

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

func testDataSourceGaussdbMysqlDiagnosisInstances_basic() string {
return `
data "huaweicloud_gaussdb_mysql_diagnosis_statistics" "test" {}
data "huaweicloud_gaussdb_mysql_diagnosis_instances" "test" {
metric_name = data.huaweicloud_gaussdb_mysql_diagnosis_statistics.test.diagnosis_info[0].metric_name
}
`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package gaussdb

import (
"context"
"encoding/json"
"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/pagination"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)

// @API GaussDBforMySQL GET /v3/{project_id}/instances/diagnosis-instance-infos
func DataSourceGaussDBMysqlDiagnosisInstances() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceGaussDBMysqlDiagnosisInstancesRead,

Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"metric_name": {
Type: schema.TypeString,
Required: true,
Description: `Specifies the metric name.`,
},
"instance_infos": {
Type: schema.TypeList,
Computed: true,
Description: `Indicates the information about the abnormal instances.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"instance_id": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the instance ID.`,
},
"master_node_id": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the primary node ID.`,
},
},
},
},
},
}
}

func dataSourceGaussDBMysqlDiagnosisInstancesRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)

var (
httpUrl = "v3/{project_id}/instances/diagnosis-instance-infos?offset=0&limit=100"
product = "gaussdb"
)
client, err := cfg.NewServiceClient(product, region)
if err != nil {
return diag.Errorf("error creating GaussDB client: %s", err)
}

listPath := client.Endpoint + httpUrl
listPath = strings.ReplaceAll(listPath, "{project_id}", client.ProjectID)
listPath += buildQueryParams(d)

listResp, err := pagination.ListAllItems(
client,
"offset",
listPath,
&pagination.QueryOpts{MarkerField: ""})

if err != nil {
return diag.Errorf("error retrieving GaussDB MySQL diagnosis instances: %s", err)
}

listRespJson, err := json.Marshal(listResp)
if err != nil {
return diag.FromErr(err)
}
var listRespBody interface{}
err = json.Unmarshal(listRespJson, &listRespBody)
if err != nil {
return diag.FromErr(err)
}

dataSourceId, err := uuid.GenerateUUID()
if err != nil {
return diag.Errorf("unable to generate ID: %s", err)
}
d.SetId(dataSourceId)

mErr := multierror.Append(
d.Set("region", region),
d.Set("instance_infos", flattenListDiagnosisInstancesBody(listRespBody)),
)

return diag.FromErr(mErr.ErrorOrNil())
}

func buildQueryParams(d *schema.ResourceData) string {
return fmt.Sprintf("&metric_name=%s", d.Get("metric_name").(string))
}

func flattenListDiagnosisInstancesBody(resp interface{}) []interface{} {
diagnosisInstancesJson := utils.PathSearch("instance_infos", resp, make([]interface{}, 0))
diagnosisInstancesArray := diagnosisInstancesJson.([]interface{})
if len(diagnosisInstancesArray) < 1 {
return nil
}
rst := make([]interface{}, 0, len(diagnosisInstancesArray))

for _, v := range diagnosisInstancesArray {
rst = append(rst, map[string]interface{}{
"instance_id": utils.PathSearch("instance_id", v, nil),
"master_node_id": utils.PathSearch("master_node_id", v, nil),
})
}
return rst
}

0 comments on commit 9653cf3

Please sign in to comment.