Skip to content

Commit

Permalink
feat(css): add data source CSS luster logs
Browse files Browse the repository at this point in the history
  • Loading branch information
luoping-12345 committed Jun 19, 2024
1 parent 10adefe commit a140cad
Show file tree
Hide file tree
Showing 4 changed files with 263 additions and 0 deletions.
63 changes: 63 additions & 0 deletions docs/data-sources/css_cluster_logs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
subcategory: "Cloud Search Service (CSS)"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_css_cluster_logs"
description: |-
Use this data source to get the list of CSS cluster logs.
---

# huaweicloud_css_cluster_logs

Use this data source to get the list of CSS cluster logs.

-> **NOTE:** Up to 100 logs can be retrieved.

## Example Usage

```hcl
variable "cluster_id" {}
variable "instance_name" {}
data "huaweicloud_css_cluster_logs" "test" {
cluster_id = var.cluster_id
instance_name = var.instance_name
log_type = "instance"
level = "WARN"
}
```

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

* `cluster_id` - (Required, String) Specifies the ID of the cluster.

* `instance_name` - (Required, String) Specifies the node name.

* `log_type` - (Required, String) Specifies the log type.
The types of logs that can be queried are **deprecation**, **indexingSlow**, **searchSlow**, and **instance**.

* `level` - (Required, String) Specifies the log level.
The levels of logs that can be queried are **INFO**, **ERROR**, **DEBUG**, and **WARN**.

## Attribute Reference

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

* `id` - The data source ID.

* `logs` - The log list.

The [logs](#logs_struct) structure is documented below.

<a name="logs_struct"></a>
The `logs` block supports:

* `level` - The log level.

* `date` - The log date.

* `content` - The log content.
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ func Provider() *schema.Provider {
"huaweicloud_css_upgrade_target_images": css.DataSourceCssUpgradeTargetImages(),
"huaweicloud_css_logstash_templates": css.DataSourceCssLogstashTemplates(),
"huaweicloud_css_cluster_tags": css.DataSourceCssClusterTags(),
"huaweicloud_css_cluster_logs": css.DataSourceCssClusterLogs(),

"huaweicloud_dataarts_architecture_ds_template_optionals": dataarts.DataSourceTemplateOptionalFields(),
"huaweicloud_dataarts_studio_data_connections": dataarts.DataSourceDataConnections(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package css

import (
"fmt"
"testing"

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

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

func TestAccDataSourceCssClusterLogs_basic(t *testing.T) {
dataSource := "data.huaweicloud_css_cluster_logs.test"
rName := acceptance.RandomAccResourceName()
dc := acceptance.InitDataSourceCheck(dataSource)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
},
ProviderFactories: acceptance.TestAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testDataSourceCssClusterLogs_basic(rName),
Check: resource.ComposeTestCheckFunc(
dc.CheckResourceExists(),
resource.TestCheckResourceAttrSet(dataSource, "logs.0.content"),
resource.TestCheckResourceAttrSet(dataSource, "logs.0.date"),
resource.TestCheckResourceAttrSet(dataSource, "logs.0.level"),
),
},
},
})
}

func testDataSourceCssClusterLogs_basic(name string) string {
return fmt.Sprintf(`
%s
data "huaweicloud_css_cluster_logs" "test" {
cluster_id = huaweicloud_css_cluster.test.id
instance_name = huaweicloud_css_cluster.test.nodes[0].name
log_type = "instance"
level = "INFO"
}
`, testAccCssCluster_basic(name, "Test@passw0rd", 7, "bar"))
}
152 changes: 152 additions & 0 deletions huaweicloud/services/css/data_source_huaweicloud_css_cluster_logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// Generated by PMS #215
package css

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"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)

func DataSourceCssClusterLogs() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceCssClusterLogsRead,

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.`,
},
"cluster_id": {
Type: schema.TypeString,
Required: true,
Description: `Specifies the ID of the cluster.`,
},
"instance_name": {
Type: schema.TypeString,
Required: true,
Description: `Specifies the node name.`,
},
"log_type": {
Type: schema.TypeString,
Required: true,
Description: `Specifies the log type.`,
},
"level": {
Type: schema.TypeString,
Required: true,
Description: `Specifies the log level.`,
},
"logs": {
Type: schema.TypeList,
Computed: true,
Description: `The log list.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"level": {
Type: schema.TypeString,
Computed: true,
Description: `The log level.`,
},
"date": {
Type: schema.TypeString,
Computed: true,
Description: `The log date.`,
},
"content": {
Type: schema.TypeString,
Computed: true,
Description: `The log content.`,
},
},
},
},
},
}
}

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

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

func dataSourceCssClusterLogsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
wrapper := newClusterLogsDSWrapper(d, meta)
showLogBackupRst, err := wrapper.ShowLogBackup()
if err != nil {
return diag.FromErr(err)
}

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

err = wrapper.showLogBackupToSchema(showLogBackupRst)
if err != nil {
return diag.FromErr(err)
}

return nil
}

// @API CSS POST /v1.0/{project_id}/clusters/{cluster_id}/logs/search
func (w *ClusterLogsDSWrapper) ShowLogBackup() (*gjson.Result, error) {
client, err := w.NewClient(w.Config, "css")
if err != nil {
return nil, err
}

uri := "/v1.0/{project_id}/clusters/{cluster_id}/logs/search"
uri = strings.ReplaceAll(uri, "{cluster_id}", w.Get("cluster_id").(string))
params := map[string]any{
"instanceName": w.Get("instance_name"),
"level": w.Get("level"),
"logType": w.Get("log_type"),
"limit": 100,
}
params = utils.RemoveNil(params)
return httphelper.New(client).
Method("POST").
URI(uri).
Body(params).
OkCode(200).
Request().
Result()
}

func (w *ClusterLogsDSWrapper) showLogBackupToSchema(body *gjson.Result) error {
d := w.ResourceData
mErr := multierror.Append(nil,
d.Set("region", w.Config.GetRegion(w.ResourceData)),
d.Set("logs", schemas.SliceToList(body.Get("logList"),
func(logs gjson.Result) any {
return map[string]any{
"level": logs.Get("level").Value(),
"date": logs.Get("date").Value(),
"content": logs.Get("content").Value(),
}
},
)),
)
return mErr.ErrorOrNil()
}

0 comments on commit a140cad

Please sign in to comment.