From a140cadc0425c0fd64fc3191e5ad756148b909bb Mon Sep 17 00:00:00 2001 From: luoping-12345 Date: Mon, 17 Jun 2024 19:55:28 +0800 Subject: [PATCH] feat(css): add data source CSS luster logs --- docs/data-sources/css_cluster_logs.md | 63 ++++++++ huaweicloud/provider.go | 1 + ...ource_huaweicloud_css_cluster_logs_test.go | 47 ++++++ ...ata_source_huaweicloud_css_cluster_logs.go | 152 ++++++++++++++++++ 4 files changed, 263 insertions(+) create mode 100644 docs/data-sources/css_cluster_logs.md create mode 100644 huaweicloud/services/acceptance/css/data_source_huaweicloud_css_cluster_logs_test.go create mode 100644 huaweicloud/services/css/data_source_huaweicloud_css_cluster_logs.go diff --git a/docs/data-sources/css_cluster_logs.md b/docs/data-sources/css_cluster_logs.md new file mode 100644 index 00000000000..83555ec3717 --- /dev/null +++ b/docs/data-sources/css_cluster_logs.md @@ -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. + + +The `logs` block supports: + +* `level` - The log level. + +* `date` - The log date. + +* `content` - The log content. diff --git a/huaweicloud/provider.go b/huaweicloud/provider.go index 45bb6648d41..21bb635221f 100644 --- a/huaweicloud/provider.go +++ b/huaweicloud/provider.go @@ -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(), diff --git a/huaweicloud/services/acceptance/css/data_source_huaweicloud_css_cluster_logs_test.go b/huaweicloud/services/acceptance/css/data_source_huaweicloud_css_cluster_logs_test.go new file mode 100644 index 00000000000..d7118a712f7 --- /dev/null +++ b/huaweicloud/services/acceptance/css/data_source_huaweicloud_css_cluster_logs_test.go @@ -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")) +} diff --git a/huaweicloud/services/css/data_source_huaweicloud_css_cluster_logs.go b/huaweicloud/services/css/data_source_huaweicloud_css_cluster_logs.go new file mode 100644 index 00000000000..50c5f7db7c0 --- /dev/null +++ b/huaweicloud/services/css/data_source_huaweicloud_css_cluster_logs.go @@ -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() +}