From 02110e7c98df79fd56e50ee82c332f5b3418f7a2 Mon Sep 17 00:00:00 2001 From: cts01586841 Date: Wed, 11 Dec 2024 14:53:34 +0800 Subject: [PATCH] resource/alicloud_db_instance:fix_add_ModifyDBInstanceConfig --- alicloud/resource_alicloud_db_instance.go | 94 ++++++++++++++++++- .../resource_alicloud_db_instance_test.go | 30 ++++++ website/docs/r/db_instance.html.markdown | 11 ++- 3 files changed, 133 insertions(+), 2 deletions(-) diff --git a/alicloud/resource_alicloud_db_instance.go b/alicloud/resource_alicloud_db_instance.go index ba8bba6a9825..03719584eaa5 100644 --- a/alicloud/resource_alicloud_db_instance.go +++ b/alicloud/resource_alicloud_db_instance.go @@ -397,7 +397,20 @@ func resourceAliCloudDBInstance() *schema.Resource { Type: schema.TypeString, Optional: true, DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { - return d.Get("engine").(string) != "PostgreSQL" && d.Get("engine").(string) != "MySQL" && d.Get("engine").(string) != "SQLServer" + engine := d.Get("engine").(string) + encryptionKey := d.Get("encryption_key").(string) + if engine != "PostgreSQL" && engine != "MySQL" && engine != "SQLServer" { + return true + } + if engine == "PostgreSQL" { + if encryptionKey == "ServiceKey" && old != "" { + return true + } + if encryptionKey == "disable" && old == "" { + return true + } + } + return false }, }, "zone_id_slave_a": { @@ -606,6 +619,20 @@ func resourceAliCloudDBInstance() *schema.Resource { Optional: true, ValidateFunc: StringInSlice([]string{"Up", "Down", "TempUpgrade", "Serverless"}, false), }, + "pg_bouncer_enabled": { + Type: schema.TypeBool, + Optional: true, + DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { + return d.Get("engine").(string) != "PostgreSQL" + }, + }, + "recovery_model": { + Type: schema.TypeString, + Optional: true, + DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool { + return d.Get("engine").(string) != "SQLServer" + }, + }, }, } } @@ -1561,6 +1588,58 @@ func resourceAliCloudDBInstanceUpdate(d *schema.ResourceData, meta interface{}) } } + // 定义一个处理配置变更的函数 + handleConfigChange := func(configName string, configValue interface{}) error { + action := "ModifyDBInstanceConfig" + request := map[string]interface{}{ + "RegionId": client.RegionId, + "DBInstanceId": d.Id(), + "ConfigName": configName, + "ConfigValue": configValue, + } + response, err := conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2014-08-15"), StringPointer("AK"), nil, request, &runtime) + if err != nil { + return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) + } + addDebug(action, response, request) + + stateConf := BuildStateConf([]string{}, []string{"Running"}, d.Timeout(schema.TimeoutUpdate), 0, rdsService.RdsDBInstanceStateRefreshFunc(d.Id(), []string{"Deleting"})) + if _, err := stateConf.WaitForState(); err != nil { + return WrapErrorf(err, IdMsg, d.Id()) + } + return nil + } + if "PostgreSQL" == d.Get("engine").(string) { + if d.HasChange("pg_bouncer_enabled") { + if err := handleConfigChange("pgbouncer", d.Get("pg_bouncer_enabled")); err != nil { + return err + } + } + + if d.HasChange("encryption_key") { + if v, ok := d.GetOk("encryption_key"); ok { + var configValue string + if v.(string) == "disabled" { + configValue = "disabled" + } else { + configValue = v.(string) + } + if err := handleConfigChange("encryptionKey", configValue); err != nil { + return err + } + } + } + + } + + if "SQLServer" == d.Get("engine").(string) { + if d.HasChange("recovery_model") { + if err := handleConfigChange("backup_recovery_model", d.Get("recovery_model")); err != nil { + return err + } + } + } + if !d.IsNewResource() && (d.HasChange("target_minor_version") || d.HasChange("upgrade_db_instance_kernel_version")) { action := "UpgradeDBInstanceKernelVersion" request := map[string]interface{}{ @@ -1659,6 +1738,16 @@ func resourceAliCloudDBInstanceRead(d *schema.ResourceData, meta interface{}) er if err != nil { return WrapError(err) } + DBInstanceEncryptionKey, err := rdsService.DescribeDBInstanceEncryptionKey(d.Id()) + if err != nil { + return WrapError(err) + } + if DBInstanceEncryptionKey["EncryptionKey"] == "" { + d.Set("encryption_key", "") + } else { + d.Set("encryption_key", DBInstanceEncryptionKey["EncryptionKey"]) + } + describeDBInstanceHAConfigObject, err := rdsService.DescribeDBInstanceHAConfig(d.Id()) hostInstanceInfos := describeDBInstanceHAConfigObject["HostInstanceInfos"].(map[string]interface{})["NodeInfo"].([]interface{}) var nodeId string @@ -1708,6 +1797,7 @@ func resourceAliCloudDBInstanceRead(d *schema.ResourceData, meta interface{}) er d.Set("zone_id", instance["ZoneId"]) d.Set("status", instance["DBInstanceStatus"]) d.Set("create_time", instance["CreationTime"]) + d.Set("pg_bouncer_enabled", instance["PGBouncerEnabled"]) // MySQL Serverless instance query PayType return SERVERLESS, need to be consistent with the participant. payType := instance["PayType"] @@ -1763,6 +1853,8 @@ func resourceAliCloudDBInstanceRead(d *schema.ResourceData, meta interface{}) er } else if len(slaveZones) == 1 { d.Set("zone_id_slave_a", slaveZones[0].(map[string]interface{})["ZoneId"]) } + recoveryModel := instance["Extra"].(map[string]interface{})["RecoveryModel"] + d.Set("recovery_model", recoveryModel) if sqlCollectorPolicy["SQLCollectorStatus"] == "Enable" { d.Set("sql_collector_status", "Enabled") } else { diff --git a/alicloud/resource_alicloud_db_instance_test.go b/alicloud/resource_alicloud_db_instance_test.go index b9258e1e84c6..a3dd07144cc4 100644 --- a/alicloud/resource_alicloud_db_instance_test.go +++ b/alicloud/resource_alicloud_db_instance_test.go @@ -1017,6 +1017,16 @@ func TestAccAliCloudRdsDBInstance_SQLServer(t *testing.T) { }), ), }, + { + Config: testAccConfig(map[string]interface{}{ + "recovery_model": "simple", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "recovery_model": "simple", + }), + ), + }, { Config: testAccConfig(map[string]interface{}{ "instance_storage": "50", @@ -1249,6 +1259,26 @@ func TestAccAliCloudRdsDBInstance_PostgreSQL_12_0(t *testing.T) { }), ), }, + { + Config: testAccConfig(map[string]interface{}{ + "pg_bouncer_enabled": "true", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "pg_bouncer_enabled": "true", + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "pg_bouncer_enabled": "false", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "pg_bouncer_enabled": "false", + }), + ), + }, { Config: testAccConfig(map[string]interface{}{ "target_minor_version": "rds_postgres_1200_20240229", diff --git a/website/docs/r/db_instance.html.markdown b/website/docs/r/db_instance.html.markdown index 1c949f4721be..b337a9e536db 100644 --- a/website/docs/r/db_instance.html.markdown +++ b/website/docs/r/db_instance.html.markdown @@ -668,7 +668,12 @@ The following arguments are supported: -> **NOTE:** The attribute `ssl_action` will be ignored when setting `instance_charge_type = "Serverless"` for SQLServer, PostgreSQL or MariaDB. * `ssl_connection_string` - (Optional, Available since v1.198.0) The internal or public endpoint for which the server certificate needs to be created or updated. * `tde_status` - (Optional, Available since 1.90.0) The TDE(Transparent Data Encryption) status. After TDE is turned on, it cannot be turned off. See more [engine and engineVersion limitation](https://www.alibabacloud.com/help/zh/doc-detail/26256.htm). +-> **NOTE:** When creating an instance and enabling disk encryption, you can only use the Key ID; you cannot use the ServiceKey. * `encryption_key` - (Optional, Available since 1.109.0) The key id of the KMS. Used for encrypting a disk if not null. Only for PostgreSQL, MySQL and SQLServer. + When the instance is PostgreSQL, this parameter can be used to enable, modify, and disable cloud disk encryption.Value range: + - ServiceKey: Enable disk encryption using the service-managed key (Default Service CMK) automatically generated by Alibaba Cloud RDS. + - <密钥>: Use a custom key to enable cloud disk encryption or change the current key. For example: 494c98ce-f2b5-48ab-96ab-36c986b6****. + - disabled: Turn off cloud disk encryption. * `ca_type` - (Optional, Available since 1.124.1) The type of the server certificate. This parameter is supported only when the instance runs PostgreSQL or MySQL with standard or enhanced SSDs. If you set the SSLEnabled parameter to 1, the default value of this parameter is aliyun. **NOTE:** From version 1.231.0, `ca_type` start support `MySQL` engine. Value range: - aliyun: a cloud certificate - custom: a custom certificate @@ -695,7 +700,11 @@ The following arguments are supported: * `ha_config` - (Optional, Available since 1.128.0) The primary/secondary switchover mode of the instance. Default value: Auto. Valid values: - Auto: The system automatically switches over services from the primary to secondary instances in the event of a fault. - Manual: You must manually switch over services from the primary to secondary instances in the event of a fault. - +* `pg_bouncer_enabled`- (Optional, Available since 1.238.0) Modify the PgBouncer feature of the RDS PostgreSQL instance. Valid values: + - true: enable. + - false: disable. +* `recovery_model` - (Optional, Available since 1.238.0) Enable the Simple Recovery Model for an RDS SQL Server Instance.The Simple Recovery Model feature is only supported by the Basic Series of RDS SQL Server instances. Once this feature is enabled, it cannot be disabled.Valid values: + - simple: Enable Simple Recovery. -> **NOTE:** If you set this parameter to Manual, you must specify the ManualHATime parameter. * `manual_ha_time` - (Optional, Available since 1.128.0) The time after when you want to enable automatic primary/secondary switchover. At most, you can set this parameter to 23:59:59 seven days later. Specify the time in the ISO 8601 standard in the yyyy-MM-ddTHH:mm:ssZ format. The time must be in UTC.