1
1
package emr
2
2
3
3
import (
4
+ "fmt"
5
+
4
6
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
5
7
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
6
8
emr "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/emr/v20190103"
9
+ "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
7
10
)
8
11
9
12
const (
@@ -35,38 +38,122 @@ func buildResourceSpecSchema() *schema.Schema {
35
38
MaxItems : 1 ,
36
39
Elem : & schema.Resource {
37
40
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
+ },
45
131
},
46
132
},
47
133
}
48
134
}
49
135
50
- func ParseMultiDisks (_multiDisks []map [string ]interface {}) []* emr.MultiDisk {
51
- multiDisks := make ([]* emr.MultiDisk , len (_multiDisks ))
52
- for _ , item := range _multiDisks {
136
+ func ParseMultiDisks (_multiDisks []interface {}) []* emr.MultiDisk {
137
+ multiDisks := make ([]* emr.MultiDisk , 0 , len (_multiDisks ))
138
+ for _ , multiDisk := range _multiDisks {
139
+ item := multiDisk .(map [string ]interface {})
53
140
var diskType string
54
- var volume int64
55
- var count int64
141
+ var volume int
142
+ var count int
56
143
for subK , subV := range item {
57
144
if subK == "disk_type" {
58
145
diskType = subV .(string )
59
146
} else if subK == "volume" {
60
- volume = subV .(int64 )
147
+ volume = subV .(int )
61
148
} else if subK == "count" {
62
- count = subV .(int64 )
149
+ count = subV .(int )
63
150
}
64
151
}
65
152
multiDisks = append (multiDisks ,
66
153
& emr.MultiDisk {
67
- DiskType : common . StringPtr (diskType ),
68
- Volume : common . Int64Ptr (volume ),
69
- Count : common . Int64Ptr (count ),
154
+ DiskType : helper . String (diskType ),
155
+ Volume : helper . IntInt64 (volume ),
156
+ Count : helper . IntInt64 (count ),
70
157
})
71
158
}
72
159
@@ -101,7 +188,7 @@ func ParseResource(_resource map[string]interface{}) *emr.Resource {
101
188
} else if k == "root_size" {
102
189
resultResource .RootSize = common .Int64Ptr ((int64 )(v .(int )))
103
190
} else if k == "multi_disks" {
104
- multiDisks := v .([] map [ string ] interface {} )
191
+ multiDisks := v .(* schema. Set ). List ( )
105
192
resultResource .MultiDisks = ParseMultiDisks (multiDisks )
106
193
} else if k == "tags" {
107
194
tags := v .([]map [string ]string )
@@ -116,3 +203,66 @@ func ParseResource(_resource map[string]interface{}) *emr.Resource {
116
203
}
117
204
return resultResource
118
205
}
206
+
207
+ func validateMultiDisks (r map [string ]interface {}) error {
208
+ if _ , ok := r ["multi_disks" ]; ! ok {
209
+ return nil
210
+ }
211
+ multiDiskList := r ["multi_disks" ].(* schema.Set ).List ()
212
+ visited := make (map [string ]struct {})
213
+
214
+ for _ , multiDisk := range multiDiskList {
215
+ multiDiskMap := multiDisk .(map [string ]interface {})
216
+ key := fmt .Sprintf ("%s-%d" , multiDiskMap ["disk_type" ].(string ), multiDiskMap ["volume" ].(int ))
217
+ if _ , ok := visited [key ]; ok {
218
+ return fmt .Errorf ("Merge disks of the same specifications" )
219
+ } else {
220
+ visited [key ] = struct {}{}
221
+ }
222
+ }
223
+
224
+ return nil
225
+ }
226
+
227
+ func fetchMultiDisks (v * emr.NodeHardwareInfo , r * emr.OutterResource ) (multiDisks []interface {}) {
228
+ var inputDataDiskTag string
229
+ if r .DiskType != nil && r .DiskSize != nil {
230
+ inputDataDiskTag = fmt .Sprintf ("%s-%d" , * r .DiskType , * r .DiskSize )
231
+ }
232
+ for _ , item := range v .MCMultiDisk {
233
+ outputDataDiskTag := ""
234
+ multiDisk := make (map [string ]interface {})
235
+ if item .Type != nil {
236
+ var diskType string
237
+ if * item .Type == 4 {
238
+ diskType = "CLOUD_SSD"
239
+ }
240
+ if * item .Type == 5 {
241
+ diskType = "CLOUD_PREMIUM"
242
+ }
243
+ if * item .Type == 6 {
244
+ diskType = "CLOUD_HSSD"
245
+ }
246
+ multiDisk ["disk_type" ] = diskType
247
+ outputDataDiskTag = diskType
248
+ }
249
+ if item .Volume != nil {
250
+ volume := int (* item .Volume / 1024 / 1024 / 1024 )
251
+ multiDisk ["volume" ] = volume
252
+ outputDataDiskTag = fmt .Sprintf ("%s-%d" , outputDataDiskTag , volume )
253
+ }
254
+ var count int
255
+ if item .Count != nil {
256
+ count = int (* item .Count )
257
+ if count > 0 && inputDataDiskTag == outputDataDiskTag {
258
+ count -= 1
259
+ }
260
+ multiDisk ["count" ] = count
261
+ }
262
+
263
+ if count != 0 {
264
+ multiDisks = append (multiDisks , multiDisk )
265
+ }
266
+ }
267
+ return
268
+ }
0 commit comments