|
| 1 | +package cdb |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "strings" |
| 8 | + |
| 9 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 10 | + |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 13 | + mysql "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdb/v20170320" |
| 14 | + |
| 15 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 16 | +) |
| 17 | + |
| 18 | +func ResourceTencentCloudMysqlClsLogAttachment() *schema.Resource { |
| 19 | + return &schema.Resource{ |
| 20 | + Create: resourceTencentCloudMysqlClsLogAttachmentCreate, |
| 21 | + Read: resourceTencentCloudMysqlClsLogAttachmentRead, |
| 22 | + Delete: resourceTencentCloudMysqlClsLogAttachmentDelete, |
| 23 | + |
| 24 | + Schema: map[string]*schema.Schema{ |
| 25 | + "instance_id": { |
| 26 | + Required: true, |
| 27 | + ForceNew: true, |
| 28 | + Type: schema.TypeString, |
| 29 | + Description: "The id of instance.", |
| 30 | + }, |
| 31 | + "log_type": { |
| 32 | + Required: true, |
| 33 | + ForceNew: true, |
| 34 | + Type: schema.TypeString, |
| 35 | + ValidateFunc: tccommon.ValidateAllowedStringValue(MYSQL_LOG_TO_CLS_TYPE), |
| 36 | + Description: "Log type. Support `error` or `slowlog`.", |
| 37 | + }, |
| 38 | + "create_log_set": { |
| 39 | + Optional: true, |
| 40 | + ForceNew: true, |
| 41 | + Type: schema.TypeBool, |
| 42 | + Description: "Whether to create log set.", |
| 43 | + }, |
| 44 | + "create_log_topic": { |
| 45 | + Optional: true, |
| 46 | + ForceNew: true, |
| 47 | + Type: schema.TypeBool, |
| 48 | + Description: "Whether to create log topic.", |
| 49 | + }, |
| 50 | + "log_set": { |
| 51 | + Required: true, |
| 52 | + ForceNew: true, |
| 53 | + Type: schema.TypeString, |
| 54 | + Description: "If `create_log_set` is `true`, use log set name, Else use log set Id.", |
| 55 | + }, |
| 56 | + "log_topic": { |
| 57 | + Required: true, |
| 58 | + ForceNew: true, |
| 59 | + Type: schema.TypeString, |
| 60 | + Description: "If `create_log_topic` is `true`, use log topic name, Else use log topic Id.", |
| 61 | + }, |
| 62 | + "period": { |
| 63 | + Optional: true, |
| 64 | + ForceNew: true, |
| 65 | + Type: schema.TypeInt, |
| 66 | + Description: "The validity period of the log theme is 30 days by default when not filled in.", |
| 67 | + }, |
| 68 | + "create_index": { |
| 69 | + Optional: true, |
| 70 | + ForceNew: true, |
| 71 | + Type: schema.TypeBool, |
| 72 | + Description: "Whether to create index.", |
| 73 | + }, |
| 74 | + "cls_region": { |
| 75 | + Optional: true, |
| 76 | + Computed: true, |
| 77 | + Type: schema.TypeString, |
| 78 | + Description: "Cls region.", |
| 79 | + }, |
| 80 | + "log_set_id": { |
| 81 | + Computed: true, |
| 82 | + Type: schema.TypeString, |
| 83 | + Description: "Log set Id.", |
| 84 | + }, |
| 85 | + "log_topic_id": { |
| 86 | + Computed: true, |
| 87 | + Type: schema.TypeString, |
| 88 | + Description: "Log topic Id.", |
| 89 | + }, |
| 90 | + "status": { |
| 91 | + Computed: true, |
| 92 | + Type: schema.TypeString, |
| 93 | + Description: "Log Status.", |
| 94 | + }, |
| 95 | + }, |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func resourceTencentCloudMysqlClsLogAttachmentCreate(d *schema.ResourceData, meta interface{}) error { |
| 100 | + defer tccommon.LogElapsed("resource.tencentcloud_mysql_cls_log_attachment.create")() |
| 101 | + defer tccommon.InconsistentCheck(d, meta)() |
| 102 | + |
| 103 | + var ( |
| 104 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 105 | + request = mysql.NewModifyDBInstanceLogToCLSRequest() |
| 106 | + instanceId string |
| 107 | + logType string |
| 108 | + ) |
| 109 | + |
| 110 | + if v, ok := d.GetOk("instance_id"); ok { |
| 111 | + instanceId = v.(string) |
| 112 | + request.InstanceId = helper.String(v.(string)) |
| 113 | + } |
| 114 | + |
| 115 | + if v, ok := d.GetOk("log_type"); ok { |
| 116 | + logType = v.(string) |
| 117 | + request.LogType = helper.String(v.(string)) |
| 118 | + } |
| 119 | + |
| 120 | + if v, ok := d.GetOkExists("create_log_set"); ok { |
| 121 | + request.CreateLogset = helper.Bool(v.(bool)) |
| 122 | + } |
| 123 | + |
| 124 | + if v, ok := d.GetOkExists("create_log_topic"); ok { |
| 125 | + request.CreateLogTopic = helper.Bool(v.(bool)) |
| 126 | + } |
| 127 | + |
| 128 | + if v, ok := d.GetOk("log_set"); ok { |
| 129 | + request.Logset = helper.String(v.(string)) |
| 130 | + } |
| 131 | + |
| 132 | + if v, ok := d.GetOk("log_topic"); ok { |
| 133 | + request.LogTopic = helper.String(v.(string)) |
| 134 | + } |
| 135 | + |
| 136 | + if v, ok := d.GetOkExists("period"); ok { |
| 137 | + request.Period = helper.IntInt64(v.(int)) |
| 138 | + } |
| 139 | + |
| 140 | + if v, ok := d.GetOkExists("create_index"); ok { |
| 141 | + request.CreateIndex = helper.Bool(v.(bool)) |
| 142 | + } |
| 143 | + |
| 144 | + if v, ok := d.GetOk("cls_region"); ok { |
| 145 | + request.ClsRegion = helper.String(v.(string)) |
| 146 | + } |
| 147 | + |
| 148 | + request.Status = helper.String("ON") |
| 149 | + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 150 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseMysqlClient().ModifyDBInstanceLogToCLS(request) |
| 151 | + if e != nil { |
| 152 | + return tccommon.RetryError(e) |
| 153 | + } else { |
| 154 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 155 | + } |
| 156 | + |
| 157 | + return nil |
| 158 | + }) |
| 159 | + |
| 160 | + if err != nil { |
| 161 | + log.Printf("[CRITAL]%s create mysql ModifyDBInstanceLogToCLS failed, reason:%+v", logId, err) |
| 162 | + return err |
| 163 | + } |
| 164 | + |
| 165 | + d.SetId(strings.Join([]string{instanceId, logType}, tccommon.FILED_SP)) |
| 166 | + |
| 167 | + return resourceTencentCloudMysqlClsLogAttachmentRead(d, meta) |
| 168 | +} |
| 169 | + |
| 170 | +func resourceTencentCloudMysqlClsLogAttachmentRead(d *schema.ResourceData, meta interface{}) error { |
| 171 | + defer tccommon.LogElapsed("resource.tencentcloud_mysql_cls_log_attachment.read")() |
| 172 | + defer tccommon.InconsistentCheck(d, meta)() |
| 173 | + |
| 174 | + var ( |
| 175 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 176 | + ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId) |
| 177 | + service = MysqlService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 178 | + ) |
| 179 | + |
| 180 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 181 | + if len(idSplit) != 2 { |
| 182 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 183 | + } |
| 184 | + |
| 185 | + instanceId := idSplit[0] |
| 186 | + logType := idSplit[1] |
| 187 | + |
| 188 | + logToCLSResponseParam, err := service.DescribeMysqlInstanceLogToCLSById(ctx, instanceId) |
| 189 | + if err != nil { |
| 190 | + return err |
| 191 | + } |
| 192 | + |
| 193 | + if logToCLSResponseParam == nil { |
| 194 | + d.SetId("") |
| 195 | + log.Printf("[WARN]%s resource `DescribeDBInstanceLogToCLS` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 196 | + return nil |
| 197 | + } |
| 198 | + |
| 199 | + _ = d.Set("instance_id", instanceId) |
| 200 | + _ = d.Set("log_type", logType) |
| 201 | + |
| 202 | + if logType == MYSQL_LOG_TO_CLS_TYPE_ERROR { |
| 203 | + if logToCLSResponseParam.ErrorLog.LogSetId != nil { |
| 204 | + _ = d.Set("log_set_id", *logToCLSResponseParam.ErrorLog.LogSetId) |
| 205 | + } |
| 206 | + |
| 207 | + if logToCLSResponseParam.ErrorLog.LogTopicId != nil { |
| 208 | + _ = d.Set("log_topic_id", *logToCLSResponseParam.ErrorLog.LogTopicId) |
| 209 | + } |
| 210 | + |
| 211 | + if logToCLSResponseParam.ErrorLog.Status != nil { |
| 212 | + _ = d.Set("status", *logToCLSResponseParam.ErrorLog.Status) |
| 213 | + } |
| 214 | + |
| 215 | + if logToCLSResponseParam.ErrorLog.ClsRegion != nil { |
| 216 | + _ = d.Set("cls_region", *logToCLSResponseParam.ErrorLog.ClsRegion) |
| 217 | + } |
| 218 | + } else { |
| 219 | + if logToCLSResponseParam.SlowLog.LogSetId != nil { |
| 220 | + _ = d.Set("log_set_id", *logToCLSResponseParam.SlowLog.LogSetId) |
| 221 | + } |
| 222 | + |
| 223 | + if logToCLSResponseParam.SlowLog.LogTopicId != nil { |
| 224 | + _ = d.Set("log_topic_id", *logToCLSResponseParam.SlowLog.LogTopicId) |
| 225 | + } |
| 226 | + |
| 227 | + if logToCLSResponseParam.SlowLog.Status != nil { |
| 228 | + _ = d.Set("status", *logToCLSResponseParam.SlowLog.Status) |
| 229 | + } |
| 230 | + |
| 231 | + if logToCLSResponseParam.SlowLog.ClsRegion != nil { |
| 232 | + _ = d.Set("cls_region", *logToCLSResponseParam.SlowLog.ClsRegion) |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + return nil |
| 237 | +} |
| 238 | + |
| 239 | +func resourceTencentCloudMysqlClsLogAttachmentDelete(d *schema.ResourceData, meta interface{}) error { |
| 240 | + defer tccommon.LogElapsed("resource.tencentcloud_mysql_cls_log_attachment.delete")() |
| 241 | + defer tccommon.InconsistentCheck(d, meta)() |
| 242 | + |
| 243 | + var ( |
| 244 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 245 | + ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId) |
| 246 | + service = MysqlService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 247 | + ) |
| 248 | + |
| 249 | + idSplit := strings.Split(d.Id(), tccommon.FILED_SP) |
| 250 | + if len(idSplit) != 2 { |
| 251 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 252 | + } |
| 253 | + |
| 254 | + instanceId := idSplit[0] |
| 255 | + logType := idSplit[1] |
| 256 | + |
| 257 | + if err := service.DeleteMysqlInstanceLogToCLSById(ctx, instanceId, logType); err != nil { |
| 258 | + return err |
| 259 | + } |
| 260 | + |
| 261 | + return nil |
| 262 | +} |
0 commit comments