66 "fmt"
77
88 tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
9+ "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
910 svccdb "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/cdb"
1011 svctag "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/services/tag"
1112
@@ -22,11 +23,14 @@ func ResourceTencentCloudEmrCluster() *schema.Resource {
2223 Read : resourceTencentCloudEmrClusterRead ,
2324 Delete : resourceTencentCloudEmrClusterDelete ,
2425 Update : resourceTencentCloudEmrClusterUpdate ,
26+ Importer : & schema.ResourceImporter {
27+ State : schema .ImportStatePassthrough ,
28+ },
2529 Schema : map [string ]* schema.Schema {
2630 "display_strategy" : {
2731 Type : schema .TypeString ,
28- Required : true ,
29- ForceNew : true ,
32+ Optional : true ,
33+ Deprecated : "It will be deprecated in later versions." ,
3034 Description : "Display strategy of EMR instance." ,
3135 },
3236 "product_id" : {
@@ -54,7 +58,7 @@ func ResourceTencentCloudEmrCluster() *schema.Resource {
5458 Description : "The private net config of EMR instance." ,
5559 },
5660 "softwares" : {
57- Type : schema .TypeList ,
61+ Type : schema .TypeSet ,
5862 Required : true ,
5963 ForceNew : true ,
6064 Elem : & schema.Schema {Type : schema .TypeString },
@@ -71,22 +75,26 @@ func ResourceTencentCloudEmrCluster() *schema.Resource {
7175 "task_resource_spec" : buildResourceSpecSchema (),
7276 "master_count" : {
7377 Type : schema .TypeInt ,
78+ Computed : true ,
7479 Optional : true ,
7580 Description : "The number of master node." ,
7681 },
7782 "core_count" : {
7883 Type : schema .TypeInt ,
84+ Computed : true ,
7985 Optional : true ,
8086 Description : "The number of core node." ,
8187 },
8288 "task_count" : {
8389 Type : schema .TypeInt ,
90+ Computed : true ,
8491 Optional : true ,
8592 Description : "The number of core node." ,
8693 },
8794 "common_resource_spec" : buildResourceSpecSchema (),
8895 "common_count" : {
8996 Type : schema .TypeInt ,
97+ Computed : true ,
9098 Optional : true ,
9199 ForceNew : true ,
92100 Description : "The number of common node." ,
@@ -116,25 +124,50 @@ func ResourceTencentCloudEmrCluster() *schema.Resource {
116124 Description : "The pay mode of instance. 0 represent POSTPAID_BY_HOUR, 1 represent PREPAID." ,
117125 },
118126 "placement" : {
119- Type : schema .TypeMap ,
120- Required : true ,
121- ForceNew : true ,
127+ Type : schema .TypeMap ,
128+ Optional : true ,
129+ Computed : true ,
130+ ExactlyOneOf : []string {"placement" , "placement_info" },
131+ Deprecated : "It will be deprecated in later versions. Use `placement_info` instead." ,
132+ Description : "The location of the instance." ,
133+ },
134+ "placement_info" : {
135+ Type : schema .TypeList ,
136+ Optional : true ,
137+ Computed : true ,
138+ MaxItems : 1 ,
139+ ExactlyOneOf : []string {"placement" , "placement_info" },
140+ Elem : & schema.Resource {
141+ Schema : map [string ]* schema.Schema {
142+ "zone" : {
143+ Type : schema .TypeString ,
144+ Required : true ,
145+ Description : "Zone." ,
146+ },
147+ "project_id" : {
148+ Type : schema .TypeInt ,
149+ Computed : true ,
150+ Optional : true ,
151+ Description : "Project id." ,
152+ },
153+ },
154+ },
122155 Description : "The location of the instance." ,
123156 },
124157 "time_span" : {
125158 Type : schema .TypeInt ,
126- Required : true ,
159+ Optional : true ,
127160 Description : "The length of time the instance was purchased. Use with TimeUnit.When TimeUnit is s, the parameter can only be filled in at 3600, representing a metered instance.\n When TimeUnit is m, the number filled in by this parameter indicates the length of purchase of the monthly instance of the package year, such as 1 for one month of purchase." ,
128161 },
129162 "time_unit" : {
130163 Type : schema .TypeString ,
131- Required : true ,
164+ Optional : true ,
132165 Description : "The unit of time in which the instance was purchased. When PayMode is 0, TimeUnit can only take values of s(second). When PayMode is 1, TimeUnit can only take the value m(month)." ,
133166 },
134167 "login_settings" : {
135168 Type : schema .TypeMap ,
136- Required : true ,
137- ForceNew : true ,
169+ Optional : true ,
170+ Sensitive : true ,
138171 Description : "Instance login settings." ,
139172 },
140173 "extend_fs_field" : {
@@ -178,6 +211,14 @@ func resourceTencentCloudEmrClusterUpdate(d *schema.ResourceData, meta interface
178211 defer tccommon .LogElapsed ("resource.tencentcloud_emr_cluster.update" )()
179212 logId := tccommon .GetLogId (tccommon .ContextNil )
180213 ctx := context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
214+
215+ immutableFields := []string {"placement" , "placement_info" , "display_strategy" , "login_settings" }
216+ for _ , f := range immutableFields {
217+ if d .HasChange (f ) {
218+ return fmt .Errorf ("cannot update argument `%s`" , f )
219+ }
220+ }
221+
181222 emrService := EMRService {
182223 client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn (),
183224 }
@@ -253,13 +294,20 @@ func resourceTencentCloudEmrClusterUpdate(d *schema.ResourceData, meta interface
253294 if err != nil {
254295 return err
255296 }
256- return nil
297+ return resourceTencentCloudEmrClusterRead ( d , meta )
257298}
258299
259300func resourceTencentCloudEmrClusterCreate (d * schema.ResourceData , meta interface {}) error {
260301 defer tccommon .LogElapsed ("resource.tencentcloud_emr_cluster.create" )()
261302 logId := tccommon .GetLogId (tccommon .ContextNil )
262303 ctx := context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
304+ immutableFields := []string {"time_unit" , "time_span" , "login_settings" }
305+ for _ , f := range immutableFields {
306+ if _ , ok := d .GetOkExists (f ); ! ok {
307+ return fmt .Errorf ("Argument `%s` must be set" , f )
308+ }
309+ }
310+
263311 emrService := EMRService {
264312 client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn (),
265313 }
@@ -272,6 +320,8 @@ func resourceTencentCloudEmrClusterCreate(d *schema.ResourceData, meta interface
272320 var displayStrategy string
273321 if v , ok := d .GetOk ("display_strategy" ); ok {
274322 displayStrategy = v .(string )
323+ } else {
324+ displayStrategy = "clusterList"
275325 }
276326 err = resource .Retry (10 * tccommon .ReadRetryTimeout , func () * resource.RetryError {
277327 clusters , err := emrService .DescribeInstancesById (ctx , instanceId , displayStrategy )
@@ -299,7 +349,7 @@ func resourceTencentCloudEmrClusterCreate(d *schema.ResourceData, meta interface
299349 return err
300350 }
301351
302- return nil
352+ return resourceTencentCloudEmrClusterRead ( d , meta )
303353}
304354
305355func resourceTencentCloudEmrClusterDelete (d * schema.ResourceData , meta interface {}) error {
@@ -376,8 +426,9 @@ func resourceTencentCloudEmrClusterRead(d *schema.ResourceData, meta interface{}
376426 client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn (),
377427 }
378428 instanceId := d .Id ()
429+ var instance * emr.ClusterInstancesInfo
379430 err := resource .Retry (tccommon .ReadRetryTimeout , func () * resource.RetryError {
380- _ , err := emrService .DescribeInstancesById (ctx , instanceId , DisplayStrategyIsclusterList )
431+ result , err := emrService .DescribeInstancesById (ctx , instanceId , DisplayStrategyIsclusterList )
381432
382433 if e , ok := err .(* errors.TencentCloudSDKError ); ok {
383434 if e .GetCode () == "InternalError.ClusterNotFound" {
@@ -388,12 +439,191 @@ func resourceTencentCloudEmrClusterRead(d *schema.ResourceData, meta interface{}
388439 if err != nil {
389440 return resource .RetryableError (err )
390441 }
442+
443+ if len (result ) > 0 {
444+ instance = result [0 ]
445+ }
446+
391447 return nil
392448 })
449+
393450 if err != nil {
394451 return err
395452 }
396453
454+ _ = d .Set ("instance_id" , instanceId )
455+ if instance != nil {
456+ _ = d .Set ("product_id" , instance .ProductId )
457+ _ = d .Set ("vpc_settings" , map [string ]interface {}{
458+ "vpc_id" : * instance .UniqVpcId ,
459+ "subnet_id" : * instance .UniqSubnetId ,
460+ })
461+ if instance .Config != nil {
462+ if instance .Config .SoftInfo != nil {
463+ _ = d .Set ("softwares" , helper .PStrings (instance .Config .SoftInfo ))
464+ }
465+
466+ if instance .Config .SupportHA != nil {
467+ if * instance .Config .SupportHA {
468+ _ = d .Set ("support_ha" , 1 )
469+ } else {
470+ _ = d .Set ("support_ha" , 0 )
471+ }
472+ }
473+
474+ if instance .Config .SecurityGroup != nil {
475+ _ = d .Set ("sg_id" , instance .Config .SecurityGroup )
476+ }
477+ resourceSpec := make (map [string ]interface {})
478+
479+ var masterCount int64
480+ if instance .Config .MasterNodeSize != nil {
481+ masterCount = * instance .Config .MasterNodeSize
482+ resourceSpec ["master_count" ] = masterCount
483+ }
484+ if masterCount != 0 && instance .Config .MasterResource != nil {
485+ masterResource := instance .Config .MasterResource
486+ masterResourceSpec := make (map [string ]interface {})
487+ if masterResource .MemSize != nil {
488+ masterResourceSpec ["mem_size" ] = * masterResource .MemSize
489+ }
490+ if masterResource .Cpu != nil {
491+ masterResourceSpec ["cpu" ] = * masterResource .Cpu
492+ }
493+ if masterResource .DiskSize != nil {
494+ masterResourceSpec ["disk_size" ] = * masterResource .DiskSize
495+ }
496+ if masterResource .DiskType != nil {
497+ masterResourceSpec ["disk_type" ] = * masterResource .DiskType
498+ }
499+ if masterResource .Spec != nil {
500+ masterResourceSpec ["spec" ] = * masterResource .Spec
501+ }
502+ if masterResource .StorageType != nil {
503+ masterResourceSpec ["storage_type" ] = * masterResource .StorageType
504+ }
505+ if masterResource .RootSize != nil {
506+ masterResourceSpec ["root_size" ] = * masterResource .RootSize
507+ }
508+ resourceSpec ["master_resource_spec" ] = []interface {}{masterResourceSpec }
509+ }
510+
511+ var coreCount int64
512+ if instance .Config .CoreNodeSize != nil {
513+ coreCount = * instance .Config .CoreNodeSize
514+ resourceSpec ["core_count" ] = coreCount
515+ }
516+ if coreCount != 0 && instance .Config .CoreResource != nil {
517+ coreResource := instance .Config .CoreResource
518+ coreResourceSpec := make (map [string ]interface {})
519+ if coreResource .MemSize != nil {
520+ coreResourceSpec ["mem_size" ] = * coreResource .MemSize
521+ }
522+ if coreResource .Cpu != nil {
523+ coreResourceSpec ["cpu" ] = * coreResource .Cpu
524+ }
525+ if coreResource .DiskSize != nil {
526+ coreResourceSpec ["disk_size" ] = * coreResource .DiskSize
527+ }
528+ if coreResource .DiskType != nil {
529+ coreResourceSpec ["disk_type" ] = * coreResource .DiskType
530+ }
531+ if coreResource .Spec != nil {
532+ coreResourceSpec ["spec" ] = * coreResource .Spec
533+ }
534+ if coreResource .StorageType != nil {
535+ coreResourceSpec ["storage_type" ] = * coreResource .StorageType
536+ }
537+ if coreResource .RootSize != nil {
538+ coreResourceSpec ["root_size" ] = * coreResource .RootSize
539+ }
540+ resourceSpec ["core_resource_spec" ] = []interface {}{coreResourceSpec }
541+ }
542+
543+ var taskCount int64
544+ if instance .Config .TaskNodeSize != nil {
545+ taskCount = * instance .Config .TaskNodeSize
546+ resourceSpec ["task_count" ] = taskCount
547+ }
548+ if taskCount != 0 && instance .Config .TaskResource != nil {
549+ taskResource := instance .Config .TaskResource
550+ taskResourceSpec := make (map [string ]interface {})
551+ if taskResource .MemSize != nil {
552+ taskResourceSpec ["mem_size" ] = * taskResource .MemSize
553+ }
554+ if taskResource .Cpu != nil {
555+ taskResourceSpec ["cpu" ] = * taskResource .Cpu
556+ }
557+ if taskResource .DiskSize != nil {
558+ taskResourceSpec ["disk_size" ] = * taskResource .DiskSize
559+ }
560+ if taskResource .DiskType != nil {
561+ taskResourceSpec ["disk_type" ] = * taskResource .DiskType
562+ }
563+ if taskResource .Spec != nil {
564+ taskResourceSpec ["spec" ] = * taskResource .Spec
565+ }
566+ if taskResource .StorageType != nil {
567+ taskResourceSpec ["storage_type" ] = * taskResource .StorageType
568+ }
569+ if taskResource .RootSize != nil {
570+ taskResourceSpec ["root_size" ] = * taskResource .RootSize
571+ }
572+ resourceSpec ["task_resource_spec" ] = []interface {}{taskResourceSpec }
573+ }
574+
575+ var commonCount int64
576+ if instance .Config .ComNodeSize != nil {
577+ commonCount = * instance .Config .ComNodeSize
578+ resourceSpec ["common_count" ] = commonCount
579+ }
580+ if commonCount != 0 && instance .Config .ComResource != nil {
581+ comResource := instance .Config .ComResource
582+ comResourceSpec := make (map [string ]interface {})
583+ if comResource .MemSize != nil {
584+ comResourceSpec ["mem_size" ] = * comResource .MemSize
585+ }
586+ if comResource .Cpu != nil {
587+ comResourceSpec ["cpu" ] = * comResource .Cpu
588+ }
589+ if comResource .DiskSize != nil {
590+ comResourceSpec ["disk_size" ] = * comResource .DiskSize
591+ }
592+ if comResource .DiskType != nil {
593+ comResourceSpec ["disk_type" ] = * comResource .DiskType
594+ }
595+ if comResource .Spec != nil {
596+ comResourceSpec ["spec" ] = * comResource .Spec
597+ }
598+ if comResource .StorageType != nil {
599+ comResourceSpec ["storage_type" ] = * comResource .StorageType
600+ }
601+ if comResource .RootSize != nil {
602+ comResourceSpec ["root_size" ] = * comResource .RootSize
603+ }
604+ resourceSpec ["common_resource_spec" ] = []interface {}{comResourceSpec }
605+ }
606+
607+ _ = d .Set ("resource_spec" , []interface {}{resourceSpec })
608+ }
609+
610+ _ = d .Set ("instance_name" , instance .ClusterName )
611+ _ = d .Set ("pay_mode" , instance .ChargeType )
612+ placement := map [string ]interface {}{
613+ "zone" : * instance .Zone ,
614+ "project_id" : * instance .ProjectId ,
615+ }
616+ _ = d .Set ("placement" , map [string ]interface {}{
617+ "zone" : * instance .Zone ,
618+ })
619+ _ = d .Set ("placement_info" , []interface {}{placement })
620+ if instance .MasterIp != nil {
621+ _ = d .Set ("need_master_wan" , "NEED_MASTER_WAN" )
622+ } else {
623+ _ = d .Set ("need_master_wan" , "NOT_NEED_MASTER_WAN" )
624+ }
625+ }
626+
397627 tagService := svctag .NewTagService (meta .(tccommon.ProviderMeta ).GetAPIV3Conn ())
398628 region := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().Region
399629 tags , err := tagService .DescribeResourceTags (ctx , "emr" , "emr-instance" , region , d .Id ())
0 commit comments