Skip to content

Commit 8c0f241

Browse files
authored
add describe auto scale records (#2375)
* add describe auto scale records * add changelog * update test
1 parent ebc6636 commit 8c0f241

File tree

14 files changed

+706
-73
lines changed

14 files changed

+706
-73
lines changed

.changelog/2375.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_auto_scale_records
3+
```

go.mod

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ require (
4646
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb v1.0.693
4747
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cloudaudit v1.0.544
4848
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cls v1.0.711
49-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.810
49+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.811
5050
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.624
5151
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cwp v1.0.762
5252
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb v1.0.692
@@ -59,7 +59,7 @@ require (
5959
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/domain v1.0.414
6060
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dts v1.0.628
6161
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/eb v1.0.760
62-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/emr v1.0.762
62+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/emr v1.0.811
6363
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/es v1.0.777
6464
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/gaap v1.0.771
6565
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms v1.0.563
@@ -349,11 +349,9 @@ require (
349349
gopkg.in/yaml.v3 v3.0.1 // indirect
350350
honnef.co/go/tools v0.4.3 // indirect
351351
k8s.io/klog/v2 v2.100.1 // indirect
352-
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect
353352
mvdan.cc/gofumpt v0.4.0 // indirect
354353
mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed // indirect
355354
mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b // indirect
356355
mvdan.cc/unparam v0.0.0-20221223090309-7455f1af531d // indirect
357-
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
358356
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
359357
)

go.sum

Lines changed: 16 additions & 28 deletions
Large diffs are not rendered by default.
Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
/*
2+
Use this data source to query detailed information of emr auto_scale_records
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_emr_auto_scale_records" "auto_scale_records" {
8+
instance_id = "emr-bpum4pad"
9+
filters {
10+
key = "StartTime"
11+
value = "2006-01-02 15:04:05"
12+
}
13+
}
14+
```
15+
*/
16+
package tencentcloud
17+
18+
import (
19+
"context"
20+
21+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
22+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
23+
emr "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/emr/v20190103"
24+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
25+
)
26+
27+
func dataSourceTencentCloudEmrAutoScaleRecords() *schema.Resource {
28+
return &schema.Resource{
29+
Read: dataSourceTencentCloudEmrAutoScaleRecordsRead,
30+
Schema: map[string]*schema.Schema{
31+
"instance_id": {
32+
Required: true,
33+
Type: schema.TypeString,
34+
Description: "EMR cluster ID.",
35+
},
36+
37+
"filters": {
38+
Optional: true,
39+
Type: schema.TypeList,
40+
Description: "Record filtering parameters, currently only `StartTime`, `EndTime` and `StrategyName` are supported. `StartTime` and `EndTime` support the time format of 2006-01-02 15:04:05 or 2006/01/02 15:04:05.",
41+
Elem: &schema.Resource{
42+
Schema: map[string]*schema.Schema{
43+
"key": {
44+
Type: schema.TypeString,
45+
Required: true,
46+
Description: "Key. Note: This field may return null, indicating that no valid value can be obtained.",
47+
},
48+
"value": {
49+
Type: schema.TypeString,
50+
Required: true,
51+
Description: "Value. Note: This field may return null, indicating that no valid value can be obtained.",
52+
},
53+
},
54+
},
55+
},
56+
57+
"record_list": {
58+
Computed: true,
59+
Type: schema.TypeList,
60+
Description: "Record list.",
61+
Elem: &schema.Resource{
62+
Schema: map[string]*schema.Schema{
63+
"strategy_name": {
64+
Type: schema.TypeString,
65+
Computed: true,
66+
Description: "Rule name of expanding and shrinking capacity.",
67+
},
68+
"scale_action": {
69+
Type: schema.TypeString,
70+
Computed: true,
71+
Description: "`SCALE_OUT` and `SCALE_IN` respectively represent expanding and shrinking capacity.",
72+
},
73+
"action_status": {
74+
Type: schema.TypeString,
75+
Computed: true,
76+
Description: "`SUCCESS`, `FAILED`, `PART_SUCCESS`, `IN_PROCESS`.",
77+
},
78+
"action_time": {
79+
Type: schema.TypeString,
80+
Computed: true,
81+
Description: "Process Trigger Time.",
82+
},
83+
"scale_info": {
84+
Type: schema.TypeString,
85+
Computed: true,
86+
Description: "Scalability-related Description.",
87+
},
88+
"expect_scale_num": {
89+
Type: schema.TypeInt,
90+
Computed: true,
91+
Description: "Effective only when ScaleAction is SCALE_OUT.",
92+
},
93+
"end_time": {
94+
Type: schema.TypeString,
95+
Computed: true,
96+
Description: "Process End Time.",
97+
},
98+
"strategy_type": {
99+
Type: schema.TypeInt,
100+
Computed: true,
101+
Description: "Strategy Type, 1 for Load scaling, 2 for Time scaling.",
102+
},
103+
"spec_info": {
104+
Type: schema.TypeString,
105+
Computed: true,
106+
Description: "Specification information used when expanding capacity.",
107+
},
108+
"compensate_flag": {
109+
Type: schema.TypeInt,
110+
Computed: true,
111+
Description: "Compensation and expansion, 0 represents no start, 1 represents start. Note: This field may return null, indicating that no valid value can be obtained.",
112+
},
113+
"compensate_count": {
114+
Type: schema.TypeInt,
115+
Computed: true,
116+
Description: "Compensation Times Note: This field may return null, indicating that no valid value can be obtained.",
117+
},
118+
},
119+
},
120+
},
121+
122+
"result_output_file": {
123+
Type: schema.TypeString,
124+
Optional: true,
125+
Description: "Used to save results.",
126+
},
127+
},
128+
}
129+
}
130+
131+
func dataSourceTencentCloudEmrAutoScaleRecordsRead(d *schema.ResourceData, meta interface{}) error {
132+
defer logElapsed("data_source.tencentcloud_emr_auto_scale_records.read")()
133+
defer inconsistentCheck(d, meta)()
134+
135+
logId := getLogId(contextNil)
136+
137+
var instanceId string
138+
139+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
140+
141+
paramMap := make(map[string]interface{})
142+
if v, ok := d.GetOk("instance_id"); ok {
143+
instanceId = v.(string)
144+
paramMap["InstanceId"] = helper.String(v.(string))
145+
}
146+
147+
if v, ok := d.GetOk("filters"); ok {
148+
filtersSet := v.([]interface{})
149+
tmpSet := make([]*emr.KeyValue, 0, len(filtersSet))
150+
151+
for _, item := range filtersSet {
152+
keyValue := emr.KeyValue{}
153+
keyValueMap := item.(map[string]interface{})
154+
155+
if v, ok := keyValueMap["key"]; ok {
156+
keyValue.Key = helper.String(v.(string))
157+
}
158+
if v, ok := keyValueMap["value"]; ok {
159+
keyValue.Value = helper.String(v.(string))
160+
}
161+
tmpSet = append(tmpSet, &keyValue)
162+
}
163+
paramMap["Filters"] = tmpSet
164+
}
165+
166+
service := EMRService{client: meta.(*TencentCloudClient).apiV3Conn}
167+
168+
var recordList []*emr.AutoScaleRecord
169+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
170+
result, e := service.DescribeEmrAutoScaleRecordsByFilter(ctx, paramMap)
171+
if e != nil {
172+
return retryError(e)
173+
}
174+
recordList = result
175+
return nil
176+
})
177+
if err != nil {
178+
return err
179+
}
180+
181+
tmpList := make([]map[string]interface{}, 0, len(recordList))
182+
183+
if recordList != nil {
184+
for _, autoScaleRecord := range recordList {
185+
autoScaleRecordMap := map[string]interface{}{}
186+
187+
if autoScaleRecord.StrategyName != nil {
188+
autoScaleRecordMap["strategy_name"] = autoScaleRecord.StrategyName
189+
}
190+
191+
if autoScaleRecord.ScaleAction != nil {
192+
autoScaleRecordMap["scale_action"] = autoScaleRecord.ScaleAction
193+
}
194+
195+
if autoScaleRecord.ActionStatus != nil {
196+
autoScaleRecordMap["action_status"] = autoScaleRecord.ActionStatus
197+
}
198+
199+
if autoScaleRecord.ActionTime != nil {
200+
autoScaleRecordMap["action_time"] = autoScaleRecord.ActionTime
201+
}
202+
203+
if autoScaleRecord.ScaleInfo != nil {
204+
autoScaleRecordMap["scale_info"] = autoScaleRecord.ScaleInfo
205+
}
206+
207+
if autoScaleRecord.ExpectScaleNum != nil {
208+
autoScaleRecordMap["expect_scale_num"] = autoScaleRecord.ExpectScaleNum
209+
}
210+
211+
if autoScaleRecord.EndTime != nil {
212+
autoScaleRecordMap["end_time"] = autoScaleRecord.EndTime
213+
}
214+
215+
if autoScaleRecord.StrategyType != nil {
216+
autoScaleRecordMap["strategy_type"] = autoScaleRecord.StrategyType
217+
}
218+
219+
if autoScaleRecord.SpecInfo != nil {
220+
autoScaleRecordMap["spec_info"] = autoScaleRecord.SpecInfo
221+
}
222+
223+
if autoScaleRecord.CompensateFlag != nil {
224+
autoScaleRecordMap["compensate_flag"] = autoScaleRecord.CompensateFlag
225+
}
226+
227+
if autoScaleRecord.CompensateCount != nil {
228+
autoScaleRecordMap["compensate_count"] = autoScaleRecord.CompensateCount
229+
}
230+
tmpList = append(tmpList, autoScaleRecordMap)
231+
}
232+
233+
_ = d.Set("record_list", tmpList)
234+
}
235+
236+
d.SetId(instanceId)
237+
output, ok := d.GetOk("result_output_file")
238+
if ok && output.(string) != "" {
239+
if e := writeToFile(output.(string), tmpList); e != nil {
240+
return e
241+
}
242+
}
243+
return nil
244+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudEmrAutoScaleRecordsDataSource_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: testAccEmrAutoScaleRecordsDataSource,
19+
Check: resource.ComposeTestCheckFunc(testAccCheckTencentCloudDataSourceID("data.tencentcloud_emr_auto_scale_records.auto_scale_records")),
20+
},
21+
},
22+
})
23+
}
24+
25+
const testAccEmrAutoScaleRecordsDataSource = `
26+
27+
data "tencentcloud_emr_auto_scale_records" "auto_scale_records" {
28+
instance_id = "emr-8j38bip0"
29+
filters {
30+
key = "StartTime"
31+
value = "2006-01-02 15:04:05"
32+
}
33+
}
34+
35+
`

tencentcloud/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,6 +1261,7 @@ MapReduce(EMR)
12611261
tencentcloud_emr
12621262
tencentcloud_emr_nodes
12631263
tencentcloud_emr_cvm_quota
1264+
tencentcloud_emr_auto_scale_records
12641265
12651266
Resource
12661267
tencentcloud_emr_cluster
@@ -2237,6 +2238,7 @@ func Provider() *schema.Provider {
22372238
"tencentcloud_emr": dataSourceTencentCloudEmr(),
22382239
"tencentcloud_emr_nodes": dataSourceTencentCloudEmrNodes(),
22392240
"tencentcloud_emr_cvm_quota": dataSourceTencentCloudEmrCvmQuota(),
2241+
"tencentcloud_emr_auto_scale_records": dataSourceTencentCloudEmrAutoScaleRecords(),
22402242
"tencentcloud_availability_zones": dataSourceTencentCloudAvailabilityZones(),
22412243
"tencentcloud_availability_zones_by_product": dataSourceTencentCloudAvailabilityZonesByProduct(),
22422244
"tencentcloud_projects": dataSourceTencentCloudProjects(),

tencentcloud/service_tencentcloud_emr.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,3 +437,54 @@ func (me *EMRService) DescribeEmrCvmQuotaByFilter(ctx context.Context, param map
437437
cvmQuota = response.Response
438438
return
439439
}
440+
441+
func (me *EMRService) DescribeEmrAutoScaleRecordsByFilter(ctx context.Context, param map[string]interface{}) (autoScaleRecords []*emr.AutoScaleRecord, errRet error) {
442+
var (
443+
logId = getLogId(ctx)
444+
request = emr.NewDescribeAutoScaleRecordsRequest()
445+
)
446+
447+
defer func() {
448+
if errRet != nil {
449+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error())
450+
}
451+
}()
452+
453+
for k, v := range param {
454+
if k == "InstanceId" {
455+
request.InstanceId = v.(*string)
456+
}
457+
if k == "Filters" {
458+
request.Filters = v.([]*emr.KeyValue)
459+
}
460+
}
461+
462+
ratelimit.Check(request.GetAction())
463+
464+
var (
465+
offset int64 = 0
466+
limit int64 = 20
467+
)
468+
for {
469+
request.Offset = &offset
470+
request.Limit = &limit
471+
response, err := me.client.UseEmrClient().DescribeAutoScaleRecords(request)
472+
if err != nil {
473+
errRet = err
474+
return
475+
}
476+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
477+
478+
if response == nil || len(response.Response.RecordList) < 1 {
479+
break
480+
}
481+
autoScaleRecords = append(autoScaleRecords, response.Response.RecordList...)
482+
if len(response.Response.RecordList) < int(limit) {
483+
break
484+
}
485+
486+
offset += limit
487+
}
488+
489+
return
490+
}

vendor/github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/http/request.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)