From f91709f1a3e636a5fd1124ea2f689fc17eacb0b2 Mon Sep 17 00:00:00 2001 From: houpeng80 <114376095+houpeng80@users.noreply.github.com> Date: Tue, 10 Sep 2024 11:38:27 +0800 Subject: [PATCH] feat(GaussDBforMySQL): gaussdb mysql parameter template support copy (#5531) --- .../gaussdb_mysql_parameter_template.md | 79 +++++- huaweicloud/services/acceptance/acceptance.go | 17 ++ ...d_gaussdb_mysql_parameter_template_test.go | 167 ++++++++++++- ...icloud_gaussdb_mysql_parameter_template.go | 227 ++++++++++++++---- 4 files changed, 430 insertions(+), 60 deletions(-) diff --git a/docs/resources/gaussdb_mysql_parameter_template.md b/docs/resources/gaussdb_mysql_parameter_template.md index 1117e70420..ef6a6d4e78 100644 --- a/docs/resources/gaussdb_mysql_parameter_template.md +++ b/docs/resources/gaussdb_mysql_parameter_template.md @@ -2,7 +2,8 @@ subcategory: "GaussDB(for MySQL)" layout: "huaweicloud" page_title: "HuaweiCloud: huaweicloud_gaussdb_mysql_parameter_template" -description: "" +description: |- + Manages a GaussDB MySQL parameter template resource within HuaweiCloud. --- # huaweicloud_gaussdb_mysql_parameter_template @@ -11,9 +12,35 @@ Manages a GaussDB MySQL parameter template resource within HuaweiCloud. ## Example Usage +### create parameter template + ```hcl +resource "huaweicloud_gaussdb_mysql_parameter_template" "test" { + name = "test_mysql_parameter_template" +} +``` + +### replica parameter template from existed configuration + +```hcl +variable "source_configuration_id" {} + resource "huaweicloud_gaussdb_mysql_parameter_template" "test" { - name = "test_mysql_parameter_template" + name = "test_copy_from_configuration" + source_configuration_id = var.source_configuration_id +} +``` + +### replica parameter template from existed instance + +```hcl +variable "instance_id" {} +variable "instance_configuration_id" {} + +resource "huaweicloud_gaussdb_mysql_parameter_template" "test" { + name = "test_copy_from_instance" + instance_id = var.instance_id + instance_configuration_id = var.instance_configuration_id } ``` @@ -38,6 +65,31 @@ The following arguments are supported: Changing this parameter will create a new resource. + -> **NOTE:** It is mandatory when `datastore_engine` is specified. + +* `source_configuration_id` - (Optional, String, ForceNew) Specifies the source parameter template ID. + + Changing this parameter will create a new resource. + +* `instance_id` - (Optional, String, ForceNew) Specifies the ID of the GaussDB MySQL instance. + + Changing this parameter will create a new resource. + +* `instance_configuration_id` - (Optional, String, ForceNew) Specifies the parameter template ID of the GaussDB MySQL + instance. + + Changing this parameter will create a new resource. + + -> **NOTE:** It is mandatory when `instance_id` is specified. + +-> **NOTE:** 1. At most one of `datastore_engine`, `source_configuration_id` and `instance_id` can be specified. +
2. If `source_configuration_id` is specified, then the resource will replicate the parameter template specified + by `source_configuration_id`. +
3. If `instance_id` is specified, then the resource will replicate the parameter template of the GaussDB MySQL + instance specified by `instance_id`. +
4. If `source_configuration_id` and `instance_id` are both not specified, then a new parameter template will be + created directly. + * `parameter_values` - (Optional, Map) Specifies the mapping between parameter names and parameter values. You can specify parameter values based on a default parameter template. @@ -47,10 +99,10 @@ 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. +* `created_at` - Indicates the creation time in the **yyyy-MM-ddTHH:mm:ssZ** format. T is the separator between calendar and hourly notation of time. Z indicates the time zone offset. -* `updated_at` - Indicates the update time in the "yyyy-MM-ddTHH:mm:ssZ" format. +* `updated_at` - Indicates the update time in the **yyyy-MM-ddTHH:mm:ssZ** format. T is the separator between calendar and hourly notation of time. Z indicates the time zone offset. ## Import @@ -60,3 +112,22 @@ The GaussDB Mysql parameter template can be imported using the `id`, e.g. ```bash $ terraform import huaweicloud_gaussdb_mysql_parameter_template.test ``` + +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`, `instance_id` +and `instance_configuration_id`. It is generally recommended running `terraform plan` after importing an GaussDB MySQL +parameter template. You can then decide if changes should be applied to the GaussDB MySQL parameter template, or the +GaussDB MySQL parameter template definition should be updated to align with the instance. Also, you can ignore changes +as below. + +```hcl +resource "huaweicloud_gaussdb_mysql_parameter_template" "test" { + ... + + lifecycle { + ignore_changes = [ + source_configuration_id, instance_id, instance_configuration_id, + ] + } +} +``` diff --git a/huaweicloud/services/acceptance/acceptance.go b/huaweicloud/services/acceptance/acceptance.go index 64b61c37ff..b3d0d40fcd 100644 --- a/huaweicloud/services/acceptance/acceptance.go +++ b/huaweicloud/services/acceptance/acceptance.go @@ -156,6 +156,9 @@ var ( HW_BUILD_IMAGE_URL = os.Getenv("HW_BUILD_IMAGE_URL") // SWR Image URL for component deployment HW_BUILD_IMAGE_URL_UPDATED = os.Getenv("HW_BUILD_IMAGE_URL_UPDATED") // SWR Image URL for component deployment update + HW_GAUSSDB_MYSQL_INSTANCE_ID = os.Getenv("HW_GAUSSDB_MYSQL_INSTANCE_ID") + HW_GAUSSDB_MYSQL_INSTANCE_CONFIGURATION_ID = os.Getenv("HW_GAUSSDB_MYSQL_INSTANCE_CONFIGURATION_ID") + HW_VOD_WATERMARK_FILE = os.Getenv("HW_VOD_WATERMARK_FILE") HW_VOD_MEDIA_ASSET_FILE = os.Getenv("HW_VOD_MEDIA_ASSET_FILE") @@ -1129,6 +1132,20 @@ func TestAccPreCheckImageUrlUpdated(t *testing.T) { } } +// lintignore:AT003 +func TestAccPreCheckGaussDBMysqlInstanceId(t *testing.T) { + if HW_GAUSSDB_MYSQL_INSTANCE_ID == "" { + t.Skip("HW_GAUSSDB_MYSQL_INSTANCE_ID must be set for GaussDB MySQL acceptance tests.") + } +} + +// lintignore:AT003 +func TestAccPreCheckGaussDBMysqlInstanceConfigurationId(t *testing.T) { + if HW_GAUSSDB_MYSQL_INSTANCE_CONFIGURATION_ID == "" { + t.Skip("HW_GAUSSDB_MYSQL_INSTANCE_CONFIGURATION_ID must be set for GaussDB MySQL acceptance tests.") + } +} + // lintignore:AT003 func TestAccPreCheckVODWatermark(t *testing.T) { if HW_VOD_WATERMARK_FILE == "" { diff --git a/huaweicloud/services/acceptance/gaussdb/resource_huaweicloud_gaussdb_mysql_parameter_template_test.go b/huaweicloud/services/acceptance/gaussdb/resource_huaweicloud_gaussdb_mysql_parameter_template_test.go index 520aced5b9..824ff8b36c 100644 --- a/huaweicloud/services/acceptance/gaussdb/resource_huaweicloud_gaussdb_mysql_parameter_template_test.go +++ b/huaweicloud/services/acceptance/gaussdb/resource_huaweicloud_gaussdb_mysql_parameter_template_test.go @@ -103,7 +103,7 @@ func TestAccGaussDBMysqlTemplate_basic(t *testing.T) { }) } -func TestAccGaussDBMysqlTemplate_with_name(t *testing.T) { +func TestAccGaussDBMysqlTemplate_with_configuration(t *testing.T) { var obj interface{} name := acceptance.RandomAccResourceName() @@ -122,21 +122,105 @@ func TestAccGaussDBMysqlTemplate_with_name(t *testing.T) { CheckDestroy: rc.CheckResourceDestroy(), Steps: []resource.TestStep{ { - Config: testParameterTemplate_with_name(name), + Config: testParameterTemplate_with_configuration(name), Check: resource.ComposeTestCheckFunc( rc.CheckResourceExists(), resource.TestCheckResourceAttr(rName, "name", name), + resource.TestCheckResourceAttr(rName, "description", + "test gaussdb mysql parameter template"), + resource.TestCheckResourceAttr(rName, "datastore_engine", "gaussdb-mysql"), + resource.TestCheckResourceAttr(rName, "datastore_version", "8.0"), + resource.TestCheckResourceAttr(rName, "parameter_values.auto_increment_offset", "5"), resource.TestCheckResourceAttrSet(rName, "created_at"), resource.TestCheckResourceAttrSet(rName, "updated_at"), ), }, { - Config: testParameterTemplate_with_name(updateName), + Config: testParameterTemplate_with_configuration_update(name, updateName), Check: resource.ComposeTestCheckFunc( rc.CheckResourceExists(), resource.TestCheckResourceAttr(rName, "name", updateName), + resource.TestCheckResourceAttr(rName, "description", + "test gaussdb mysql parameter template update"), + resource.TestCheckResourceAttr(rName, "datastore_engine", "gaussdb-mysql"), + resource.TestCheckResourceAttr(rName, "datastore_version", "8.0"), + resource.TestCheckResourceAttr(rName, "parameter_values.auto_increment_increment", "6"), + resource.TestCheckResourceAttr(rName, "parameter_values.auto_increment_offset", "8"), ), }, + { + ResourceName: rName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{ + "parameter_values", + "source_configuration_id", + }, + }, + }, + }) +} + +func TestAccGaussDBMysqlTemplate_with_instance(t *testing.T) { + var obj interface{} + + name := acceptance.RandomAccResourceName() + updateName := acceptance.RandomAccResourceName() + rName := "huaweicloud_gaussdb_mysql_parameter_template.test" + + rc := acceptance.InitResourceCheck( + rName, + &obj, + getGaussDBMysqlTemplateResourceFunc, + ) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + acceptance.TestAccPreCheck(t) + acceptance.TestAccPreCheckGaussDBMysqlInstanceId(t) + acceptance.TestAccPreCheckGaussDBMysqlInstanceConfigurationId(t) + }, + ProviderFactories: acceptance.TestAccProviderFactories, + CheckDestroy: rc.CheckResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: testParameterTemplate_with_instance(name), + Check: resource.ComposeTestCheckFunc( + rc.CheckResourceExists(), + resource.TestCheckResourceAttr(rName, "name", name), + resource.TestCheckResourceAttr(rName, "description", + "test gaussdb mysql parameter template"), + resource.TestCheckResourceAttr(rName, "datastore_engine", "gaussdb-mysql"), + resource.TestCheckResourceAttr(rName, "datastore_version", "8.0"), + resource.TestCheckResourceAttr(rName, "parameter_values.auto_increment_increment", "4"), + resource.TestCheckResourceAttr(rName, "parameter_values.auto_increment_offset", "5"), + resource.TestCheckResourceAttrSet(rName, "created_at"), + resource.TestCheckResourceAttrSet(rName, "updated_at"), + ), + }, + { + Config: testParameterTemplate_with_instance_update(updateName), + Check: resource.ComposeTestCheckFunc( + rc.CheckResourceExists(), + resource.TestCheckResourceAttr(rName, "name", updateName), + resource.TestCheckResourceAttr(rName, "description", + "test gaussdb mysql parameter template update"), + resource.TestCheckResourceAttr(rName, "datastore_engine", "gaussdb-mysql"), + resource.TestCheckResourceAttr(rName, "datastore_version", "8.0"), + resource.TestCheckResourceAttr(rName, "parameter_values.auto_increment_increment", "6"), + resource.TestCheckResourceAttr(rName, "parameter_values.auto_increment_offset", "8"), + ), + }, + { + ResourceName: rName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{ + "parameter_values", + "instance_id", + "instance_configuration_id", + }, + }, }, }) } @@ -173,10 +257,83 @@ resource "huaweicloud_gaussdb_mysql_parameter_template" "test" { `, name) } -func testParameterTemplate_with_name(name string) string { +func testParameterTemplate_with_configuration(name string) string { return fmt.Sprintf(` +resource "huaweicloud_gaussdb_mysql_parameter_template" "source_template" { + name = "%[1]s_source" + datastore_engine = "gaussdb-mysql" + datastore_version = "8.0" + + parameter_values = { + auto_increment_increment = "4" + } +} + resource "huaweicloud_gaussdb_mysql_parameter_template" "test" { - name = "%s" + name = "%[1]s" + description = "test gaussdb mysql parameter template" + source_configuration_id = huaweicloud_gaussdb_mysql_parameter_template.source_template.id + + parameter_values = { + auto_increment_offset = "5" + } } `, name) } + +func testParameterTemplate_with_configuration_update(name, updateName string) string { + return fmt.Sprintf(` +resource "huaweicloud_gaussdb_mysql_parameter_template" "source_template" { + name = "%[1]s_source" + datastore_engine = "gaussdb-mysql" + datastore_version = "8.0" + + parameter_values = { + auto_increment_increment = "4" + } +} + +resource "huaweicloud_gaussdb_mysql_parameter_template" "test" { + name = "%[2]s" + description = "test gaussdb mysql parameter template update" + source_configuration_id = huaweicloud_gaussdb_mysql_parameter_template.source_template.id + + parameter_values = { + auto_increment_increment = "6" + auto_increment_offset = "8" + } +} +`, name, updateName) +} + +func testParameterTemplate_with_instance(name string) string { + return fmt.Sprintf(` +resource "huaweicloud_gaussdb_mysql_parameter_template" "test" { + name = "%[1]s" + description = "test gaussdb mysql parameter template" + instance_id = "%[2]s" + instance_configuration_id = "%[3]s" + + parameter_values = { + auto_increment_increment = "4" + auto_increment_offset = "5" + } +} +`, name, acceptance.HW_GAUSSDB_MYSQL_INSTANCE_ID, acceptance.HW_GAUSSDB_MYSQL_INSTANCE_CONFIGURATION_ID) +} + +func testParameterTemplate_with_instance_update(name string) string { + return fmt.Sprintf(` +resource "huaweicloud_gaussdb_mysql_parameter_template" "test" { + name = "%[1]s" + description = "test gaussdb mysql parameter template update" + instance_id = "%[2]s" + instance_configuration_id = "%[3]s" + + parameter_values = { + auto_increment_increment = "6" + auto_increment_offset = "8" + } +} +`, name, acceptance.HW_GAUSSDB_MYSQL_INSTANCE_ID, acceptance.HW_GAUSSDB_MYSQL_INSTANCE_CONFIGURATION_ID) +} diff --git a/huaweicloud/services/gaussdb/resource_huaweicloud_gaussdb_mysql_parameter_template.go b/huaweicloud/services/gaussdb/resource_huaweicloud_gaussdb_mysql_parameter_template.go index de7aaa0591..41f92a2ebf 100644 --- a/huaweicloud/services/gaussdb/resource_huaweicloud_gaussdb_mysql_parameter_template.go +++ b/huaweicloud/services/gaussdb/resource_huaweicloud_gaussdb_mysql_parameter_template.go @@ -7,13 +7,13 @@ package gaussdb import ( "context" + "fmt" "log" "strings" "github.com/hashicorp/go-multierror" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - "github.com/jmespath/go-jmespath" "github.com/chnsz/golangsdk" @@ -23,6 +23,8 @@ import ( ) // @API GaussDBforMySQL POST /v3/{project_id}/configurations +// @API GaussDBforMySQL POST /v3/{project_id}/configurations/{configuration_id}/copy +// @API GaussDBforMySQL POST /v3/{project_id}/instances/{instance_id}/configurations/{configuration_id}/copy // @API GaussDBforMySQL PUT /v3/{project_id}/configurations/{configuration_id} // @API GaussDBforMySQL GET /v3/{project_id}/configurations/{configuration_id} // @API GaussDBforMySQL DELETE /v3/{project_id}/configurations/{configuration_id} @@ -55,21 +57,43 @@ func ResourceGaussDBMysqlTemplate() *schema.Resource { Description: `Specifies the parameter template description.`, }, "datastore_engine": { - Type: schema.TypeString, - Optional: true, - Computed: true, - ForceNew: true, - Description: `Specifies the DB engine.`, + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + RequiredWith: []string{"datastore_version"}, + ConflictsWith: []string{"source_configuration_id", "instance_id"}, + Description: `Specifies the DB engine.`, }, "datastore_version": { - Type: schema.TypeString, - Optional: true, - Computed: true, - ForceNew: true, - Description: `Specifies the DB version.`, - RequiredWith: []string{ - "datastore_engine", - }, + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + RequiredWith: []string{"datastore_engine"}, + Description: `Specifies the DB version.`, + }, + "source_configuration_id": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + ConflictsWith: []string{"datastore_engine", "instance_id"}, + Description: `Specifies the source parameter template ID.`, + }, + "instance_id": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + RequiredWith: []string{"instance_configuration_id"}, + ConflictsWith: []string{"datastore_engine", "source_configuration_id"}, + Description: `Specifies the ID of the GaussDB MySQL instance.`, + }, + "instance_configuration_id": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + RequiredWith: []string{"instance_id"}, + Description: `Specifies the parameter template ID of the GaussDB MySQL instance.`, }, "parameter_values": { Type: schema.TypeMap, @@ -98,45 +122,130 @@ func resourceParameterTemplateCreate(ctx context.Context, d *schema.ResourceData // createGaussDBMysqlParameterTemplate: create a GaussDB MySQL parameter Template var ( - createParameterTemplateHttpUrl = "v3/{project_id}/configurations" - createParameterTemplateProduct = "gaussdb" + product = "gaussdb" ) - createParameterTemplateClient, err := cfg.NewServiceClient(createParameterTemplateProduct, region) + client, err := cfg.NewServiceClient(product, region) if err != nil { - return diag.Errorf("error creating GaussDB Client: %s", err) + return diag.Errorf("error creating GaussDB client: %s", err) + } + + var id string + isCopy := false + if _, ok := d.GetOk("source_configuration_id"); ok { + id, err = copyParameterTemplate(d, client) + if err != nil { + return diag.FromErr(err) + } + isCopy = true + } else if _, ok = d.GetOk("instance_id"); ok { + id, err = copyInstanceParameterTemplate(d, client) + if err != nil { + return diag.FromErr(err) + } + isCopy = true + } else { + id, err = createParameterTemplate(d, client) + if err != nil { + return diag.FromErr(err) + } } - createParameterTemplatePath := createParameterTemplateClient.Endpoint + createParameterTemplateHttpUrl - createParameterTemplatePath = strings.ReplaceAll(createParameterTemplatePath, "{project_id}", - createParameterTemplateClient.ProjectID) + d.SetId(id) + + if _, ok := d.GetOk("parameter_values"); ok && isCopy { + err = updateParameterTemplate(d, client) + if err != nil { + return diag.FromErr(err) + } + } + + return resourceParameterTemplateRead(ctx, d, meta) +} + +func createParameterTemplate(d *schema.ResourceData, client *golangsdk.ServiceClient) (string, error) { + httpUrl := "v3/{project_id}/configurations" + createPath := client.Endpoint + httpUrl + createPath = strings.ReplaceAll(createPath, "{project_id}", client.ProjectID) - createParameterTemplateOpt := golangsdk.RequestOpts{ + createOpt := golangsdk.RequestOpts{ KeepResponseBody: true, } - createParameterTemplateOpt.JSONBody = utils.RemoveNil(buildCreateParameterTemplateBodyParams(d)) - createParameterTemplateResp, err := createParameterTemplateClient.Request("POST", - createParameterTemplatePath, &createParameterTemplateOpt) + createOpt.JSONBody = utils.RemoveNil(buildCreateParameterTemplateBodyParams(d)) + createResp, err := client.Request("POST", createPath, &createOpt) if err != nil { - return diag.Errorf("error creating GaussDB MySQL Parameter Template: %s", err) + return "", fmt.Errorf("error creating GaussDB MySQL parameter template: %s", err) } - createParameterTemplateRespBody, err := utils.FlattenResponse(createParameterTemplateResp) + createRespBody, err := utils.FlattenResponse(createResp) if err != nil { - return diag.FromErr(err) + return "", err + } + + id := utils.PathSearch("configurations.id", createRespBody, "").(string) + if id == "" { + return "", fmt.Errorf("error creating GaussDB MySQL parameter template: ID is not found in API response") } + return id, nil +} - id, err := jmespath.Search("configurations.id", createParameterTemplateRespBody) +func copyParameterTemplate(d *schema.ResourceData, client *golangsdk.ServiceClient) (string, error) { + httpUrl := "v3/{project_id}/configurations/{configuration_id}/copy" + createPath := client.Endpoint + httpUrl + createPath = strings.ReplaceAll(createPath, "{project_id}", client.ProjectID) + createPath = strings.ReplaceAll(createPath, "{configuration_id}", d.Get("source_configuration_id").(string)) + + createOpt := golangsdk.RequestOpts{ + KeepResponseBody: true, + } + createOpt.JSONBody = utils.RemoveNil(buildCopyParameterTemplateBodyParams(d)) + createResp, err := client.Request("POST", createPath, &createOpt) if err != nil { - return diag.Errorf("error creating GaussDB MySQL Parameter Template: ID is not found in API response") + return "", fmt.Errorf("error creating GaussDB MySQL parameter template: %s", err) } - d.SetId(id.(string)) - return resourceParameterTemplateRead(ctx, d, meta) + createRespBody, err := utils.FlattenResponse(createResp) + if err != nil { + return "", err + } + + id := utils.PathSearch("configuration_id", createRespBody, "").(string) + if id == "" { + return "", fmt.Errorf("error creating GaussDB MySQL parameter template: ID is not found in API response") + } + return id, nil +} + +func copyInstanceParameterTemplate(d *schema.ResourceData, client *golangsdk.ServiceClient) (string, error) { + httpUrl := "v3/{project_id}/instances/{instance_id}/configurations/{configuration_id}/copy" + createPath := client.Endpoint + httpUrl + createPath = strings.ReplaceAll(createPath, "{project_id}", client.ProjectID) + createPath = strings.ReplaceAll(createPath, "{instance_id}", d.Get("instance_id").(string)) + createPath = strings.ReplaceAll(createPath, "{configuration_id}", d.Get("instance_configuration_id").(string)) + + createOpt := golangsdk.RequestOpts{ + KeepResponseBody: true, + } + createOpt.JSONBody = utils.RemoveNil(buildCopyParameterTemplateBodyParams(d)) + createResp, err := client.Request("POST", createPath, &createOpt) + if err != nil { + return "", fmt.Errorf("error creating GaussDB MySQL parameter template: %s", err) + } + + createRespBody, err := utils.FlattenResponse(createResp) + if err != nil { + return "", err + } + + id := utils.PathSearch("configuration_id", createRespBody, "").(string) + if id == "" { + return "", fmt.Errorf("error creating GaussDB MySQL parameter template: ID is not found in API response") + } + return id, nil } func buildCreateParameterTemplateBodyParams(d *schema.ResourceData) map[string]interface{} { bodyParams := map[string]interface{}{ - "name": utils.ValueIgnoreEmpty(d.Get("name")), + "name": d.Get("name"), "description": utils.ValueIgnoreEmpty(d.Get("description")), "datastore": buildCreateParameterTemplateDatastoreChildBody(d), "parameter_values": utils.ValueIgnoreEmpty(d.Get("parameter_values")), @@ -144,6 +253,14 @@ func buildCreateParameterTemplateBodyParams(d *schema.ResourceData) map[string]i return bodyParams } +func buildCopyParameterTemplateBodyParams(d *schema.ResourceData) map[string]interface{} { + bodyParams := map[string]interface{}{ + "name": d.Get("name"), + "description": utils.ValueIgnoreEmpty(d.Get("description")), + } + return bodyParams +} + func buildCreateParameterTemplateDatastoreChildBody(d *schema.ResourceData) map[string]interface{} { datastoreEngine := d.Get("datastore_engine").(string) if datastoreEngine == "" { @@ -236,36 +353,44 @@ func resourceParameterTemplateUpdate(ctx context.Context, d *schema.ResourceData if d.HasChanges(updateGaussDBMysqlTemplateHasChanges...) { // updateParameterTemplate: update the GaussDB MySQL parameter Template var ( - updateParameterTemplateHttpUrl = "v3/{project_id}/configurations/{configuration_id}" - updateParameterTemplateProduct = "gaussdb" + product = "gaussdb" ) - updateParameterTemplateClient, err := cfg.NewServiceClient(updateParameterTemplateProduct, region) + client, err := cfg.NewServiceClient(product, region) if err != nil { - return diag.Errorf("error creating GaussDB Client: %s", err) + return diag.Errorf("error creating GaussDB client: %s", err) } - updateParameterTemplatePath := updateParameterTemplateClient.Endpoint + updateParameterTemplateHttpUrl - updateParameterTemplatePath = strings.ReplaceAll(updateParameterTemplatePath, - "{project_id}", updateParameterTemplateClient.ProjectID) - updateParameterTemplatePath = strings.ReplaceAll(updateParameterTemplatePath, - "{configuration_id}", d.Id()) - - updateParameterTemplateOpt := golangsdk.RequestOpts{ - KeepResponseBody: true, - } - updateParameterTemplateOpt.JSONBody = utils.RemoveNil(buildUpdateParameterTemplateBodyParams(d)) - _, err = updateParameterTemplateClient.Request("PUT", updateParameterTemplatePath, - &updateParameterTemplateOpt) + err = updateParameterTemplate(d, client) if err != nil { - return diag.Errorf("error updating GaussDB MySQL ParameterTemplate: %s", err) + return diag.FromErr(err) } } return resourceParameterTemplateRead(ctx, d, meta) } +func updateParameterTemplate(d *schema.ResourceData, client *golangsdk.ServiceClient) error { + var ( + httpUrl = "v3/{project_id}/configurations/{configuration_id}" + ) + + updatePath := client.Endpoint + httpUrl + updatePath = strings.ReplaceAll(updatePath, "{project_id}", client.ProjectID) + updatePath = strings.ReplaceAll(updatePath, "{configuration_id}", d.Id()) + + updateOpt := golangsdk.RequestOpts{ + KeepResponseBody: true, + } + updateOpt.JSONBody = utils.RemoveNil(buildUpdateParameterTemplateBodyParams(d)) + _, err := client.Request("PUT", updatePath, &updateOpt) + if err != nil { + return fmt.Errorf("error updating GaussDB MySQL parameter template: %s", err) + } + return nil +} + func buildUpdateParameterTemplateBodyParams(d *schema.ResourceData) map[string]interface{} { bodyParams := map[string]interface{}{ - "name": utils.ValueIgnoreEmpty(d.Get("name")), + "name": d.Get("name"), "description": utils.ValueIgnoreEmpty(d.Get("description")), "parameter_values": utils.ValueIgnoreEmpty(d.Get("parameter_values")), }