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,123 @@ 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
},
133
+ Description : "Resource details." ,
47
134
}
48
135
}
49
136
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 {})
53
141
var diskType string
54
- var volume int64
55
- var count int64
142
+ var volume int
143
+ var count int
56
144
for subK , subV := range item {
57
145
if subK == "disk_type" {
58
146
diskType = subV .(string )
59
147
} else if subK == "volume" {
60
- volume = subV .(int64 )
148
+ volume = subV .(int )
61
149
} else if subK == "count" {
62
- count = subV .(int64 )
150
+ count = subV .(int )
63
151
}
64
152
}
65
153
multiDisks = append (multiDisks ,
66
154
& 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 ),
70
158
})
71
159
}
72
160
@@ -101,7 +189,7 @@ func ParseResource(_resource map[string]interface{}) *emr.Resource {
101
189
} else if k == "root_size" {
102
190
resultResource .RootSize = common .Int64Ptr ((int64 )(v .(int )))
103
191
} else if k == "multi_disks" {
104
- multiDisks := v .([] map [ string ] interface {} )
192
+ multiDisks := v .(* schema. Set ). List ( )
105
193
resultResource .MultiDisks = ParseMultiDisks (multiDisks )
106
194
} else if k == "tags" {
107
195
tags := v .([]map [string ]string )
@@ -116,3 +204,66 @@ func ParseResource(_resource map[string]interface{}) *emr.Resource {
116
204
}
117
205
return resultResource
118
206
}
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