|
| 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 | +} |
0 commit comments