Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(GaussDB): add gaussdb opengauss parameter template apply records data source #6141

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions docs/data-sources/gaussdb_opengauss_pt_apply_records.md
Original file line number Diff line number Diff line change
@@ -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.

<a name="histories_struct"></a>
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.
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,7 @@ func Provider() *schema.Provider {
"huaweicloud_gaussdb_opengauss_databases": gaussdb.DataSourceOpenGaussDatabases(),
"huaweicloud_gaussdb_opengauss_parameter_templates": gaussdb.DataSourceGaussdbOpengaussParameterTemplates(),
"huaweicloud_gaussdb_opengauss_pt_modify_records": gaussdb.DataSourceGaussdbOpengaussPtModifyRecords(),
"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(),
Expand Down
Original file line number Diff line number Diff line change
@@ -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))
}
Original file line number Diff line number Diff line change
@@ -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()
}
Loading