-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(css): add data source CSS luster logs
- Loading branch information
1 parent
10adefe
commit 2855088
Showing
4 changed files
with
259 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
--- | ||
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" {} | ||
data "huaweicloud_css_cluster_logs" "test" { | ||
cluster_id = var.cluster_id | ||
} | ||
``` | ||
|
||
## 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
huaweicloud/services/acceptance/css/data_source_huaweicloud_css_cluster_logs_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
152
huaweicloud/services/css/data_source_huaweicloud_css_cluster_logs.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |