@@ -9,6 +9,20 @@ data "tencentcloud_instance_types" "foo" {
9
9
cpu_core_count = 2
10
10
memory_size = 4
11
11
}
12
+
13
+ data tencentcloud_instance_types "t1c1g" {
14
+ cpu_core_count = 1
15
+ memory_size = 1
16
+ exclude_sold_out=true
17
+ filter {
18
+ name = "instance-charge-type"
19
+ values = ["POSTPAID_BY_HOUR"]
20
+ }
21
+ filter {
22
+ name = "zone"
23
+ values = ["ap-shanghai-2"]
24
+ }
25
+ }
12
26
```
13
27
*/
14
28
package tencentcloud
@@ -60,7 +74,7 @@ func dataSourceInstanceTypes() *schema.Resource {
60
74
"name" : {
61
75
Type : schema .TypeString ,
62
76
Required : true ,
63
- Description : "The filter name. Valid values: `zone` and `instance-family `." ,
77
+ Description : "The filter name. Valid values: `zone`, `instance-family` and `instance-charge-type `." ,
64
78
},
65
79
"values" : {
66
80
Type : schema .TypeList ,
@@ -149,131 +163,71 @@ func dataSourceTencentCloudInstanceTypesRead(d *schema.ResourceData, meta interf
149
163
cpu , cpuOk := d .GetOk ("cpu_core_count" )
150
164
gpu , gpuOk := d .GetOk ("gpu_core_count" )
151
165
memory , memoryOk := d .GetOk ("memory_size" )
152
- var instanceTypes []* cvm.InstanceTypeConfig
153
166
var instanceSellTypes []* cvm.InstanceTypeQuotaItem
154
167
var errRet error
155
168
var err error
156
169
typeList := make ([]map [string ]interface {}, 0 )
157
170
ids := make ([]string , 0 )
158
171
159
- if ! isExcludeSoldOut {
160
- if v , ok := d .GetOk ("availability_zone" ); ok {
161
- zone := v .(string )
162
- err = resource .Retry (readRetryTimeout , func () * resource.RetryError {
163
- instanceTypes , errRet = cvmService .DescribeInstanceTypes (ctx , zone )
164
- if errRet != nil {
165
- return retryError (errRet , InternalError )
166
- }
167
- return nil
168
- })
169
- } else {
170
- filters := d .Get ("filter" ).(* schema.Set ).List ()
171
- filterMap := make (map [string ][]string , len (filters ))
172
- for _ , v := range filters {
173
- item := v .(map [string ]interface {})
174
- name := item ["name" ].(string )
175
- values := item ["values" ].([]interface {})
176
- filterValues := make ([]string , 0 , len (values ))
177
- for _ , value := range values {
178
- filterValues = append (filterValues , value .(string ))
179
- }
180
- filterMap [name ] = filterValues
181
- }
182
- err = resource .Retry (readRetryTimeout , func () * resource.RetryError {
183
- instanceTypes , errRet = cvmService .DescribeInstanceTypesByFilter (ctx , filterMap )
184
- if errRet != nil {
185
- return retryError (errRet , InternalError )
186
- }
187
- return nil
188
- })
189
- }
190
- if err != nil {
191
- return err
172
+ var zone string
173
+ var zone_in = 0
174
+ if v , ok := d .GetOk ("availability_zone" ); ok {
175
+ zone = v .(string )
176
+ zone_in = 1
177
+ }
178
+ filters := d .Get ("filter" ).(* schema.Set ).List ()
179
+ filterMap := make (map [string ][]string , len (filters )+ zone_in )
180
+ for _ , v := range filters {
181
+ item := v .(map [string ]interface {})
182
+ name := item ["name" ].(string )
183
+ values := item ["values" ].([]interface {})
184
+ filterValues := make ([]string , 0 , len (values ))
185
+ for _ , value := range values {
186
+ filterValues = append (filterValues , value .(string ))
192
187
}
193
- for _ , instanceType := range instanceTypes {
194
- flag := true
195
- if cpuOk && int64 (cpu .(int )) != * instanceType .CPU {
196
- flag = false
197
- }
198
- if gpuOk && int64 (gpu .(int )) != * instanceType .GPU {
199
- flag = false
200
- }
201
- if memoryOk && int64 (memory .(int )) != * instanceType .Memory {
202
- flag = false
203
- }
204
-
205
- if flag {
206
- mapping := map [string ]interface {}{
207
- "availability_zone" : instanceType .Zone ,
208
- "cpu_core_count" : instanceType .CPU ,
209
- "gpu_core_count" : instanceType .GPU ,
210
- "memory_size" : instanceType .Memory ,
211
- "family" : instanceType .InstanceFamily ,
212
- "instance_type" : instanceType .InstanceType ,
213
- }
214
- typeList = append (typeList , mapping )
215
- ids = append (ids , * instanceType .InstanceType )
216
- }
188
+ filterMap [name ] = filterValues
189
+ }
190
+ if zone != "" {
191
+ filterMap ["zone" ] = []string {zone }
192
+ }
193
+ err = resource .Retry (readRetryTimeout , func () * resource.RetryError {
194
+ instanceSellTypes , errRet = cvmService .DescribeInstancesSellTypeByFilter (ctx , filterMap )
195
+ if errRet != nil {
196
+ return retryError (errRet , InternalError )
217
197
}
218
- } else {
219
- //exclude sold out
220
- var zone string
221
- var zone_in = 0
222
- if v , ok := d .GetOk ("availability_zone" ); ok {
223
- zone = v .(string )
224
- zone_in = 1
198
+ return nil
199
+ })
200
+ if err != nil {
201
+ return err
202
+ }
203
+ for _ , instanceType := range instanceSellTypes {
204
+ flag := true
205
+ if cpuOk && int64 (cpu .(int )) != * instanceType .Cpu {
206
+ flag = false
225
207
}
226
- filters := d .Get ("filter" ).(* schema.Set ).List ()
227
- filterMap := make (map [string ][]string , len (filters )+ zone_in )
228
- for _ , v := range filters {
229
- item := v .(map [string ]interface {})
230
- name := item ["name" ].(string )
231
- values := item ["values" ].([]interface {})
232
- filterValues := make ([]string , 0 , len (values ))
233
- for _ , value := range values {
234
- filterValues = append (filterValues , value .(string ))
235
- }
236
- filterMap [name ] = filterValues
208
+ if gpuOk && int64 (gpu .(int )) != * instanceType .Gpu {
209
+ flag = false
237
210
}
238
- if zone != "" {
239
- filterMap [ "zone" ] = [] string { zone }
211
+ if memoryOk && int64 ( memory .( int )) != * instanceType . Memory {
212
+ flag = false
240
213
}
241
- err = resource .Retry (readRetryTimeout , func () * resource.RetryError {
242
- instanceSellTypes , errRet = cvmService .DescribeInstancesSellTypeByFilter (ctx , filterMap )
243
- if errRet != nil {
244
- return retryError (errRet , InternalError )
245
- }
246
- return nil
247
- })
248
- if err != nil {
249
- return err
214
+ if isExcludeSoldOut && CVM_SOLD_OUT_STATUS == * instanceType .Status {
215
+ flag = false
250
216
}
251
- for _ , instanceType := range instanceSellTypes {
252
- flag := true
253
- if cpuOk && int64 (cpu .(int )) != * instanceType .Cpu {
254
- flag = false
255
- }
256
- if gpuOk && int64 (gpu .(int )) != * instanceType .Gpu {
257
- flag = false
258
- }
259
- if memoryOk && int64 (memory .(int )) != * instanceType .Memory {
260
- flag = false
261
- }
262
217
263
- if flag {
264
- mapping := map [string ]interface {}{
265
- "availability_zone" : instanceType .Zone ,
266
- "cpu_core_count" : instanceType .Cpu ,
267
- "gpu_core_count" : instanceType .Gpu ,
268
- "memory_size" : instanceType .Memory ,
269
- "family" : instanceType .InstanceFamily ,
270
- "instance_type" : instanceType .InstanceType ,
271
- "instance_charge_type" : instanceType .InstanceChargeType ,
272
- "status" : instanceType .Status ,
273
- }
274
- typeList = append (typeList , mapping )
275
- ids = append (ids , * instanceType .InstanceType )
218
+ if flag {
219
+ mapping := map [string ]interface {}{
220
+ "availability_zone" : instanceType .Zone ,
221
+ "cpu_core_count" : instanceType .Cpu ,
222
+ "gpu_core_count" : instanceType .Gpu ,
223
+ "memory_size" : instanceType .Memory ,
224
+ "family" : instanceType .InstanceFamily ,
225
+ "instance_type" : instanceType .InstanceType ,
226
+ "instance_charge_type" : instanceType .InstanceChargeType ,
227
+ "status" : instanceType .Status ,
276
228
}
229
+ typeList = append (typeList , mapping )
230
+ ids = append (ids , * instanceType .InstanceType )
277
231
}
278
232
}
279
233
0 commit comments