|
| 1 | +/* |
| 2 | +Provides a resource to create a dbbrain security_audit_log_export_task |
| 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 = "2020-12-28 00:00:00" |
| 10 | + end_time = "2020-12-28 01:00:00" |
| 11 | + product = "mysql" |
| 12 | + danger_levels = [0,1,2] |
| 13 | +} |
| 14 | +
|
| 15 | +``` |
| 16 | +*/ |
| 17 | +package tencentcloud |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + "log" |
| 24 | + "strings" |
| 25 | + |
| 26 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 27 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 28 | + dbbrain "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dbbrain/v20210527" |
| 29 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 30 | +) |
| 31 | + |
| 32 | +func resourceTencentCloudDbbrainSecurityAuditLogExportTask() *schema.Resource { |
| 33 | + return &schema.Resource{ |
| 34 | + Read: resourceTencentCloudDbbrainSecurityAuditLogExportTaskRead, |
| 35 | + Create: resourceTencentCloudDbbrainSecurityAuditLogExportTaskCreate, |
| 36 | + Delete: resourceTencentCloudDbbrainSecurityAuditLogExportTaskDelete, |
| 37 | + // Importer: &schema.ResourceImporter{ |
| 38 | + // State: schema.ImportStatePassthrough, |
| 39 | + // }, |
| 40 | + Schema: map[string]*schema.Schema{ |
| 41 | + "sec_audit_group_id": { |
| 42 | + Type: schema.TypeString, |
| 43 | + Required: true, |
| 44 | + ForceNew: true, |
| 45 | + Description: "security audit group id.", |
| 46 | + }, |
| 47 | + |
| 48 | + "start_time": { |
| 49 | + Type: schema.TypeString, |
| 50 | + Required: true, |
| 51 | + ForceNew: true, |
| 52 | + Description: "start time.", |
| 53 | + }, |
| 54 | + |
| 55 | + "end_time": { |
| 56 | + Type: schema.TypeString, |
| 57 | + Required: true, |
| 58 | + ForceNew: true, |
| 59 | + Description: "end time.", |
| 60 | + }, |
| 61 | + |
| 62 | + "product": { |
| 63 | + Type: schema.TypeString, |
| 64 | + Required: true, |
| 65 | + ForceNew: true, |
| 66 | + Description: "product, optional value is mysql.", |
| 67 | + }, |
| 68 | + |
| 69 | + "danger_levels": { |
| 70 | + Type: schema.TypeSet, |
| 71 | + Elem: &schema.Schema{ |
| 72 | + Type: schema.TypeInt, |
| 73 | + }, |
| 74 | + Optional: true, |
| 75 | + ForceNew: true, |
| 76 | + Description: "List of log risk levels, supported values include: 0 no risk; 1 low risk; 2 medium risk; 3 high risk.", |
| 77 | + }, |
| 78 | + }, |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func resourceTencentCloudDbbrainSecurityAuditLogExportTaskCreate(d *schema.ResourceData, meta interface{}) error { |
| 83 | + defer logElapsed("resource.tencentcloud_dbbrain_security_audit_log_export_task.create")() |
| 84 | + defer inconsistentCheck(d, meta)() |
| 85 | + |
| 86 | + logId := getLogId(contextNil) |
| 87 | + |
| 88 | + var ( |
| 89 | + request = dbbrain.NewCreateSecurityAuditLogExportTaskRequest() |
| 90 | + response *dbbrain.CreateSecurityAuditLogExportTaskResponse |
| 91 | + service = DbbrainService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 92 | + ctx = context.WithValue(context.TODO(), logIdKey, logId) |
| 93 | + secAuditGroupId string |
| 94 | + asyncRequestId string |
| 95 | + ) |
| 96 | + |
| 97 | + if v, ok := d.GetOk("sec_audit_group_id"); ok { |
| 98 | + secAuditGroupId = v.(string) |
| 99 | + request.SecAuditGroupId = helper.String(v.(string)) |
| 100 | + } |
| 101 | + |
| 102 | + if v, ok := d.GetOk("start_time"); ok { |
| 103 | + request.StartTime = helper.String(v.(string)) |
| 104 | + } |
| 105 | + |
| 106 | + if v, ok := d.GetOk("end_time"); ok { |
| 107 | + request.EndTime = helper.String(v.(string)) |
| 108 | + } |
| 109 | + |
| 110 | + if v, ok := d.GetOk("product"); ok { |
| 111 | + request.Product = helper.String(v.(string)) |
| 112 | + } |
| 113 | + |
| 114 | + if v, ok := d.GetOk("danger_levels"); ok { |
| 115 | + dangerLevelsSet := v.(*schema.Set).List() |
| 116 | + for i := range dangerLevelsSet { |
| 117 | + dangerLevels := dangerLevelsSet[i].(int) |
| 118 | + request.DangerLevels = append(request.DangerLevels, helper.IntInt64(dangerLevels)) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 123 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseDbbrainClient().CreateSecurityAuditLogExportTask(request) |
| 124 | + if e != nil { |
| 125 | + return retryError(e) |
| 126 | + } else { |
| 127 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", |
| 128 | + logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 129 | + } |
| 130 | + response = result |
| 131 | + return nil |
| 132 | + }) |
| 133 | + if err != nil { |
| 134 | + log.Printf("[CRITAL]%s create dbbrain securityAuditLogExportTask failed, reason:%+v", logId, err) |
| 135 | + return err |
| 136 | + } |
| 137 | + |
| 138 | + asyncRequestId = helper.UInt64ToStr(*response.Response.AsyncRequestId) |
| 139 | + |
| 140 | + err = resource.Retry(2*readRetryTimeout, func() *resource.RetryError { |
| 141 | + ret, err := service.DescribeDbbrainSecurityAuditLogExportTask(ctx, helper.String(secAuditGroupId), helper.String(asyncRequestId), nil) |
| 142 | + if err != nil { |
| 143 | + return retryError(err) |
| 144 | + } |
| 145 | + if ret != nil { |
| 146 | + log.Printf("[###########] task.Status:[%s]\n", *ret.Status) |
| 147 | + return nil |
| 148 | + } |
| 149 | + return resource.RetryableError(errors.New("[DEBUG] describe the audit log export task is nil, retry...")) |
| 150 | + }) |
| 151 | + if err != nil { |
| 152 | + log.Printf("[CRITAL]%s query dbbrain securityAuditLogExportTask failed, reason:%+v", logId, err) |
| 153 | + return err |
| 154 | + } |
| 155 | + |
| 156 | + d.SetId(secAuditGroupId + FILED_SP + asyncRequestId) |
| 157 | + return resourceTencentCloudDbbrainSecurityAuditLogExportTaskRead(d, meta) |
| 158 | +} |
| 159 | + |
| 160 | +func resourceTencentCloudDbbrainSecurityAuditLogExportTaskRead(d *schema.ResourceData, meta interface{}) error { |
| 161 | + defer logElapsed("resource.tencentcloud_dbbrain_security_audit_log_export_task.read")() |
| 162 | + defer inconsistentCheck(d, meta)() |
| 163 | + |
| 164 | + logId := getLogId(contextNil) |
| 165 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 166 | + |
| 167 | + service := DbbrainService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 168 | + |
| 169 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 170 | + if len(idSplit) != 2 { |
| 171 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 172 | + } |
| 173 | + secAuditGroupId := helper.String(idSplit[0]) |
| 174 | + asyncRequestId := helper.String(idSplit[1]) |
| 175 | + |
| 176 | + securityAuditLogExportTask, err := service.DescribeDbbrainSecurityAuditLogExportTask(ctx, secAuditGroupId, asyncRequestId, nil) |
| 177 | + if err != nil { |
| 178 | + return err |
| 179 | + } |
| 180 | + |
| 181 | + // _ = d.Set("sec_audit_group_id", secAuditGroupId) |
| 182 | + |
| 183 | + if securityAuditLogExportTask == nil { |
| 184 | + d.SetId("") |
| 185 | + return fmt.Errorf("resource `securityAuditLogExportTask` %s does not exist", d.Id()) |
| 186 | + } |
| 187 | + |
| 188 | + if securityAuditLogExportTask.LogStartTime != nil { |
| 189 | + _ = d.Set("start_time", securityAuditLogExportTask.LogStartTime) |
| 190 | + } |
| 191 | + |
| 192 | + if securityAuditLogExportTask.LogEndTime != nil { |
| 193 | + _ = d.Set("end_time", securityAuditLogExportTask.LogEndTime) |
| 194 | + } |
| 195 | + |
| 196 | + if securityAuditLogExportTask.DangerLevels != nil { |
| 197 | + _ = d.Set("danger_levels", securityAuditLogExportTask.DangerLevels) |
| 198 | + } |
| 199 | + |
| 200 | + return nil |
| 201 | +} |
| 202 | + |
| 203 | +func resourceTencentCloudDbbrainSecurityAuditLogExportTaskDelete(d *schema.ResourceData, meta interface{}) error { |
| 204 | + defer logElapsed("resource.tencentcloud_dbbrain_security_audit_log_export_task.delete")() |
| 205 | + defer inconsistentCheck(d, meta)() |
| 206 | + |
| 207 | + logId := getLogId(contextNil) |
| 208 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 209 | + |
| 210 | + service := DbbrainService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 211 | + |
| 212 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 213 | + if len(idSplit) != 2 { |
| 214 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 215 | + } |
| 216 | + secAuditGroupId := helper.String(idSplit[0]) |
| 217 | + asyncRequestId := helper.String(idSplit[1]) |
| 218 | + |
| 219 | + if err := service.DeleteDbbrainSecurityAuditLogExportTaskById(ctx, secAuditGroupId, asyncRequestId, nil); err != nil { |
| 220 | + return err |
| 221 | + } |
| 222 | + |
| 223 | + return nil |
| 224 | +} |
0 commit comments