-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(GaussDB): add guassdb opengauss error logs data source
- Loading branch information
Showing
5 changed files
with
287 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,62 @@ | ||
--- | ||
subcategory: "GaussDB" | ||
layout: "huaweicloud" | ||
page_title: "HuaweiCloud: huaweicloud_gaussdb_opengauss_error_logs" | ||
description: |- | ||
Use this data source to get the link for downloading error logs. | ||
--- | ||
|
||
# huaweicloud_gaussdb_opengauss_error_logs | ||
|
||
Use this data source to get the link for downloading error logs. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
variable "instance_id" {} | ||
data "huaweicloud_gaussdb_opengauss_error_logs" "test" { | ||
instance_id = var.instance_id | ||
start_time = "2025-01-20T19:41:14+0800" | ||
end_time = "2025-01-20T20:41:14+0800" | ||
} | ||
``` | ||
|
||
## 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. | ||
|
||
* `instance_id` - (Required, String) Specifies the ID of the GaussDB OpenGauss instance. | ||
|
||
* `start_time` - (Required, String) Specifies the start time in the **yyyy-mm-ddThh:mm:ssZ** format. | ||
|
||
* `end_time` - (Required, String) Specifies the end time in the **yyyy-mm-ddThh:mm:ssZ** format. | ||
Only error logs generated in the past 30 days can be queried. | ||
|
||
## Attribute Reference | ||
|
||
In addition to all arguments above, the following attributes are exported: | ||
|
||
* `id` - The data source ID. | ||
|
||
* `log_files` - Indicates the log files. | ||
|
||
The [log_files](#log_files_struct) structure is documented below. | ||
|
||
<a name="log_files_struct"></a> | ||
The `log_files` block supports: | ||
|
||
* `file_link` - Indicates the link for downloading the log file. | ||
|
||
* `file_name` - Indicates the log file name. | ||
|
||
* `file_size` - Indicates the log file size in KB. | ||
|
||
* `status` - Indicates the log collection status. | ||
|
||
* `start_time` - Indicates the log start time. | ||
|
||
* `end_time` - Indicates the log end time. |
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
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
43 changes: 43 additions & 0 deletions
43
.../services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_error_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,43 @@ | ||
package gaussdb | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
|
||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" | ||
) | ||
|
||
func TestAccDataSourceGaussdbOpengaussErrorLogs_basic(t *testing.T) { | ||
dataSource := "data.huaweicloud_gaussdb_opengauss_error_logs.test" | ||
dc := acceptance.InitDataSourceCheck(dataSource) | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { | ||
acceptance.TestAccPreCheck(t) | ||
acceptance.TestAccPreCheckGaussDBOpenGaussInstanceId(t) | ||
acceptance.TestAccPreCheckGaussDBOpenGaussTimeRange(t) | ||
}, | ||
ProviderFactories: acceptance.TestAccProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testDataSourceGaussdbOpengaussErrorLogs_basic(), | ||
Check: resource.ComposeTestCheckFunc( | ||
dc.CheckResourceExists(), | ||
resource.TestCheckResourceAttrSet(dataSource, "log_files.#"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testDataSourceGaussdbOpengaussErrorLogs_basic() string { | ||
return fmt.Sprintf(` | ||
data "huaweicloud_gaussdb_opengauss_error_logs" "test" { | ||
instance_id = "%[1]s" | ||
start_time = "%[2]s" | ||
end_time = "%[3]s" | ||
} | ||
`, acceptance.HW_GAUSSDB_OPENGAUSS_INSTANCE_ID, acceptance.HW_GAUSSDB_OPENGAUSS_START_TIME, acceptance.HW_GAUSSDB_OPENGAUSS_END_TIME) | ||
} |
163 changes: 163 additions & 0 deletions
163
huaweicloud/services/gaussdb/data_source_huaweicloud_gaussdb_opengauss_error_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,163 @@ | ||
// Generated by PMS #537 | ||
package gaussdb | ||
|
||
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 DataSourceGaussdbOpengaussErrorLogs() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataSourceGaussdbOpengaussErrorLogsRead, | ||
|
||
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.`, | ||
}, | ||
"instance_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: `Specifies the ID of the GaussDB OpenGauss instance.`, | ||
}, | ||
"start_time": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: `Specifies the start time in the **yyyy-mm-ddThh:mm:ssZ** format.`, | ||
}, | ||
"end_time": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: `Specifies the end time in the **yyyy-mm-ddThh:mm:ssZ** format.`, | ||
}, | ||
"log_files": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Description: `Indicates the log files.`, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"file_link": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `Indicates the link for downloading the log file.`, | ||
}, | ||
"file_name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `Indicates the log file name.`, | ||
}, | ||
"file_size": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `Indicates the log file size in KB.`, | ||
}, | ||
"status": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `Indicates the log collection status.`, | ||
}, | ||
"start_time": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `Indicates the log start time.`, | ||
}, | ||
"end_time": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `Indicates the log end time.`, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
type OpengaussErrorLogsDSWrapper struct { | ||
*schemas.ResourceDataWrapper | ||
Config *config.Config | ||
} | ||
|
||
func newOpengaussErrorLogsDSWrapper(d *schema.ResourceData, meta interface{}) *OpengaussErrorLogsDSWrapper { | ||
return &OpengaussErrorLogsDSWrapper{ | ||
ResourceDataWrapper: schemas.NewSchemaWrapper(d), | ||
Config: meta.(*config.Config), | ||
} | ||
} | ||
|
||
func dataSourceGaussdbOpengaussErrorLogsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
wrapper := newOpengaussErrorLogsDSWrapper(d, meta) | ||
lisInsErrLogRst, err := wrapper.ListInstanceErrorLogs() | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
id, err := uuid.GenerateUUID() | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
d.SetId(id) | ||
|
||
err = wrapper.listInstanceErrorLogsToSchema(lisInsErrLogRst) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// @API GaussDB GET /v3/{project_id}/instances/{instance_id}/error-log | ||
func (w *OpengaussErrorLogsDSWrapper) ListInstanceErrorLogs() (*gjson.Result, error) { | ||
client, err := w.NewClient(w.Config, "opengauss") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
uri := "/v3/{project_id}/instances/{instance_id}/error-log" | ||
uri = strings.ReplaceAll(uri, "{instance_id}", w.Get("instance_id").(string)) | ||
params := map[string]any{ | ||
"start_time": w.Get("start_time"), | ||
"end_time": w.Get("end_time"), | ||
} | ||
params = utils.RemoveNil(params) | ||
return httphelper.New(client). | ||
Method("GET"). | ||
URI(uri). | ||
Query(params). | ||
OffsetPager("", "", "", 0). | ||
Request(). | ||
Result() | ||
} | ||
|
||
func (w *OpengaussErrorLogsDSWrapper) listInstanceErrorLogsToSchema(body *gjson.Result) error { | ||
d := w.ResourceData | ||
mErr := multierror.Append(nil, | ||
d.Set("region", w.Config.GetRegion(w.ResourceData)), | ||
d.Set("log_files", schemas.SliceToList(body.Get("log_files"), | ||
func(logFiles gjson.Result) any { | ||
return map[string]any{ | ||
"file_link": logFiles.Get("file_link").Value(), | ||
"file_name": logFiles.Get("file_name").Value(), | ||
"file_size": logFiles.Get("file_size").Value(), | ||
"status": logFiles.Get("status").Value(), | ||
"start_time": logFiles.Get("start_time").Value(), | ||
"end_time": logFiles.Get("end_time").Value(), | ||
} | ||
}, | ||
)), | ||
) | ||
return mErr.ErrorOrNil() | ||
} |