Skip to content

Commit 31db895

Browse files
authored
fix: support binlog setting (#2272)
* fix: support binlog setting * feat: add changelog
1 parent 112c5d6 commit 31db895

File tree

5 files changed

+100
-21
lines changed

5 files changed

+100
-21
lines changed

.changelog/2272.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
tencentcloud_mysql_backup_policy: Support instance binlog setting.
3+
```

tencentcloud/resource_tc_mysql_backup_policy.go

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,12 @@ resource "tencentcloud_mysql_instance" "example" {
5656
5757
resource "tencentcloud_mysql_backup_policy" "example" {
5858
mysql_id = tencentcloud_mysql_instance.example.id
59-
retention_period = 7
60-
backup_model = "physical"
61-
backup_time = "01:00-05:00"
59+
retention_period = 7
60+
backup_model = "physical"
61+
backup_time = "22:00-02:00"
62+
binlog_period = 32
63+
enable_binlog_standby = "off"
64+
binlog_standby_days = 31
6265
}
6366
```
6467
*/
@@ -89,10 +92,10 @@ func resourceTencentCloudMysqlBackupPolicy() *schema.Resource {
8992
},
9093
"retention_period": {
9194
Type: schema.TypeInt,
92-
ValidateFunc: validateIntegerInRange(7, 732),
95+
ValidateFunc: validateIntegerInRange(7, 1830),
9396
Optional: true,
9497
Default: 7,
95-
Description: "Instance backup retention days. Valid value ranges: [7~730]. And default value is `7`.",
98+
Description: "The retention time of backup files, in days. The minimum value is 7 days and the maximum value is 1830 days. And default value is `7`.",
9699
},
97100
"backup_model": {
98101
Type: schema.TypeString,
@@ -109,11 +112,25 @@ func resourceTencentCloudMysqlBackupPolicy() *schema.Resource {
109112
Description: "Instance backup time, in the format of 'HH:mm-HH:mm'. Time setting interval is four hours. Default to `02:00-06:00`. The following value can be supported: `02:00-06:00`, `06:00-10:00`, `10:00-14:00`, `14:00-18:00`, `18:00-22:00`, and `22:00-02:00`.",
110113
},
111114

112-
// Computed values
113115
"binlog_period": {
114116
Type: schema.TypeInt,
117+
Optional: true,
115118
Computed: true,
116-
Description: "Retention period for binlog in days.",
119+
Description: "Binlog retention time, in days. The minimum value is 7 days and the maximum value is 1830 days. This value cannot be set greater than the backup file retention time.",
120+
},
121+
122+
"enable_binlog_standby": {
123+
Type: schema.TypeString,
124+
Optional: true,
125+
Default: "off",
126+
Description: "Whether to enable the log backup standard storage policy, `off` - close, `on` - open, the default is off.",
127+
},
128+
129+
"binlog_standby_days": {
130+
Type: schema.TypeInt,
131+
Optional: true,
132+
Computed: true,
133+
Description: "The standard starting number of days for log backup storage. The log backup will be converted when it reaches the standard starting number of days for storage. The minimum is 30 days and must not be greater than the number of days for log backup retention.",
117134
},
118135
},
119136
}
@@ -159,6 +176,15 @@ func resourceTencentCloudMysqlBackupPolicyRead(d *schema.ResourceData, meta inte
159176
buf.WriteString(fmt.Sprintf("%d:00", *desResponse.Response.StartTimeMax))
160177
_ = d.Set("backup_time", buf.String())
161178
_ = d.Set("binlog_period", int(*desResponse.Response.BinlogExpireDays))
179+
180+
if desResponse.Response.EnableBinlogStandby != nil {
181+
_ = d.Set("enable_binlog_standby", desResponse.Response.EnableBinlogStandby)
182+
}
183+
184+
if desResponse.Response.BinlogStandbyDays != nil {
185+
_ = d.Set("binlog_standby_days", desResponse.Response.BinlogStandbyDays)
186+
}
187+
162188
return nil
163189
})
164190
if err != nil {
@@ -182,17 +208,34 @@ func resourceTencentCloudMysqlBackupPolicyUpdate(d *schema.ResourceData, meta in
182208
retentionPeriod = int64(d.Get("retention_period").(int))
183209
backupModel = d.Get("backup_model").(string)
184210
backupTime = d.Get("backup_time").(string)
211+
212+
binlogExpireDays int64
213+
enableBinlogStandby string
214+
binlogStandbyDays int64
185215
)
186216

187-
if d.HasChange("retention_period") || d.HasChange("backup_model") || d.HasChange("backup_time") {
217+
if v, ok := d.GetOkExists("binlog_period"); ok {
218+
binlogExpireDays = int64(v.(int))
219+
}
220+
221+
if v, ok := d.GetOk("enable_binlog_standby"); ok {
222+
enableBinlogStandby = v.(string)
223+
}
224+
225+
if v, ok := d.GetOkExists("binlog_standby_days"); ok {
226+
binlogStandbyDays = int64(v.(int))
227+
}
228+
229+
if d.HasChange("retention_period") || d.HasChange("backup_model") || d.HasChange("backup_time") ||
230+
d.HasChange("binlog_period") || d.HasChange("enable_binlog_standby") || d.HasChange("binlog_standby_days") {
188231
if backupModel != "physical" {
189232
return fmt.Errorf("`backup_model` only support 'physical'")
190233
}
191234
isUpdate = true
192235
}
193236

194237
if isUpdate {
195-
err := mysqlService.ModifyBackupConfigByMysqlId(ctx, mysqlId, retentionPeriod, backupModel, backupTime)
238+
err := mysqlService.ModifyBackupConfigByMysqlId(ctx, mysqlId, retentionPeriod, backupModel, backupTime, binlogExpireDays, enableBinlogStandby, binlogStandbyDays)
196239
if err != nil {
197240
return err
198241
}
@@ -210,11 +253,14 @@ func resourceTencentCloudMysqlBackupPolicyDelete(d *schema.ResourceData, meta in
210253
mysqlService := MysqlService{client: meta.(*TencentCloudClient).apiV3Conn}
211254

212255
var (
213-
retentionPeriod int64 = 7
214-
backupModel = MYSQL_ALLOW_BACKUP_MODEL[1]
215-
backupTime = MYSQL_ALLOW_BACKUP_TIME[0]
256+
retentionPeriod int64 = 7
257+
backupModel = MYSQL_ALLOW_BACKUP_MODEL[1]
258+
backupTime = MYSQL_ALLOW_BACKUP_TIME[0]
259+
binlogExpireDays int64 = 7
260+
enableBinlogStandby = "off"
261+
binlogStandbyDays int64 = 180
216262
)
217-
err := mysqlService.ModifyBackupConfigByMysqlId(ctx, d.Id(), retentionPeriod, backupModel, backupTime)
263+
err := mysqlService.ModifyBackupConfigByMysqlId(ctx, d.Id(), retentionPeriod, backupModel, backupTime, binlogExpireDays, enableBinlogStandby, binlogStandbyDays)
218264
if err != nil {
219265
return err
220266
}

tencentcloud/resource_tc_mysql_backup_policy_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ func TestAccTencentCloudMysqlBackupPolicyResource_basic(t *testing.T) {
2626
resource.TestCheckResourceAttr("tencentcloud_mysql_backup_policy.mysql_backup_policy", "retention_period", "56"),
2727
resource.TestCheckResourceAttr("tencentcloud_mysql_backup_policy.mysql_backup_policy", "backup_time", "10:00-14:00"),
2828
resource.TestCheckResourceAttrSet("tencentcloud_mysql_backup_policy.mysql_backup_policy", "binlog_period"),
29+
resource.TestCheckResourceAttr("tencentcloud_mysql_backup_policy.mysql_backup_policy", "binlog_period", "35"),
30+
resource.TestCheckResourceAttr("tencentcloud_mysql_backup_policy.mysql_backup_policy", "enable_binlog_standby", "on"),
31+
resource.TestCheckResourceAttr("tencentcloud_mysql_backup_policy.mysql_backup_policy", "binlog_standby_days", "33"),
2932
),
3033
},
3134
{
@@ -36,6 +39,9 @@ func TestAccTencentCloudMysqlBackupPolicyResource_basic(t *testing.T) {
3639
resource.TestCheckResourceAttr("tencentcloud_mysql_backup_policy.mysql_backup_policy", "retention_period", "80"),
3740
resource.TestCheckResourceAttr("tencentcloud_mysql_backup_policy.mysql_backup_policy", "backup_time", "06:00-10:00"),
3841
resource.TestCheckResourceAttrSet("tencentcloud_mysql_backup_policy.mysql_backup_policy", "binlog_period"),
42+
resource.TestCheckResourceAttr("tencentcloud_mysql_backup_policy.mysql_backup_policy", "binlog_period", "32"),
43+
resource.TestCheckResourceAttr("tencentcloud_mysql_backup_policy.mysql_backup_policy", "enable_binlog_standby", "off"),
44+
resource.TestCheckResourceAttr("tencentcloud_mysql_backup_policy.mysql_backup_policy", "binlog_standby_days", "31"),
3945
),
4046
},
4147
},
@@ -112,6 +118,9 @@ resource "tencentcloud_mysql_backup_policy" "mysql_backup_policy" {
112118
mysql_id = local.mysql_id
113119
retention_period = 56
114120
backup_time = "10:00-14:00"
121+
binlog_period = 35
122+
enable_binlog_standby = "on"
123+
binlog_standby_days = 33
115124
}`, CommonPresetMysql)
116125
}
117126

@@ -122,5 +131,8 @@ resource "tencentcloud_mysql_backup_policy" "mysql_backup_policy" {
122131
mysql_id = local.mysql_id
123132
retention_period = 80
124133
backup_time = "06:00-10:00"
134+
binlog_period = 32
135+
enable_binlog_standby = "off"
136+
binlog_standby_days = 31
125137
}`, CommonPresetMysql)
126138
}

tencentcloud/service_tencentcloud_mysql.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ func (me *MysqlService) DescribeBackupConfigByMysqlId(ctx context.Context, mysql
159159
return
160160
}
161161

162-
func (me *MysqlService) ModifyBackupConfigByMysqlId(ctx context.Context, mysqlId string,
163-
retentionPeriod int64, backupModel, backupTime string) (errRet error) {
162+
func (me *MysqlService) ModifyBackupConfigByMysqlId(ctx context.Context, mysqlId string, retentionPeriod int64, backupModel,
163+
backupTime string, binlogExpireDays int64, enableBinlogStandby string, binlogStandbyDays int64) (errRet error) {
164164

165165
logId := getLogId(ctx)
166166
request := cdb.NewModifyBackupConfigRequest()
@@ -169,6 +169,18 @@ func (me *MysqlService) ModifyBackupConfigByMysqlId(ctx context.Context, mysqlId
169169
request.StartTime = &backupTime
170170
request.BackupMethod = &backupModel
171171

172+
if binlogExpireDays > 0 {
173+
request.BinlogExpireDays = &binlogExpireDays
174+
}
175+
176+
if enableBinlogStandby != "" {
177+
request.EnableBinlogStandby = &enableBinlogStandby
178+
}
179+
180+
if binlogStandbyDays > 0 {
181+
request.BinlogStandbyDays = &binlogStandbyDays
182+
}
183+
172184
defer func() {
173185
if errRet != nil {
174186
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",

website/docs/r/mysql_backup_policy.html.markdown

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,13 @@ resource "tencentcloud_mysql_instance" "example" {
6565
}
6666
6767
resource "tencentcloud_mysql_backup_policy" "example" {
68-
mysql_id = tencentcloud_mysql_instance.example.id
69-
retention_period = 7
70-
backup_model = "physical"
71-
backup_time = "01:00-05:00"
68+
mysql_id = tencentcloud_mysql_instance.example.id
69+
retention_period = 7
70+
backup_model = "physical"
71+
backup_time = "22:00-02:00"
72+
binlog_period = 32
73+
enable_binlog_standby = "off"
74+
binlog_standby_days = 31
7275
}
7376
```
7477

@@ -79,13 +82,16 @@ The following arguments are supported:
7982
* `mysql_id` - (Required, String, ForceNew) Instance ID to which policies will be applied.
8083
* `backup_model` - (Optional, String) Backup method. Supported values include: `physical` - physical backup.
8184
* `backup_time` - (Optional, String) Instance backup time, in the format of 'HH:mm-HH:mm'. Time setting interval is four hours. Default to `02:00-06:00`. The following value can be supported: `02:00-06:00`, `06:00-10:00`, `10:00-14:00`, `14:00-18:00`, `18:00-22:00`, and `22:00-02:00`.
82-
* `retention_period` - (Optional, Int) Instance backup retention days. Valid value ranges: [7~730]. And default value is `7`.
85+
* `binlog_period` - (Optional, Int) Binlog retention time, in days. The minimum value is 7 days and the maximum value is 1830 days. This value cannot be set greater than the backup file retention time.
86+
* `binlog_standby_days` - (Optional, Int) The standard starting number of days for log backup storage. The log backup will be converted when it reaches the standard starting number of days for storage. The minimum is 30 days and must not be greater than the number of days for log backup retention.
87+
* `enable_binlog_standby` - (Optional, String) Whether to enable the log backup standard storage policy, `off` - close, `on` - open, the default is off.
88+
* `retention_period` - (Optional, Int) The retention time of backup files, in days. The minimum value is 7 days and the maximum value is 1830 days. And default value is `7`.
8389

8490
## Attributes Reference
8591

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

8894
* `id` - ID of the resource.
89-
* `binlog_period` - Retention period for binlog in days.
95+
9096

9197

0 commit comments

Comments
 (0)