@@ -63,6 +63,7 @@ func ResourceTencentCloudTrocketRocketmqInstance() *schema.Resource {
6363 Required : true ,
6464 Description : "VPC id." ,
6565 },
66+
6667 "subnet_id" : {
6768 Type : schema .TypeString ,
6869 Required : true ,
@@ -135,15 +136,16 @@ func resourceTencentCloudTrocketRocketmqInstanceCreate(d *schema.ResourceData, m
135136 defer tccommon .LogElapsed ("resource.tencentcloud_trocket_rocketmq_instance.create" )()
136137 defer tccommon .InconsistentCheck (d , meta )()
137138
138- logId := tccommon .GetLogId (tccommon .ContextNil )
139- ctx := context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
140139 var (
140+ logId = tccommon .GetLogId (tccommon .ContextNil )
141+ ctx = context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
141142 request = trocket .NewCreateInstanceRequest ()
142143 response = trocket .NewCreateInstanceResponse ()
143144 instanceId string
144145 enablePublic bool
145146 bandwidth int
146147 )
148+
147149 if v , ok := d .GetOk ("instance_type" ); ok {
148150 request .InstanceType = helper .String (v .(string ))
149151 }
@@ -180,19 +182,23 @@ func resourceTencentCloudTrocketRocketmqInstanceCreate(d *schema.ResourceData, m
180182 if enablePublic && bandwidth <= 0 {
181183 return fmt .Errorf ("`bandwidth` must be greater than zero when `enable_public` equal true." )
182184 }
185+
183186 if v , ok := d .GetOk ("ip_rules" ); ok {
184187 for _ , item := range v .([]interface {}) {
185188 dMap := item .(map [string ]interface {})
186189 ipRule := trocket.IpRule {}
187190 if v , ok := dMap ["ip" ]; ok {
188191 ipRule .Ip = helper .String (v .(string ))
189192 }
193+
190194 if v , ok := dMap ["allow" ]; ok {
191195 ipRule .Allow = helper .Bool (v .(bool ))
192196 }
197+
193198 if v , ok := dMap ["remark" ]; ok {
194199 ipRule .Remark = helper .String (v .(string ))
195200 }
201+
196202 request .IpRules = append (request .IpRules , & ipRule )
197203 }
198204 }
@@ -208,15 +214,28 @@ func resourceTencentCloudTrocketRocketmqInstanceCreate(d *schema.ResourceData, m
208214 } else {
209215 log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " , logId , request .GetAction (), request .ToJsonString (), result .ToJsonString ())
210216 }
217+
218+ if result == nil || result .Response == nil {
219+ return resource .NonRetryableError (fmt .Errorf ("Create trocket rocketmqInstance failed, Response is nil." ))
220+ }
221+
211222 response = result
212223 return nil
213224 })
225+
214226 if err != nil {
215227 log .Printf ("[CRITAL]%s create trocket rocketmqInstance failed, reason:%+v" , logId , err )
216228 return err
217229 }
230+
231+ if response .Response .InstanceId == nil {
232+ return fmt .Errorf ("InstanceId is nil." )
233+ }
234+
218235 instanceId = * response .Response .InstanceId
236+ d .SetId (instanceId )
219237
238+ // wait
220239 service := TrocketService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
221240 conf := tccommon .BuildStateChangeConf ([]string {}, []string {"RUNNING" }, 10 * tccommon .ReadRetryTimeout , time .Second , service .TrocketRocketmqInstanceStateRefreshFunc (instanceId , []string {}))
222241 if _ , e := conf .WaitForState (); e != nil {
@@ -232,31 +251,28 @@ func resourceTencentCloudTrocketRocketmqInstanceCreate(d *schema.ResourceData, m
232251 }
233252 }
234253
235- d .SetId (instanceId )
236-
237254 return resourceTencentCloudTrocketRocketmqInstanceRead (d , meta )
238255}
239256
240257func resourceTencentCloudTrocketRocketmqInstanceRead (d * schema.ResourceData , meta interface {}) error {
241258 defer tccommon .LogElapsed ("resource.tencentcloud_trocket_rocketmq_instance.read" )()
242259 defer tccommon .InconsistentCheck (d , meta )()
243260
244- logId := tccommon .GetLogId (tccommon .ContextNil )
245-
246- ctx := context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
247-
248- service := TrocketService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
249-
250- instanceId := d .Id ()
261+ var (
262+ logId = tccommon .GetLogId (tccommon .ContextNil )
263+ ctx = context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
264+ service = TrocketService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
265+ instanceId = d .Id ()
266+ )
251267
252268 rocketmqInstance , err := service .DescribeTrocketRocketmqInstanceById (ctx , instanceId )
253269 if err != nil {
254270 return err
255271 }
256272
257273 if rocketmqInstance == nil {
258- d .SetId ("" )
259274 log .Printf ("[WARN]%s resource `TrocketRocketmqInstance` [%s] not found, please check if it has been deleted.\n " , logId , d .Id ())
275+ d .SetId ("" )
260276 return nil
261277 }
262278
@@ -282,6 +298,7 @@ func resourceTencentCloudTrocketRocketmqInstanceRead(d *schema.ResourceData, met
282298 if endpointType == nil {
283299 continue
284300 }
301+
285302 if * endpointType == ENDPOINT_TYPE_PUBLIC {
286303 enablePublic = true
287304 if len (endpoint .IpRules ) != 0 {
@@ -293,13 +310,17 @@ func resourceTencentCloudTrocketRocketmqInstanceRead(d *schema.ResourceData, met
293310 ipRuleMap ["remark" ] = ipRule .Remark
294311 ipRuleList = append (ipRuleList , ipRuleMap )
295312 }
313+
296314 _ = d .Set ("ip_rules" , ipRuleList )
297315 }
316+
298317 if endpoint .Bandwidth != nil {
299318 _ = d .Set ("bandwidth" , endpoint .Bandwidth )
300319 }
320+
301321 _ = d .Set ("public_end_point" , endpoint .EndpointUrl )
302322 }
323+
303324 if * endpointType == ENDPOINT_TYPE_VPC {
304325 if endpoint .VpcId != nil {
305326 _ = d .Set ("vpc_id" , endpoint .VpcId )
@@ -308,10 +329,12 @@ func resourceTencentCloudTrocketRocketmqInstanceRead(d *schema.ResourceData, met
308329 if endpoint .SubnetId != nil {
309330 _ = d .Set ("subnet_id" , endpoint .SubnetId )
310331 }
332+
311333 _ = d .Set ("vpc_end_point" , endpoint .EndpointUrl )
312334 }
313335
314336 }
337+
315338 _ = d .Set ("enable_public" , enablePublic )
316339
317340 if rocketmqInstance .MessageRetention != nil {
@@ -334,16 +357,17 @@ func resourceTencentCloudTrocketRocketmqInstanceUpdate(d *schema.ResourceData, m
334357 defer tccommon .LogElapsed ("resource.tencentcloud_trocket_rocketmq_instance.update" )()
335358 defer tccommon .InconsistentCheck (d , meta )()
336359
337- logId := tccommon . GetLogId ( tccommon . ContextNil )
338- ctx := context . WithValue ( context . TODO (), tccommon .LogIdKey , logId )
339- request := trocket . NewModifyInstanceRequest ( )
340-
341- instanceId := d . Id ()
342-
343- request . InstanceId = & instanceId
344-
345- immutableArgs := [] string { "instance_type" , "vpc_id" , "subnet_id" , "enable_public" , "bandwidth" , "ip_rules" }
360+ var (
361+ logId = tccommon . GetLogId ( tccommon .ContextNil )
362+ ctx = context . WithValue ( context . TODO (), tccommon . LogIdKey , logId )
363+ request1 = trocket . NewModifyInstanceRequest ()
364+ request2 = trocket . NewModifyInstanceEndpointRequest ()
365+ instanceId = d . Id ()
366+ needModifyInstance bool
367+ needModifyInstanceEndpoint bool
368+ )
346369
370+ immutableArgs := []string {"instance_type" , "vpc_id" , "subnet_id" , "enable_public" }
347371 for _ , v := range immutableArgs {
348372 if d .HasChange (v ) {
349373 return fmt .Errorf ("argument `%s` cannot be changed" , v )
@@ -352,46 +376,125 @@ func resourceTencentCloudTrocketRocketmqInstanceUpdate(d *schema.ResourceData, m
352376
353377 if d .HasChange ("name" ) {
354378 if v , ok := d .GetOk ("name" ); ok {
355- request .Name = helper .String (v .(string ))
379+ request1 .Name = helper .String (v .(string ))
356380 }
381+
382+ needModifyInstance = true
357383 }
358384
359385 if d .HasChange ("sku_code" ) {
360386 if v , ok := d .GetOk ("sku_code" ); ok {
361- request .SkuCode = helper .String (v .(string ))
387+ request1 .SkuCode = helper .String (v .(string ))
362388 }
389+
390+ needModifyInstance = true
363391 }
364392
365393 if d .HasChange ("remark" ) {
366394 if v , ok := d .GetOk ("remark" ); ok {
367- request .Remark = helper .String (v .(string ))
395+ request1 .Remark = helper .String (v .(string ))
368396 }
397+
398+ needModifyInstance = true
369399 }
370400
371401 if d .HasChange ("message_retention" ) {
372402 if v , ok := d .GetOkExists ("message_retention" ); ok {
373- request .MessageRetention = helper .IntInt64 (v .(int ))
403+ request1 .MessageRetention = helper .IntInt64 (v .(int ))
374404 }
405+
406+ needModifyInstance = true
375407 }
376408
377- err := resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
378- result , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UseTrocketClient ().ModifyInstance (request )
379- if e != nil {
380- return tccommon .RetryError (e )
381- } else {
382- log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " , logId , request .GetAction (), request .ToJsonString (), result .ToJsonString ())
409+ if needModifyInstance {
410+ request1 .InstanceId = & instanceId
411+ err := resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
412+ result , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UseTrocketClient ().ModifyInstance (request1 )
413+ if e != nil {
414+ return tccommon .RetryError (e )
415+ } else {
416+ log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " , logId , request1 .GetAction (), request1 .ToJsonString (), result .ToJsonString ())
417+ }
418+
419+ return nil
420+ })
421+
422+ if err != nil {
423+ log .Printf ("[CRITAL]%s update trocket rocketmqInstance failed, reason:%+v" , logId , err )
424+ return err
425+ }
426+
427+ // wait
428+ service := TrocketService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
429+ conf := tccommon .BuildStateChangeConf ([]string {}, []string {"RUNNING" }, 10 * tccommon .ReadRetryTimeout , time .Second , service .TrocketRocketmqInstanceStateRefreshFunc (instanceId , []string {}))
430+ if _ , e := conf .WaitForState (); e != nil {
431+ return e
383432 }
384- return nil
385- })
386- if err != nil {
387- log .Printf ("[CRITAL]%s update trocket rocketmqInstance failed, reason:%+v" , logId , err )
388- return err
389433 }
390434
391- service := TrocketService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
392- conf := tccommon .BuildStateChangeConf ([]string {}, []string {"RUNNING" }, 10 * tccommon .ReadRetryTimeout , time .Second , service .TrocketRocketmqInstanceStateRefreshFunc (instanceId , []string {}))
393- if _ , e := conf .WaitForState (); e != nil {
394- return e
435+ if d .HasChange ("bandwidth" ) {
436+ if v , ok := d .GetOkExists ("bandwidth" ); ok {
437+ request2 .Bandwidth = helper .IntInt64 (v .(int ))
438+ }
439+
440+ needModifyInstanceEndpoint = true
441+ }
442+
443+ if d .HasChange ("ip_rules" ) {
444+ if v , ok := d .GetOk ("ip_rules" ); ok {
445+ for _ , item := range v .([]interface {}) {
446+ dMap := item .(map [string ]interface {})
447+ ipRule := trocket.IpRule {}
448+ if v , ok := dMap ["ip" ]; ok {
449+ ipRule .Ip = helper .String (v .(string ))
450+ }
451+
452+ if v , ok := dMap ["allow" ]; ok {
453+ ipRule .Allow = helper .Bool (v .(bool ))
454+ }
455+
456+ if v , ok := dMap ["remark" ]; ok {
457+ ipRule .Remark = helper .String (v .(string ))
458+ }
459+
460+ request2 .IpRules = append (request2 .IpRules , & ipRule )
461+ }
462+ }
463+
464+ needModifyInstanceEndpoint = true
465+ }
466+
467+ if needModifyInstanceEndpoint {
468+ if v , ok := d .GetOkExists ("enable_public" ); ok {
469+ if v .(bool ) {
470+ request2 .InstanceId = & instanceId
471+ request2 .Type = helper .String ("PUBLIC" )
472+ err := resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
473+ result , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UseTrocketClient ().ModifyInstanceEndpoint (request2 )
474+ if e != nil {
475+ return tccommon .RetryError (e )
476+ } else {
477+ log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " , logId , request1 .GetAction (), request1 .ToJsonString (), result .ToJsonString ())
478+ }
479+
480+ return nil
481+ })
482+
483+ if err != nil {
484+ log .Printf ("[CRITAL]%s update trocket rocketmqInstance failed, reason:%+v" , logId , err )
485+ return err
486+ }
487+
488+ // wait
489+ service := TrocketService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
490+ conf := tccommon .BuildStateChangeConf ([]string {}, []string {"RUNNING" }, 10 * tccommon .ReadRetryTimeout , time .Second , service .TrocketRocketmqInstanceStateRefreshFunc (instanceId , []string {}))
491+ if _ , e := conf .WaitForState (); e != nil {
492+ return e
493+ }
494+ } else {
495+ return fmt .Errorf ("Only instances with public network access can modify `bandwidth` or `ip_rules`." )
496+ }
497+ }
395498 }
396499
397500 if d .HasChange ("tags" ) {
@@ -404,31 +507,35 @@ func resourceTencentCloudTrocketRocketmqInstanceUpdate(d *schema.ResourceData, m
404507 return err
405508 }
406509 }
510+
407511 return resourceTencentCloudTrocketRocketmqInstanceRead (d , meta )
408512}
409513
410514func resourceTencentCloudTrocketRocketmqInstanceDelete (d * schema.ResourceData , meta interface {}) error {
411515 defer tccommon .LogElapsed ("resource.tencentcloud_trocket_rocketmq_instance.delete" )()
412516 defer tccommon .InconsistentCheck (d , meta )()
413517
414- logId := tccommon .GetLogId (tccommon .ContextNil )
415- ctx := context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
416-
417- service := TrocketService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
418- instanceId := d .Id ()
518+ var (
519+ logId = tccommon .GetLogId (tccommon .ContextNil )
520+ ctx = context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
521+ service = TrocketService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
522+ instanceId = d .Id ()
523+ )
419524
420525 if err := service .DeleteTrocketRocketmqInstanceById (ctx , instanceId ); err != nil {
421526 return err
422527 }
423- conf := tccommon .BuildStateChangeConf ([]string {}, []string {"" }, 10 * tccommon .ReadRetryTimeout , time .Second , service .TrocketRocketmqInstanceStateRefreshFunc (d .Id (), []string {}))
424528
529+ conf := tccommon .BuildStateChangeConf ([]string {}, []string {"" }, 10 * tccommon .ReadRetryTimeout , time .Second , service .TrocketRocketmqInstanceStateRefreshFunc (d .Id (), []string {}))
425530 if _ , err := conf .WaitForState (); err != nil {
426531 if sdkerr , ok := err .(* sdkErrors.TencentCloudSDKError ); ok {
427532 if sdkerr .Code == "ResourceNotFound.Instance" {
428533 return nil
429534 }
430535 }
536+
431537 return err
432538 }
539+
433540 return nil
434541}
0 commit comments