Skip to content

Commit

Permalink
feat(GaussDBforMySQL): add gaussdb mysql parameter template compare r…
Browse files Browse the repository at this point in the history
…esource (#5526)
  • Loading branch information
houpeng80 authored Sep 10, 2024
1 parent f50480a commit 642383f
Show file tree
Hide file tree
Showing 4 changed files with 284 additions and 13 deletions.
54 changes: 54 additions & 0 deletions docs/resources/gaussdb_mysql_parameter_template_compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
subcategory: "GaussDB(for MySQL)"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_gaussdb_mysql_parameter_template_compare"
description: |-
Manages a GaussDB MySQL parameter template apply resource within HuaweiCloud.
---

# huaweicloud_gaussdb_mysql_parameter_template_compare

Manages a GaussDB MySQL parameter template compare resource within HuaweiCloud.

## Example Usage

```hcl
variable "source_configuration_id" {}
variable "target_configuration_id" {}
resource "huaweicloud_gaussdb_mysql_parameter_template_compare" "test" {
source_configuration_id = var.source_configuration_id
target_configuration_id = var.target_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.

* `source_configuration_id` - (Required, String, ForceNew) Specifies the ID of the source parameter template to be
compared. Changing this parameter will create a new resource.

* `target_configuration_id` - (Required, String, ForceNew) Specifies the ID of the destination parameter template to be
compared. Changing this parameter will create a new resource.

## Attribute Reference

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

* `id` - The resource ID in format of `<source_configuration_id>/<target_configuration_id>`.

* `differences` - Indicates the differences between parameters.
The [differences](#differences_struct) structure is documented below.

<a name="differences_struct"></a>
The `differences` block supports:

* `parameter_name` - Indicates the parameter name.

* `source_value` - Indicates the parameter value in the source parameter template.

* `target_value` - Indicates the parameter value in the destination parameter template.
27 changes: 14 additions & 13 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1498,19 +1498,20 @@ func Provider() *schema.Provider {

"huaweicloud_gaussdb_cassandra_instance": gaussdb.ResourceGeminiDBInstanceV3(),

"huaweicloud_gaussdb_mysql_instance": gaussdb.ResourceGaussDBInstance(),
"huaweicloud_gaussdb_mysql_instance_restart": gaussdb.ResourceGaussDBMysqlRestart(),
"huaweicloud_gaussdb_mysql_proxy": gaussdb.ResourceGaussDBProxy(),
"huaweicloud_gaussdb_mysql_proxy_restart": gaussdb.ResourceGaussDBProxyRestart(),
"huaweicloud_gaussdb_mysql_database": gaussdb.ResourceGaussDBDatabase(),
"huaweicloud_gaussdb_mysql_account": gaussdb.ResourceGaussDBAccount(),
"huaweicloud_gaussdb_mysql_account_privilege": gaussdb.ResourceGaussDBAccountPrivilege(),
"huaweicloud_gaussdb_mysql_sql_control_rule": gaussdb.ResourceGaussDBSqlControlRule(),
"huaweicloud_gaussdb_mysql_parameter_template": gaussdb.ResourceGaussDBMysqlTemplate(),
"huaweicloud_gaussdb_mysql_backup": gaussdb.ResourceGaussDBMysqlBackup(),
"huaweicloud_gaussdb_mysql_parameter_template_apply": gaussdb.ResourceGaussDBMysqlTemplateApply(),
"huaweicloud_gaussdb_mysql_restore": gaussdb.ResourceGaussDBMysqlRestore(),
"huaweicloud_gaussdb_mysql_eip_associate": gaussdb.ResourceGaussMysqlEipAssociate(),
"huaweicloud_gaussdb_mysql_instance": gaussdb.ResourceGaussDBInstance(),
"huaweicloud_gaussdb_mysql_instance_restart": gaussdb.ResourceGaussDBMysqlRestart(),
"huaweicloud_gaussdb_mysql_proxy": gaussdb.ResourceGaussDBProxy(),
"huaweicloud_gaussdb_mysql_proxy_restart": gaussdb.ResourceGaussDBProxyRestart(),
"huaweicloud_gaussdb_mysql_database": gaussdb.ResourceGaussDBDatabase(),
"huaweicloud_gaussdb_mysql_account": gaussdb.ResourceGaussDBAccount(),
"huaweicloud_gaussdb_mysql_account_privilege": gaussdb.ResourceGaussDBAccountPrivilege(),
"huaweicloud_gaussdb_mysql_sql_control_rule": gaussdb.ResourceGaussDBSqlControlRule(),
"huaweicloud_gaussdb_mysql_parameter_template": gaussdb.ResourceGaussDBMysqlTemplate(),
"huaweicloud_gaussdb_mysql_parameter_template_apply": gaussdb.ResourceGaussDBMysqlTemplateApply(),
"huaweicloud_gaussdb_mysql_parameter_template_compare": gaussdb.ResourceGaussDBMysqlTemplateCompare(),
"huaweicloud_gaussdb_mysql_backup": gaussdb.ResourceGaussDBMysqlBackup(),
"huaweicloud_gaussdb_mysql_restore": gaussdb.ResourceGaussDBMysqlRestore(),
"huaweicloud_gaussdb_mysql_eip_associate": gaussdb.ResourceGaussMysqlEipAssociate(),

"huaweicloud_gaussdb_opengauss_instance": gaussdb.ResourceOpenGaussInstance(),
"huaweicloud_gaussdb_opengauss_database": gaussdb.ResourceOpenGaussDatabase(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package gaussdb

import (
"fmt"
"testing"

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

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

func TestAccGaussDBMysqlTemplateCompare_basic(t *testing.T) {
name := acceptance.RandomAccResourceName()
rName := "huaweicloud_gaussdb_mysql_parameter_template_compare.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.TestAccPreCheck(t) },
ProviderFactories: acceptance.TestAccProviderFactories,
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: testParameterTemplateCompare_basic(name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(rName, "differences.#", "1"),
resource.TestCheckResourceAttr(rName, "differences.0.parameter_name", "auto_increment_increment"),
resource.TestCheckResourceAttr(rName, "differences.0.source_value", "1"),
resource.TestCheckResourceAttr(rName, "differences.0.target_value", "4"),
),
},
},
})
}

func testParameterTemplateCompare_basic(name string) string {
return fmt.Sprintf(`
resource "huaweicloud_gaussdb_mysql_parameter_template" "source" {
name = "%[1]s_source"
description = "test gaussdb mysql parameter template"
datastore_engine = "gaussdb-mysql"
datastore_version = "8.0"
parameter_values = {
auto_increment_increment = "1"
}
}
resource "huaweicloud_gaussdb_mysql_parameter_template" "target" {
name = "%[1]s_target"
description = "test gaussdb mysql parameter template"
datastore_engine = "gaussdb-mysql"
datastore_version = "8.0"
parameter_values = {
auto_increment_increment = "4"
}
}
resource "huaweicloud_gaussdb_mysql_parameter_template_compare" "test" {
source_configuration_id = huaweicloud_gaussdb_mysql_parameter_template.source.id
target_configuration_id = huaweicloud_gaussdb_mysql_parameter_template.target.id
}
`, name)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package gaussdb

import (
"context"
"fmt"
"strings"
"time"

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/chnsz/golangsdk"

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

// @API GaussDBforMySQL POST /v3/{project_id}/configurations/comparison
func ResourceGaussDBMysqlTemplateCompare() *schema.Resource {
return &schema.Resource{
CreateContext: resourceParameterTemplateCompareCreate,
ReadContext: resourceParameterTemplateCompareRead,
DeleteContext: resourceParameterTemplateCompareDelete,

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(60 * time.Minute),
},

Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"source_configuration_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"target_configuration_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"differences": {
Type: schema.TypeList,
Computed: true,
Elem: gaussDBTemplateCompareDifferencesSchema(),
},
},
}
}

func gaussDBTemplateCompareDifferencesSchema() *schema.Resource {
sc := schema.Resource{
Schema: map[string]*schema.Schema{
"parameter_name": {
Type: schema.TypeString,
Computed: true,
},
"source_value": {
Type: schema.TypeString,
Computed: true,
},
"target_value": {
Type: schema.TypeString,
Computed: true,
},
},
}
return &sc
}

func resourceParameterTemplateCompareCreate(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)

var (
httpUrl = "v3/{project_id}/configurations/comparison"
product = "gaussdb"
)
client, err := cfg.NewServiceClient(product, region)
if err != nil {
return diag.Errorf("error creating GaussDB client: %s", err)
}

createPath := client.Endpoint + httpUrl
createPath = strings.ReplaceAll(createPath, "{project_id}", client.ProjectID)

createOpt := golangsdk.RequestOpts{KeepResponseBody: true}
createOpt.JSONBody = utils.RemoveNil(buildCreateParameterTemplateCompareBodyParams(d))

createResp, err := client.Request("POST", createPath, &createOpt)
if err != nil {
return diag.Errorf("error creating GaussDB MySQL parameter template compare: %s", err)
}

sourceConfigurationId := d.Get("source_configuration_id").(string)
targetConfigurationId := d.Get("target_configuration_id").(string)
d.SetId(fmt.Sprintf("%s/%s", sourceConfigurationId, targetConfigurationId))

createRespBody, err := utils.FlattenResponse(createResp)
if err != nil {
return diag.FromErr(err)
}

mErr := multierror.Append(
d.Set("differences", flattenGaussDBParameterTemplateCompareResponseBodyNodes(createRespBody)),
)

return diag.FromErr(mErr.ErrorOrNil())
}

func buildCreateParameterTemplateCompareBodyParams(d *schema.ResourceData) map[string]interface{} {
bodyParams := map[string]interface{}{
"source_configuration_id": d.Get("source_configuration_id"),
"target_configuration_id": d.Get("target_configuration_id"),
}
return bodyParams
}

func flattenGaussDBParameterTemplateCompareResponseBodyNodes(resp interface{}) []interface{} {
differencesJson := utils.PathSearch("differences", resp, make([]interface{}, 0))
differencesArray := differencesJson.([]interface{})
if len(differencesArray) < 1 {
return nil
}
rst := make([]interface{}, 0, len(differencesArray))
for _, v := range differencesArray {
rst = append(rst, map[string]interface{}{
"parameter_name": utils.PathSearch("parameter_name", v, nil),
"source_value": utils.PathSearch("source_value", v, nil),
"target_value": utils.PathSearch("target_value", v, nil),
})
}
return rst
}

func resourceParameterTemplateCompareRead(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
return nil
}

func resourceParameterTemplateCompareDelete(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
errorMsg := "Deleting parameter template compare resource is not supported. The resource is only removed from the" +
"state, the GaussDB MySQL instance remains in the cloud."
return diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Warning,
Summary: errorMsg,
},
}
}

0 comments on commit 642383f

Please sign in to comment.