Skip to content

Commit 8e00856

Browse files
committed
[review fix]
1. fix the issues commented in review.
1 parent ad2004b commit 8e00856

13 files changed

+247
-376
lines changed

examples/tencentcloud-mongodb/main.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ resource "tencentcloud_mongodb_instance" "mongodb" {
1818
password = "test1234"
1919

2020
tags = {
21-
"test" = "test"
21+
test = "test"
2222
}
2323
}
2424

@@ -35,15 +35,15 @@ resource "tencentcloud_mongodb_sharding_instance" "mongodb_sharding" {
3535
password = "test1234"
3636

3737
tags = {
38-
"test" = "test"
38+
test = "test"
3939
}
4040
}
4141

4242
data "tencentcloud_mongodb_instances" "mongodb_instances" {
4343
instance_id = tencentcloud_mongodb_instance.mongodb.id
4444

4545
tags = {
46-
"test" = "test"
46+
test = "test"
4747
}
4848
}
4949

@@ -60,6 +60,6 @@ resource "tencentcloud_mongodb_standby_instance" "mongodb" {
6060
prepaid_period = 1
6161

6262
tags = {
63-
"test" = "test"
63+
test = "test"
6464
}
6565
}

tencentcloud/extension_mongodb.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ const (
4141
)
4242

4343
const (
44-
MONGODB_CHARGE_TYPE_POSTPAID = "POSTPAID"
45-
MONGODB_CHARGE_TYPE_PREPAID = "PREPAID"
44+
MONGODB_CHARGE_TYPE_POSTPAID = COMMON_PAYTYPE_POSTPAID
45+
MONGODB_CHARGE_TYPE_PREPAID = COMMON_PAYTYPE_PREPAID
4646
)
4747

4848
var MONGODB_CHARGE_TYPE = map[uint64]string{
@@ -124,7 +124,7 @@ func TencentMongodbBasicInfo() map[string]*schema.Schema {
124124
Type: schema.TypeString,
125125
Optional: true,
126126
ForceNew: true,
127-
Description: "ID of the subnet within this VPC. The vaule is required if VpcId is set.",
127+
Description: "ID of the subnet within this VPC. The value is required if `vpc_id` is set.",
128128
},
129129
"project_id": {
130130
Type: schema.TypeInt,
@@ -162,7 +162,7 @@ func TencentMongodbBasicInfo() map[string]*schema.Schema {
162162
ForceNew: true,
163163
Default: MONGODB_CHARGE_TYPE_POSTPAID,
164164
ValidateFunc: validateAllowedStringValue([]string{MONGODB_CHARGE_TYPE_POSTPAID, MONGODB_CHARGE_TYPE_PREPAID}),
165-
Description: "The charge type of instance. Valid values are `PREPAID` and `POSTPAID`. Default value is `POSTPAID`. Note: TencentCloud International only supports `POSTPAID`. Caution that update operation on this field will delete old instances and create new with new charge type.",
165+
Description: "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.",
166166
},
167167
"prepaid_period": {
168168
Type: schema.TypeInt,
@@ -173,7 +173,8 @@ func TencentMongodbBasicInfo() map[string]*schema.Schema {
173173
"auto_renew_flag": {
174174
Type: schema.TypeInt,
175175
Optional: true,
176-
Description: "Auto renew flag. Valid values are `0`(NOTIFY_AND_MANUAL_RENEW), `1`(NOTIFY_AND_AUTO_RENEW) and `2`(DISABLE_NOTIFY_AND_MANUAL_RENEW). Note: only works for PREPAID instance. Only supports`0` and `1` for creation.",
176+
Default: 0,
177+
Description: "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.",
177178
},
178179
// Computed
179180
"status": {

tencentcloud/internal/helper/transform.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package helper
22

3-
import "github.com/terraform-providers/terraform-provider-tencentcloud/tencentcloud/connectivity"
4-
53
func Bool(i bool) *bool { return &i }
64

75
func String(i string) *string { return &i }
@@ -111,8 +109,3 @@ func BoolToInt64Ptr(s bool) (i *int64) {
111109
i = &result
112110
return
113111
}
114-
115-
func CopySelf(client *connectivity.TencentCloudClient) *connectivity.TencentCloudClient {
116-
tmpClient := *client
117-
return &tmpClient
118-
}

tencentcloud/resource_tc_mongodb_instance.go

Lines changed: 60 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ import (
3333
"errors"
3434
"fmt"
3535
"log"
36+
"reflect"
37+
"strings"
3638

3739
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
38-
"github.com/terraform-providers/terraform-provider-tencentcloud/tencentcloud/ratelimit"
39-
4040
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
4141
mongodb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mongodb/v20190725"
4242
"github.com/terraform-providers/terraform-provider-tencentcloud/tencentcloud/internal/helper"
43+
"github.com/terraform-providers/terraform-provider-tencentcloud/tencentcloud/ratelimit"
4344
)
4445

4546
func resourceTencentCloudMongodbInstance() *schema.Resource {
@@ -83,9 +84,6 @@ func resourceTencentCloudMongodbInstance() *schema.Resource {
8384
}
8485

8586
func mongodbAllInstanceReqSet(requestInter interface{}, d *schema.ResourceData) error {
86-
requestByMonth, okByMonth := requestInter.(*mongodb.CreateDBInstanceRequest)
87-
requestByUse, _ := requestInter.(*mongodb.CreateDBInstanceHourRequest)
88-
8987
var (
9088
replicateSetNum = 1
9189
nodeNum = 3
@@ -98,6 +96,7 @@ func mongodbAllInstanceReqSet(requestInter interface{}, d *schema.ResourceData)
9896
machine = d.Get("machine_type").(string)
9997
password = d.Get("password").(string)
10098
instanceType = MONGO_INSTANCE_TYPE_FORMAL
99+
projectId = d.Get("project_id").(int)
101100
)
102101

103102
if machine == MONGODB_MACHINE_TYPE_GIO {
@@ -106,79 +105,50 @@ func mongodbAllInstanceReqSet(requestInter interface{}, d *schema.ResourceData)
106105
machine = MONGODB_MACHINE_TYPE_HIO10G
107106
}
108107

109-
if okByMonth {
108+
getType := reflect.TypeOf(requestInter)
109+
value := reflect.ValueOf(requestInter).Elem()
110+
111+
for k, v := range map[string]interface{}{
112+
"ReplicateSetNum": helper.IntUint64(replicateSetNum),
113+
"NodeNum": helper.IntUint64(nodeNum),
114+
"GoodsNum": helper.IntUint64(goodsNum),
115+
"ClusterType": &clusterType,
116+
"Memory": helper.IntUint64(memoryInterface),
117+
"Volume": helper.IntUint64(volumeInterface),
118+
"MongoVersion": &mongoVersionInterface,
119+
"Zone": &zoneInterface,
120+
"MachineCode": &machine,
121+
"Password": &password,
122+
"Clone": helper.IntInt64(instanceType),
123+
"ProjectId": helper.IntInt64(projectId),
124+
} {
125+
value.FieldByName(k).Set(reflect.ValueOf(v))
126+
}
127+
128+
var okVpc, okSubnet bool
129+
if v, ok := d.GetOk("vpc_id"); ok {
130+
okVpc = ok
131+
value.FieldByName("VpcId").Set(reflect.ValueOf(helper.String(v.(string))))
132+
}
133+
if v, ok := d.GetOk("subnet_id"); ok {
134+
okSubnet = ok
135+
value.FieldByName("SubnetId").Set(reflect.ValueOf(helper.String(v.(string))))
136+
}
137+
if (okVpc && !okSubnet) || (!okVpc && okSubnet) {
138+
return fmt.Errorf("you have to set vpc_id and subnet_id both")
139+
}
140+
if v, ok := d.GetOk("security_groups"); ok {
141+
sliceReflect := helper.InterfacesStringsPoint(v.(*schema.Set).List())
142+
value.FieldByName("SecurityGroup").Set(reflect.ValueOf(sliceReflect))
143+
}
144+
145+
if strings.Contains(getType.String(), "CreateDBInstanceRequest") {
110146
if v, ok := d.GetOk("prepaid_period"); ok {
111-
requestByMonth.Period = helper.IntUint64(v.(int))
147+
value.FieldByName("Period").Set(reflect.ValueOf(helper.IntUint64(v.(int))))
112148
} else {
113149
return fmt.Errorf("prepaid_period must be specified for a PREPAID instance")
114150
}
115-
requestByMonth.AutoRenewFlag = helper.IntUint64(d.Get("auto_renew_flag").(int))
116-
117-
requestByMonth.ReplicateSetNum = helper.IntUint64(replicateSetNum)
118-
requestByMonth.NodeNum = helper.IntUint64(nodeNum)
119-
requestByMonth.GoodsNum = helper.IntUint64(goodsNum)
120-
requestByMonth.ClusterType = &clusterType
121-
requestByMonth.Memory = helper.IntUint64(memoryInterface)
122-
requestByMonth.Volume = helper.IntUint64(volumeInterface)
123-
requestByMonth.MongoVersion = &mongoVersionInterface
124-
requestByMonth.Zone = &zoneInterface
125-
requestByMonth.MachineCode = &machine
126-
requestByMonth.Password = &password
127-
requestByMonth.Clone = helper.IntInt64(instanceType)
128-
129-
if v, ok := d.GetOk("vpc_id"); ok {
130-
requestByMonth.VpcId = helper.String(v.(string))
131-
}
132-
if v, ok := d.GetOk("subnet_id"); ok {
133-
requestByMonth.SubnetId = helper.String(v.(string))
134-
}
135-
err := fmt.Errorf("you have to set vpc_id and subnet_id both")
136-
if (requestByMonth.VpcId != nil && requestByMonth.SubnetId == nil) || (requestByMonth.VpcId == nil && requestByMonth.SubnetId != nil) {
137-
return err
138-
}
139-
if v, ok := d.GetOk("project_id"); ok {
140-
requestByMonth.ProjectId = helper.IntInt64(v.(int))
141-
}
142-
if v, ok := d.GetOk("security_groups"); ok {
143-
securityGroups := v.(*schema.Set).List()
144-
requestByMonth.SecurityGroup = make([]*string, 0, len(securityGroups))
145-
for _, v := range securityGroups {
146-
requestByMonth.SecurityGroup = append(requestByMonth.SecurityGroup, helper.String(v.(string)))
147-
}
148-
}
149-
} else {
150-
requestByUse.ReplicateSetNum = helper.IntUint64(replicateSetNum)
151-
requestByUse.NodeNum = helper.IntUint64(nodeNum)
152-
requestByUse.GoodsNum = helper.IntUint64(goodsNum)
153-
requestByUse.ClusterType = &clusterType
154-
requestByUse.Memory = helper.IntUint64(memoryInterface)
155-
requestByUse.Volume = helper.IntUint64(volumeInterface)
156-
requestByUse.MongoVersion = &mongoVersionInterface
157-
requestByUse.Zone = &zoneInterface
158-
requestByUse.MachineCode = &machine
159-
requestByUse.Password = &password
160-
requestByUse.Clone = helper.IntInt64(instanceType)
161-
162-
if v, ok := d.GetOk("vpc_id"); ok {
163-
requestByUse.VpcId = helper.String(v.(string))
164-
}
165-
if v, ok := d.GetOk("subnet_id"); ok {
166-
requestByUse.SubnetId = helper.String(v.(string))
167-
}
168-
err := fmt.Errorf("you have to set vpc_id and subnet_id both")
169-
if (requestByUse.VpcId != nil && requestByUse.SubnetId == nil) || (requestByUse.VpcId == nil && requestByUse.SubnetId != nil) {
170-
return err
171-
}
172-
if v, ok := d.GetOk("project_id"); ok {
173-
requestByUse.ProjectId = helper.IntInt64(v.(int))
174-
}
175-
if v, ok := d.GetOk("security_groups"); ok {
176-
securityGroups := v.(*schema.Set).List()
177-
requestByUse.SecurityGroup = make([]*string, 0, len(securityGroups))
178-
for _, v := range securityGroups {
179-
requestByUse.SecurityGroup = append(requestByUse.SecurityGroup, helper.String(v.(string)))
180-
}
181-
}
151+
value.FieldByName("AutoRenewFlag").Set(reflect.ValueOf(helper.IntUint64(d.Get("auto_renew_flag").(int))))
182152
}
183153

184154
return nil
@@ -193,21 +163,19 @@ func mongodbCreateInstanceByUse(ctx context.Context, d *schema.ResourceData, met
193163
}
194164

195165
var response *mongodb.CreateDBInstanceHourResponse
196-
err := resource.Retry(6*writeRetryTimeout, func() *resource.RetryError {
166+
var err error
167+
err = resource.Retry(writeRetryTimeout, func() *resource.RetryError {
197168
ratelimit.Check(request.GetAction())
198-
result, e := meta.(*TencentCloudClient).apiV3Conn.UseMongodbClient().CreateDBInstanceHour(request)
199-
if e != nil {
200-
log.Printf("[CRITAL]%s api[%s] fail, reason:%s\n", logId, request.GetAction(), e.Error())
201-
return retryError(e)
169+
response, err = meta.(*TencentCloudClient).apiV3Conn.UseMongodbClient().CreateDBInstanceHour(request)
170+
if err != nil {
171+
log.Printf("[CRITAL]%s api[%s] fail, reason:%s", logId, request.GetAction(), err.Error())
172+
return retryError(err)
202173
}
203-
response = result
204174
return nil
205175
})
206176
if err != nil {
207177
return err
208178
}
209-
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
210-
logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
211179

212180
if len(response.Response.InstanceIds) < 1 {
213181
return fmt.Errorf("mongodb instance id is nil")
@@ -226,21 +194,19 @@ func mongodbCreateInstanceByMonth(ctx context.Context, d *schema.ResourceData, m
226194
}
227195

228196
var response *mongodb.CreateDBInstanceResponse
229-
err := resource.Retry(6*writeRetryTimeout, func() *resource.RetryError {
197+
var err error
198+
err = resource.Retry(writeRetryTimeout, func() *resource.RetryError {
230199
ratelimit.Check(request.GetAction())
231-
result, e := meta.(*TencentCloudClient).apiV3Conn.UseMongodbClient().CreateDBInstance(request)
232-
if e != nil {
233-
log.Printf("[CRITAL]%s api[%s] fail, reason:%s\n", logId, request.GetAction(), e.Error())
234-
return retryError(e)
200+
response, err = meta.(*TencentCloudClient).apiV3Conn.UseMongodbClient().CreateDBInstance(request)
201+
if err != nil {
202+
log.Printf("[CRITAL]%s api[%s] fail, reason:%s", logId, request.GetAction(), err.Error())
203+
return retryError(err)
235204
}
236-
response = result
237205
return nil
238206
})
239207
if err != nil {
240208
return err
241209
}
242-
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
243-
logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
244210

245211
if len(response.Response.InstanceIds) < 1 {
246212
return fmt.Errorf("mongodb instance id is nil")
@@ -273,7 +239,7 @@ func resourceTencentCloudMongodbInstanceCreate(d *schema.ResourceData, meta inte
273239
_, ok := d.GetOk("prepaid_period")
274240
_, ok1 := d.GetOk("auto_renew_flag")
275241
if ok || ok1 {
276-
return fmt.Errorf("prepaid_period and auto_renew_flag don't make sense for POSTPAID instance, please remove them from your template")
242+
return fmt.Errorf("prepaid_period and auto_renew_flag don't make sense for POSTPAID_BY_HOUR instance, please remove them from your template")
277243
}
278244
if err := mongodbCreateInstanceByUse(ctx, d, meta); err != nil {
279245
return err
@@ -308,7 +274,6 @@ func resourceTencentCloudMongodbInstanceCreate(d *schema.ResourceData, meta inte
308274
if !has {
309275
return fmt.Errorf("[CRITAL]%s creating mongodb instance failed, instance doesn't exist\n", logId)
310276
}
311-
d.SetId(instanceId)
312277

313278
if tags := helper.GetTags(d, "tags"); len(tags) > 0 {
314279
resourceName := BuildTagResourceName("mongodb", "instance", region, instanceId)
@@ -426,12 +391,9 @@ func resourceTencentCloudMongodbInstanceUpdate(d *schema.ResourceData, meta inte
426391

427392
if d.HasChange("memory") || d.HasChange("volume") {
428393
// precheck
429-
if !(d.HasChange("memory") && d.HasChange("volume")) {
430-
return fmt.Errorf("[CRITAL] updating memory and volume of mongodb instance failed, memory and volume must upgrade/downgrade at same time")
431-
}
432394
oldMemory, newMemory := d.GetChange("memory")
433395
oldVolume, newVolume := d.GetChange("volume")
434-
if (newMemory.(int)-oldMemory.(int))^(newVolume.(int)-oldVolume.(int)) <= 0 {
396+
if (newMemory.(int) >= oldMemory.(int) || newVolume.(int) >= oldVolume.(int)) && (newMemory.(int) <= oldMemory.(int) || newVolume.(int) <= oldVolume.(int)) {
435397
return fmt.Errorf("[CRITAL] updating memory and volume of mongodb instance failed, memory and volume must upgrade/downgrade at same time")
436398
}
437399
memory := d.Get("memory").(int)
@@ -453,13 +415,13 @@ func resourceTencentCloudMongodbInstanceUpdate(d *schema.ResourceData, meta inte
453415

454416
memoryDes := *infos.Memory / 1024 / (*infos.ReplicationSetNum)
455417
volumeDes := *infos.Volume / 1024 / (*infos.ReplicationSetNum)
456-
if d.Get("memory").(int) != int(memoryDes) || d.Get("volume").(int) != int(volumeDes) {
418+
if memory != int(memoryDes) || volume != int(volumeDes) {
457419
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)))
458420
}
459421
return nil
460422
})
461423
if errUpdate != nil {
462-
return fmt.Errorf("[CRITAL] updating mongodb instance failed, memory and volume values don't change")
424+
return errUpdate
463425
}
464426

465427
d.SetPartial("memory")

0 commit comments

Comments
 (0)