@@ -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
4546func resourceTencentCloudMongodbInstance () * schema.Resource {
@@ -83,9 +84,6 @@ func resourceTencentCloudMongodbInstance() *schema.Resource {
8384}
8485
8586func 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