Skip to content

Commit 9c1b568

Browse files
tongyimingmikatong
andauthored
feat(mongodb): [115436303]support mongodb update node number (#2531)
* support mongodb update node number * add changelog --------- Co-authored-by: mikatong <mikatong@tencent.com>
1 parent 2a86e77 commit 9c1b568

File tree

15 files changed

+1251
-876
lines changed

15 files changed

+1251
-876
lines changed

.changelog/2531.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 `add_node_list` and `remove_node_list`
3+
```

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ require (
6666
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lighthouse v1.0.729
6767
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/live v1.0.777
6868
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mariadb v1.0.672
69-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mongodb v1.0.651
69+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mongodb v1.0.828
7070
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor v1.0.844
7171
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/organization v1.0.856
7272
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mps v1.0.853

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor v1.0.839 h1:eRI
10061006
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor v1.0.839/go.mod h1:4OoBA087NHVdyeRJFy/SML7kIJmt54DtNkNlJWZp8oE=
10071007
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor v1.0.844 h1:ZR7FSnv4k1j0BpJHg6JDbJ6YJ2YfLhX2xVIUcuweKm4=
10081008
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor v1.0.844/go.mod h1:a7PfDWl0B3QV+g99asQHumDNScEEMl+7lkXP0KXt8Xg=
1009+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mongodb v1.0.828 h1:ck4fIo8e7HlMQmIh77r8rbUjZCiCuO4DJ7KYAjHQ/Ws=
1010+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mongodb v1.0.828/go.mod h1:pnGYJR0EUzVMjcGdecYwsU+UUKrrHBM8kDgJHAwCOK4=
10091011
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mps v1.0.777 h1:SoH/KkqkEUw8iDmQDZCw5Saf319Ceyt3v8Vqodo8DiY=
10101012
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mps v1.0.777/go.mod h1:sBmwqD3GkczHFiArdYpHPp9QriIbzNtO99DueYVyGwE=
10111013
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mps v1.0.853 h1:abGpWsAtEuF2QIYKm2m9/hv9OqyHwWNLsd5+67z86BE=

tencentcloud/acctest/basic.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -818,17 +818,17 @@ locals {
818818

819819
// MongoDB
820820
const (
821-
DefaultMongoDBVPCId = "vpc-rwj54lfr"
822-
DefaultMongoDBSubnetId = "subnet-nyt57zps"
821+
DefaultMongoDBVPCId = "vpc-axrsmmrv"
822+
DefaultMongoDBSubnetId = "subnet-9hcctbue"
823823
)
824824
const DefaultMongoDBSecurityGroupId = "sg-if748odn"
825825
const DefaultMongoDBSpec = `
826826
data "tencentcloud_mongodb_zone_config" "zone_config" {
827-
available_zone = "ap-guangzhou-6"
827+
available_zone = "ap-guangzhou-3"
828828
}
829829
830830
data "tencentcloud_security_group" "foo" {
831-
name = "default"
831+
name = "keep-mongodb-sg"
832832
}
833833
834834
variable "engine_versions" {
@@ -850,22 +850,22 @@ variable "subnet_id" {
850850
}
851851
852852
locals {
853-
filtered_spec = [for i in data.tencentcloud_mongodb_zone_config.zone_config.list: i if lookup(i, "machine_type") == "HIO10G" && lookup(i, "engine_version") != "3.2"]
853+
filtered_spec = [for i in data.tencentcloud_mongodb_zone_config.zone_config.list: i if lookup(i, "machine_type") == "HIO10G" && lookup(i, "engine_version") == "4.4" && lookup(i, "memory") == 4096 && lookup(i, "default_storage") == 256000]
854854
spec = concat(local.filtered_spec, data.tencentcloud_mongodb_zone_config.zone_config.list)
855855
machine_type = local.spec.0.machine_type
856856
cluster_type = local.spec.0.cluster_type
857857
memory = local.spec.0.memory / 1024
858-
volume = local.spec.0.min_storage / 1024
858+
volume = local.spec.0.default_storage / 1000
859859
engine_version = lookup(var.engine_versions, local.spec.0.engine_version)
860860
security_group_id = data.tencentcloud_security_group.foo.id
861861
}
862862
863863
locals {
864-
filtered_sharding_spec = [for i in data.tencentcloud_mongodb_zone_config.zone_config.list: i if lookup(i, "cluster_type") == "SHARD" && lookup(i, "min_replicate_set_num") > 0 && lookup(i, "machine_type") == "HIO10G" && lookup(i, "engine_version") != "3.2"]
864+
filtered_sharding_spec = [for i in data.tencentcloud_mongodb_zone_config.zone_config.list: i if lookup(i, "cluster_type") == "SHARD" && lookup(i, "min_replicate_set_num") > 0 && lookup(i, "machine_type") == "HIO10G" && lookup(i, "engine_version") == "4.4" && lookup(i, "memory") == 4096 && lookup(i, "default_storage") == 256000]
865865
sharding_spec = concat(local.filtered_sharding_spec, [for i in data.tencentcloud_mongodb_zone_config.zone_config.list: i if lookup(i, "cluster_type") == "SHARD" && lookup(i, "min_replicate_set_num") > 0])
866866
sharding_machine_type = local.sharding_spec.0.machine_type
867867
sharding_memory = local.sharding_spec.0.memory / 1024
868-
sharding_volume = local.sharding_spec.0.min_storage / 1024
868+
sharding_volume = local.sharding_spec.0.default_storage / 1000
869869
sharding_engine_version = lookup(var.engine_versions, local.sharding_spec.0.engine_version)
870870
}
871871
`

tencentcloud/services/mongodb/extension_mongodb.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ const (
6868
MONGODB_TASK_SUCCESS = "success"
6969
)
7070

71+
const (
72+
MONGODB_STATUS_DELIVERY_SUCCESS = 4
73+
)
74+
7175
func TencentMongodbBasicInfo() map[string]*schema.Schema {
7276
return map[string]*schema.Schema{
7377
"instance_name": {

tencentcloud/services/mongodb/resource_tc_mongodb_instance.go

Lines changed: 85 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1414
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
15+
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
1516
mongodb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mongodb/v20190725"
1617

1718
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
@@ -48,6 +49,59 @@ func ResourceTencentCloudMongodbInstance() *schema.Resource {
4849
Computed: true,
4950
Description: "The number of nodes in each replica set. Default value: 3.",
5051
},
52+
"add_node_list": {
53+
Type: schema.TypeList,
54+
Optional: true,
55+
Description: "Add node attribute list.",
56+
Elem: &schema.Resource{
57+
Schema: map[string]*schema.Schema{
58+
"role": {
59+
Type: schema.TypeString,
60+
Required: true,
61+
Description: "The node role that needs to be added.\n" +
62+
"- SECONDARY: Mongod node;\n" +
63+
"- READONLY: read-only node;\n" +
64+
"- MONGOS: Mongos node.",
65+
},
66+
"zone": {
67+
Type: schema.TypeString,
68+
Required: true,
69+
Description: "The availability zone corresponding to the node.\n" +
70+
"- single availability zone, where all nodes are in the same availability zone;\n" +
71+
"- multiple availability zones: the current standard specification is the distribution of three availability zones, and the master and slave nodes are not in the same availability zone. You should pay attention to configuring the availability zone corresponding to the new node, and the rule that the number of nodes in any two availability zones is greater than the third availability zone must be met after the addition.",
72+
},
73+
},
74+
},
75+
},
76+
"remove_node_list": {
77+
Type: schema.TypeList,
78+
Optional: true,
79+
Description: "Add node attribute list.",
80+
Elem: &schema.Resource{
81+
Schema: map[string]*schema.Schema{
82+
"role": {
83+
Type: schema.TypeString,
84+
Required: true,
85+
Description: "The node role that needs to be deleted.\n" +
86+
"- SECONDARY: Mongod node;\n" +
87+
"- READONLY: read-only node;\n" +
88+
"- MONGOS: Mongos node.",
89+
},
90+
"node_name": {
91+
Type: schema.TypeString,
92+
Required: true,
93+
Description: "The node ID to delete. The shard cluster must specify the name of the node to be deleted by a group of shards, and the rest of the shards should be grouped and aligned.",
94+
},
95+
"zone": {
96+
Type: schema.TypeString,
97+
Required: true,
98+
Description: "The availability zone corresponding to the node.\n" +
99+
"- single availability zone, where all nodes are in the same availability zone;\n" +
100+
"- multiple availability zones: the current standard specification is the distribution of three availability zones, and the master and slave nodes are not in the same availability zone. You should pay attention to configuring the availability zone corresponding to the new node, and the rule that the number of nodes in any two availability zones is greater than the third availability zone must be met after the addition.",
101+
},
102+
},
103+
},
104+
},
51105
"availability_zone_list": {
52106
Type: schema.TypeList,
53107
Optional: true,
@@ -446,36 +500,53 @@ func resourceTencentCloudMongodbInstanceUpdate(d *schema.ResourceData, meta inte
446500

447501
d.Partial(true)
448502

449-
immutableArgs := []string{"node_num", "availability_zone_list", "hidden_zone"}
503+
immutableArgs := []string{"availability_zone_list", "hidden_zone"}
450504

451505
for _, v := range immutableArgs {
452506
if d.HasChange(v) {
453507
return fmt.Errorf("argument `%s` cannot be changed", v)
454508
}
455509
}
456510

457-
if d.HasChange("memory") || d.HasChange("volume") {
511+
if d.HasChange("memory") || d.HasChange("volume") || d.HasChange("node_num") {
458512
memory := d.Get("memory").(int)
459513
volume := d.Get("volume").(int)
460-
err := mongodbService.UpgradeInstance(ctx, instanceId, memory, volume)
514+
params := make(map[string]interface{})
515+
var nodeNum int
516+
if v, ok := d.GetOk("node_num"); ok {
517+
nodeNum = v.(int)
518+
params["node_num"] = nodeNum
519+
}
520+
if v, ok := d.GetOk("add_node_list"); ok {
521+
addNodeList := v.([]interface{})
522+
params["add_node_list"] = addNodeList
523+
}
524+
if v, ok := d.GetOk("remove_node_list"); ok {
525+
removeNodeList := v.([]interface{})
526+
params["remove_node_list"] = removeNodeList
527+
}
528+
dealId, err := mongodbService.UpgradeInstance(ctx, instanceId, memory, volume, params)
461529
if err != nil {
462530
return err
463531
}
464532

465-
// it will take time to wait for memory and volume change even describe request succeeded even the status returned in describe response is running
533+
if dealId == "" {
534+
return fmt.Errorf("deal id is empty")
535+
}
536+
466537
errUpdate := resource.Retry(20*tccommon.ReadRetryTimeout, func() *resource.RetryError {
467-
infos, has, e := mongodbService.DescribeInstanceById(ctx, instanceId)
468-
if e != nil {
469-
return resource.NonRetryableError(e)
470-
}
471-
if !has {
472-
return resource.NonRetryableError(fmt.Errorf("[CRITAL]%s updating mongodb instance failed, instance doesn't exist", logId))
538+
dealResponseParams, err := mongodbService.DescribeDBInstanceDeal(ctx, dealId)
539+
if err != nil {
540+
if sdkError, ok := err.(*errors.TencentCloudSDKError); ok {
541+
if sdkError.Code == "InvalidParameter" && sdkError.Message == "deal resource not found." {
542+
return resource.RetryableError(err)
543+
}
544+
}
545+
return resource.NonRetryableError(err)
473546
}
474547

475-
memoryDes := *infos.Memory / 1024 / (*infos.ReplicationSetNum)
476-
volumeDes := *infos.Volume / 1024 / (*infos.ReplicationSetNum)
477-
if memory != int(memoryDes) || volume != int(volumeDes) {
478-
return resource.RetryableError(fmt.Errorf("[CRITAL] updating mongodb instance, current memory and volume values: %d, %d, waiting for them becoming new value: %d, %d", memoryDes, volumeDes, d.Get("memory").(int), d.Get("volume").(int)))
548+
if *dealResponseParams.Status != MONGODB_STATUS_DELIVERY_SUCCESS {
549+
return resource.RetryableError(fmt.Errorf("mongodb status is not delivery success"))
479550
}
480551
return nil
481552
})

tencentcloud/services/mongodb/resource_tc_mongodb_instance_test.go

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ func TestAccTencentCloudMongodbInstanceResourcePostPaid(t *testing.T) {
7979
CheckDestroy: testAccCheckMongodbInstanceDestroy,
8080
Steps: []resource.TestStep{
8181
{
82-
PreConfig: func() { tcacctest.AccStepPreConfigSetTempAKSK(t, tcacctest.ACCOUNT_TYPE_PREPAY) },
83-
Config: testAccMongodbInstance,
82+
Config: testAccMongodbInstance,
8483
Check: resource.ComposeTestCheckFunc(
8584
testAccCheckMongodbInstanceExists("tencentcloud_mongodb_instance.mongodb"),
8685
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb", "instance_name", "tf-mongodb-test"),
@@ -102,20 +101,25 @@ func TestAccTencentCloudMongodbInstanceResourcePostPaid(t *testing.T) {
102101
{
103102
ResourceName: "tencentcloud_mongodb_instance.mongodb",
104103
ImportState: true,
105-
ImportStateVerify: true,
106104
ImportStateVerifyIgnore: []string{"security_groups", "password", "auto_renew_flag"},
107105
},
108106
{
109-
SkipFunc: func() (bool, error) {
110-
log.Printf("[WARN] MongoDB Update Need DealID query available, skip checking.")
111-
return true, nil
112-
},
113-
PreConfig: func() { tcacctest.AccStepPreConfigSetTempAKSK(t, tcacctest.ACCOUNT_TYPE_PREPAY) },
114-
Config: testAccMongodbInstance_update,
107+
Config: testAccMongodbInstance_updateConfig,
115108
Check: resource.ComposeTestCheckFunc(
116109
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb", "instance_name", "tf-mongodb-update"),
117110
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb", "memory", "8"),
118-
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb", "volume", "200"),
111+
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb", "volume", "512"),
112+
resource.TestCheckNoResourceAttr("tencentcloud_mongodb_instance.mongodb", "tags.test"),
113+
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb", "tags.abc", "abc"),
114+
),
115+
},
116+
{
117+
Config: testAccMongodbInstance_updateNode,
118+
Check: resource.ComposeTestCheckFunc(
119+
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb", "instance_name", "tf-mongodb-update"),
120+
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb", "memory", "8"),
121+
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb", "volume", "512"),
122+
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb", "node_num", "5"),
119123
resource.TestCheckNoResourceAttr("tencentcloud_mongodb_instance.mongodb", "tags.test"),
120124
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb", "tags.abc", "abc"),
121125
),
@@ -132,8 +136,7 @@ func TestAccTencentCloudMongodbInstanceResource_multiZone(t *testing.T) {
132136
CheckDestroy: testAccCheckMongodbInstanceDestroy,
133137
Steps: []resource.TestStep{
134138
{
135-
PreConfig: func() { tcacctest.AccStepPreConfigSetTempAKSK(t, tcacctest.ACCOUNT_TYPE_PREPAY) },
136-
Config: testAccMongodbInstance_multiZone,
139+
Config: testAccMongodbInstance_multiZone,
137140
Check: resource.ComposeTestCheckFunc(
138141
testAccCheckMongodbInstanceExists("tencentcloud_mongodb_instance.mongodb_mutil_zone"),
139142
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb_mutil_zone", "node_num", "5"),
@@ -148,12 +151,11 @@ func TestAccTencentCloudMongodbInstanceResource_multiZone(t *testing.T) {
148151
func TestAccTencentCloudMongodbInstanceResourcePrepaid(t *testing.T) {
149152
// Avoid to set Parallel to make sure EnvVar secure
150153
resource.Test(t, resource.TestCase{
151-
PreCheck: func() { tcacctest.AccPreCheckCommon(t, tcacctest.ACCOUNT_TYPE_PREPAY) },
154+
PreCheck: func() { tcacctest.AccPreCheck(t) },
152155
Providers: tcacctest.AccProviders,
153156
Steps: []resource.TestStep{
154157
{
155-
PreConfig: func() { tcacctest.AccStepPreConfigSetTempAKSK(t, tcacctest.ACCOUNT_TYPE_PREPAY) },
156-
Config: testAccMongodbInstancePrepaid,
158+
Config: testAccMongodbInstancePrepaid,
157159
Check: resource.ComposeTestCheckFunc(
158160
testAccCheckMongodbInstanceExists("tencentcloud_mongodb_instance.mongodb_prepaid"),
159161
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb_prepaid", "instance_name", "tf-mongodb-test-prepaid"),
@@ -174,8 +176,7 @@ func TestAccTencentCloudMongodbInstanceResourcePrepaid(t *testing.T) {
174176
),
175177
},
176178
{
177-
PreConfig: func() { tcacctest.AccStepPreConfigSetTempAKSK(t, tcacctest.ACCOUNT_TYPE_PREPAY) },
178-
Config: testAccMongodbInstancePrepaid_update,
179+
Config: testAccMongodbInstancePrepaid_update,
179180
Check: resource.ComposeTestCheckFunc(
180181
resource.TestCheckResourceAttr("tencentcloud_mongodb_instance.mongodb_prepaid", "instance_name", "tf-mongodb-test-prepaid-update"),
181182
resource.TestCheckNoResourceAttr("tencentcloud_mongodb_instance.mongodb_prepaid", "tags.test"),
@@ -185,7 +186,6 @@ func TestAccTencentCloudMongodbInstanceResourcePrepaid(t *testing.T) {
185186
{
186187
ResourceName: "tencentcloud_mongodb_instance.mongodb_prepaid",
187188
ImportState: true,
188-
ImportStateVerify: true,
189189
ImportStateVerifyIgnore: []string{"security_groups", "password", "auto_renew_flag", "prepaid_period"},
190190
},
191191
},
@@ -258,7 +258,7 @@ resource "tencentcloud_mongodb_instance" "mongodb" {
258258
}
259259
`
260260

261-
const testAccMongodbInstance_update = tcacctest.DefaultMongoDBSpec + `
261+
const testAccMongodbInstance_updateConfig = tcacctest.DefaultMongoDBSpec + `
262262
resource "tencentcloud_mongodb_instance" "mongodb" {
263263
instance_name = "tf-mongodb-update"
264264
memory = local.memory * 2
@@ -271,7 +271,35 @@ resource "tencentcloud_mongodb_instance" "mongodb" {
271271
password = "test1234update"
272272
vpc_id = var.vpc_id
273273
subnet_id = var.subnet_id
274+
tags = {
275+
abc = "abc"
276+
}
277+
}
278+
`
274279

280+
const testAccMongodbInstance_updateNode = tcacctest.DefaultMongoDBSpec + `
281+
resource "tencentcloud_mongodb_instance" "mongodb" {
282+
instance_name = "tf-mongodb-update"
283+
memory = local.memory * 2
284+
volume = local.volume * 2
285+
engine_version = local.engine_version
286+
machine_type = local.machine_type
287+
security_groups = [local.security_group_id]
288+
available_zone = "ap-guangzhou-3"
289+
project_id = 0
290+
password = "test1234update"
291+
vpc_id = var.vpc_id
292+
subnet_id = var.subnet_id
293+
294+
node_num = 5
295+
add_node_list {
296+
role = "SECONDARY"
297+
zone = "ap-guangzhou-3"
298+
}
299+
add_node_list {
300+
role = "SECONDARY"
301+
zone = "ap-guangzhou-3"
302+
}
275303
tags = {
276304
abc = "abc"
277305
}

tencentcloud/services/mongodb/resource_tc_mongodb_sharding_instance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ func resourceMongodbShardingInstanceUpdate(d *schema.ResourceData, meta interfac
437437
if d.HasChange("memory") || d.HasChange("volume") {
438438
memory := d.Get("memory").(int)
439439
volume := d.Get("volume").(int)
440-
err := mongodbService.UpgradeInstance(ctx, instanceId, memory, volume)
440+
_, err := mongodbService.UpgradeInstance(ctx, instanceId, memory, volume, nil)
441441
if err != nil {
442442
return err
443443
}

tencentcloud/services/mongodb/resource_tc_mongodb_standby_instance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ func resourceTencentCloudMongodbStandbyInstanceUpdate(d *schema.ResourceData, me
402402
if d.HasChange("memory") || d.HasChange("volume") {
403403
memory := d.Get("memory").(int)
404404
volume := d.Get("volume").(int)
405-
err := mongodbService.UpgradeInstance(ctx, instanceId, memory, volume)
405+
_, err := mongodbService.UpgradeInstance(ctx, instanceId, memory, volume, nil)
406406
if err != nil {
407407
return err
408408
}

0 commit comments

Comments
 (0)