Skip to content

Commit

Permalink
resource/alicloud_db_instance: fix_add_ModifyDBInstanceConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
chaitiansheng0524 committed Dec 18, 2024
1 parent c671482 commit 658fc3c
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 5 deletions.
100 changes: 98 additions & 2 deletions alicloud/resource_alicloud_db_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,26 @@ 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
},
},
"tde_encryption_key": {
Type: schema.TypeString,
Optional: true,
},
"zone_id_slave_a": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -606,6 +623,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"
},
},
},
}
}
Expand Down Expand Up @@ -1122,7 +1153,7 @@ func resourceAliCloudDBInstanceUpdate(d *schema.ResourceData, meta interface{})
if v, ok := d.GetOk("role_arn"); ok && v.(string) != "" {
request["RoleARN"] = v.(string)
}
if v, ok := d.GetOk("encryption_key"); ok && v.(string) != "" {
if v, ok := d.GetOk("tde_encryption_key"); ok && v.(string) != "" {
request["EncryptionKey"] = v.(string)
if ro, ok := request["RoleARN"].(string); !ok || ro == "" {
roleArn, err := findKmsRoleArn(client, v.(string))
Expand Down Expand Up @@ -1561,6 +1592,57 @@ 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{}{
Expand Down Expand Up @@ -1659,6 +1741,17 @@ func resourceAliCloudDBInstanceRead(d *schema.ResourceData, meta interface{}) er
if err != nil {
return WrapError(err)
}
if "PostgreSQL" == instance["Engine"] {
DBInstanceEncryptionKey, err := rdsService.DescribeDBInstanceEncryptionKey(d.Id())
if err != nil {
return WrapError(err)
}
d.Set("encryption_key", "")
if DBInstanceEncryptionKey["EncryptionKey"] != "" {
d.Set("encryption_key", DBInstanceEncryptionKey["EncryptionKey"])
}
}

describeDBInstanceHAConfigObject, err := rdsService.DescribeDBInstanceHAConfig(d.Id())
hostInstanceInfos := describeDBInstanceHAConfigObject["HostInstanceInfos"].(map[string]interface{})["NodeInfo"].([]interface{})
var nodeId string
Expand Down Expand Up @@ -1708,6 +1801,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"]
Expand Down Expand Up @@ -1763,6 +1857,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 {
Expand Down
38 changes: 36 additions & 2 deletions alicloud/resource_alicloud_db_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ func TestAccAliCloudRdsDBInstance_Mysql_8_0(t *testing.T) {
"value": "70",
},
},
"encryption_key": "${alicloud_kms_key.default.id}",
"tde_encryption_key": "${alicloud_kms_key.default.id}",
"port": "3306",
"connection_string_prefix": connectionStringPrefixSecond,
}),
Expand Down Expand Up @@ -492,7 +492,7 @@ func TestAccAliCloudRdsDBInstance_Mysql_8_0(t *testing.T) {
ResourceName: resourceId,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"force_restart", "db_is_ignore_case", "parameters", "encryption_key", "security_group_id", "storage_auto_scale", "storage_threshold", "storage_upper_bound"},
ImportStateVerifyIgnore: []string{"force_restart", "db_is_ignore_case", "parameters", "tde_encryption_key", "security_group_id", "storage_auto_scale", "storage_threshold", "storage_upper_bound"},
},
},
})
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -1504,6 +1534,7 @@ func TestAccAliCloudRdsDBInstance_PostgreSQL_13_0_SSL(t *testing.T) {
"db_time_zone": "America/New_York",
"connection_string_prefix": "${var.name}",
"port": "5999",
"pg_bouncer_enabled": "true",
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
Expand All @@ -1518,6 +1549,7 @@ func TestAccAliCloudRdsDBInstance_PostgreSQL_13_0_SSL(t *testing.T) {
"port": "5999",
"connection_string_prefix": CHECKSET,
"instance_name": CHECKSET,
"pg_bouncer_enabled": "true",
}),
),
},
Expand Down Expand Up @@ -3166,6 +3198,7 @@ func TestAccAliCloudRdsDBInstance_SQLServer_2019_ServerlessHA(t *testing.T) {
"min_capacity": "2",
},
},
"recovery_model": "simple",
}),
Check: resource.ComposeTestCheckFunc(
testAccCheck(map[string]string{
Expand All @@ -3179,6 +3212,7 @@ func TestAccAliCloudRdsDBInstance_SQLServer_2019_ServerlessHA(t *testing.T) {
"serverless_config.#": "1",
"serverless_config.0.max_capacity": "8",
"serverless_config.0.min_capacity": "2",
"recovery_model": "simple",
}),
),
},
Expand Down
13 changes: 12 additions & 1 deletion website/docs/r/db_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,14 @@ 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.
- <EncryptionKey>: 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.
-> **NOTE:** This parameter is available when the instance runs MySQL.
* `tde_encryption_key` - (Optional, Available since 1.239.0) The ID of the custom key.
* `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
Expand All @@ -695,7 +702,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.239.0) Modify the PgBouncer feature of the RDS PostgreSQL instance. Valid values:
- true: enable.
- false: disable.
* `recovery_model` - (Optional, Available since 1.239.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.

Expand Down

0 comments on commit 658fc3c

Please sign in to comment.