Skip to content

Commit c11aac7

Browse files
authored
add emr cvm quota (#2311)
* add emr cvm quota * add changelog
1 parent ea1617e commit c11aac7

File tree

7 files changed

+392
-0
lines changed

7 files changed

+392
-0
lines changed

.changelog/2311.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-data-source
2+
tencentcloud_emr_cvm_quota
3+
```
Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
/*
2+
Use this data source to query detailed information of emr cvm_quota
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_emr_cvm_quota" "cvm_quota" {
8+
cluster_id = "emr-0ze36vnp"
9+
zone_id = 100003
10+
}
11+
```
12+
*/
13+
package tencentcloud
14+
15+
import (
16+
"context"
17+
18+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
19+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
20+
emr "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/emr/v20190103"
21+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
22+
)
23+
24+
func dataSourceTencentCloudEmrCvmQuota() *schema.Resource {
25+
return &schema.Resource{
26+
Read: dataSourceTencentCloudEmrCvmQuotaRead,
27+
Schema: map[string]*schema.Schema{
28+
"cluster_id": {
29+
Required: true,
30+
Type: schema.TypeString,
31+
Description: "EMR cluster ID.",
32+
},
33+
34+
"zone_id": {
35+
Optional: true,
36+
Type: schema.TypeInt,
37+
Description: "Zone ID.",
38+
},
39+
40+
"post_paid_quota_set": {
41+
Computed: true,
42+
Type: schema.TypeList,
43+
Description: "Postpaid quota list Note: This field may return null, indicating that no valid value can be obtained.",
44+
Elem: &schema.Resource{
45+
Schema: map[string]*schema.Schema{
46+
"used_quota": {
47+
Type: schema.TypeInt,
48+
Computed: true,
49+
Description: "Used quota Note: This field may return null, indicating that a valid value cannot be obtained.",
50+
},
51+
"remaining_quota": {
52+
Type: schema.TypeInt,
53+
Computed: true,
54+
Description: "Residual quota Note: This field may return null, indicating that a valid value cannot be obtained.",
55+
},
56+
"total_quota": {
57+
Type: schema.TypeInt,
58+
Computed: true,
59+
Description: "Total quota Note: This field may return null, indicating that a valid value cannot be obtained.",
60+
},
61+
"zone": {
62+
Type: schema.TypeString,
63+
Computed: true,
64+
Description: "Available area Note: This field may return null, indicating that a valid value cannot be obtained.",
65+
},
66+
},
67+
},
68+
},
69+
70+
"spot_paid_quota_set": {
71+
Computed: true,
72+
Type: schema.TypeList,
73+
Description: "Biding instance quota list Note: This field may return null, indicating that a valid value cannot be obtained.",
74+
Elem: &schema.Resource{
75+
Schema: map[string]*schema.Schema{
76+
"used_quota": {
77+
Type: schema.TypeInt,
78+
Computed: true,
79+
Description: "Used quota Note: This field may return null, indicating that a valid value cannot be obtained.",
80+
},
81+
"remaining_quota": {
82+
Type: schema.TypeInt,
83+
Computed: true,
84+
Description: "Residual quota Note: This field may return null, indicating that a valid value cannot be obtained.",
85+
},
86+
"total_quota": {
87+
Type: schema.TypeInt,
88+
Computed: true,
89+
Description: "Total quota Note: This field may return null, indicating that a valid value cannot be obtained.",
90+
},
91+
"zone": {
92+
Type: schema.TypeString,
93+
Computed: true,
94+
Description: "Available area Note: This field may return null, indicating that a valid value cannot be obtained.",
95+
},
96+
},
97+
},
98+
},
99+
100+
"eks_quota_set": {
101+
Computed: true,
102+
Type: schema.TypeList,
103+
Description: "Eks quota Note: This field may return null, indicating that a valid value cannot be obtained.",
104+
Elem: &schema.Resource{
105+
Schema: map[string]*schema.Schema{
106+
"node_type": {
107+
Type: schema.TypeString,
108+
Computed: true,
109+
Description: "The specifications of the marketable resource are as follows: `TASK`, `CORE`, `MASTER`, `ROUTER`.",
110+
},
111+
"cpu": {
112+
Type: schema.TypeInt,
113+
Computed: true,
114+
Description: "Cpu cores.",
115+
},
116+
"memory": {
117+
Type: schema.TypeInt,
118+
Computed: true,
119+
Description: "Memory quantity (unit: GB).",
120+
},
121+
"number": {
122+
Type: schema.TypeInt,
123+
Computed: true,
124+
Description: "Specifies the maximum number of resources that can be applied for.",
125+
},
126+
},
127+
},
128+
},
129+
130+
"result_output_file": {
131+
Type: schema.TypeString,
132+
Optional: true,
133+
Description: "Used to save results.",
134+
},
135+
},
136+
}
137+
}
138+
139+
func dataSourceTencentCloudEmrCvmQuotaRead(d *schema.ResourceData, meta interface{}) error {
140+
defer logElapsed("data_source.tencentcloud_emr_cvm_quota.read")()
141+
defer inconsistentCheck(d, meta)()
142+
143+
logId := getLogId(contextNil)
144+
145+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
146+
147+
var clusterId string
148+
149+
paramMap := make(map[string]interface{})
150+
if v, ok := d.GetOk("cluster_id"); ok {
151+
clusterId = v.(string)
152+
paramMap["ClusterId"] = helper.String(v.(string))
153+
}
154+
155+
if v, ok := d.GetOkExists("zone_id"); ok {
156+
paramMap["ZoneId"] = helper.IntInt64(v.(int))
157+
}
158+
159+
service := EMRService{client: meta.(*TencentCloudClient).apiV3Conn}
160+
161+
var cvmQuota *emr.DescribeCvmQuotaResponseParams
162+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
163+
result, e := service.DescribeEmrCvmQuotaByFilter(ctx, paramMap)
164+
if e != nil {
165+
return retryError(e)
166+
}
167+
cvmQuota = result
168+
return nil
169+
})
170+
if err != nil {
171+
return err
172+
}
173+
174+
//ids := make([]string, 0, len(postPaidQuotaSet))
175+
tmpList := make([]map[string]interface{}, 0)
176+
177+
if cvmQuota.PostPaidQuotaSet != nil {
178+
tmpList := make([]map[string]interface{}, 0, len(cvmQuota.PostPaidQuotaSet))
179+
180+
for _, quotaEntity := range cvmQuota.PostPaidQuotaSet {
181+
quotaEntityMap := map[string]interface{}{}
182+
183+
if quotaEntity.UsedQuota != nil {
184+
quotaEntityMap["used_quota"] = quotaEntity.UsedQuota
185+
}
186+
187+
if quotaEntity.RemainingQuota != nil {
188+
quotaEntityMap["remaining_quota"] = quotaEntity.RemainingQuota
189+
}
190+
191+
if quotaEntity.TotalQuota != nil {
192+
quotaEntityMap["total_quota"] = quotaEntity.TotalQuota
193+
}
194+
195+
if quotaEntity.Zone != nil {
196+
quotaEntityMap["zone"] = quotaEntity.Zone
197+
}
198+
199+
tmpList = append(tmpList, quotaEntityMap)
200+
}
201+
_ = d.Set("post_paid_quota_set", tmpList)
202+
}
203+
204+
if cvmQuota.SpotPaidQuotaSet != nil {
205+
tmpList := make([]map[string]interface{}, 0, len(cvmQuota.SpotPaidQuotaSet))
206+
207+
for _, quotaEntity := range cvmQuota.SpotPaidQuotaSet {
208+
quotaEntityMap := map[string]interface{}{}
209+
210+
if quotaEntity.UsedQuota != nil {
211+
quotaEntityMap["used_quota"] = quotaEntity.UsedQuota
212+
}
213+
214+
if quotaEntity.RemainingQuota != nil {
215+
quotaEntityMap["remaining_quota"] = quotaEntity.RemainingQuota
216+
}
217+
218+
if quotaEntity.TotalQuota != nil {
219+
quotaEntityMap["total_quota"] = quotaEntity.TotalQuota
220+
}
221+
222+
if quotaEntity.Zone != nil {
223+
quotaEntityMap["zone"] = quotaEntity.Zone
224+
}
225+
tmpList = append(tmpList, quotaEntityMap)
226+
}
227+
228+
_ = d.Set("spot_paid_quota_set", tmpList)
229+
}
230+
231+
if cvmQuota.EksQuotaSet != nil {
232+
tmpList := make([]map[string]interface{}, 0, len(cvmQuota.EksQuotaSet))
233+
234+
for _, podSaleSpec := range cvmQuota.EksQuotaSet {
235+
podSaleSpecMap := map[string]interface{}{}
236+
237+
if podSaleSpec.NodeType != nil {
238+
podSaleSpecMap["node_type"] = podSaleSpec.NodeType
239+
}
240+
241+
if podSaleSpec.Cpu != nil {
242+
podSaleSpecMap["cpu"] = podSaleSpec.Cpu
243+
}
244+
245+
if podSaleSpec.Memory != nil {
246+
podSaleSpecMap["memory"] = podSaleSpec.Memory
247+
}
248+
249+
if podSaleSpec.Number != nil {
250+
podSaleSpecMap["number"] = podSaleSpec.Number
251+
}
252+
253+
tmpList = append(tmpList, podSaleSpecMap)
254+
}
255+
256+
_ = d.Set("eks_quota_set", tmpList)
257+
}
258+
259+
d.SetId(clusterId)
260+
output, ok := d.GetOk("result_output_file")
261+
if ok && output.(string) != "" {
262+
if e := writeToFile(output.(string), tmpList); e != nil {
263+
return e
264+
}
265+
}
266+
return nil
267+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudEmrCvmQuotaDataSource_basic(t *testing.T) {
10+
t.Parallel()
11+
resource.Test(t, resource.TestCase{
12+
PreCheck: func() {
13+
testAccPreCheck(t)
14+
},
15+
Providers: testAccProviders,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testAccEmrCvmQuotaDataSource,
19+
Check: resource.ComposeTestCheckFunc(testAccCheckTencentCloudDataSourceID("data.tencentcloud_emr_cvm_quota.cvm_quota")),
20+
},
21+
},
22+
})
23+
}
24+
25+
const testAccEmrCvmQuotaDataSource = `
26+
27+
data "tencentcloud_emr_cvm_quota" "cvm_quota" {
28+
cluster_id = "emr-gmz8tdmv"
29+
zone_id = 800007
30+
}
31+
32+
`

tencentcloud/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,7 @@ MapReduce(EMR)
12331233
Data Source
12341234
tencentcloud_emr
12351235
tencentcloud_emr_nodes
1236+
tencentcloud_emr_cvm_quota
12361237
12371238
Resource
12381239
tencentcloud_emr_cluster
@@ -2169,6 +2170,7 @@ func Provider() *schema.Provider {
21692170
"tencentcloud_availability_regions": dataSourceTencentCloudAvailabilityRegions(),
21702171
"tencentcloud_emr": dataSourceTencentCloudEmr(),
21712172
"tencentcloud_emr_nodes": dataSourceTencentCloudEmrNodes(),
2173+
"tencentcloud_emr_cvm_quota": dataSourceTencentCloudEmrCvmQuota(),
21722174
"tencentcloud_availability_zones": dataSourceTencentCloudAvailabilityZones(),
21732175
"tencentcloud_availability_zones_by_product": dataSourceTencentCloudAvailabilityZonesByProduct(),
21742176
"tencentcloud_projects": dataSourceTencentCloudProjects(),

tencentcloud/service_tencentcloud_emr.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,3 +403,37 @@ func (me *EMRService) DeleteEmrUserManagerById(ctx context.Context, instanceId s
403403

404404
return
405405
}
406+
407+
func (me *EMRService) DescribeEmrCvmQuotaByFilter(ctx context.Context, param map[string]interface{}) (cvmQuota *emr.DescribeCvmQuotaResponseParams, errRet error) {
408+
var (
409+
logId = getLogId(ctx)
410+
request = emr.NewDescribeCvmQuotaRequest()
411+
)
412+
413+
defer func() {
414+
if errRet != nil {
415+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error())
416+
}
417+
}()
418+
419+
for k, v := range param {
420+
if k == "ClusterId" {
421+
request.ClusterId = v.(*string)
422+
}
423+
if k == "ZoneId" {
424+
request.ZoneId = v.(*int64)
425+
}
426+
}
427+
428+
ratelimit.Check(request.GetAction())
429+
430+
response, err := me.client.UseEmrClient().DescribeCvmQuota(request)
431+
if err != nil {
432+
errRet = err
433+
return
434+
}
435+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
436+
437+
cvmQuota = response.Response
438+
return
439+
}

0 commit comments

Comments
 (0)