4
4
"context"
5
5
innerErr "errors"
6
6
"fmt"
7
+ "strconv"
8
+ "time"
7
9
8
10
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
9
11
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
@@ -103,6 +105,28 @@ func ResourceTencentCloudEmrCluster() *schema.Resource {
103
105
},
104
106
Description : "Resource specification of EMR instance." ,
105
107
},
108
+ "terminate_node_info" : {
109
+ Type : schema .TypeList ,
110
+ Optional : true ,
111
+ Description : "Terminate nodes. Note: it only works when the number of nodes decreases." ,
112
+ Elem : & schema.Resource {
113
+ Schema : map [string ]* schema.Schema {
114
+ "cvm_instance_ids" : {
115
+ Type : schema .TypeList ,
116
+ Optional : true ,
117
+ Elem : & schema.Schema {
118
+ Type : schema .TypeString ,
119
+ },
120
+ Description : "Destroy resource list." ,
121
+ },
122
+ "node_flag" : {
123
+ Type : schema .TypeString ,
124
+ Optional : true ,
125
+ Description : "Value range of destruction node type: `MASTER`, `TASK`, `CORE`, `ROUTER`." ,
126
+ },
127
+ },
128
+ },
129
+ },
106
130
"support_ha" : {
107
131
Type : schema .TypeInt ,
108
132
Required : true ,
@@ -220,7 +244,7 @@ func resourceTencentCloudEmrClusterUpdate(d *schema.ResourceData, meta interface
220
244
logId := tccommon .GetLogId (tccommon .ContextNil )
221
245
ctx := context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
222
246
223
- immutableFields := []string {"auto_renew" , "placement" , "placement_info" , "display_strategy" , "login_settings" , "resource_spec.0.master_count" , "resource_spec.0.task_count" , "resource_spec.0.core_count " }
247
+ immutableFields := []string {"auto_renew" , "placement" , "placement_info" , "display_strategy" , "login_settings" , "extend_fs_field " }
224
248
for _ , f := range immutableFields {
225
249
if d .HasChange (f ) {
226
250
return fmt .Errorf ("cannot update argument `%s`" , f )
@@ -245,63 +269,92 @@ func resourceTencentCloudEmrClusterUpdate(d *schema.ResourceData, meta interface
245
269
}
246
270
}
247
271
248
- hasChange := false
249
- request := emr .NewScaleOutInstanceRequest ()
250
- request .TimeUnit = common .StringPtr (timeUnit .(string ))
251
- request .TimeSpan = common .Uint64Ptr ((uint64 )(timeSpan .(int )))
252
- request .PayMode = common .Uint64Ptr ((uint64 )(payMode .(int )))
253
- request .InstanceId = common .StringPtr (instanceId )
254
-
255
- tmpResourceSpec := d .Get ("resource_spec" ).([]interface {})
256
- resourceSpec := tmpResourceSpec [0 ].(map [string ]interface {})
257
-
258
272
if d .HasChange ("resource_spec.0.master_count" ) {
259
- request .MasterCount = common .Uint64Ptr ((uint64 )(resourceSpec ["master_count" ].(int )))
260
- hasChange = true
273
+ request := emr .NewScaleOutInstanceRequest ()
274
+ request .TimeUnit = common .StringPtr (timeUnit .(string ))
275
+ request .TimeSpan = common .Uint64Ptr ((uint64 )(timeSpan .(int )))
276
+ request .PayMode = common .Uint64Ptr ((uint64 )(payMode .(int )))
277
+ request .InstanceId = common .StringPtr (instanceId )
278
+
279
+ o , n := d .GetChange ("resource_spec.0.master_count" )
280
+ if o .(int ) < n .(int ) {
281
+ request .MasterCount = common .Uint64Ptr ((uint64 )(n .(int ) - o .(int )))
282
+ traceId , err := emrService .ScaleOutInstance (ctx , request )
283
+ if err != nil {
284
+ return err
285
+ }
286
+ time .Sleep (5 * time .Second )
287
+ conf := tccommon .BuildStateChangeConf ([]string {}, []string {"2" }, 10 * tccommon .ReadRetryTimeout , time .Second , emrService .FlowStatusRefreshFunc (instanceId , traceId , F_KEY_TRACE_ID , []string {}))
288
+ if _ , e := conf .WaitForState (); e != nil {
289
+ return e
290
+ }
291
+ }
261
292
}
262
293
if d .HasChange ("resource_spec.0.task_count" ) {
263
- request .TaskCount = common .Uint64Ptr ((uint64 )(resourceSpec ["task_count" ].(int )))
264
- hasChange = true
294
+ request := emr .NewScaleOutInstanceRequest ()
295
+ request .TimeUnit = common .StringPtr (timeUnit .(string ))
296
+ request .TimeSpan = common .Uint64Ptr ((uint64 )(timeSpan .(int )))
297
+ request .PayMode = common .Uint64Ptr ((uint64 )(payMode .(int )))
298
+ request .InstanceId = common .StringPtr (instanceId )
299
+
300
+ o , n := d .GetChange ("resource_spec.0.task_count" )
301
+ if o .(int ) < n .(int ) {
302
+ request .TaskCount = common .Uint64Ptr ((uint64 )(n .(int ) - o .(int )))
303
+ traceId , err := emrService .ScaleOutInstance (ctx , request )
304
+ if err != nil {
305
+ return err
306
+ }
307
+ time .Sleep (5 * time .Second )
308
+ conf := tccommon .BuildStateChangeConf ([]string {}, []string {"2" }, 10 * tccommon .ReadRetryTimeout , time .Second , emrService .FlowStatusRefreshFunc (instanceId , traceId , F_KEY_TRACE_ID , []string {}))
309
+ if _ , e := conf .WaitForState (); e != nil {
310
+ return e
311
+ }
312
+ }
265
313
}
266
314
if d .HasChange ("resource_spec.0.core_count" ) {
267
- request .CoreCount = common .Uint64Ptr ((uint64 )(resourceSpec ["core_count" ].(int )))
268
- hasChange = true
269
- }
270
- if d .HasChange ("extend_fs_field" ) {
271
- return innerErr .New ("extend_fs_field not support update." )
272
- }
273
- if ! hasChange {
274
- return nil
275
- }
276
- _ , err := emrService .UpdateInstance (ctx , request )
277
- if err != nil {
278
- return err
279
- }
280
- err = resource .Retry (10 * tccommon .ReadRetryTimeout , func () * resource.RetryError {
281
- clusters , err := emrService .DescribeInstancesById (ctx , instanceId , DisplayStrategyIsclusterList )
282
-
283
- if e , ok := err .(* errors.TencentCloudSDKError ); ok {
284
- if e .GetCode () == "InternalError.ClusterNotFound" {
285
- return nil
315
+ request := emr .NewScaleOutInstanceRequest ()
316
+ request .TimeUnit = common .StringPtr (timeUnit .(string ))
317
+ request .TimeSpan = common .Uint64Ptr ((uint64 )(timeSpan .(int )))
318
+ request .PayMode = common .Uint64Ptr ((uint64 )(payMode .(int )))
319
+ request .InstanceId = common .StringPtr (instanceId )
320
+
321
+ o , n := d .GetChange ("resource_spec.0.core_count" )
322
+ if o .(int ) < n .(int ) {
323
+ request .CoreCount = common .Uint64Ptr ((uint64 )(n .(int ) - o .(int )))
324
+ traceId , err := emrService .ScaleOutInstance (ctx , request )
325
+ if err != nil {
326
+ return err
286
327
}
287
- }
288
-
289
- if len (clusters ) > 0 {
290
- status := * (clusters [0 ].Status )
291
- if status != EmrInternetStatusCreated {
292
- return resource .RetryableError (
293
- fmt .Errorf ("%v create cluster endpoint status still is %v" , instanceId , status ))
328
+ time .Sleep (5 * time .Second )
329
+ conf := tccommon .BuildStateChangeConf ([]string {}, []string {"2" }, 10 * tccommon .ReadRetryTimeout , time .Second , emrService .FlowStatusRefreshFunc (instanceId , traceId , F_KEY_TRACE_ID , []string {}))
330
+ if _ , e := conf .WaitForState (); e != nil {
331
+ return e
294
332
}
295
333
}
334
+ }
296
335
297
- if err != nil {
298
- return resource .RetryableError (err )
336
+ if d .HasChange ("resource_spec.0.master_count" ) || d .HasChange ("resource_spec.0.task_count" ) || d .HasChange ("resource_spec.0.core_count" ) {
337
+ if v , ok := d .GetOk ("terminate_node_info" ); ok {
338
+ terminateNodeInfos := v .([]interface {})
339
+ for _ , terminateNodeInfo := range terminateNodeInfos {
340
+ terminateNodeInfoMap := terminateNodeInfo .(map [string ]interface {})
341
+ instanceIds := make ([]string , 0 )
342
+ for _ , instanceId := range terminateNodeInfoMap ["cvm_instance_ids" ].([]interface {}) {
343
+ instanceIds = append (instanceIds , instanceId .(string ))
344
+ }
345
+ flowId , err := emrService .TerminateClusterNodes (ctx , instanceIds , instanceId , terminateNodeInfoMap ["node_flag" ].(string ))
346
+ if err != nil {
347
+ return err
348
+ }
349
+ time .Sleep (5 * time .Second )
350
+ conf := tccommon .BuildStateChangeConf ([]string {}, []string {"2" }, 10 * tccommon .ReadRetryTimeout , time .Second , emrService .FlowStatusRefreshFunc (instanceId , strconv .FormatInt (flowId , 10 ), F_KEY_FLOW_ID , []string {}))
351
+ if _ , e := conf .WaitForState (); e != nil {
352
+ return e
353
+ }
354
+ }
299
355
}
300
- return nil
301
- })
302
- if err != nil {
303
- return err
304
356
}
357
+
305
358
return resourceTencentCloudEmrClusterRead (d , meta )
306
359
}
307
360
0 commit comments