|
| 1 | +/* |
| 2 | +Use this data source to query the COS batch. |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_cos_batchs" "cos_batchs" { |
| 8 | + uin = "xxxxxx" |
| 9 | + appid = "xxxxxx" |
| 10 | +} |
| 11 | +``` |
| 12 | +*/ |
| 13 | +package tencentcloud |
| 14 | + |
| 15 | +import ( |
| 16 | + "context" |
| 17 | + "encoding/json" |
| 18 | + "log" |
| 19 | + |
| 20 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 21 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 22 | + "github.com/tencentyun/cos-go-sdk-v5" |
| 23 | +) |
| 24 | + |
| 25 | +func dataSourceTencentCloudCosBatchs() *schema.Resource { |
| 26 | + return &schema.Resource{ |
| 27 | + Read: dataSourceTencentCloudCosBatchsRead, |
| 28 | + |
| 29 | + Schema: map[string]*schema.Schema{ |
| 30 | + "uin": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Required: true, |
| 33 | + Description: "Uin.", |
| 34 | + }, |
| 35 | + "appid": { |
| 36 | + Type: schema.TypeInt, |
| 37 | + Required: true, |
| 38 | + Description: "Appid.", |
| 39 | + }, |
| 40 | + "job_statuses": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Optional: true, |
| 43 | + Description: "The task status information you need to query. If you do not specify a task status, COS returns the status of all tasks that have been executed, including those that are in progress. If you specify a task status, COS returns the task in the specified state. Optional task states include: Active, Cancelled, Cancelling, Complete, Completing, Failed, Failing, New, Paused, Pausing, Preparing, Ready, Suspended.", |
| 44 | + }, |
| 45 | + "jobs": { |
| 46 | + Type: schema.TypeList, |
| 47 | + Computed: true, |
| 48 | + Description: "Multiple batch processing task information.", |
| 49 | + Elem: &schema.Resource{ |
| 50 | + Schema: map[string]*schema.Schema{ |
| 51 | + "creation_time": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Computed: true, |
| 54 | + Description: "Job creation time.", |
| 55 | + }, |
| 56 | + "description": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Computed: true, |
| 59 | + Description: "Mission description. The length is limited to 0-256 bytes.", |
| 60 | + }, |
| 61 | + "job_id": { |
| 62 | + Type: schema.TypeString, |
| 63 | + Computed: true, |
| 64 | + Description: "Job ID. The length is limited to 1-64 bytes.", |
| 65 | + }, |
| 66 | + "operation": { |
| 67 | + Type: schema.TypeString, |
| 68 | + Computed: true, |
| 69 | + Description: "Actions performed on objects in a batch processing job. For example, COSPutObjectCopy.", |
| 70 | + }, |
| 71 | + "priority": { |
| 72 | + Type: schema.TypeInt, |
| 73 | + Computed: true, |
| 74 | + Description: "Mission priority. Tasks with higher values will be given priority. The priority size is limited to 0-2147483647.", |
| 75 | + }, |
| 76 | + "status": { |
| 77 | + Type: schema.TypeString, |
| 78 | + Computed: true, |
| 79 | + Description: "Task execution status. Legal parameter values include Active, Cancelled, Cancelling, Complete, Completing, Failed, Failing, New, Paused, Pausing, Preparing, Ready, Suspended.", |
| 80 | + }, |
| 81 | + "termination_date": { |
| 82 | + Type: schema.TypeString, |
| 83 | + Computed: true, |
| 84 | + Description: "The end time of the batch processing job.", |
| 85 | + }, |
| 86 | + "progress_summary": { |
| 87 | + Type: schema.TypeList, |
| 88 | + Computed: true, |
| 89 | + Description: "Summary of the status of task implementation. Describe the total number of operations performed in this task, the number of successful operations, and the number of failed operations.", |
| 90 | + Elem: &schema.Resource{ |
| 91 | + Schema: map[string]*schema.Schema{ |
| 92 | + "number_of_tasks_failed": { |
| 93 | + Type: schema.TypeInt, |
| 94 | + Computed: true, |
| 95 | + Description: "The current failed Operand.", |
| 96 | + }, |
| 97 | + "number_of_tasks_succeeded": { |
| 98 | + Type: schema.TypeInt, |
| 99 | + Computed: true, |
| 100 | + Description: "The current successful Operand.", |
| 101 | + }, |
| 102 | + "total_number_of_tasks": { |
| 103 | + Type: schema.TypeInt, |
| 104 | + Computed: true, |
| 105 | + Description: "Total Operand.", |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + }, |
| 112 | + }, |
| 113 | + "result_output_file": { |
| 114 | + Type: schema.TypeString, |
| 115 | + Optional: true, |
| 116 | + Description: "Used to save results.", |
| 117 | + }, |
| 118 | + }, |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +func dataSourceTencentCloudCosBatchsRead(d *schema.ResourceData, meta interface{}) error { |
| 123 | + defer logElapsed("data_source.tencentcloud_cos_batchs.read")() |
| 124 | + |
| 125 | + logId := getLogId(contextNil) |
| 126 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 127 | + uin := d.Get("uin").(string) |
| 128 | + appid := d.Get("appid").(int) |
| 129 | + jobs := make([]map[string]interface{}, 0) |
| 130 | + |
| 131 | + opt := &cos.BatchListJobsOptions{} |
| 132 | + if v, ok := d.GetOk("job_statuses"); ok { |
| 133 | + opt.JobStatuses = v.(string) |
| 134 | + } |
| 135 | + headers := &cos.BatchRequestHeaders{ |
| 136 | + XCosAppid: appid, |
| 137 | + } |
| 138 | + ids := make([]string, 0) |
| 139 | + for { |
| 140 | + req, _ := json.Marshal(opt) |
| 141 | + result, response, err := meta.(*TencentCloudClient).apiV3Conn.UseCosBatchClient(uin).Batch.ListJobs(ctx, nil, headers) |
| 142 | + responseBody, _ := json.Marshal(response.Body) |
| 143 | + log.Printf("[DEBUG]%s api[ListJobs] success, request body [%s], response body [%v]\n", logId, req, responseBody) |
| 144 | + if err != nil { |
| 145 | + return err |
| 146 | + } |
| 147 | + for _, item := range result.Jobs.Members { |
| 148 | + jobItem := make(map[string]interface{}) |
| 149 | + jobItem["creation_time"] = item.CreationTime |
| 150 | + jobItem["description"] = item.Description |
| 151 | + jobItem["job_id"] = item.JobId |
| 152 | + jobItem["operation"] = item.Operation |
| 153 | + jobItem["priority"] = item.Priority |
| 154 | + jobItem["status"] = item.Status |
| 155 | + jobItem["termination_date"] = item.TerminationDate |
| 156 | + progressSummary := map[string]interface{}{ |
| 157 | + "number_of_tasks_failed": item.ProgressSummary.NumberOfTasksFailed, |
| 158 | + "number_of_tasks_succeeded": item.ProgressSummary.NumberOfTasksSucceeded, |
| 159 | + "total_number_of_tasks": item.ProgressSummary.TotalNumberOfTasks, |
| 160 | + } |
| 161 | + jobItem["progress_summary"] = []interface{}{progressSummary} |
| 162 | + ids = append(ids, item.JobId) |
| 163 | + jobs = append(jobs, jobItem) |
| 164 | + } |
| 165 | + if result.NextToken != "" { |
| 166 | + opt.NextToken = result.NextToken |
| 167 | + } else { |
| 168 | + break |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 173 | + _ = d.Set("jobs", jobs) |
| 174 | + output, ok := d.GetOk("result_output_file") |
| 175 | + if ok && output.(string) != "" { |
| 176 | + if err := writeToFile(output.(string), jobs); err != nil { |
| 177 | + return err |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + return nil |
| 182 | +} |
0 commit comments