Skip to content

Commit

Permalink
feat(GaussDB): add gaussdb opengauss parameter template resource
Browse files Browse the repository at this point in the history
  • Loading branch information
houpeng80 committed Dec 20, 2024
1 parent 7eb7279 commit 7f99c89
Show file tree
Hide file tree
Showing 4 changed files with 700 additions and 0 deletions.
152 changes: 152 additions & 0 deletions docs/resources/gaussdb_opengauss_parameter_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
---
subcategory: "GaussDB"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_gaussdb_opengauss_parameter_template"
description: |-
Manages a GaussDB OpenGauss parameter template resource within HuaweiCloud.
---

# huaweicloud_gaussdb_opengauss_parameter_template

Manages a GaussDB OpenGauss parameter template resource within HuaweiCloud.

## Example Usage

### create parameter template

```hcl
resource "huaweicloud_gaussdb_opengauss_parameter_template" "test" {
name = "test_gaussdb_opengauss_parameter_template"
engine_version = "8.201"
instance_mode = "independent"
parameters {
name = "audit_system_object"
value = "100"
}
parameters {
name = "cms:enable_finishredo_retrieve"
value = "on"
}
}
```

### replica parameter template from existed configuration

```hcl
variable "source_configuration_id" {}
resource "huaweicloud_gaussdb_opengauss_parameter_template" "test" {
name = "test_copy_from_configuration"
source_configuration_id = var.source_configuration_id
}
```

## Argument Reference

The following arguments are supported:

* `region` - (Optional, String, ForceNew) Specifies the region in which to create the resource.
If omitted, the provider-level region will be used. Changing this parameter will create a new resource.

* `name` - (Required, String, ForceNew) Specifies the name of the parameter template, which must be unique. The template
name can contain up to **64** characters. It can contain only letters (case-sensitive), digits, hyphens (-),
underscores (_), and periods (.).

Changing this parameter will create a new resource.

* `description` - (Optional, String, ForceNew) Specifies the Parameter template description. This parameter is left blank
by default. Up to **256** characters are displayed. Carriage return characters or special characters (>!<"&'=) are not
allowed.

Changing this parameter will create a new resource.

* `engine_version` - (Optional, String, ForceNew) Specifies the DB engine version.

Changing this parameter will create a new resource.

-> **NOTE:** It is mandatory when `instance_mode` is specified, and can not be specified when `source_configuration_id`
is specified.

* `instance_mode` - (Optional, String, ForceNew) Specifies the deployment model.

Changing this parameter will create a new resource.

-> **NOTE:** It is mandatory when `engine_version` is specified, and can not be specified when `source_configuration_id`
is specified.

* `parameters` - (Optional, List, ForceNew) Specifies the list of the template parameters.
The [parameters](#parameters_struct) structure is documented below.

Changing this parameter will create a new resource.

-> **NOTE:** It can not be specified when `source_configuration_id` is specified.

* `source_configuration_id` - (Optional, String, ForceNew) Specifies the source parameter template ID.

Changing this parameter will create a new resource.

-> **NOTE:** It can not be specified when `engine_version`, `instance_mode` or `parameters` are specified.

-> **NOTE:** Exactly one of `engine_version` and `source_configuration_id` must be provided.

<a name="parameters_struct"></a>
The `parameters` block supports:

* `name` - (Required, String, ForceNew) Specifies the name of a specific parameter.

* `value` - (Required, String, ForceNew) Specifies the value of a specific parameter.

## Attribute Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The resource ID.

* `created_at` - Indicates the creation time in the **yyyy-mm-ddThh:mm:ssZ** format.

* `updated_at` - Indicates the modification time in the **yyyy-mm-ddThh:mm:ssZ** format.

* `parameters` - Indicates the list of the template parameters.
The [parameters](#parameters_struct) structure is documented below.

<a name="parameters_struct"></a>
The `parameters` block supports:

* `need_restart` - Indicates whether the instance needs to be rebooted.

* `readonly` - Indicates whether the parameter is read-only.

* `value_range` - Indicates the parameter value range.

* `data_type` - Indicates the data type. The value can be **string**, **integer**, **boolean**, **list**, **all**,
or **float**.

* `description` - Indicates the parameter description.

## Import

The GaussDB OpenGauss parameter template can be imported using the `id`, e.g.

```bash
$ terraform import huaweicloud_gaussdb_opengauss_parameter_template.test <id>
```

Note that the imported state may not be identical to your resource definition, due to some attributes missing from the
API response, security or some other reason. The missing attributes include: `source_configuration_id` and `parameters`.
It is generally recommended running `terraform plan` after importing a GaussDB OpenGauss parameter template. You can then
decide if changes should be applied to the GaussDB OpenGauss parameter template, or the resource definition should be
updated to align with the GaussDB OpenGauss parameter template. Also you can ignore changes as below.

```hcl
resource "huaweicloud_gaussdb_opengauss_parameter_template" "test" {
...
lifecycle {
ignore_changes = [
source_configuration_id, parameters,
]
}
}
```
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1752,6 +1752,7 @@ func Provider() *schema.Provider {
"huaweicloud_gaussdb_opengauss_backup_stop": gaussdb.ResourceOpenGaussBackupStop(),
"huaweicloud_gaussdb_opengauss_eip_associate": gaussdb.ResourceOpenGaussEipAssociate(),
"huaweicloud_gaussdb_opengauss_primary_standby_switch": gaussdb.ResourceOpenGaussPrimaryStandbySwitch(),
"huaweicloud_gaussdb_opengauss_parameter_template": gaussdb.ResourceOpenGaussParameterTemplate(),

"huaweicloud_ges_graph": ges.ResourceGesGraph(),
"huaweicloud_ges_metadata": ges.ResourceGesMetadata(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
package gaussdb

import (
"fmt"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

"github.com/chnsz/golangsdk"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)

func getOpenGaussParameterTemplateResourceFunc(cfg *config.Config, state *terraform.ResourceState) (interface{}, error) {
region := acceptance.HW_REGION_NAME
var (
httpUrl = "v3/{project_id}/configurations/{config_id}"
product = "opengauss"
)
client, err := cfg.NewServiceClient(product, region)
if err != nil {
return nil, fmt.Errorf("error creating GaussDB client: %s", err)
}

getPath := client.Endpoint + httpUrl
getPath = strings.ReplaceAll(getPath, "{project_id}", client.ProjectID)
getPath = strings.ReplaceAll(getPath, "{config_id}", state.Primary.ID)

getOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
MoreHeaders: map[string]string{"Content-Type": "application/json"},
}

getResp, err := client.Request("GET", getPath, &getOpt)
if err != nil {
return nil, fmt.Errorf("error retrieving GaussDB OpenGauss parameter template: %s", err)
}

getRespBody, err := utils.FlattenResponse(getResp)
if err != nil {
return nil, fmt.Errorf("error retrieving GaussDB OpenGauss parameter template: %s", err)
}

return getRespBody, nil
}

func TestAccOpenGaussParameterTemplate_basic(t *testing.T) {
var obj interface{}

name := acceptance.RandomAccResourceName()
rName := "huaweicloud_gaussdb_opengauss_parameter_template.test"

rc := acceptance.InitResourceCheck(
rName,
&obj,
getOpenGaussParameterTemplateResourceFunc,
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.TestAccPreCheck(t) },
ProviderFactories: acceptance.TestAccProviderFactories,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testOpenGaussParameterTemplate_basic(name),
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(rName, "name", name),
resource.TestCheckResourceAttr(rName, "description", "test terraform description"),
resource.TestCheckResourceAttr(rName, "engine_version", "8.201"),
resource.TestCheckResourceAttr(rName, "instance_mode", "independent"),
resource.TestCheckResourceAttr(rName, "parameters.#", "1"),
resource.TestCheckResourceAttr(rName, "parameters.0.name", "audit_system_object"),
resource.TestCheckResourceAttr(rName, "parameters.0.value", "100"),
resource.TestCheckResourceAttrSet(rName, "created_at"),
resource.TestCheckResourceAttrSet(rName, "updated_at"),
resource.TestCheckResourceAttrSet(rName, "parameters.0.need_restart"),
resource.TestCheckResourceAttrSet(rName, "parameters.0.readonly"),
resource.TestCheckResourceAttrSet(rName, "parameters.0.value_range"),
resource.TestCheckResourceAttrSet(rName, "parameters.0.data_type"),
resource.TestCheckResourceAttrSet(rName, "parameters.0.description"),
),
},
{
ResourceName: rName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"source_configuration_id", "parameters"},
},
},
})
}

func TestAccOpenGaussParameterTemplate_copy(t *testing.T) {
var obj interface{}

name := acceptance.RandomAccResourceName()
rName := "huaweicloud_gaussdb_opengauss_parameter_template.test"

rc := acceptance.InitResourceCheck(
rName,
&obj,
getOpenGaussParameterTemplateResourceFunc,
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.TestAccPreCheck(t) },
ProviderFactories: acceptance.TestAccProviderFactories,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testOpenGaussParameterTemplate_copy(name),
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(rName, "name", name),
resource.TestCheckResourceAttr(rName, "description", "test terraform description"),
resource.TestCheckResourceAttrPair(rName, "engine_version",
"huaweicloud_gaussdb_opengauss_parameter_template.source", "engine_version"),
resource.TestCheckResourceAttrPair(rName, "instance_mode",
"huaweicloud_gaussdb_opengauss_parameter_template.source", "instance_mode"),
resource.TestCheckResourceAttrPair(rName, "source_configuration_id",
"huaweicloud_gaussdb_opengauss_parameter_template.source", "id"),
resource.TestCheckResourceAttrSet(rName, "created_at"),
resource.TestCheckResourceAttrSet(rName, "updated_at"),
),
},
{
ResourceName: rName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"source_configuration_id", "engine_version", "instance_mode"},
},
},
})
}

func testOpenGaussParameterTemplate_basic(name string) string {
return fmt.Sprintf(`
resource "huaweicloud_gaussdb_opengauss_parameter_template" "test" {
name = "%[1]s"
description = "test terraform description"
engine_version = "8.201"
instance_mode = "independent"
parameters {
name = "audit_system_object"
value = "100"
}
}
`, name)
}

func testOpenGaussParameterTemplate_copy(name string) string {
return fmt.Sprintf(`
resource "huaweicloud_gaussdb_opengauss_parameter_template" "source" {
name = "%[1]s_source"
description = "test terraform description"
engine_version = "8.201"
instance_mode = "independent"
parameters {
name = "audit_system_object"
value = "100"
}
}
resource "huaweicloud_gaussdb_opengauss_parameter_template" "test" {
name = "%[1]s"
description = "test terraform description"
source_configuration_id = huaweicloud_gaussdb_opengauss_parameter_template.source.id
}
`, name)
}
Loading

0 comments on commit 7f99c89

Please sign in to comment.