Skip to content

Commit c34a09f

Browse files
authored
Merge pull request #712 from tencentcloudstack/feat/redis-replica-zone-ids
feat: redis - support multi replica az
2 parents 1ba77ab + 344b3f6 commit c34a09f

8 files changed

+183
-18
lines changed

tencentcloud/data_source_tc_redis_instances.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,30 @@ func dataSourceTencentRedisInstances() *schema.Resource {
152152
Computed: true,
153153
Description: "The charge type of instance. Valid values are `POSTPAID` and `PREPAID`.",
154154
},
155+
"node_info": {
156+
Type: schema.TypeList,
157+
Computed: true,
158+
Description: "List of instance node information. Currently, information about the node type (master or replica) and node availability zone can be passed in.",
159+
Elem: &schema.Resource{
160+
Schema: map[string]*schema.Schema{
161+
"master": {
162+
Type: schema.TypeBool,
163+
Computed: true,
164+
Description: "Indicates whether the node is master.",
165+
},
166+
"id": {
167+
Type: schema.TypeInt,
168+
Computed: true,
169+
Description: "ID of the master or replica node.",
170+
},
171+
"zone_id": {
172+
Type: schema.TypeInt,
173+
Computed: true,
174+
Description: "ID of the availability zone of the master or replica node.",
175+
},
176+
},
177+
},
178+
},
155179
},
156180
},
157181
},
@@ -241,6 +265,7 @@ instanceLoop:
241265
instanceDes["redis_replicas_num"] = instance.RedisReplicasNum
242266
instanceDes["type_id"] = instance.TypeId
243267
instanceDes["charge_type"] = instance.BillingMode
268+
instanceDes["node_info"] = instance.NodeInfo
244269
instanceList = append(instanceList, instanceDes)
245270
}
246271

tencentcloud/extension_redis.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const (
66
REDIS_VERSION_MASTER_SLAVE_CKV = 3
77
REDIS_VERSION_CLUSTER_CKV = 4
88
REDIS_VERSION_STANDALONE_REDIS = 5
9+
REDIS_VERSION_MASTER_SLAVE_REDIS4 = 6
910
REDIS_VERSION_CLUSTER_REDIS = 7
1011
REDIS_VERSION_MASTER_SLAVE_REDIS5 = 8
1112
REDIS_VERSION_CLUSTER_REDIS5 = 9
@@ -17,6 +18,7 @@ var REDIS_NAMES = map[int64]string{
1718
REDIS_VERSION_CLUSTER_REDIS: "cluster_redis",
1819
REDIS_VERSION_CLUSTER_CKV: "cluster_ckv",
1920
REDIS_VERSION_STANDALONE_REDIS: "standalone_redis",
21+
REDIS_VERSION_MASTER_SLAVE_REDIS4: "master_slave_redis4.0",
2022
REDIS_VERSION_MASTER_SLAVE_REDIS5: "master_slave_redis5.0",
2123
REDIS_VERSION_CLUSTER_REDIS5: "cluster_redis5.0",
2224
}

tencentcloud/provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,7 @@ func Provider() terraform.ResourceProvider {
930930
"tencentcloud_monitor_policy_group": resourceTencentMonitorPolicyGroup(),
931931
"tencentcloud_monitor_binding_object": resourceTencentMonitorBindingObject(),
932932
"tencentcloud_monitor_binding_receiver": resourceTencentMonitorBindingAlarmReceiver(),
933-
"tencentcloud_monitor_alarm_policy": resourceTencentMonitorAlarmPolicy(),
933+
"tencentcloud_monitor_alarm_policy": resourceTencentMonitorAlarmPolicy(),
934934
"tencentcloud_mongodb_standby_instance": resourceTencentCloudMongodbStandbyInstance(),
935935
"tencentcloud_elasticsearch_instance": resourceTencentCloudElasticsearchInstance(),
936936
"tencentcloud_postgresql_instance": resourceTencentCloudPostgresqlInstance(),

tencentcloud/resource_tc_monitor_policy_group.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ import (
6767
func resourceTencentMonitorPolicyGroup() *schema.Resource {
6868
return &schema.Resource{
6969
DeprecationMessage: "This resource has been deprecated in Terraform TencentCloud provider version 1.59.18. Please use 'tencentcloud_alarm_policy' instead.",
70-
Create: resourceTencentMonitorPolicyGroupCreate,
71-
Read: resourceTencentMonitorPolicyGroupRead,
72-
Update: resourceTencentMonitorPolicyGroupUpdate,
73-
Delete: resourceTencentMonitorPolicyGroupDelete,
70+
Create: resourceTencentMonitorPolicyGroupCreate,
71+
Read: resourceTencentMonitorPolicyGroupRead,
72+
Update: resourceTencentMonitorPolicyGroupUpdate,
73+
Delete: resourceTencentMonitorPolicyGroupDelete,
7474
Importer: &schema.ResourceImporter{
7575
State: schema.ImportStatePassthrough,
7676
},

tencentcloud/resource_tc_redis_instance.go

Lines changed: 87 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,39 @@ resource "tencentcloud_redis_instance" "redis_instance_test_2" {
1919
}
2020
```
2121
22+
Using multi replica zone set
23+
```
24+
data "tencentcloud_availability_zones" "az" {
25+
26+
}
27+
28+
variable "redis_replicas_num" {
29+
default = 3
30+
}
31+
32+
resource "tencentcloud_redis_instance" "red1" {
33+
availability_zone = data.tencentcloud_availability_zones.az.zones[0].name
34+
charge_type = "POSTPAID"
35+
mem_size = 1024
36+
name = "test-redis"
37+
port = 6379
38+
project_id = 0
39+
redis_replicas_num = var.redis_replicas_num
40+
redis_shard_num = 1
41+
security_groups = [
42+
"sg-d765yoec",
43+
]
44+
subnet_id = "subnet-ie01x91v"
45+
type_id = 6
46+
vpc_id = "vpc-k4lrsafc"
47+
password = "a12121312334"
48+
49+
replica_zone_ids = [
50+
for i in range(var.redis_replicas_num)
51+
: data.tencentcloud_availability_zones.az.zones[i % length(data.tencentcloud_availability_zones.az.zones)].id ]
52+
}
53+
```
54+
2255
Import
2356
2457
Redis instance can be imported, e.g.
@@ -95,6 +128,13 @@ func resourceTencentCloudRedisInstance() *schema.Resource {
95128
Default: 1,
96129
Description: "The number of instance copies. This is not required for standalone and master slave versions.",
97130
},
131+
"replica_zone_ids": {
132+
Type: schema.TypeList,
133+
Optional: true,
134+
ForceNew: true,
135+
Description: "ID of replica nodes available zone. This is not required for standalone and master slave versions.",
136+
Elem: &schema.Schema{Type: schema.TypeInt},
137+
},
98138
"type": {
99139
Type: schema.TypeString,
100140
ForceNew: true,
@@ -203,7 +243,6 @@ func resourceTencentCloudRedisInstance() *schema.Resource {
203243
"force_delete": {
204244
Type: schema.TypeBool,
205245
Optional: true,
206-
Default: false,
207246
Description: "Indicate whether to delete Redis instance directly or not. Default is false. If set true, the instance will be deleted instead of staying recycle bin. Note: only works for `PREPAID` instance.",
208247
},
209248
},
@@ -320,6 +359,32 @@ func resourceTencentCloudRedisInstanceCreate(d *schema.ResourceData, meta interf
320359
requestSecurityGroup = append(requestSecurityGroup, v.(string))
321360
}
322361

362+
service := RedisService{client: meta.(*TencentCloudClient).apiV3Conn}
363+
364+
nodeInfo := make([]*redis.RedisNodeInfo, 0)
365+
if raw, ok := d.GetOk("replica_zone_ids"); ok {
366+
zoneIds := raw.([]interface{})
367+
368+
masterZoneId, err := service.getZoneId(availabilityZone)
369+
if err != nil {
370+
return err
371+
}
372+
373+
// insert master node
374+
nodeInfo = append(nodeInfo, &redis.RedisNodeInfo{
375+
NodeType: helper.Int64(0),
376+
ZoneId: helper.Int64Uint64(masterZoneId),
377+
})
378+
379+
for _, v := range zoneIds {
380+
id := v.(int)
381+
nodeInfo = append(nodeInfo, &redis.RedisNodeInfo{
382+
NodeType: helper.Int64(1),
383+
ZoneId: helper.IntUint64(id),
384+
})
385+
}
386+
}
387+
323388
instanceIds, err := redisService.CreateInstances(ctx,
324389
availabilityZone,
325390
typeId,
@@ -335,6 +400,7 @@ func resourceTencentCloudRedisInstanceCreate(d *schema.ResourceData, meta interf
335400
redisReplicasNum,
336401
chargeTypeID,
337402
chargePeriod,
403+
nodeInfo,
338404
)
339405

340406
if err != nil {
@@ -429,8 +495,9 @@ func resourceTencentCloudRedisInstanceRead(d *schema.ResourceData, meta interfac
429495
if err != nil {
430496
return err
431497
}
432-
//not set field type_id
433-
if d.Get("type_id").(int) == 0 {
498+
// not set field type_id
499+
// process import case
500+
if d.Get("type_id").(int) == 0 && d.Get("type").(string) != "" {
434501
typeName := REDIS_NAMES[*info.Type]
435502
if typeName == "" {
436503
err = fmt.Errorf("redis read unkwnow type %d", *info.Type)
@@ -442,14 +509,8 @@ func resourceTencentCloudRedisInstanceRead(d *schema.ResourceData, meta interfac
442509
_ = d.Set("type_id", info.Type)
443510
}
444511

445-
if d.Get("redis_shard_num").(int) != 0 {
446-
_ = d.Set("redis_shard_num", info.RedisShardNum)
447-
}
448-
449-
if d.Get("redis_replicas_num").(int) != 0 {
450-
_ = d.Set("redis_replicas_num", info.RedisReplicasNum)
451-
}
452-
512+
_ = d.Set("redis_shard_num", info.RedisShardNum)
513+
_ = d.Set("redis_replicas_num", info.RedisReplicasNum)
453514
_ = d.Set("availability_zone", zoneName)
454515
_ = d.Set("mem_size", info.RedisShardSize)
455516
_ = d.Set("vpc_id", info.UniqVpcId)
@@ -468,6 +529,21 @@ func resourceTencentCloudRedisInstanceRead(d *schema.ResourceData, meta interfac
468529
}
469530
}
470531

532+
if info.NodeSet != nil {
533+
var zoneIds []uint64
534+
for i := range info.NodeSet {
535+
nodeInfo := info.NodeSet[i]
536+
if *nodeInfo.NodeType == 0 {
537+
continue
538+
}
539+
zoneIds = append(zoneIds, *nodeInfo.ZoneId)
540+
}
541+
542+
if err := d.Set("replica_zone_ids", zoneIds); err != nil {
543+
log.Printf("[WARN] replica_zone_ids set error: %s", err.Error())
544+
}
545+
}
546+
471547
tcClient := meta.(*TencentCloudClient).apiV3Conn
472548
tagService := &TagService{client: tcClient}
473549
tags, err := tagService.DescribeResourceTags(ctx, "redis", "instance", tcClient.Region, d.Id())

tencentcloud/service_tencentcloud_redis.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type TencentCloudRedisDetail struct {
3737
CreateTime string
3838
Tags map[string]string
3939
BillingMode string
40+
NodeInfo []map[string]interface{}
4041
}
4142

4243
func (me *RedisService) fullZoneId() (errRet error) {
@@ -196,6 +197,23 @@ needMoreItems:
196197
if item.RedisShardNum != nil {
197198
instance.RedisShardNum = *item.RedisShardNum
198199
}
200+
201+
if item.NodeSet != nil {
202+
nodeInfos := make([]map[string]interface{}, 0, len(item.NodeSet))
203+
for i := range item.NodeSet {
204+
dMap := make(map[string]interface{})
205+
nodeInfo := item.NodeSet[i]
206+
if *nodeInfo.NodeType == 0 {
207+
dMap["master"] = true
208+
} else {
209+
dMap["master"] = false
210+
}
211+
dMap["id"] = *nodeInfo.NodeId
212+
dMap["zone_id"] = *nodeInfo.ZoneId
213+
nodeInfos = append(nodeInfos, dMap)
214+
}
215+
instance.NodeInfo = nodeInfos
216+
}
199217
instance.Tags = make(map[string]string, len(item.InstanceTags))
200218
for _, tag := range item.InstanceTags {
201219
if tag.TagKey == nil {
@@ -226,7 +244,7 @@ func (me *RedisService) CreateInstances(ctx context.Context,
226244
memSize, projectId, port int64,
227245
securityGroups []string,
228246
redisShardNum,
229-
redisReplicasNum int, chargeTypeID int64, chargePeriod uint64) (instanceIds []*string, errRet error) {
247+
redisReplicasNum int, chargeTypeID int64, chargePeriod uint64, nodeInfo []*redis.RedisNodeInfo) (instanceIds []*string, errRet error) {
230248

231249
logId := getLogId(ctx)
232250
request := redis.NewCreateInstancesRequest()
@@ -293,6 +311,11 @@ func (me *RedisService) CreateInstances(ctx context.Context,
293311
request.SecurityGroupIdList = append(request.SecurityGroupIdList, &securityGroups[v])
294312
}
295313
}
314+
315+
if len(nodeInfo) > 0 {
316+
request.NodeSet = nodeInfo
317+
}
318+
296319
ratelimit.Check(request.GetAction())
297320
response, err := me.client.UseRedisClient().CreateInstances(request)
298321
if err != nil {

website/docs/d/redis_instances.html.markdown

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ In addition to all arguments above, the following attributes are exported:
4444
* `ip` - IP address of an instance.
4545
* `mem_size` - Memory size in MB.
4646
* `name` - Name of a redis instance.
47+
* `node_info` - List of instance node information. Currently, information about the node type (master or replica) and node availability zone can be passed in.
48+
* `id` - ID of the master or replica node.
49+
* `master` - Indicates whether the node is master.
50+
* `zone_id` - ID of the availability zone of the master or replica node.
4751
* `port` - The port used to access a redis instance.
4852
* `project_id` - ID of the project to which a redis instance belongs.
4953
* `redis_id` - ID of a redis instance.

website/docs/r/redis_instance.html.markdown

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,40 @@ resource "tencentcloud_redis_instance" "redis_instance_test_2" {
2929
}
3030
```
3131

32+
Using multi replica zone set
33+
34+
```hcl
35+
data "tencentcloud_availability_zones" "az" {
36+
37+
}
38+
39+
variable "redis_replicas_num" {
40+
default = 3
41+
}
42+
43+
resource "tencentcloud_redis_instance" "red1" {
44+
availability_zone = data.tencentcloud_availability_zones.az.zones[0].name
45+
charge_type = "POSTPAID"
46+
mem_size = 1024
47+
name = "test-redis"
48+
port = 6379
49+
project_id = 0
50+
redis_replicas_num = var.redis_replicas_num
51+
redis_shard_num = 1
52+
security_groups = [
53+
"sg-d765yoec",
54+
]
55+
subnet_id = "subnet-ie01x91v"
56+
type_id = 6
57+
vpc_id = "vpc-k4lrsafc"
58+
password = "a12121312334"
59+
60+
replica_zone_ids = [
61+
for i in range(var.redis_replicas_num)
62+
: data.tencentcloud_availability_zones.az.zones[i % length(data.tencentcloud_availability_zones.az.zones)].id]
63+
}
64+
```
65+
3266
## Argument Reference
3367

3468
The following arguments are supported:
@@ -44,11 +78,12 @@ The following arguments are supported:
4478
* `project_id` - (Optional) Specifies which project the instance should belong to.
4579
* `redis_replicas_num` - (Optional, ForceNew) The number of instance copies. This is not required for standalone and master slave versions.
4680
* `redis_shard_num` - (Optional, ForceNew) The number of instance shard. This is not required for standalone and master slave versions.
81+
* `replica_zone_ids` - (Optional, ForceNew) ID of replica nodes available zone. This is not required for standalone and master slave versions.
4782
* `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.
4883
* `subnet_id` - (Optional, ForceNew) Specifies which subnet the instance should belong to.
4984
* `tags` - (Optional) Instance tags.
5085
* `type_id` - (Optional, ForceNew) Instance type. Available values reference data source `tencentcloud_redis_zone_config` or [document](https://intl.cloud.tencent.com/document/product/239/32069).
51-
* `type` - (Optional, ForceNew, **Deprecated**) It has been deprecated from version 1.33.1. Please use 'type_id' instead. Instance type. Available values: `cluster_ckv`,`cluster_redis5.0`,`cluster_redis`,`master_slave_ckv`,`master_slave_redis5.0`,`master_slave_redis`,`standalone_redis`, specific region support specific types, need to refer data `tencentcloud_redis_zone_config`.
86+
* `type` - (Optional, ForceNew, **Deprecated**) It has been deprecated from version 1.33.1. Please use 'type_id' instead. Instance type. Available values: `cluster_ckv`,`cluster_redis5.0`,`cluster_redis`,`master_slave_ckv`,`master_slave_redis4.0`,`master_slave_redis5.0`,`master_slave_redis`,`standalone_redis`, specific region support specific types, need to refer data `tencentcloud_redis_zone_config`.
5287
* `vpc_id` - (Optional, ForceNew) ID of the vpc with which the instance is to be associated.
5388

5489
## Attributes Reference

0 commit comments

Comments
 (0)