|
| 1 | +/* |
| 2 | +Provides a resource to create a dbbrain db_diag_report_task |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_dbbrain_db_diag_report_task" "db_diag_report_task" { |
| 8 | + instance_id = "%s" |
| 9 | + start_time = "%s" |
| 10 | + end_time = "%s" |
| 11 | + send_mail_flag = 0 |
| 12 | + product = "mysql" |
| 13 | +} |
| 14 | +``` |
| 15 | +
|
| 16 | +*/ |
| 17 | +package tencentcloud |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "log" |
| 23 | + "strings" |
| 24 | + "time" |
| 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 resourceTencentCloudDbbrainDbDiagReportTask() *schema.Resource { |
| 33 | + return &schema.Resource{ |
| 34 | + Create: resourceTencentCloudDbbrainDbDiagReportTaskCreate, |
| 35 | + Read: resourceTencentCloudDbbrainDbDiagReportTaskRead, |
| 36 | + Delete: resourceTencentCloudDbbrainDbDiagReportTaskDelete, |
| 37 | + // contact_group, contact_person, send_mail_flag and product fileds can not query by read api |
| 38 | + // Importer: &schema.ResourceImporter{ |
| 39 | + // State: schema.ImportStatePassthrough, |
| 40 | + // }, |
| 41 | + Schema: map[string]*schema.Schema{ |
| 42 | + "instance_id": { |
| 43 | + Required: true, |
| 44 | + ForceNew: true, |
| 45 | + Type: schema.TypeString, |
| 46 | + Description: "instance id.", |
| 47 | + }, |
| 48 | + |
| 49 | + "product": { |
| 50 | + Required: true, |
| 51 | + ForceNew: true, |
| 52 | + Type: schema.TypeString, |
| 53 | + Description: "Service product type, supported values include: mysql - cloud database MySQL, cynosdb - cloud database CynosDB for MySQL.", |
| 54 | + }, |
| 55 | + |
| 56 | + "start_time": { |
| 57 | + Required: true, |
| 58 | + ForceNew: true, |
| 59 | + Type: schema.TypeString, |
| 60 | + Description: "Start time, such as 2020-11-08T14:00:00+08:00.", |
| 61 | + }, |
| 62 | + |
| 63 | + "end_time": { |
| 64 | + Required: true, |
| 65 | + ForceNew: true, |
| 66 | + Type: schema.TypeString, |
| 67 | + Description: "End time, such as 2020-11-09T14:00:00+08:00.", |
| 68 | + }, |
| 69 | + |
| 70 | + "send_mail_flag": { |
| 71 | + Required: true, |
| 72 | + ForceNew: true, |
| 73 | + Type: schema.TypeInt, |
| 74 | + Description: "Whether to send mail: 0 - no, 1 - yes.", |
| 75 | + }, |
| 76 | + |
| 77 | + "contact_person": { |
| 78 | + Optional: true, |
| 79 | + ForceNew: true, |
| 80 | + Type: schema.TypeSet, |
| 81 | + Elem: &schema.Schema{ |
| 82 | + Type: schema.TypeInt, |
| 83 | + }, |
| 84 | + Description: "An array of contact IDs to receive emails from.", |
| 85 | + }, |
| 86 | + |
| 87 | + "contact_group": { |
| 88 | + Optional: true, |
| 89 | + ForceNew: true, |
| 90 | + Type: schema.TypeSet, |
| 91 | + Elem: &schema.Schema{ |
| 92 | + Type: schema.TypeInt, |
| 93 | + }, |
| 94 | + Description: "An array of contact group IDs to receive mail from.", |
| 95 | + }, |
| 96 | + }, |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func resourceTencentCloudDbbrainDbDiagReportTaskCreate(d *schema.ResourceData, meta interface{}) error { |
| 101 | + defer logElapsed("resource.tencentcloud_dbbrain_db_diag_report_task.create")() |
| 102 | + defer inconsistentCheck(d, meta)() |
| 103 | + |
| 104 | + logId := getLogId(contextNil) |
| 105 | + |
| 106 | + var ( |
| 107 | + request = dbbrain.NewCreateDBDiagReportTaskRequest() |
| 108 | + response = dbbrain.NewCreateDBDiagReportTaskResponse() |
| 109 | + instanceId string |
| 110 | + product string |
| 111 | + ) |
| 112 | + if v, ok := d.GetOk("instance_id"); ok { |
| 113 | + instanceId = v.(string) |
| 114 | + request.InstanceId = helper.String(v.(string)) |
| 115 | + } |
| 116 | + |
| 117 | + if v, ok := d.GetOk("start_time"); ok { |
| 118 | + request.StartTime = helper.String(v.(string)) |
| 119 | + } |
| 120 | + |
| 121 | + if v, ok := d.GetOk("end_time"); ok { |
| 122 | + request.EndTime = helper.String(v.(string)) |
| 123 | + } |
| 124 | + |
| 125 | + if v, ok := d.GetOkExists("send_mail_flag"); ok { |
| 126 | + request.SendMailFlag = helper.IntInt64(v.(int)) |
| 127 | + } |
| 128 | + |
| 129 | + if v, ok := d.GetOk("contact_person"); ok { |
| 130 | + contactPersonSet := v.(*schema.Set).List() |
| 131 | + for i := range contactPersonSet { |
| 132 | + contactPerson := contactPersonSet[i].(int) |
| 133 | + request.ContactPerson = append(request.ContactPerson, helper.IntInt64(contactPerson)) |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + if v, ok := d.GetOk("contact_group"); ok { |
| 138 | + contactGroupSet := v.(*schema.Set).List() |
| 139 | + for i := range contactGroupSet { |
| 140 | + contactGroup := contactGroupSet[i].(int) |
| 141 | + request.ContactGroup = append(request.ContactGroup, helper.IntInt64(contactGroup)) |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + if v, ok := d.GetOk("product"); ok { |
| 146 | + product = v.(string) |
| 147 | + request.Product = helper.String(v.(string)) |
| 148 | + } |
| 149 | + |
| 150 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 151 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseDbbrainClient().CreateDBDiagReportTask(request) |
| 152 | + if e != nil { |
| 153 | + return retryError(e) |
| 154 | + } else { |
| 155 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 156 | + } |
| 157 | + response = result |
| 158 | + return nil |
| 159 | + }) |
| 160 | + if err != nil { |
| 161 | + log.Printf("[CRITAL]%s create dbbrain dbDiagReportTask failed, reason:%+v", logId, err) |
| 162 | + return err |
| 163 | + } |
| 164 | + if response == nil || response.Response.AsyncRequestId == nil { |
| 165 | + return fmt.Errorf("[CRITAL]%s The dbbrain dbDiagReportTask id not found after creation", logId) |
| 166 | + } |
| 167 | + |
| 168 | + asyncRequestId := response.Response.AsyncRequestId |
| 169 | + d.SetId(helper.Int64ToStr(*asyncRequestId) + FILED_SP + instanceId + FILED_SP + product) |
| 170 | + |
| 171 | + service := DbbrainService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 172 | + |
| 173 | + conf := BuildStateChangeConf([]string{}, []string{"100"}, 3*readRetryTimeout, time.Second, service.DbbrainDbDiagReportTaskStateRefreshFunc(asyncRequestId, instanceId, product, []string{})) |
| 174 | + |
| 175 | + if _, e := conf.WaitForState(); e != nil { |
| 176 | + return e |
| 177 | + } |
| 178 | + |
| 179 | + return resourceTencentCloudDbbrainDbDiagReportTaskRead(d, meta) |
| 180 | +} |
| 181 | + |
| 182 | +func resourceTencentCloudDbbrainDbDiagReportTaskRead(d *schema.ResourceData, meta interface{}) error { |
| 183 | + defer logElapsed("resource.tencentcloud_dbbrain_db_diag_report_task.read")() |
| 184 | + defer inconsistentCheck(d, meta)() |
| 185 | + |
| 186 | + logId := getLogId(contextNil) |
| 187 | + |
| 188 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 189 | + |
| 190 | + service := DbbrainService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 191 | + |
| 192 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 193 | + if len(idSplit) != 3 { |
| 194 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 195 | + } |
| 196 | + asyncRequestId := idSplit[0] |
| 197 | + instanceId := idSplit[1] |
| 198 | + product := idSplit[2] |
| 199 | + |
| 200 | + dbDiagReportTask, err := service.DescribeDbbrainDbDiagReportTaskById(ctx, helper.StrToInt64Point(asyncRequestId), instanceId, product) |
| 201 | + if err != nil { |
| 202 | + return err |
| 203 | + } |
| 204 | + |
| 205 | + if dbDiagReportTask == nil { |
| 206 | + d.SetId("") |
| 207 | + log.Printf("[WARN]%s resource `DbbrainDbDiagReportTask` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 208 | + return nil |
| 209 | + } |
| 210 | + |
| 211 | + if dbDiagReportTask.StartTime != nil { |
| 212 | + _ = d.Set("start_time", dbDiagReportTask.StartTime) |
| 213 | + } |
| 214 | + |
| 215 | + if dbDiagReportTask.EndTime != nil { |
| 216 | + _ = d.Set("end_time", dbDiagReportTask.EndTime) |
| 217 | + } |
| 218 | + |
| 219 | + // if dbDiagReportTask.SendMailFlag != nil { |
| 220 | + // _ = d.Set("send_mail_flag", dbDiagReportTask.SendMailFlag) |
| 221 | + // } |
| 222 | + |
| 223 | + // if dbDiagReportTask.ContactPerson != nil { |
| 224 | + // _ = d.Set("contact_person", dbDiagReportTask.ContactPerson) |
| 225 | + // } |
| 226 | + |
| 227 | + // if dbDiagReportTask.ContactGroup != nil { |
| 228 | + // _ = d.Set("contact_group", dbDiagReportTask.ContactGroup) |
| 229 | + // } |
| 230 | + |
| 231 | + // if dbDiagReportTask.Product != nil { |
| 232 | + // _ = d.Set("product", dbDiagReportTask.Product) |
| 233 | + // } |
| 234 | + |
| 235 | + return nil |
| 236 | +} |
| 237 | + |
| 238 | +func resourceTencentCloudDbbrainDbDiagReportTaskDelete(d *schema.ResourceData, meta interface{}) error { |
| 239 | + defer logElapsed("resource.tencentcloud_dbbrain_db_diag_report_task.delete")() |
| 240 | + defer inconsistentCheck(d, meta)() |
| 241 | + |
| 242 | + logId := getLogId(contextNil) |
| 243 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 244 | + |
| 245 | + service := DbbrainService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 246 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 247 | + if len(idSplit) != 3 { |
| 248 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 249 | + } |
| 250 | + asyncRequestId := idSplit[0] |
| 251 | + instanceId := idSplit[1] |
| 252 | + product := idSplit[2] |
| 253 | + |
| 254 | + if err := service.DeleteDbbrainDbDiagReportTaskById(ctx, *helper.StrToInt64Point(asyncRequestId), instanceId, product); err != nil { |
| 255 | + return err |
| 256 | + } |
| 257 | + |
| 258 | + return nil |
| 259 | +} |
0 commit comments