Skip to content

Commit 42e7e76

Browse files
tongyimingmikatong
andauthored
fix: clear cvm data_disk & redis support replicas_read_only (#882)
Co-authored-by: mikatong <[email protected]>
1 parent 9b0484b commit 42e7e76

File tree

4 files changed

+95
-1
lines changed

4 files changed

+95
-1
lines changed

tencentcloud/resource_tc_instance.go

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1577,7 +1577,81 @@ func resourceTencentCloudInstanceDelete(d *schema.ResourceData, meta interface{}
15771577
if err != nil {
15781578
return err
15791579
}
1580-
1580+
if v, ok := d.GetOk("data_disks"); ok {
1581+
dataDisks := v.([]interface{})
1582+
for _, d := range dataDisks {
1583+
value := d.(map[string]interface{})
1584+
diskId := value["data_disk_id"].(string)
1585+
deleteWithInstance := value["delete_with_instance"].(bool)
1586+
if deleteWithInstance {
1587+
cbsService := CbsService{client: meta.(*TencentCloudClient).apiV3Conn}
1588+
err := resource.Retry(readRetryTimeout*2, func() *resource.RetryError {
1589+
diskInfo, e := cbsService.DescribeDiskById(ctx, diskId)
1590+
if e != nil {
1591+
return retryError(e, InternalError)
1592+
}
1593+
if *diskInfo.DiskState != CBS_STORAGE_STATUS_UNATTACHED {
1594+
return resource.RetryableError(fmt.Errorf("cbs storage status is %s", *diskInfo.DiskState))
1595+
}
1596+
return nil
1597+
})
1598+
if err != nil {
1599+
log.Printf("[CRITAL]%s delete cbs failed, reason:%s\n ", logId, err.Error())
1600+
return err
1601+
}
1602+
err = resource.Retry(writeRetryTimeout, func() *resource.RetryError {
1603+
e := cbsService.DeleteDiskById(ctx, diskId)
1604+
if e != nil {
1605+
return retryError(e, InternalError)
1606+
}
1607+
return nil
1608+
})
1609+
if err != nil {
1610+
log.Printf("[CRITAL]%s delete cbs failed, reason:%s\n ", logId, err.Error())
1611+
return err
1612+
}
1613+
err = resource.Retry(readRetryTimeout*2, func() *resource.RetryError {
1614+
diskInfo, e := cbsService.DescribeDiskById(ctx, diskId)
1615+
if e != nil {
1616+
return retryError(e, InternalError)
1617+
}
1618+
if *diskInfo.DiskState == CBS_STORAGE_STATUS_TORECYCLE {
1619+
return resource.RetryableError(fmt.Errorf("cbs storage status is %s", *diskInfo.DiskState))
1620+
}
1621+
return nil
1622+
})
1623+
if err != nil {
1624+
log.Printf("[CRITAL]%s read cbs status failed, reason:%s\n ", logId, err.Error())
1625+
return err
1626+
}
1627+
err = resource.Retry(writeRetryTimeout, func() *resource.RetryError {
1628+
e := cbsService.DeleteDiskById(ctx, diskId)
1629+
if e != nil {
1630+
return retryError(e, InternalError)
1631+
}
1632+
return nil
1633+
})
1634+
if err != nil {
1635+
log.Printf("[CRITAL]%s delete cbs failed, reason:%s\n ", logId, err.Error())
1636+
return err
1637+
}
1638+
err = resource.Retry(readRetryTimeout*2, func() *resource.RetryError {
1639+
diskInfo, e := cbsService.DescribeDiskById(ctx, diskId)
1640+
if e != nil {
1641+
return retryError(e, InternalError)
1642+
}
1643+
if diskInfo != nil {
1644+
return resource.RetryableError(fmt.Errorf("cbs storage status is %s", *diskInfo.DiskState))
1645+
}
1646+
return nil
1647+
})
1648+
if err != nil {
1649+
log.Printf("[CRITAL]%s read cbs status failed, reason:%s\n ", logId, err.Error())
1650+
return err
1651+
}
1652+
}
1653+
}
1654+
}
15811655
return nil
15821656
}
15831657

tencentcloud/resource_tc_redis_instance.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,13 @@ func resourceTencentCloudRedisInstance() *schema.Resource {
165165
ForceNew: true,
166166
Description: "Indicates whether the redis instance support no-auth access. NOTE: Only available in private cloud environment.",
167167
},
168+
"replicas_read_only": {
169+
Type: schema.TypeBool,
170+
Optional: true,
171+
ForceNew: true,
172+
Default: false,
173+
Description: "Whether copy read-only is supported, Redis 2.8 Standard Edition and CKV Standard Edition do not support replica read-only, turn on replica read-only, the instance will automatically read and write separate, write requests are routed to the primary node, read requests are routed to the replica node, if you need to open replica read-only, the recommended number of replicas >=2.",
174+
},
168175
"mem_size": {
169176
Type: schema.TypeInt,
170177
Required: true,
@@ -291,6 +298,7 @@ func resourceTencentCloudRedisInstanceCreate(d *schema.ResourceData, meta interf
291298
chargeType := d.Get("charge_type").(string)
292299
autoRenewFlag := d.Get("auto_renew_flag").(int)
293300
chargeTypeID := REDIS_CHARGE_TYPE_ID[chargeType]
301+
replicasReadonly := d.Get("replicas_read_only").(bool)
294302
var chargePeriod uint64 = 1
295303
if chargeType == REDIS_CHARGE_TYPE_PREPAID {
296304
if period, ok := d.GetOk("prepaid_period"); ok {
@@ -427,6 +435,7 @@ func resourceTencentCloudRedisInstanceCreate(d *schema.ResourceData, meta interf
427435
nodeInfo,
428436
noAuth,
429437
autoRenewFlag,
438+
replicasReadonly,
430439
)
431440

432441
if err != nil {
@@ -546,6 +555,12 @@ func resourceTencentCloudRedisInstanceRead(d *schema.ResourceData, meta interfac
546555
_ = d.Set("ip", info.WanIp)
547556
_ = d.Set("create_time", info.Createtime)
548557
_ = d.Set("auto_renew_flag", info.AutoRenewFlag)
558+
slaveReadWeight := *info.SlaveReadWeight
559+
if slaveReadWeight == 0 {
560+
_ = d.Set("replicas_read_only", false)
561+
} else if slaveReadWeight == 100 {
562+
_ = d.Set("replicas_read_only", true)
563+
}
549564

550565
// only true or user explicit declared will set for import case.
551566
if _, ok := d.GetOk("no_auth"); ok || *info.NoAuth {

tencentcloud/service_tencentcloud_redis.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ func (me *RedisService) CreateInstances(ctx context.Context,
250250
nodeInfo []*redis.RedisNodeInfo,
251251
noAuth bool,
252252
autoRenewFlag int,
253+
replicasReadonly bool,
253254
) (instanceIds []*string, errRet error) {
254255

255256
logId := getLogId(ctx)
@@ -328,6 +329,9 @@ func (me *RedisService) CreateInstances(ctx context.Context,
328329
if chargeTypeID == REDIS_CHARGE_TYPE_ID[REDIS_CHARGE_TYPE_PREPAID] {
329330
request.AutoRenew = helper.IntUint64(autoRenewFlag)
330331
}
332+
if replicasReadonly {
333+
request.ReplicasReadonly = &replicasReadonly
334+
}
331335
ratelimit.Check(request.GetAction())
332336
response, err := me.client.UseRedisClient().CreateInstances(request)
333337
if err != nil {

website/docs/r/redis_instance.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ The following arguments are supported:
8181
* `redis_replicas_num` - (Optional, ForceNew) The number of instance copies. This is not required for standalone and master slave versions.
8282
* `redis_shard_num` - (Optional, ForceNew) The number of instance shard. This is not required for standalone and master slave versions.
8383
* `replica_zone_ids` - (Optional, ForceNew) ID of replica nodes available zone. This is not required for standalone and master slave versions.
84+
* `replicas_read_only` - (Optional, ForceNew) Whether copy read-only is supported, Redis 2.8 Standard Edition and CKV Standard Edition do not support replica read-only, turn on replica read-only, the instance will automatically read and write separate, write requests are routed to the primary node, read requests are routed to the replica node, if you need to open replica read-only, the recommended number of replicas >=2.
8485
* `security_groups` - (Optional, ForceNew) ID of security group. If both vpc_id and subnet_id are not set, this argument should not be set either.
8586
* `subnet_id` - (Optional, ForceNew) Specifies which subnet the instance should belong to.
8687
* `tags` - (Optional) Instance tags.

0 commit comments

Comments
 (0)