From 992f3116349d2835e8bc6f9dd9adc71371bfe12d Mon Sep 17 00:00:00 2001 From: houpeng80 Date: Fri, 3 Jan 2025 16:06:15 +0800 Subject: [PATCH] feat(GaussDB): add gaussdb opengauss parameter template apply records data source --- .../gaussdb_opengauss_pt_apply_records.md | 54 +++++++ huaweicloud/provider.go | 1 + ...gaussdb_opengauss_pt_apply_records_test.go | 134 +++++++++++++++++ ...loud_gaussdb_opengauss_pt_apply_records.go | 140 ++++++++++++++++++ 4 files changed, 329 insertions(+) create mode 100644 docs/data-sources/gaussdb_opengauss_pt_apply_records.md create mode 100644 huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_pt_apply_records_test.go create mode 100644 huaweicloud/services/gaussdb/data_source_huaweicloud_gaussdb_opengauss_pt_apply_records.go diff --git a/docs/data-sources/gaussdb_opengauss_pt_apply_records.md b/docs/data-sources/gaussdb_opengauss_pt_apply_records.md new file mode 100644 index 0000000000..1257d39b99 --- /dev/null +++ b/docs/data-sources/gaussdb_opengauss_pt_apply_records.md @@ -0,0 +1,54 @@ +--- +subcategory: "GaussDB" +layout: "huaweicloud" +page_title: "HuaweiCloud: huaweicloud_gaussdb_opengauss_pt_apply_records" +description: |- + Use this data source to get the application apply records of a parameter template +--- + +# huaweicloud_gaussdb_opengauss_pt_apply_records + +Use this data source to get the application apply records of a parameter template + +## Example Usage + +```hcl +variable "config_id" {} + +data "huaweicloud_gaussdb_opengauss_pt_apply_records" "test" { + config_id = var.config_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. + +* `config_id` - (Required, String) Specifies the parameter template ID. + +## Attribute Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - The data source ID. + +* `histories` - Indicates the application records. + + The [histories](#histories_struct) structure is documented below. + + +The `histories` block supports: + +* `instance_id` - Indicates the GaussDB OpenGauss instance ID. + +* `instance_name` - Indicates the GaussDB OpenGauss instance name. + +* `applied_at` - Indicates the application time in the **yyyy-mm-ddThh:mm:ssZ** format. + +* `apply_result` - Indicates the application status. + The value can be: **SUCCESS**, **FAILED**, **APPLYING**. + +* `error_code` - Indicates the error code of the failure cause. diff --git a/huaweicloud/provider.go b/huaweicloud/provider.go index c357f3c57c..b7bc00640b 100644 --- a/huaweicloud/provider.go +++ b/huaweicloud/provider.go @@ -822,6 +822,7 @@ func Provider() *schema.Provider { "huaweicloud_gaussdb_opengauss_instance_coordinators": gaussdb.DataSourceGaussdbOpengaussInstanceCoordinators(), "huaweicloud_gaussdb_opengauss_databases": gaussdb.DataSourceOpenGaussDatabases(), "huaweicloud_gaussdb_opengauss_parameter_templates": gaussdb.DataSourceGaussdbOpengaussParameterTemplates(), + "huaweicloud_gaussdb_opengauss_pt_apply_records": gaussdb.DataSourceGaussdbOpengaussPtApplyRecords(), "huaweicloud_gaussdb_opengauss_restore_time_ranges": gaussdb.DataSourceGaussdbOpengaussRestoreTimeRanges(), "huaweicloud_gaussdb_opengauss_restorable_instances": gaussdb.DataSourceGaussdbOpengaussRestorableInstances(), "huaweicloud_gaussdb_opengauss_backups": gaussdb.DataSourceGaussdbOpengaussBackups(), diff --git a/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_pt_apply_records_test.go b/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_pt_apply_records_test.go new file mode 100644 index 0000000000..b8ec84bbbb --- /dev/null +++ b/huaweicloud/services/acceptance/gaussdb/data_source_huaweicloud_gaussdb_opengauss_pt_apply_records_test.go @@ -0,0 +1,134 @@ +package gaussdb + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance/common" +) + +func TestAccDataSourceGaussdbOpengaussPtApplyRecords_basic(t *testing.T) { + dataSource := "data.huaweicloud_gaussdb_opengauss_pt_apply_records.test" + rName := acceptance.RandomAccResourceName() + dc := acceptance.InitDataSourceCheck(dataSource) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + acceptance.TestAccPreCheck(t) + acceptance.TestAccPreCheckEpsID(t) + acceptance.TestAccPreCheckHighCostAllow(t) + }, + ProviderFactories: acceptance.TestAccProviderFactories, + Steps: []resource.TestStep{ + { + Config: testDataSourceGaussdbOpengaussPtApplyRecords_basic(rName), + Check: resource.ComposeTestCheckFunc( + dc.CheckResourceExists(), + resource.TestCheckResourceAttrSet(dataSource, "histories.#"), + resource.TestCheckResourceAttrSet(dataSource, "histories.0.instance_id"), + resource.TestCheckResourceAttrSet(dataSource, "histories.0.instance_name"), + resource.TestCheckResourceAttrSet(dataSource, "histories.0.applied_at"), + resource.TestCheckResourceAttrSet(dataSource, "histories.0.apply_result"), + ), + }, + }, + }) +} + +func testDataSourceGaussdbOpengaussPtApplyRecords_base(rName string) string { + return fmt.Sprintf(` +%[1]s + +data "huaweicloud_availability_zones" "test" {} + +data "huaweicloud_gaussdb_opengauss_flavors" "test" { + version = "8.201" + ha_mode = "centralization_standard" +} + +resource "huaweicloud_networking_secgroup_rule" "in_v4_tcp_opengauss" { + security_group_id = huaweicloud_networking_secgroup.test.id + ethertype = "IPv4" + direction = "ingress" + protocol = "tcp" + remote_ip_prefix = "0.0.0.0/0" +} + +resource "huaweicloud_networking_secgroup_rule" "in_v4_tcp_opengauss_egress" { + security_group_id = huaweicloud_networking_secgroup.test.id + ethertype = "IPv4" + direction = "egress" + protocol = "tcp" + remote_ip_prefix = "0.0.0.0/0" +} + +resource "huaweicloud_gaussdb_opengauss_instance" "test" { + depends_on = [ + huaweicloud_networking_secgroup_rule.in_v4_tcp_opengauss, + huaweicloud_networking_secgroup_rule.in_v4_tcp_opengauss_egress + ] + + vpc_id = huaweicloud_vpc.test.id + subnet_id = huaweicloud_vpc_subnet.test.id + security_group_id = huaweicloud_networking_secgroup.test.id + + flavor = data.huaweicloud_gaussdb_opengauss_flavors.test.flavors[0].spec_code + name = "%[2]s" + password = "Huangwei!120521" + replica_num = 3 + availability_zone = join(",", [data.huaweicloud_availability_zones.test.names[0], + data.huaweicloud_availability_zones.test.names[1], + data.huaweicloud_availability_zones.test.names[2]]) + + enterprise_project_id = "%[3]s" + + ha { + mode = "centralization_standard" + replication_mode = "sync" + consistency = "eventual" + instance_mode = "basic" + } + + volume { + type = "ULTRAHIGH" + size = 40 + } +} + +resource "huaweicloud_gaussdb_opengauss_parameter_template" "test" { + name = "%[2]s" + engine_version = "8.201" + instance_mode = "ha" + + parameters { + name = "autovacuum_naptime" + value = "1000" + } + + parameters { + name = "dn:check_disconnect_query" + value = "off" + } +} + +resource "huaweicloud_gaussdb_opengauss_parameter_template_apply" "test" { + config_id = huaweicloud_gaussdb_opengauss_parameter_template.test.id + instance_id = huaweicloud_gaussdb_opengauss_instance.test.id +} +`, common.TestBaseNetwork(rName), rName, acceptance.HW_ENTERPRISE_PROJECT_ID_TEST) +} + +func testDataSourceGaussdbOpengaussPtApplyRecords_basic(name string) string { + return fmt.Sprintf(` +%[1]s + +data "huaweicloud_gaussdb_opengauss_pt_apply_records" "test" { + depends_on = [huaweicloud_gaussdb_opengauss_parameter_template_apply.test] + + config_id = huaweicloud_gaussdb_opengauss_parameter_template.test.id +} +`, testDataSourceGaussdbOpengaussPtApplyRecords_base(name)) +} diff --git a/huaweicloud/services/gaussdb/data_source_huaweicloud_gaussdb_opengauss_pt_apply_records.go b/huaweicloud/services/gaussdb/data_source_huaweicloud_gaussdb_opengauss_pt_apply_records.go new file mode 100644 index 0000000000..02362b11b7 --- /dev/null +++ b/huaweicloud/services/gaussdb/data_source_huaweicloud_gaussdb_opengauss_pt_apply_records.go @@ -0,0 +1,140 @@ +// Generated by PMS #505 +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" +) + +func DataSourceGaussdbOpengaussPtApplyRecords() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceGaussdbOpengaussPtApplyRecordsRead, + + 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.`, + }, + "config_id": { + Type: schema.TypeString, + Required: true, + Description: `Specifies the parameter template ID.`, + }, + "histories": { + Type: schema.TypeList, + Computed: true, + Description: `Indicates the application records.`, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "instance_id": { + Type: schema.TypeString, + Computed: true, + Description: `Indicates the GaussDB OpenGauss instance ID.`, + }, + "instance_name": { + Type: schema.TypeString, + Computed: true, + Description: `Indicates the GaussDB OpenGauss instance name.`, + }, + "applied_at": { + Type: schema.TypeString, + Computed: true, + Description: `Indicates the application time in the **yyyy-mm-ddThh:mm:ssZ** format.`, + }, + "apply_result": { + Type: schema.TypeString, + Computed: true, + Description: `Indicates the application status.`, + }, + "error_code": { + Type: schema.TypeString, + Computed: true, + Description: `Indicates the error code of the failure cause.`, + }, + }, + }, + }, + }, + } +} + +type OpengaussPtApplyRecordsDSWrapper struct { + *schemas.ResourceDataWrapper + Config *config.Config +} + +func newOpengaussPtApplyRecordsDSWrapper(d *schema.ResourceData, meta interface{}) *OpengaussPtApplyRecordsDSWrapper { + return &OpengaussPtApplyRecordsDSWrapper{ + ResourceDataWrapper: schemas.NewSchemaWrapper(d), + Config: meta.(*config.Config), + } +} + +func dataSourceGaussdbOpengaussPtApplyRecordsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + wrapper := newOpengaussPtApplyRecordsDSWrapper(d, meta) + lisAppHisRst, err := wrapper.ListAppliedHistories() + if err != nil { + return diag.FromErr(err) + } + + id, err := uuid.GenerateUUID() + if err != nil { + return diag.FromErr(err) + } + d.SetId(id) + + err = wrapper.listAppliedHistoriesToSchema(lisAppHisRst) + if err != nil { + return diag.FromErr(err) + } + + return nil +} + +// @API GaussDB GET /v3/{project_id}/configurations/{config_id}/applied-histories +func (w *OpengaussPtApplyRecordsDSWrapper) ListAppliedHistories() (*gjson.Result, error) { + client, err := w.NewClient(w.Config, "opengauss") + if err != nil { + return nil, err + } + + uri := "/v3/{project_id}/configurations/{config_id}/applied-histories" + uri = strings.ReplaceAll(uri, "{config_id}", w.Get("config_id").(string)) + return httphelper.New(client). + Method("GET"). + URI(uri). + OffsetPager("histories", "offset", "limit", 0). + Request(). + Result() +} + +func (w *OpengaussPtApplyRecordsDSWrapper) listAppliedHistoriesToSchema(body *gjson.Result) error { + d := w.ResourceData + mErr := multierror.Append(nil, + d.Set("region", w.Config.GetRegion(w.ResourceData)), + d.Set("histories", schemas.SliceToList(body.Get("histories"), + func(histories gjson.Result) any { + return map[string]any{ + "instance_id": histories.Get("instance_id").Value(), + "instance_name": histories.Get("instance_name").Value(), + "applied_at": histories.Get("applied_at").Value(), + "apply_result": histories.Get("apply_result").Value(), + "error_code": histories.Get("error_code").Value(), + } + }, + )), + ) + return mErr.ErrorOrNil() +}