From 642383fe7682eb167ca0cd0f33429f41200232c3 Mon Sep 17 00:00:00 2001 From: houpeng80 <114376095+houpeng80@users.noreply.github.com> Date: Tue, 10 Sep 2024 11:36:36 +0800 Subject: [PATCH] feat(GaussDBforMySQL): add gaussdb mysql parameter template compare resource (#5526) --- ...aussdb_mysql_parameter_template_compare.md | 54 +++++++ huaweicloud/provider.go | 27 ++-- ...b_mysql_parameter_template_compare_test.go | 63 ++++++++ ...aussdb_mysql_parameter_template_compare.go | 153 ++++++++++++++++++ 4 files changed, 284 insertions(+), 13 deletions(-) create mode 100644 docs/resources/gaussdb_mysql_parameter_template_compare.md create mode 100644 huaweicloud/services/acceptance/gaussdb/resource_huaweicloud_gaussdb_mysql_parameter_template_compare_test.go create mode 100644 huaweicloud/services/gaussdb/huaweicloud_gaussdb_mysql_parameter_template_compare.go diff --git a/docs/resources/gaussdb_mysql_parameter_template_compare.md b/docs/resources/gaussdb_mysql_parameter_template_compare.md new file mode 100644 index 0000000000..687d1bd72f --- /dev/null +++ b/docs/resources/gaussdb_mysql_parameter_template_compare.md @@ -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 `/`. + +* `differences` - Indicates the differences between parameters. + The [differences](#differences_struct) structure is documented below. + + +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. diff --git a/huaweicloud/provider.go b/huaweicloud/provider.go index 69c0d848f8..fd0067586a 100644 --- a/huaweicloud/provider.go +++ b/huaweicloud/provider.go @@ -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(), diff --git a/huaweicloud/services/acceptance/gaussdb/resource_huaweicloud_gaussdb_mysql_parameter_template_compare_test.go b/huaweicloud/services/acceptance/gaussdb/resource_huaweicloud_gaussdb_mysql_parameter_template_compare_test.go new file mode 100644 index 0000000000..7c40280a4b --- /dev/null +++ b/huaweicloud/services/acceptance/gaussdb/resource_huaweicloud_gaussdb_mysql_parameter_template_compare_test.go @@ -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) +} diff --git a/huaweicloud/services/gaussdb/huaweicloud_gaussdb_mysql_parameter_template_compare.go b/huaweicloud/services/gaussdb/huaweicloud_gaussdb_mysql_parameter_template_compare.go new file mode 100644 index 0000000000..c84dc1e973 --- /dev/null +++ b/huaweicloud/services/gaussdb/huaweicloud_gaussdb_mysql_parameter_template_compare.go @@ -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, + }, + } +}