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 applicable instances data source #6142

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
4 changes: 2 additions & 2 deletions docs/data-sources/gaussdb_opengauss_flavors.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ The following arguments are supported:

* `ha_mode` - (Optional, String) Specifies the instance type.
Value options:
+ **Primary/standby**: centralization_standard
+ **Distributed (independent deployment)**: enterprise
+ **centralization_standard**: Primary/standby
+ **enterprise**: Distributed (independent deployment)

## Attribute Reference

Expand Down
47 changes: 47 additions & 0 deletions docs/data-sources/gaussdb_opengauss_pt_applicable_instances.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
subcategory: "GaussDB"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_gaussdb_opengauss_pt_applicable_instances"
description: |-
Use this data source to get the GaussDB OpenGauss instances that a parameter template can be applied to.
---

# huaweicloud_gaussdb_opengauss_pt_applicable_instances

Use this data source to get the GaussDB OpenGauss instances that a parameter template can be applied to.

## Example Usage

```hcl
variable "config_id" {}

data "huaweicloud_gaussdb_opengauss_pt_applicable_instances" "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.

* `instances` - Indicates the list of instances that the parameter template can be applied to.

The [instances](#instances_struct) structure is documented below.

<a name="instances_struct"></a>
The `instances` block supports:

* `instance_id` - Indicates the instance ID.

* `instance_name` - Indicates the instance name.
29 changes: 15 additions & 14 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -815,20 +815,21 @@ func Provider() *schema.Provider {
"huaweicloud_gaussdb_redis_flavors": geminidb.DataSourceGaussDBRedisFlavors(),
"huaweicloud_gaussdb_influx_instances": geminidb.DataSourceGaussDBInfluxInstances(),

"huaweicloud_gaussdb_opengauss_storage_types": gaussdb.DataSourceGaussdbOpengaussStorageTypes(),
"huaweicloud_gaussdb_opengauss_datastores": gaussdb.DataSourceGaussdbOpengaussDatastores(),
"huaweicloud_gaussdb_opengauss_flavors": gaussdb.DataSourceGaussdbOpengaussFlavors(),
"huaweicloud_gaussdb_opengauss_instance": gaussdb.DataSourceOpenGaussInstance(),
"huaweicloud_gaussdb_opengauss_instances": gaussdb.DataSourceOpenGaussInstances(),
"huaweicloud_gaussdb_opengauss_instance_nodes": gaussdb.DataSourceGaussdbOpengaussInstanceNodes(),
"huaweicloud_gaussdb_opengauss_instance_coordinators": gaussdb.DataSourceGaussdbOpengaussInstanceCoordinators(),
"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(),
"huaweicloud_gaussdb_opengauss_storage_types": gaussdb.DataSourceGaussdbOpengaussStorageTypes(),
"huaweicloud_gaussdb_opengauss_datastores": gaussdb.DataSourceGaussdbOpengaussDatastores(),
"huaweicloud_gaussdb_opengauss_flavors": gaussdb.DataSourceGaussdbOpengaussFlavors(),
"huaweicloud_gaussdb_opengauss_instance": gaussdb.DataSourceOpenGaussInstance(),
"huaweicloud_gaussdb_opengauss_instances": gaussdb.DataSourceOpenGaussInstances(),
"huaweicloud_gaussdb_opengauss_instance_nodes": gaussdb.DataSourceGaussdbOpengaussInstanceNodes(),
"huaweicloud_gaussdb_opengauss_instance_coordinators": gaussdb.DataSourceGaussdbOpengaussInstanceCoordinators(),
"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_pt_applicable_instances": gaussdb.DataSourceGaussdbOpengaussPtApplicableInstances(),
"huaweicloud_gaussdb_opengauss_restore_time_ranges": gaussdb.DataSourceGaussdbOpengaussRestoreTimeRanges(),
"huaweicloud_gaussdb_opengauss_restorable_instances": gaussdb.DataSourceGaussdbOpengaussRestorableInstances(),
"huaweicloud_gaussdb_opengauss_backups": gaussdb.DataSourceGaussdbOpengaussBackups(),

"huaweicloud_gaussdb_mysql_engine_versions": taurusdb.DataSourceGaussdbMysqlEngineVersions(),
"huaweicloud_gaussdb_mysql_configuration": taurusdb.DataSourceGaussdbMysqlConfiguration(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
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 TestAccDataSourceGaussdbOpengaussPtApplicableInstances_basic(t *testing.T) {
dataSource := "data.huaweicloud_gaussdb_opengauss_pt_applicable_instances.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: testDataSourceGaussdbOpengaussPtApplicableInstances_basic(rName),
Check: resource.ComposeTestCheckFunc(
dc.CheckResourceExists(),
resource.TestCheckResourceAttrSet(dataSource, "instances.#"),
resource.TestCheckResourceAttrSet(dataSource, "instances.0.instance_id"),
resource.TestCheckResourceAttrSet(dataSource, "instances.0.instance_name"),
),
},
},
})
}

func testDataSourceGaussdbOpengaussPtApplicableInstances_base(name 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"
}
}
`, common.TestBaseNetwork(name), name, acceptance.HW_ENTERPRISE_PROJECT_ID_TEST)
}

func testDataSourceGaussdbOpengaussPtApplicableInstances_basic(name string) string {
return fmt.Sprintf(`
%s

data "huaweicloud_gaussdb_opengauss_pt_applicable_instances" "test" {
depends_on = [huaweicloud_gaussdb_opengauss_instance.test]

config_id = huaweicloud_gaussdb_opengauss_parameter_template.test.id
}
`, testDataSourceGaussdbOpengaussPtApplicableInstances_base(name))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Generated by PMS #507
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 DataSourceGaussdbOpengaussPtApplicableInstances() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceGaussdbOpengaussPtApplicableInstancesRead,

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.`,
},
"instances": {
Type: schema.TypeList,
Computed: true,
Description: `Indicates the list of instances that the parameter template can be applied to.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"instance_id": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the instance ID.`,
},
"instance_name": {
Type: schema.TypeString,
Computed: true,
Description: `Indicates the instance name.`,
},
},
},
},
},
}
}

type OpengaussPtApplicableInstancesDSWrapper struct {
*schemas.ResourceDataWrapper
Config *config.Config
}

func newOpengaussPtApplicableInstancesDSWrapper(d *schema.ResourceData, meta interface{}) *OpengaussPtApplicableInstancesDSWrapper {
return &OpengaussPtApplicableInstancesDSWrapper{
ResourceDataWrapper: schemas.NewSchemaWrapper(d),
Config: meta.(*config.Config),
}
}

func dataSourceGaussdbOpengaussPtApplicableInstancesRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
wrapper := newOpengaussPtApplicableInstancesDSWrapper(d, meta)
lisAppInsRst, err := wrapper.ListApplicableInstances()
if err != nil {
return diag.FromErr(err)
}

id, err := uuid.GenerateUUID()
if err != nil {
return diag.FromErr(err)
}
d.SetId(id)

err = wrapper.listApplicableInstancesToSchema(lisAppInsRst)
if err != nil {
return diag.FromErr(err)
}

return nil
}

// @API GaussDB GET /v3/{project_id}/configurations/{config_id}/applicable-instances
func (w *OpengaussPtApplicableInstancesDSWrapper) ListApplicableInstances() (*gjson.Result, error) {
client, err := w.NewClient(w.Config, "opengauss")
if err != nil {
return nil, err
}

uri := "/v3/{project_id}/configurations/{config_id}/applicable-instances"
uri = strings.ReplaceAll(uri, "{config_id}", w.Get("config_id").(string))
return httphelper.New(client).
Method("GET").
URI(uri).
OffsetPager("instances", "offset", "limit", 0).
Request().
Result()
}

func (w *OpengaussPtApplicableInstancesDSWrapper) listApplicableInstancesToSchema(body *gjson.Result) error {
d := w.ResourceData
mErr := multierror.Append(nil,
d.Set("region", w.Config.GetRegion(w.ResourceData)),
d.Set("instances", schemas.SliceToList(body.Get("instances"),
func(instances gjson.Result) any {
return map[string]any{
"instance_id": instances.Get("instance_id").Value(),
"instance_name": instances.Get("instance_name").Value(),
}
},
)),
)
return mErr.ErrorOrNil()
}
Loading