Skip to content

Commit 85e9633

Browse files
tongyimingmikatong
andauthored
mongodb support multi zone (#2013)
* mongodb support multi zone * add changelog * rm needfix --------- Co-authored-by: mikatong <mikatong@tencent.com>
1 parent 5e6a6b5 commit 85e9633

File tree

4 files changed

+151
-26
lines changed

4 files changed

+151
-26
lines changed

.changelog/2013.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/tencentcloud_mongodb_instance: support params `node_num`, `availability_zone_list` and `hidden_zone`
3+
```

tencentcloud/resource_tc_mongodb_instance.go

Lines changed: 78 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ resource "tencentcloud_mongodb_instance" "mongodb" {
88
instance_name = "mongodb"
99
memory = 4
1010
volume = 100
11-
engine_version = "MONGO_3_WT"
12-
machine_type = "GIO"
11+
engine_version = "MONGO_36_WT"
12+
machine_type = "HIO10G"
1313
available_zone = "ap-guangzhou-2"
14-
vpc_id = "vpc-mz3efvbw"
15-
subnet_id = "subnet-lk0svi3p"
14+
vpc_id = "vpc-xxxxxx"
15+
subnet_id = "subnet-xxxxxx"
1616
project_id = 0
1717
password = "password1234"
1818
}
@@ -63,6 +63,33 @@ func resourceTencentCloudMongodbInstance() *schema.Resource {
6363
},
6464
},
6565
},
66+
"node_num": {
67+
Type: schema.TypeInt,
68+
Optional: true,
69+
Computed: true,
70+
Description: "The number of nodes in each replica set. Default value: 3.",
71+
},
72+
"availability_zone_list": {
73+
Type: schema.TypeList,
74+
Optional: true,
75+
Computed: true,
76+
Elem: &schema.Schema{
77+
Type: schema.TypeString,
78+
},
79+
RequiredWith: []string{"hidden_zone"},
80+
Description: `A list of nodes deployed in multiple availability zones. For more information, please use the API DescribeSpecInfo.
81+
- Multi-availability zone deployment nodes can only be deployed in 3 different availability zones. It is not supported to deploy most nodes of the cluster in the same availability zone. For example, a 3-node cluster does not support the deployment of 2 nodes in the same zone.
82+
- Version 4.2 and above are not supported.
83+
- Read-only disaster recovery instances are not supported.
84+
- Basic network cannot be selected.`,
85+
},
86+
"hidden_zone": {
87+
Type: schema.TypeString,
88+
Optional: true,
89+
Computed: true,
90+
RequiredWith: []string{"availability_zone_list"},
91+
Description: "The availability zone to which the Hidden node belongs. This parameter must be configured to deploy instances across availability zones.",
92+
},
6693
}
6794
basic := TencentMongodbBasicInfo()
6895
conflictList := []string{"mongos_cpu", "mongos_memory", "mongos_node_num"}
@@ -108,6 +135,10 @@ func mongodbAllInstanceReqSet(requestInter interface{}, d *schema.ResourceData)
108135
machine = MONGODB_MACHINE_TYPE_HIO10G
109136
}
110137

138+
if v, ok := d.GetOk("node_num"); ok {
139+
nodeNum = v.(int)
140+
}
141+
111142
if v, ok := d.GetOk("password"); ok && v.(string) != "" {
112143
password = v.(string)
113144
} else {
@@ -159,7 +190,13 @@ func mongodbAllInstanceReqSet(requestInter interface{}, d *schema.ResourceData)
159190
}
160191
value.FieldByName("AutoRenewFlag").Set(reflect.ValueOf(helper.IntUint64(d.Get("auto_renew_flag").(int))))
161192
}
162-
193+
if v, ok := d.GetOk("availability_zone_list"); ok {
194+
availabilityZoneList := helper.InterfacesStringsPoint(v.([]interface{}))
195+
value.FieldByName("AvailabilityZoneList").Set(reflect.ValueOf(availabilityZoneList))
196+
}
197+
if v, ok := d.GetOk("hidden_zone"); ok {
198+
value.FieldByName("HiddenZone").Set(reflect.ValueOf(helper.String(v.(string))))
199+
}
163200
return nil
164201
}
165202

@@ -361,6 +398,25 @@ func resourceTencentCloudMongodbInstanceRead(d *schema.ResourceData, meta interf
361398
_ = d.Set("vip", instance.Vip)
362399
_ = d.Set("vport", instance.Vport)
363400
_ = d.Set("create_time", instance.CreateTime)
401+
_ = d.Set("node_num", *instance.SecondaryNum+1)
402+
403+
replicateSets, err := mongodbService.DescribeDBInstanceNodeProperty(ctx, instanceId)
404+
if err != nil {
405+
return err
406+
}
407+
if len(replicateSets) > 0 {
408+
var hiddenZone string
409+
availabilityZoneList := make([]string, 0, 3)
410+
for _, replicate := range replicateSets[0].Nodes {
411+
itemZone := *replicate.Zone
412+
if *replicate.Hidden {
413+
hiddenZone = itemZone
414+
}
415+
availabilityZoneList = append(availabilityZoneList, itemZone)
416+
}
417+
_ = d.Set("hidden_zone", hiddenZone)
418+
_ = d.Set("availability_zone_list", availabilityZoneList)
419+
}
364420

365421
// standby instance list
366422
var standbyInsList []map[string]string
@@ -401,6 +457,14 @@ func resourceTencentCloudMongodbInstanceUpdate(d *schema.ResourceData, meta inte
401457

402458
d.Partial(true)
403459

460+
immutableArgs := []string{"node_num", "availability_zone_list", "hidden_zone"}
461+
462+
for _, v := range immutableArgs {
463+
if d.HasChange(v) {
464+
return fmt.Errorf("argument `%s` cannot be changed", v)
465+
}
466+
}
467+
404468
if d.HasChange("memory") || d.HasChange("volume") {
405469
memory := d.Get("memory").(int)
406470
volume := d.Get("volume").(int)
@@ -531,12 +595,15 @@ func resourceTencentCloudMongodbInstanceDelete(d *schema.ResourceData, meta inte
531595
return nil
532596
}
533597
if MONGODB_CHARGE_TYPE[*instanceDetail.PayMode] == MONGODB_CHARGE_TYPE_PREPAID {
534-
return fmt.Errorf("PREPAID instances are not allowed to be deleted now, please isolate them on console")
535-
}
536-
537-
err = mongodbService.IsolateInstance(ctx, instanceId)
538-
if err != nil {
539-
return err
598+
err := mongodbService.TerminateDBInstances(ctx, instanceId)
599+
if err != nil {
600+
return err
601+
}
602+
} else {
603+
err := mongodbService.IsolateInstance(ctx, instanceId)
604+
if err != nil {
605+
return err
606+
}
540607
}
541608
err = mongodbService.OfflineIsolatedDBInstance(ctx, instanceId, true)
542609
if err != nil {

tencentcloud/resource_tc_mongodb_instance_test.go

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func TestAccTencentCloudMongodbInstanceResourcePostPaid(t *testing.T) {
7575
CheckDestroy: testAccCheckMongodbInstanceDestroy,
7676
Steps: []resource.TestStep{
7777
{
78-
PreConfig: func() { testAccStepPreConfigSetTempAKSK(t, ACCOUNT_TYPE_COMMON) },
78+
PreConfig: func() { testAccStepPreConfigSetTempAKSK(t, ACCOUNT_TYPE_PREPAY) },
7979
Config: testAccMongodbInstance,
8080
Check: resource.ComposeTestCheckFunc(
8181
testAccCheckMongodbInstanceExists("tencentcloud_mongodb_instance.mongodb"),
@@ -84,7 +84,7 @@ func TestAccTencentCloudMongodbInstanceResourcePostPaid(t *testing.T) {
8484
resource.TestCheckResourceAttrSet("tencentcloud_mongodb_instance.mongodb", "volume"),
8585
resource.TestCheckResourceAttrSet("tencentcloud_mongodb_instance.mongodb", "engine_version"),
8686
resource.TestCheckResourceAttrSet("tencentcloud_mongodb_instance.mongodb", "machine_type"),
87-
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb", "available_zone", "ap-guangzhou-6"),
87+
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb", "available_zone", "ap-guangzhou-3"),
8888
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb", "project_id", "0"),
8989
resource.TestCheckResourceAttrSet("tencentcloud_mongodb_instance.mongodb", "status"),
9090
resource.TestCheckResourceAttrSet("tencentcloud_mongodb_instance.mongodb", "vip"),
@@ -106,7 +106,7 @@ func TestAccTencentCloudMongodbInstanceResourcePostPaid(t *testing.T) {
106106
log.Printf("[WARN] MongoDB Update Need DealID query available, skip checking.")
107107
return true, nil
108108
},
109-
PreConfig: func() { testAccStepPreConfigSetTempAKSK(t, ACCOUNT_TYPE_COMMON) },
109+
PreConfig: func() { testAccStepPreConfigSetTempAKSK(t, ACCOUNT_TYPE_PREPAY) },
110110
Config: testAccMongodbInstance_update,
111111
Check: resource.ComposeTestCheckFunc(
112112
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb", "instance_name", "tf-mongodb-update"),
@@ -120,7 +120,28 @@ func TestAccTencentCloudMongodbInstanceResourcePostPaid(t *testing.T) {
120120
})
121121
}
122122

123-
func TestAccTencentCloudNeedFixMongodbInstanceResourcePrepaid(t *testing.T) {
123+
func TestAccTencentCloudMongodbInstanceResource_multiZone(t *testing.T) {
124+
t.Parallel()
125+
resource.Test(t, resource.TestCase{
126+
PreCheck: func() { testAccPreCheck(t) },
127+
Providers: testAccProviders,
128+
CheckDestroy: testAccCheckMongodbInstanceDestroy,
129+
Steps: []resource.TestStep{
130+
{
131+
PreConfig: func() { testAccStepPreConfigSetTempAKSK(t, ACCOUNT_TYPE_PREPAY) },
132+
Config: testAccMongodbInstance_multiZone,
133+
Check: resource.ComposeTestCheckFunc(
134+
testAccCheckMongodbInstanceExists("tencentcloud_mongodb_instance.mongodb_mutil_zone"),
135+
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb_mutil_zone", "node_num", "5"),
136+
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb_mutil_zone", "availability_zone_list.#", "5"),
137+
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb_mutil_zone", "hidden_zone", "ap-guangzhou-6"),
138+
),
139+
},
140+
},
141+
})
142+
}
143+
144+
func TestAccTencentCloudMongodbInstanceResourcePrepaid(t *testing.T) {
124145
// Avoid to set Parallel to make sure EnvVar secure
125146
resource.Test(t, resource.TestCase{
126147
PreCheck: func() { testAccPreCheckCommon(t, ACCOUNT_TYPE_PREPAY) },
@@ -136,7 +157,7 @@ func TestAccTencentCloudNeedFixMongodbInstanceResourcePrepaid(t *testing.T) {
136157
resource.TestCheckResourceAttrSet("tencentcloud_mongodb_instance.mongodb_prepaid", "volume"),
137158
resource.TestCheckResourceAttrSet("tencentcloud_mongodb_instance.mongodb_prepaid", "engine_version"),
138159
resource.TestCheckResourceAttrSet("tencentcloud_mongodb_instance.mongodb_prepaid", "machine_type"),
139-
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb_prepaid", "available_zone", "ap-guangzhou-6"),
160+
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb_prepaid", "available_zone", "ap-guangzhou-3"),
140161
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb_prepaid", "project_id", "0"),
141162
resource.TestCheckResourceAttrSet("tencentcloud_mongodb_instance.mongodb_prepaid", "status"),
142163
resource.TestCheckResourceAttrSet("tencentcloud_mongodb_instance.mongodb_prepaid", "vip"),
@@ -153,8 +174,6 @@ func TestAccTencentCloudNeedFixMongodbInstanceResourcePrepaid(t *testing.T) {
153174
Config: testAccMongodbInstancePrepaid_update,
154175
Check: resource.ComposeTestCheckFunc(
155176
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb_prepaid", "instance_name", "tf-mongodb-test-prepaid-update"),
156-
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb_prepaid", "memory", "4"),
157-
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb_prepaid", "volume", "100"),
158177
resource.TestCheckNoResourceAttr("tencentcloud_mongodb_instance.mongodb_prepaid", "tags.test"),
159178
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb_prepaid", "tags.prepaid", "prepaid"),
160179
),
@@ -226,9 +245,11 @@ resource "tencentcloud_mongodb_instance" "mongodb" {
226245
volume = local.volume
227246
engine_version = local.engine_version
228247
machine_type = local.machine_type
229-
available_zone = "ap-guangzhou-6"
248+
available_zone = "ap-guangzhou-3"
230249
project_id = 0
231250
password = "test1234"
251+
vpc_id = var.vpc_id
252+
subnet_id = var.subnet_id
232253
233254
tags = {
234255
test = "test"
@@ -243,9 +264,11 @@ resource "tencentcloud_mongodb_instance" "mongodb" {
243264
volume = local.volume * 2
244265
engine_version = local.engine_version
245266
machine_type = local.machine_type
246-
available_zone = "ap-guangzhou-6"
267+
available_zone = "ap-guangzhou-3"
247268
project_id = 0
248269
password = "test1234update"
270+
vpc_id = var.vpc_id
271+
subnet_id = var.subnet_id
249272
250273
tags = {
251274
abc = "abc"
@@ -260,12 +283,14 @@ resource "tencentcloud_mongodb_instance" "mongodb_prepaid" {
260283
volume = local.volume
261284
engine_version = local.engine_version
262285
machine_type = local.machine_type
263-
available_zone = "ap-guangzhou-6"
286+
available_zone = "ap-guangzhou-3"
264287
project_id = 0
265288
password = "test1234"
266289
charge_type = "PREPAID"
267290
prepaid_period = 1
268291
auto_renew_flag = 1
292+
vpc_id = var.vpc_id
293+
subnet_id = var.subnet_id
269294
270295
tags = {
271296
test = "test-prepaid"
@@ -280,15 +305,38 @@ resource "tencentcloud_mongodb_instance" "mongodb_prepaid" {
280305
volume = local.volume
281306
engine_version = local.engine_version
282307
machine_type = local.machine_type
283-
available_zone = "ap-guangzhou-6"
308+
available_zone = "ap-guangzhou-3"
284309
project_id = 0
285310
password = "test1234update"
286311
charge_type = "PREPAID"
287312
prepaid_period = 1
288313
auto_renew_flag = 1
314+
vpc_id = var.vpc_id
315+
subnet_id = var.subnet_id
289316
290317
tags = {
291318
prepaid = "prepaid"
292319
}
293320
}
294321
`
322+
323+
const testAccMongodbInstance_multiZone = DefaultMongoDBSpec + `
324+
resource "tencentcloud_mongodb_instance" "mongodb_mutil_zone" {
325+
instance_name = "mongodb-mutil-zone-test"
326+
memory = local.memory
327+
volume = local.volume
328+
engine_version = local.engine_version
329+
machine_type = local.machine_type
330+
available_zone = "ap-guangzhou-3"
331+
project_id = 0
332+
password = "test1234"
333+
vpc_id = var.vpc_id
334+
subnet_id = var.subnet_id
335+
node_num = 5
336+
availability_zone_list = ["ap-guangzhou-3", "ap-guangzhou-3", "ap-guangzhou-4", "ap-guangzhou-4", "ap-guangzhou-6"]
337+
hidden_zone = "ap-guangzhou-6"
338+
tags = {
339+
test = "test"
340+
}
341+
}
342+
`

website/docs/r/mongodb_instance.html.markdown

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ resource "tencentcloud_mongodb_instance" "mongodb" {
1818
instance_name = "mongodb"
1919
memory = 4
2020
volume = 100
21-
engine_version = "MONGO_3_WT"
22-
machine_type = "GIO"
21+
engine_version = "MONGO_36_WT"
22+
machine_type = "HIO10G"
2323
available_zone = "ap-guangzhou-2"
24-
vpc_id = "vpc-mz3efvbw"
25-
subnet_id = "subnet-lk0svi3p"
24+
vpc_id = "vpc-xxxxxx"
25+
subnet_id = "subnet-xxxxxx"
2626
project_id = 0
2727
password = "password1234"
2828
}
@@ -39,7 +39,14 @@ The following arguments are supported:
3939
* `memory` - (Required, Int) Memory size. The minimum value is 2, and unit is GB. Memory and volume must be upgraded or degraded simultaneously.
4040
* `volume` - (Required, Int) Disk size. The minimum value is 25, and unit is GB. Memory and volume must be upgraded or degraded simultaneously.
4141
* `auto_renew_flag` - (Optional, Int) Auto renew flag. Valid values are `0`(NOTIFY_AND_MANUAL_RENEW), `1`(NOTIFY_AND_AUTO_RENEW) and `2`(DISABLE_NOTIFY_AND_MANUAL_RENEW). Default value is `0`. Note: only works for PREPAID instance. Only supports`0` and `1` for creation.
42+
* `availability_zone_list` - (Optional, List: [`String`]) A list of nodes deployed in multiple availability zones. For more information, please use the API DescribeSpecInfo.
43+
- Multi-availability zone deployment nodes can only be deployed in 3 different availability zones. It is not supported to deploy most nodes of the cluster in the same availability zone. For example, a 3-node cluster does not support the deployment of 2 nodes in the same zone.
44+
- Version 4.2 and above are not supported.
45+
- Read-only disaster recovery instances are not supported.
46+
- Basic network cannot be selected.
4247
* `charge_type` - (Optional, String, ForceNew) The charge type of instance. Valid values are `PREPAID` and `POSTPAID_BY_HOUR`. Default value is `POSTPAID_BY_HOUR`. Note: TencentCloud International only supports `POSTPAID_BY_HOUR`. Caution that update operation on this field will delete old instances and create new one with new charge type.
48+
* `hidden_zone` - (Optional, String) The availability zone to which the Hidden node belongs. This parameter must be configured to deploy instances across availability zones.
49+
* `node_num` - (Optional, Int) The number of nodes in each replica set. Default value: 3.
4350
* `password` - (Optional, String) Password of this Mongodb account.
4451
* `prepaid_period` - (Optional, Int) The tenancy (time unit is month) of the prepaid instance. Valid values are 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 24, 36. NOTE: it only works when charge_type is set to `PREPAID`.
4552
* `project_id` - (Optional, Int) ID of the project which the instance belongs.

0 commit comments

Comments
 (0)