diff --git a/docs/data-sources/gaussdb_opengauss_error_logs.md b/docs/data-sources/gaussdb_opengauss_error_logs.md new file mode 100644 index 0000000000..18899b2787 --- /dev/null +++ b/docs/data-sources/gaussdb_opengauss_error_logs.md @@ -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. + + +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. diff --git a/huaweicloud/provider.go b/huaweicloud/provider.go index 19946a3077..b493b233b4 100644 --- a/huaweicloud/provider.go +++ b/huaweicloud/provider.go @@ -858,6 +858,7 @@ func Provider() *schema.Provider { "huaweicloud_gaussdb_opengauss_tags": gaussdb.DataSourceOpenGaussTags(), "huaweicloud_gaussdb_opengauss_predefined_tags": gaussdb.DataSourceOpenGaussPredefinedTags(), "huaweicloud_gaussdb_opengauss_slow_logs": gaussdb.DataSourceOpenGaussSlowLogs(), + "huaweicloud_gaussdb_opengauss_error_logs": gaussdb.DataSourceGaussdbOpengaussErrorLogs(), "huaweicloud_gaussdb_mysql_engine_versions": taurusdb.DataSourceGaussdbMysqlEngineVersions(), "huaweicloud_gaussdb_mysql_configuration": taurusdb.DataSourceGaussdbMysqlConfiguration(), diff --git a/huaweicloud/services/acceptance/acceptance.go b/huaweicloud/services/acceptance/acceptance.go index b976bfc057..780dc284dd 100644 --- a/huaweicloud/services/acceptance/acceptance.go +++ b/huaweicloud/services/acceptance/acceptance.go @@ -187,6 +187,9 @@ var ( HW_GAUSSDB_OPENGAUSS_PARAMETER_TEMPLATE_ID = os.Getenv("HW_GAUSSDB_OPENGAUSS_PARAMETER_TEMPLATE_ID") HW_GAUSSDB_OPENGAUSS_JOB_ID = os.Getenv("HW_GAUSSDB_OPENGAUSS_JOB_ID") + HW_GAUSSDB_OPENGAUSS_INSTANCE_ID = os.Getenv("HW_GAUSSDB_OPENGAUSS_INSTANCE_ID") + HW_GAUSSDB_OPENGAUSS_START_TIME = os.Getenv("HW_GAUSSDB_OPENGAUSS_START_TIME") + HW_GAUSSDB_OPENGAUSS_END_TIME = os.Getenv("HW_GAUSSDB_OPENGAUSS_END_TIME") HW_VOD_WATERMARK_FILE = os.Getenv("HW_VOD_WATERMARK_FILE") HW_VOD_MEDIA_ASSET_FILE = os.Getenv("HW_VOD_MEDIA_ASSET_FILE") @@ -1398,6 +1401,21 @@ func TestAccPreCheckGaussDBOpenGaussJobId(t *testing.T) { } } +// lintignore:AT003 +func TestAccPreCheckGaussDBOpenGaussInstanceId(t *testing.T) { + if HW_GAUSSDB_OPENGAUSS_INSTANCE_ID == "" { + t.Skip("HW_GAUSSDB_OPENGAUSS_INSTANCE_ID must be set for GaussDB OpenGauss acceptance tests") + } +} + +// lintignore:AT003 +func TestAccPreCheckGaussDBOpenGaussTimeRange(t *testing.T) { + if HW_GAUSSDB_OPENGAUSS_START_TIME == "" || HW_GAUSSDB_OPENGAUSS_END_TIME == "" { + t.Skip("HW_GAUSSDB_OPENGAUSS_START_TIME and HW_GAUSSDB_OPENGAUSS_END_TIME must be set for GaussDB " + + "OpenGauss acceptance tests") + } +} + // lintignore:AT003 func TestAccPreCheckVODWatermark(t *testing.T) { if HW_VOD_WATERMARK_FILE == "" { diff --git a/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_error_logs_test.go b/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_error_logs_test.go new file mode 100644 index 0000000000..2628fc2271 --- /dev/null +++ b/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_error_logs_test.go @@ -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) +} diff --git a/huaweicloud/services/gaussdb/data_source_huaweicloud_gaussdb_opengauss_error_logs.go b/huaweicloud/services/gaussdb/data_source_huaweicloud_gaussdb_opengauss_error_logs.go new file mode 100644 index 0000000000..ee5ce85cd9 --- /dev/null +++ b/huaweicloud/services/gaussdb/data_source_huaweicloud_gaussdb_opengauss_error_logs.go @@ -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() +}