|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of mps tasks |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_mps_tasks" "tasks" { |
| 8 | + status = "FINISH" |
| 9 | + limit = 20 |
| 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 | + mps "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mps/v20190612" |
| 21 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 22 | +) |
| 23 | + |
| 24 | +func dataSourceTencentCloudMpsTasks() *schema.Resource { |
| 25 | + return &schema.Resource{ |
| 26 | + Read: dataSourceTencentCloudMpsTasksRead, |
| 27 | + Schema: map[string]*schema.Schema{ |
| 28 | + "status": { |
| 29 | + Required: true, |
| 30 | + Type: schema.TypeString, |
| 31 | + Description: "Filter condition: task status, optional values: WAITING, PROCESSING, FINISH.", |
| 32 | + }, |
| 33 | + |
| 34 | + "limit": { |
| 35 | + Optional: true, |
| 36 | + Type: schema.TypeInt, |
| 37 | + Description: "Return the number of records, default value: 10, maximum value: 100.", |
| 38 | + }, |
| 39 | + |
| 40 | + "scroll_token": { |
| 41 | + Optional: true, |
| 42 | + Computed: true, |
| 43 | + Type: schema.TypeString, |
| 44 | + Description: "Page turning flag, used when pulling in batches: when a single request cannot pull all the data, the interface will return a ScrollToken, and the next request will carry this Token, and it will be obtained from the next record.", |
| 45 | + }, |
| 46 | + |
| 47 | + "task_set": { |
| 48 | + Computed: true, |
| 49 | + Type: schema.TypeList, |
| 50 | + Description: "Task list.", |
| 51 | + Elem: &schema.Resource{ |
| 52 | + Schema: map[string]*schema.Schema{ |
| 53 | + "task_id": { |
| 54 | + Type: schema.TypeString, |
| 55 | + Computed: true, |
| 56 | + Description: "Task ID.", |
| 57 | + }, |
| 58 | + "task_type": { |
| 59 | + Type: schema.TypeString, |
| 60 | + Computed: true, |
| 61 | + Description: "Task type, including:WorkflowTask, EditMediaTask, LiveProcessTask.", |
| 62 | + }, |
| 63 | + "create_time": { |
| 64 | + Type: schema.TypeString, |
| 65 | + Computed: true, |
| 66 | + Description: "Creation time, in ISO date format. Refer to https://cloud.tencent.com/document/product/862/37710#52.", |
| 67 | + }, |
| 68 | + "begin_process_time": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Computed: true, |
| 71 | + Description: "Begin process time, in ISO date format. Refer to https://cloud.tencent.com/document/product/862/37710#52. If the task has not started yet, this field is: 0000-00-00T00:00:00Z.", |
| 72 | + }, |
| 73 | + "finish_time": { |
| 74 | + Type: schema.TypeString, |
| 75 | + Computed: true, |
| 76 | + Description: "Task finish time, in ISO date format. Refer to https://cloud.tencent.com/document/product/862/37710#52. If the task has not been completed, this field is: 0000-00-00T00:00:00Z.", |
| 77 | + }, |
| 78 | + "sub_task_types": { |
| 79 | + Type: schema.TypeSet, |
| 80 | + Elem: &schema.Schema{ |
| 81 | + Type: schema.TypeString, |
| 82 | + }, |
| 83 | + Computed: true, |
| 84 | + Description: "Sub task types.", |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + |
| 90 | + "result_output_file": { |
| 91 | + Type: schema.TypeString, |
| 92 | + Optional: true, |
| 93 | + Description: "Used to save results.", |
| 94 | + }, |
| 95 | + }, |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func dataSourceTencentCloudMpsTasksRead(d *schema.ResourceData, meta interface{}) error { |
| 100 | + defer logElapsed("data_source.tencentcloud_mps_tasks.read")() |
| 101 | + defer inconsistentCheck(d, meta)() |
| 102 | + |
| 103 | + logId := getLogId(contextNil) |
| 104 | + |
| 105 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 106 | + |
| 107 | + paramMap := make(map[string]interface{}) |
| 108 | + if v, ok := d.GetOk("status"); ok { |
| 109 | + paramMap["Status"] = helper.String(v.(string)) |
| 110 | + } |
| 111 | + |
| 112 | + if v, _ := d.GetOk("limit"); v != nil { |
| 113 | + paramMap["Limit"] = helper.IntUint64(v.(int)) |
| 114 | + } |
| 115 | + |
| 116 | + if v, ok := d.GetOk("scroll_token"); ok { |
| 117 | + paramMap["ScrollToken"] = helper.String(v.(string)) |
| 118 | + } |
| 119 | + |
| 120 | + service := MpsService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 121 | + |
| 122 | + var taskSet []*mps.TaskSimpleInfo |
| 123 | + |
| 124 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 125 | + result, e := service.DescribeMpsTasksByFilter(ctx, paramMap) |
| 126 | + if e != nil { |
| 127 | + return retryError(e) |
| 128 | + } |
| 129 | + taskSet = result |
| 130 | + return nil |
| 131 | + }) |
| 132 | + if err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + |
| 136 | + ids := make([]string, 0, len(taskSet)) |
| 137 | + tmpList := make([]map[string]interface{}, 0, len(taskSet)) |
| 138 | + |
| 139 | + if taskSet != nil { |
| 140 | + for _, taskSimpleInfo := range taskSet { |
| 141 | + taskSimpleInfoMap := map[string]interface{}{} |
| 142 | + |
| 143 | + if taskSimpleInfo.TaskId != nil { |
| 144 | + taskSimpleInfoMap["task_id"] = taskSimpleInfo.TaskId |
| 145 | + } |
| 146 | + |
| 147 | + if taskSimpleInfo.TaskType != nil { |
| 148 | + taskSimpleInfoMap["task_type"] = taskSimpleInfo.TaskType |
| 149 | + } |
| 150 | + |
| 151 | + if taskSimpleInfo.CreateTime != nil { |
| 152 | + taskSimpleInfoMap["create_time"] = taskSimpleInfo.CreateTime |
| 153 | + } |
| 154 | + |
| 155 | + if taskSimpleInfo.BeginProcessTime != nil { |
| 156 | + taskSimpleInfoMap["begin_process_time"] = taskSimpleInfo.BeginProcessTime |
| 157 | + } |
| 158 | + |
| 159 | + if taskSimpleInfo.FinishTime != nil { |
| 160 | + taskSimpleInfoMap["finish_time"] = taskSimpleInfo.FinishTime |
| 161 | + } |
| 162 | + |
| 163 | + if taskSimpleInfo.SubTaskTypes != nil { |
| 164 | + taskSimpleInfoMap["sub_task_types"] = taskSimpleInfo.SubTaskTypes |
| 165 | + } |
| 166 | + |
| 167 | + ids = append(ids, *taskSimpleInfo.TaskId) |
| 168 | + tmpList = append(tmpList, taskSimpleInfoMap) |
| 169 | + } |
| 170 | + |
| 171 | + _ = d.Set("task_set", tmpList) |
| 172 | + } |
| 173 | + |
| 174 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 175 | + output, ok := d.GetOk("result_output_file") |
| 176 | + if ok && output.(string) != "" { |
| 177 | + if e := writeToFile(output.(string), tmpList); e != nil { |
| 178 | + return e |
| 179 | + } |
| 180 | + } |
| 181 | + return nil |
| 182 | +} |
0 commit comments