11package emr
22
33import (
4+ "fmt"
5+
46 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
57 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
68 emr "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/emr/v20190103"
9+ "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
710)
811
912const (
@@ -35,38 +38,123 @@ func buildResourceSpecSchema() *schema.Schema {
3538 MaxItems : 1 ,
3639 Elem : & schema.Resource {
3740 Schema : map [string ]* schema.Schema {
38- "spec" : {Type : schema .TypeString , Optional : true },
39- "storage_type" : {Type : schema .TypeInt , Optional : true },
40- "disk_type" : {Type : schema .TypeString , Optional : true },
41- "mem_size" : {Type : schema .TypeInt , Optional : true },
42- "cpu" : {Type : schema .TypeInt , Optional : true },
43- "disk_size" : {Type : schema .TypeInt , Optional : true },
44- "root_size" : {Type : schema .TypeInt , Optional : true },
41+ "spec" : {
42+ Type : schema .TypeString ,
43+ Optional : true ,
44+ ForceNew : true ,
45+ Description : "Node specification description, such as CVM.SA2." ,
46+ },
47+ "storage_type" : {
48+ Type : schema .TypeInt ,
49+ Optional : true ,
50+ ForceNew : true ,
51+ Description : "Storage type. Value range:\n " +
52+ " - 4: Represents cloud SSD;\n " +
53+ " - 5: Represents efficient cloud disk;\n " +
54+ " - 6: Represents enhanced SSD Cloud Block Storage;\n " +
55+ " - 11: Represents throughput Cloud Block Storage;\n " +
56+ " - 12: Represents extremely fast SSD Cloud Block Storage." ,
57+ },
58+ "disk_type" : {
59+ Type : schema .TypeString ,
60+ Optional : true ,
61+ ForceNew : true ,
62+ Description : "disk types. Value range:\n " +
63+ " - CLOUD_SSD: Represents cloud SSD;\n " +
64+ " - CLOUD_PREMIUM: Represents efficient cloud disk;\n " +
65+ " - CLOUD_BASIC: Represents Cloud Block Storage." ,
66+ },
67+ "mem_size" : {
68+ Type : schema .TypeInt ,
69+ Optional : true ,
70+ ForceNew : true ,
71+ Description : "Memory size in M." ,
72+ },
73+ "cpu" : {
74+ Type : schema .TypeInt ,
75+ Optional : true ,
76+ ForceNew : true ,
77+ Description : "Number of CPU cores." ,
78+ },
79+ "disk_size" : {
80+ Type : schema .TypeInt ,
81+ Optional : true ,
82+ ForceNew : true ,
83+ Description : "Data disk capacity." ,
84+ },
85+ "root_size" : {
86+ Type : schema .TypeInt ,
87+ Optional : true ,
88+ ForceNew : true ,
89+ Description : "Root disk capacity." ,
90+ },
91+ "multi_disks" : {
92+ Type : schema .TypeSet ,
93+ Optional : true ,
94+ Computed : true ,
95+ ForceNew : true ,
96+ Description : "Cloud disk list. When the data disk is a cloud disk, use disk_type and disk_size parameters directly, and use multi_disks for excess parts." ,
97+ Elem : & schema.Resource {
98+ Schema : map [string ]* schema.Schema {
99+ "disk_type" : {
100+ Type : schema .TypeString ,
101+ Optional : true ,
102+ ForceNew : true ,
103+ Elem : & schema.Schema {
104+ Type : schema .TypeString ,
105+ },
106+ Description : "Cloud disk type\n " +
107+ " - CLOUD_SSD: Represents cloud SSD;\n " +
108+ " - CLOUD_PREMIUM: Represents efficient cloud disk;\n " +
109+ " - CLOUD_HSSD: Represents enhanced SSD Cloud Block Storage." ,
110+ },
111+ "volume" : {
112+ Type : schema .TypeInt ,
113+ Optional : true ,
114+ ForceNew : true ,
115+ Description : "Cloud disk size." ,
116+ },
117+ "count" : {
118+ Type : schema .TypeInt ,
119+ Optional : true ,
120+ ForceNew : true ,
121+ Description : "Number of cloud disks of this type." ,
122+ },
123+ },
124+ },
125+ Set : func (v interface {}) int {
126+ m := v .(map [string ]interface {})
127+ return helper .HashString (fmt .Sprintf ("%s-%d-%d" , m ["disk_type" ].(string ), m ["volume" ].(int ), m ["count" ].(int )))
128+
129+ },
130+ },
45131 },
46132 },
133+ Description : "Resource details." ,
47134 }
48135}
49136
50- func ParseMultiDisks (_multiDisks []map [string ]interface {}) []* emr.MultiDisk {
51- multiDisks := make ([]* emr.MultiDisk , len (_multiDisks ))
52- for _ , item := range _multiDisks {
137+ func ParseMultiDisks (_multiDisks []interface {}) []* emr.MultiDisk {
138+ multiDisks := make ([]* emr.MultiDisk , 0 , len (_multiDisks ))
139+ for _ , multiDisk := range _multiDisks {
140+ item := multiDisk .(map [string ]interface {})
53141 var diskType string
54- var volume int64
55- var count int64
142+ var volume int
143+ var count int
56144 for subK , subV := range item {
57145 if subK == "disk_type" {
58146 diskType = subV .(string )
59147 } else if subK == "volume" {
60- volume = subV .(int64 )
148+ volume = subV .(int )
61149 } else if subK == "count" {
62- count = subV .(int64 )
150+ count = subV .(int )
63151 }
64152 }
65153 multiDisks = append (multiDisks ,
66154 & emr.MultiDisk {
67- DiskType : common . StringPtr (diskType ),
68- Volume : common . Int64Ptr (volume ),
69- Count : common . Int64Ptr (count ),
155+ DiskType : helper . String (diskType ),
156+ Volume : helper . IntInt64 (volume ),
157+ Count : helper . IntInt64 (count ),
70158 })
71159 }
72160
@@ -101,7 +189,7 @@ func ParseResource(_resource map[string]interface{}) *emr.Resource {
101189 } else if k == "root_size" {
102190 resultResource .RootSize = common .Int64Ptr ((int64 )(v .(int )))
103191 } else if k == "multi_disks" {
104- multiDisks := v .([] map [ string ] interface {} )
192+ multiDisks := v .(* schema. Set ). List ( )
105193 resultResource .MultiDisks = ParseMultiDisks (multiDisks )
106194 } else if k == "tags" {
107195 tags := v .([]map [string ]string )
@@ -116,3 +204,66 @@ func ParseResource(_resource map[string]interface{}) *emr.Resource {
116204 }
117205 return resultResource
118206}
207+
208+ func validateMultiDisks (r map [string ]interface {}) error {
209+ if _ , ok := r ["multi_disks" ]; ! ok {
210+ return nil
211+ }
212+ multiDiskList := r ["multi_disks" ].(* schema.Set ).List ()
213+ visited := make (map [string ]struct {})
214+
215+ for _ , multiDisk := range multiDiskList {
216+ multiDiskMap := multiDisk .(map [string ]interface {})
217+ key := fmt .Sprintf ("%s-%d" , multiDiskMap ["disk_type" ].(string ), multiDiskMap ["volume" ].(int ))
218+ if _ , ok := visited [key ]; ok {
219+ return fmt .Errorf ("Merge disks of the same specifications" )
220+ } else {
221+ visited [key ] = struct {}{}
222+ }
223+ }
224+
225+ return nil
226+ }
227+
228+ func fetchMultiDisks (v * emr.NodeHardwareInfo , r * emr.OutterResource ) (multiDisks []interface {}) {
229+ var inputDataDiskTag string
230+ if r .DiskType != nil && r .DiskSize != nil {
231+ inputDataDiskTag = fmt .Sprintf ("%s-%d" , * r .DiskType , * r .DiskSize )
232+ }
233+ for _ , item := range v .MCMultiDisk {
234+ outputDataDiskTag := ""
235+ multiDisk := make (map [string ]interface {})
236+ if item .Type != nil {
237+ var diskType string
238+ if * item .Type == 4 {
239+ diskType = "CLOUD_SSD"
240+ }
241+ if * item .Type == 5 {
242+ diskType = "CLOUD_PREMIUM"
243+ }
244+ if * item .Type == 6 {
245+ diskType = "CLOUD_HSSD"
246+ }
247+ multiDisk ["disk_type" ] = diskType
248+ outputDataDiskTag = diskType
249+ }
250+ if item .Volume != nil {
251+ volume := int (* item .Volume / 1024 / 1024 / 1024 )
252+ multiDisk ["volume" ] = volume
253+ outputDataDiskTag = fmt .Sprintf ("%s-%d" , outputDataDiskTag , volume )
254+ }
255+ var count int
256+ if item .Count != nil {
257+ count = int (* item .Count )
258+ if count > 0 && inputDataDiskTag == outputDataDiskTag {
259+ count -= 1
260+ }
261+ multiDisk ["count" ] = count
262+ }
263+
264+ if count != 0 {
265+ multiDisks = append (multiDisks , multiDisk )
266+ }
267+ }
268+ return
269+ }
0 commit comments