|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of dbbrain securityAuditLogExportTasks |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_dbbrain_security_audit_log_export_task" "task" { |
| 8 | + sec_audit_group_id = "sec_audit_group_id" |
| 9 | + start_time = "start_time" |
| 10 | + end_time = "end_time" |
| 11 | + product = "mysql" |
| 12 | + danger_levels = [0,1,2] |
| 13 | +} |
| 14 | +
|
| 15 | +data "tencentcloud_dbbrain_security_audit_log_export_tasks" "tasks" { |
| 16 | + sec_audit_group_id = "sec_audit_group_id" |
| 17 | + product = "mysql" |
| 18 | + async_request_ids = [tencentcloud_dbbrain_security_audit_log_export_task.task.async_request_id] |
| 19 | +} |
| 20 | +``` |
| 21 | +*/ |
| 22 | +package tencentcloud |
| 23 | + |
| 24 | +import ( |
| 25 | + "context" |
| 26 | + "log" |
| 27 | + |
| 28 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 29 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 30 | + dbbrain "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dbbrain/v20210527" |
| 31 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 32 | +) |
| 33 | + |
| 34 | +func dataSourceTencentCloudDbbrainSecurityAuditLogExportTasks() *schema.Resource { |
| 35 | + return &schema.Resource{ |
| 36 | + Read: dataSourceTencentCloudDbbrainSecurityAuditLogExportTasksRead, |
| 37 | + Schema: map[string]*schema.Schema{ |
| 38 | + "sec_audit_group_id": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Required: true, |
| 41 | + Description: "security audit group id.", |
| 42 | + }, |
| 43 | + |
| 44 | + "product": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Required: true, |
| 47 | + Description: "product, optional value is mysql.", |
| 48 | + }, |
| 49 | + |
| 50 | + "async_request_ids": { |
| 51 | + Type: schema.TypeSet, |
| 52 | + Elem: &schema.Schema{ |
| 53 | + Type: schema.TypeInt, |
| 54 | + }, |
| 55 | + Optional: true, |
| 56 | + Description: "async request id list.", |
| 57 | + }, |
| 58 | + |
| 59 | + "list": { |
| 60 | + Type: schema.TypeList, |
| 61 | + Computed: true, |
| 62 | + Description: "security audit log export task list.", |
| 63 | + Elem: &schema.Resource{ |
| 64 | + Schema: map[string]*schema.Schema{ |
| 65 | + "async_request_id": { |
| 66 | + Type: schema.TypeInt, |
| 67 | + Computed: true, |
| 68 | + Description: "async request id.", |
| 69 | + }, |
| 70 | + "start_time": { |
| 71 | + Type: schema.TypeString, |
| 72 | + Computed: true, |
| 73 | + Description: "start time.", |
| 74 | + }, |
| 75 | + "end_time": { |
| 76 | + Type: schema.TypeString, |
| 77 | + Computed: true, |
| 78 | + Description: "end time.", |
| 79 | + }, |
| 80 | + "create_time": { |
| 81 | + Type: schema.TypeString, |
| 82 | + Computed: true, |
| 83 | + Description: "create time.", |
| 84 | + }, |
| 85 | + "status": { |
| 86 | + Type: schema.TypeString, |
| 87 | + Computed: true, |
| 88 | + Description: "status.", |
| 89 | + }, |
| 90 | + "progress": { |
| 91 | + Type: schema.TypeInt, |
| 92 | + Computed: true, |
| 93 | + Description: "task progress.", |
| 94 | + }, |
| 95 | + "log_start_time": { |
| 96 | + Type: schema.TypeString, |
| 97 | + Computed: true, |
| 98 | + Description: "log start time.", |
| 99 | + }, |
| 100 | + "log_end_time": { |
| 101 | + Type: schema.TypeString, |
| 102 | + Computed: true, |
| 103 | + Description: "log end time.", |
| 104 | + }, |
| 105 | + "total_size": { |
| 106 | + Type: schema.TypeInt, |
| 107 | + Computed: true, |
| 108 | + Description: "the total size of log.", |
| 109 | + }, |
| 110 | + "danger_levels": { |
| 111 | + Type: schema.TypeSet, |
| 112 | + Elem: &schema.Schema{ |
| 113 | + Type: schema.TypeInt, |
| 114 | + }, |
| 115 | + Computed: true, |
| 116 | + Description: "danger level list.", |
| 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 dataSourceTencentCloudDbbrainSecurityAuditLogExportTasksRead(d *schema.ResourceData, meta interface{}) error { |
| 132 | + defer logElapsed("data_source.tencentcloud_dbbrain_security_audit_log_export_tasks.read")() |
| 133 | + defer inconsistentCheck(d, meta)() |
| 134 | + |
| 135 | + logId := getLogId(contextNil) |
| 136 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 137 | + var sag_id string |
| 138 | + |
| 139 | + paramMap := make(map[string]interface{}) |
| 140 | + if v, ok := d.GetOk("sec_audit_group_id"); ok { |
| 141 | + paramMap["sec_audit_group_id"] = helper.String(v.(string)) |
| 142 | + sag_id = v.(string) |
| 143 | + } |
| 144 | + |
| 145 | + if v, ok := d.GetOk("product"); ok { |
| 146 | + paramMap["product"] = helper.String(v.(string)) |
| 147 | + } |
| 148 | + |
| 149 | + if v, ok := d.GetOk("async_request_ids"); ok { |
| 150 | + async_request_idSet := v.(*schema.Set).List() |
| 151 | + tmpList := make([]*uint64, 0, len(async_request_idSet)) |
| 152 | + for i := range async_request_idSet { |
| 153 | + async_request_id := async_request_idSet[i].(int) |
| 154 | + tmpList = append(tmpList, helper.IntUint64(async_request_id)) |
| 155 | + } |
| 156 | + paramMap["async_request_ids"] = tmpList |
| 157 | + } |
| 158 | + |
| 159 | + dbbrainService := DbbrainService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 160 | + |
| 161 | + var tasks []*dbbrain.SecLogExportTaskInfo |
| 162 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 163 | + results, e := dbbrainService.DescribeDbbrainSecurityAuditLogExportTasksByFilter(ctx, paramMap) |
| 164 | + if e != nil { |
| 165 | + return retryError(e) |
| 166 | + } |
| 167 | + tasks = results |
| 168 | + return nil |
| 169 | + }) |
| 170 | + if err != nil { |
| 171 | + log.Printf("[CRITAL]%s read Dbbrain tasks failed, reason:%+v", logId, err) |
| 172 | + return err |
| 173 | + } |
| 174 | + |
| 175 | + ids := make([]string, 0, len(tasks)) |
| 176 | + taskList := make([]map[string]interface{}, 0, len(tasks)) |
| 177 | + |
| 178 | + if tasks != nil { |
| 179 | + |
| 180 | + for _, task := range tasks { |
| 181 | + taskMap := map[string]interface{}{} |
| 182 | + if task.AsyncRequestId != nil { |
| 183 | + taskMap["async_request_id"] = task.AsyncRequestId |
| 184 | + } |
| 185 | + if task.StartTime != nil { |
| 186 | + taskMap["start_time"] = task.StartTime |
| 187 | + } |
| 188 | + if task.EndTime != nil { |
| 189 | + taskMap["end_time"] = task.EndTime |
| 190 | + } |
| 191 | + if task.CreateTime != nil { |
| 192 | + taskMap["create_time"] = task.CreateTime |
| 193 | + } |
| 194 | + if task.Status != nil { |
| 195 | + taskMap["status"] = task.Status |
| 196 | + } |
| 197 | + if task.Progress != nil { |
| 198 | + taskMap["progress"] = task.Progress |
| 199 | + } |
| 200 | + if task.LogStartTime != nil { |
| 201 | + taskMap["log_start_time"] = task.LogStartTime |
| 202 | + } |
| 203 | + if task.LogEndTime != nil { |
| 204 | + taskMap["log_end_time"] = task.LogEndTime |
| 205 | + } |
| 206 | + if task.TotalSize != nil { |
| 207 | + taskMap["total_size"] = task.TotalSize |
| 208 | + } |
| 209 | + if task.DangerLevels != nil { |
| 210 | + taskMap["danger_levels"] = task.DangerLevels |
| 211 | + } |
| 212 | + ids = append(ids, sag_id+FILED_SP+helper.UInt64ToStr(*task.AsyncRequestId)) |
| 213 | + taskList = append(taskList, taskMap) |
| 214 | + } |
| 215 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 216 | + _ = d.Set("list", taskList) |
| 217 | + } |
| 218 | + |
| 219 | + output, ok := d.GetOk("result_output_file") |
| 220 | + if ok && output.(string) != "" { |
| 221 | + if e := writeToFile(output.(string), taskList); e != nil { |
| 222 | + return e |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + return nil |
| 227 | +} |
0 commit comments