Skip to content

Commit 8bd8ee9

Browse files
committed
feat(css): add data source CSS luster logs
1 parent 43b5fbf commit 8bd8ee9

File tree

4 files changed

+263
-0
lines changed

4 files changed

+263
-0
lines changed

docs/data-sources/css_cluster_logs.md

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
subcategory: "Cloud Search Service (CSS)"
3+
layout: "huaweicloud"
4+
page_title: "HuaweiCloud: huaweicloud_css_cluster_logs"
5+
description: |-
6+
Use this data source to get the list of CSS cluster logs.
7+
---
8+
9+
# huaweicloud_css_cluster_logs
10+
11+
Use this data source to get the list of CSS cluster logs.
12+
13+
-> **NOTE:** Up to 100 logs can be retrieved.
14+
15+
## Example Usage
16+
17+
```hcl
18+
variable "cluster_id" {}
19+
variable "instance_name" {}
20+
21+
data "huaweicloud_css_cluster_logs" "test" {
22+
cluster_id = var.cluster_id
23+
instance_name = var.instance_name
24+
log_type = "instance"
25+
level = "WARN"
26+
}
27+
```
28+
29+
## Argument Reference
30+
31+
The following arguments are supported:
32+
33+
* `region` - (Optional, String) Specifies the region in which to query the resource.
34+
If omitted, the provider-level region will be used.
35+
36+
* `cluster_id` - (Required, String) Specifies the ID of the cluster.
37+
38+
* `instance_name` - (Required, String) Specifies the node name.
39+
40+
* `log_type` - (Required, String) Specifies the log type.
41+
The types of logs that can be queried are **deprecation**, **indexingSlow**, **searchSlow**, and **instance**.
42+
43+
* `level` - (Required, String) Specifies the log level.
44+
The levels of logs that can be queried are **INFO**, **ERROR**, **DEBUG**, and **WARN**.
45+
46+
## Attribute Reference
47+
48+
In addition to all arguments above, the following attributes are exported:
49+
50+
* `id` - The data source ID.
51+
52+
* `logs` - The log list.
53+
54+
The [logs](#logs_struct) structure is documented below.
55+
56+
<a name="logs_struct"></a>
57+
The `logs` block supports:
58+
59+
* `level` - The log level.
60+
61+
* `date` - The log date.
62+
63+
* `content` - The log content.

huaweicloud/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,7 @@ func Provider() *schema.Provider {
548548
"huaweicloud_css_logstash_templates": css.DataSourceCssLogstashTemplates(),
549549
"huaweicloud_css_cluster_tags": css.DataSourceCssClusterTags(),
550550
"huaweicloud_css_log_backup_records": css.DataSourceCssLogBackupRecords(),
551+
"huaweicloud_css_cluster_logs": css.DataSourceCssClusterLogs(),
551552

552553
"huaweicloud_dataarts_architecture_ds_template_optionals": dataarts.DataSourceTemplateOptionalFields(),
553554
"huaweicloud_dataarts_studio_data_connections": dataarts.DataSourceDataConnections(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package css
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
9+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
10+
)
11+
12+
func TestAccDataSourceCssClusterLogs_basic(t *testing.T) {
13+
dataSource := "data.huaweicloud_css_cluster_logs.test"
14+
rName := acceptance.RandomAccResourceName()
15+
dc := acceptance.InitDataSourceCheck(dataSource)
16+
17+
resource.ParallelTest(t, resource.TestCase{
18+
PreCheck: func() {
19+
acceptance.TestAccPreCheck(t)
20+
},
21+
ProviderFactories: acceptance.TestAccProviderFactories,
22+
Steps: []resource.TestStep{
23+
{
24+
Config: testDataSourceCssClusterLogs_basic(rName),
25+
Check: resource.ComposeTestCheckFunc(
26+
dc.CheckResourceExists(),
27+
resource.TestCheckResourceAttrSet(dataSource, "logs.0.content"),
28+
resource.TestCheckResourceAttrSet(dataSource, "logs.0.date"),
29+
resource.TestCheckResourceAttrSet(dataSource, "logs.0.level"),
30+
),
31+
},
32+
},
33+
})
34+
}
35+
36+
func testDataSourceCssClusterLogs_basic(name string) string {
37+
return fmt.Sprintf(`
38+
%s
39+
40+
data "huaweicloud_css_cluster_logs" "test" {
41+
cluster_id = huaweicloud_css_cluster.test.id
42+
instance_name = huaweicloud_css_cluster.test.nodes[0].name
43+
log_type = "instance"
44+
level = "INFO"
45+
}
46+
`, testAccCssCluster_basic(name, "Test@passw0rd", 7, "bar"))
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
// Generated by PMS #215
2+
package css
3+
4+
import (
5+
"context"
6+
"strings"
7+
8+
"github.com/hashicorp/go-multierror"
9+
"github.com/hashicorp/go-uuid"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
"github.com/tidwall/gjson"
13+
14+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
15+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/httphelper"
16+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/schemas"
17+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
18+
)
19+
20+
func DataSourceCssClusterLogs() *schema.Resource {
21+
return &schema.Resource{
22+
ReadContext: dataSourceCssClusterLogsRead,
23+
24+
Schema: map[string]*schema.Schema{
25+
"region": {
26+
Type: schema.TypeString,
27+
Optional: true,
28+
Computed: true,
29+
Description: `Specifies the region in which to query the resource. If omitted, the provider-level region will be used.`,
30+
},
31+
"cluster_id": {
32+
Type: schema.TypeString,
33+
Required: true,
34+
Description: `Specifies the ID of the cluster.`,
35+
},
36+
"instance_name": {
37+
Type: schema.TypeString,
38+
Required: true,
39+
Description: `Specifies the node name.`,
40+
},
41+
"log_type": {
42+
Type: schema.TypeString,
43+
Required: true,
44+
Description: `Specifies the log type.`,
45+
},
46+
"level": {
47+
Type: schema.TypeString,
48+
Required: true,
49+
Description: `Specifies the log level.`,
50+
},
51+
"logs": {
52+
Type: schema.TypeList,
53+
Computed: true,
54+
Description: `The log list.`,
55+
Elem: &schema.Resource{
56+
Schema: map[string]*schema.Schema{
57+
"level": {
58+
Type: schema.TypeString,
59+
Computed: true,
60+
Description: `The log level.`,
61+
},
62+
"date": {
63+
Type: schema.TypeString,
64+
Computed: true,
65+
Description: `The log date.`,
66+
},
67+
"content": {
68+
Type: schema.TypeString,
69+
Computed: true,
70+
Description: `The log content.`,
71+
},
72+
},
73+
},
74+
},
75+
},
76+
}
77+
}
78+
79+
type ClusterLogsDSWrapper struct {
80+
*schemas.ResourceDataWrapper
81+
Config *config.Config
82+
}
83+
84+
func newClusterLogsDSWrapper(d *schema.ResourceData, meta interface{}) *ClusterLogsDSWrapper {
85+
return &ClusterLogsDSWrapper{
86+
ResourceDataWrapper: schemas.NewSchemaWrapper(d),
87+
Config: meta.(*config.Config),
88+
}
89+
}
90+
91+
func dataSourceCssClusterLogsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
92+
wrapper := newClusterLogsDSWrapper(d, meta)
93+
showLogBackupRst, err := wrapper.ShowLogBackup()
94+
if err != nil {
95+
return diag.FromErr(err)
96+
}
97+
98+
id, err := uuid.GenerateUUID()
99+
if err != nil {
100+
return diag.FromErr(err)
101+
}
102+
d.SetId(id)
103+
104+
err = wrapper.showLogBackupToSchema(showLogBackupRst)
105+
if err != nil {
106+
return diag.FromErr(err)
107+
}
108+
109+
return nil
110+
}
111+
112+
// @API CSS POST /v1.0/{project_id}/clusters/{cluster_id}/logs/search
113+
func (w *ClusterLogsDSWrapper) ShowLogBackup() (*gjson.Result, error) {
114+
client, err := w.NewClient(w.Config, "css")
115+
if err != nil {
116+
return nil, err
117+
}
118+
119+
uri := "/v1.0/{project_id}/clusters/{cluster_id}/logs/search"
120+
uri = strings.ReplaceAll(uri, "{cluster_id}", w.Get("cluster_id").(string))
121+
params := map[string]any{
122+
"instanceName": w.Get("instance_name"),
123+
"level": w.Get("level"),
124+
"logType": w.Get("log_type"),
125+
"limit": 100,
126+
}
127+
params = utils.RemoveNil(params)
128+
return httphelper.New(client).
129+
Method("POST").
130+
URI(uri).
131+
Body(params).
132+
OkCode(200).
133+
Request().
134+
Result()
135+
}
136+
137+
func (w *ClusterLogsDSWrapper) showLogBackupToSchema(body *gjson.Result) error {
138+
d := w.ResourceData
139+
mErr := multierror.Append(nil,
140+
d.Set("region", w.Config.GetRegion(w.ResourceData)),
141+
d.Set("logs", schemas.SliceToList(body.Get("logList"),
142+
func(logs gjson.Result) any {
143+
return map[string]any{
144+
"level": logs.Get("level").Value(),
145+
"date": logs.Get("date").Value(),
146+
"content": logs.Get("content").Value(),
147+
}
148+
},
149+
)),
150+
)
151+
return mErr.ErrorOrNil()
152+
}

0 commit comments

Comments
 (0)