|
| 1 | +package cynosdb |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | + cynosdbv20190107 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb/v20190107" |
| 11 | + |
| 12 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 13 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 14 | +) |
| 15 | + |
| 16 | +func ResourceTencentCloudCynosdbAuditService() *schema.Resource { |
| 17 | + return &schema.Resource{ |
| 18 | + Create: ResourceTencentCloudCynosdbAuditServiceCreate, |
| 19 | + Read: ResourceTencentCloudCynosdbAuditServiceRead, |
| 20 | + Update: ResourceTencentCloudCynosdbAuditServiceUpdate, |
| 21 | + Delete: ResourceTencentCloudCynosdbAuditServiceDelete, |
| 22 | + Importer: &schema.ResourceImporter{ |
| 23 | + State: schema.ImportStatePassthrough, |
| 24 | + }, |
| 25 | + Schema: map[string]*schema.Schema{ |
| 26 | + "instance_id": { |
| 27 | + Type: schema.TypeString, |
| 28 | + Required: true, |
| 29 | + ForceNew: true, |
| 30 | + Description: "Instance ID.", |
| 31 | + }, |
| 32 | + |
| 33 | + "log_expire_day": { |
| 34 | + Type: schema.TypeInt, |
| 35 | + Required: true, |
| 36 | + Description: "Log retention period.", |
| 37 | + }, |
| 38 | + |
| 39 | + "high_log_expire_day": { |
| 40 | + Type: schema.TypeInt, |
| 41 | + Optional: true, |
| 42 | + Description: "Frequent log retention period.", |
| 43 | + }, |
| 44 | + |
| 45 | + "rule_template_ids": { |
| 46 | + Type: schema.TypeSet, |
| 47 | + Optional: true, |
| 48 | + Description: "Rule template ID set.", |
| 49 | + Elem: &schema.Schema{ |
| 50 | + Type: schema.TypeString, |
| 51 | + }, |
| 52 | + }, |
| 53 | + |
| 54 | + "audit_all": { |
| 55 | + Type: schema.TypeBool, |
| 56 | + Optional: true, |
| 57 | + Description: "Audit type. true - full audit; default false - rule-based audit.", |
| 58 | + }, |
| 59 | + }, |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func ResourceTencentCloudCynosdbAuditServiceCreate(d *schema.ResourceData, meta interface{}) error { |
| 64 | + defer tccommon.LogElapsed("resource.tencentcloud_cynosdb_audit_service.create")() |
| 65 | + defer tccommon.InconsistentCheck(d, meta)() |
| 66 | + |
| 67 | + var ( |
| 68 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 69 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 70 | + request = cynosdbv20190107.NewOpenAuditServiceRequest() |
| 71 | + instanceId string |
| 72 | + ) |
| 73 | + |
| 74 | + if v, ok := d.GetOk("instance_id"); ok { |
| 75 | + request.InstanceId = helper.String(v.(string)) |
| 76 | + instanceId = v.(string) |
| 77 | + } |
| 78 | + |
| 79 | + if v, ok := d.GetOkExists("log_expire_day"); ok { |
| 80 | + request.LogExpireDay = helper.IntUint64(v.(int)) |
| 81 | + } |
| 82 | + |
| 83 | + if v, ok := d.GetOkExists("high_log_expire_day"); ok { |
| 84 | + request.HighLogExpireDay = helper.IntUint64(v.(int)) |
| 85 | + } |
| 86 | + |
| 87 | + if v, ok := d.GetOk("rule_template_ids"); ok { |
| 88 | + ruleTemplateIdsSet := v.(*schema.Set).List() |
| 89 | + for i := range ruleTemplateIdsSet { |
| 90 | + ruleTemplateIds := ruleTemplateIdsSet[i].(string) |
| 91 | + request.RuleTemplateIds = append(request.RuleTemplateIds, helper.String(ruleTemplateIds)) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + if v, ok := d.GetOkExists("audit_all"); ok { |
| 96 | + request.AuditAll = helper.Bool(v.(bool)) |
| 97 | + } |
| 98 | + |
| 99 | + reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 100 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseCynosdbClient().OpenAuditServiceWithContext(ctx, request) |
| 101 | + if e != nil { |
| 102 | + return tccommon.RetryError(e) |
| 103 | + } else { |
| 104 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 105 | + } |
| 106 | + |
| 107 | + return nil |
| 108 | + }) |
| 109 | + |
| 110 | + if reqErr != nil { |
| 111 | + log.Printf("[CRITAL]%s create cynosdb audit service failed, reason:%+v", logId, reqErr) |
| 112 | + return reqErr |
| 113 | + } |
| 114 | + |
| 115 | + d.SetId(instanceId) |
| 116 | + |
| 117 | + // wait |
| 118 | + waitReq := cynosdbv20190107.NewDescribeAuditInstanceListRequest() |
| 119 | + waitReq.Offset = helper.Uint64(0) |
| 120 | + waitReq.Limit = helper.Uint64(1) |
| 121 | + waitReq.Filters = []*cynosdbv20190107.AuditInstanceFilters{ |
| 122 | + { |
| 123 | + Name: helper.String("InstanceId"), |
| 124 | + ExactMatch: helper.Bool(true), |
| 125 | + Values: helper.Strings([]string{instanceId}), |
| 126 | + }, |
| 127 | + } |
| 128 | + |
| 129 | + reqErr = resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 130 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseCynosdbClient().DescribeAuditInstanceListWithContext(ctx, waitReq) |
| 131 | + if e != nil { |
| 132 | + return tccommon.RetryError(e) |
| 133 | + } else { |
| 134 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, waitReq.GetAction(), waitReq.ToJsonString(), result.ToJsonString()) |
| 135 | + } |
| 136 | + |
| 137 | + if result == nil || result.Response == nil || result.Response.Items == nil || len(result.Response.Items) == 0 { |
| 138 | + return resource.NonRetryableError(fmt.Errorf("Describe cynosdb audit service failed, Response is nil.")) |
| 139 | + } |
| 140 | + |
| 141 | + item := result.Response.Items[0] |
| 142 | + if item.AuditStatus == nil { |
| 143 | + return resource.NonRetryableError(fmt.Errorf("AuditStatus is nil.")) |
| 144 | + } |
| 145 | + |
| 146 | + if *item.AuditStatus == "ON" { |
| 147 | + return nil |
| 148 | + } |
| 149 | + |
| 150 | + return resource.RetryableError(fmt.Errorf("cynosdb audit service is still running, audit status is %s.", *item.AuditStatus)) |
| 151 | + }) |
| 152 | + |
| 153 | + if reqErr != nil { |
| 154 | + log.Printf("[CRITAL]%s create cynosdb audit service failed, reason:%+v", logId, reqErr) |
| 155 | + return reqErr |
| 156 | + } |
| 157 | + |
| 158 | + return ResourceTencentCloudCynosdbAuditServiceRead(d, meta) |
| 159 | +} |
| 160 | + |
| 161 | +func ResourceTencentCloudCynosdbAuditServiceRead(d *schema.ResourceData, meta interface{}) error { |
| 162 | + defer tccommon.LogElapsed("resource.tencentcloud_cynosdb_audit_service.read")() |
| 163 | + defer tccommon.InconsistentCheck(d, meta)() |
| 164 | + |
| 165 | + var ( |
| 166 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 167 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 168 | + service = CynosdbService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 169 | + instanceId = d.Id() |
| 170 | + ) |
| 171 | + |
| 172 | + respData, err := service.DescribeCynosdbAuditServiceById(ctx, instanceId) |
| 173 | + if err != nil { |
| 174 | + return err |
| 175 | + } |
| 176 | + |
| 177 | + if respData == nil { |
| 178 | + log.Printf("[WARN]%s resource `tencentcloud_cynosdb_audit_service` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 179 | + d.SetId("") |
| 180 | + return nil |
| 181 | + } |
| 182 | + |
| 183 | + if respData.InstanceId != nil { |
| 184 | + _ = d.Set("instance_id", respData.InstanceId) |
| 185 | + } |
| 186 | + |
| 187 | + if respData.LogExpireDay != nil { |
| 188 | + _ = d.Set("log_expire_day", respData.LogExpireDay) |
| 189 | + } |
| 190 | + |
| 191 | + if respData.HighLogExpireDay != nil { |
| 192 | + _ = d.Set("high_log_expire_day", respData.HighLogExpireDay) |
| 193 | + } |
| 194 | + |
| 195 | + if respData.RuleTemplateIds != nil { |
| 196 | + _ = d.Set("rule_template_ids", respData.RuleTemplateIds) |
| 197 | + } |
| 198 | + |
| 199 | + if respData.AuditAll != nil { |
| 200 | + _ = d.Set("audit_all", respData.AuditAll) |
| 201 | + } |
| 202 | + |
| 203 | + return nil |
| 204 | +} |
| 205 | + |
| 206 | +func ResourceTencentCloudCynosdbAuditServiceUpdate(d *schema.ResourceData, meta interface{}) error { |
| 207 | + defer tccommon.LogElapsed("resource.tencentcloud_cynosdb_audit_service.update")() |
| 208 | + defer tccommon.InconsistentCheck(d, meta)() |
| 209 | + |
| 210 | + var ( |
| 211 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 212 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 213 | + instanceId = d.Id() |
| 214 | + ) |
| 215 | + |
| 216 | + needChange := false |
| 217 | + mutableArgs := []string{"log_expire_day", "high_log_expire_day", "rule_template_ids", "audit_all"} |
| 218 | + for _, v := range mutableArgs { |
| 219 | + if d.HasChange(v) { |
| 220 | + needChange = true |
| 221 | + break |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + if needChange { |
| 226 | + request := cynosdbv20190107.NewModifyAuditServiceRequest() |
| 227 | + if v, ok := d.GetOkExists("log_expire_day"); ok { |
| 228 | + request.LogExpireDay = helper.IntUint64(v.(int)) |
| 229 | + } |
| 230 | + |
| 231 | + if v, ok := d.GetOkExists("high_log_expire_day"); ok { |
| 232 | + request.HighLogExpireDay = helper.IntUint64(v.(int)) |
| 233 | + } |
| 234 | + |
| 235 | + if v, ok := d.GetOk("rule_template_ids"); ok { |
| 236 | + ruleTemplateIdsSet := v.(*schema.Set).List() |
| 237 | + for i := range ruleTemplateIdsSet { |
| 238 | + ruleTemplateIds := ruleTemplateIdsSet[i].(string) |
| 239 | + request.RuleTemplateIds = append(request.RuleTemplateIds, helper.String(ruleTemplateIds)) |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + if v, ok := d.GetOkExists("audit_all"); ok { |
| 244 | + request.AuditAll = helper.Bool(v.(bool)) |
| 245 | + } |
| 246 | + |
| 247 | + request.InstanceId = &instanceId |
| 248 | + reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 249 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseCynosdbClient().ModifyAuditServiceWithContext(ctx, request) |
| 250 | + if e != nil { |
| 251 | + return tccommon.RetryError(e) |
| 252 | + } else { |
| 253 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 254 | + } |
| 255 | + |
| 256 | + return nil |
| 257 | + }) |
| 258 | + |
| 259 | + if reqErr != nil { |
| 260 | + log.Printf("[CRITAL]%s update cynosdb audit service failed, reason:%+v", logId, reqErr) |
| 261 | + return reqErr |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + return ResourceTencentCloudCynosdbAuditServiceRead(d, meta) |
| 266 | +} |
| 267 | + |
| 268 | +func ResourceTencentCloudCynosdbAuditServiceDelete(d *schema.ResourceData, meta interface{}) error { |
| 269 | + defer tccommon.LogElapsed("resource.tencentcloud_cynosdb_audit_service.delete")() |
| 270 | + defer tccommon.InconsistentCheck(d, meta)() |
| 271 | + |
| 272 | + var ( |
| 273 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 274 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 275 | + request = cynosdbv20190107.NewCloseAuditServiceRequest() |
| 276 | + instanceId = d.Id() |
| 277 | + ) |
| 278 | + |
| 279 | + request.InstanceId = &instanceId |
| 280 | + reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 281 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseCynosdbClient().CloseAuditServiceWithContext(ctx, request) |
| 282 | + if e != nil { |
| 283 | + return tccommon.RetryError(e) |
| 284 | + } else { |
| 285 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 286 | + } |
| 287 | + |
| 288 | + return nil |
| 289 | + }) |
| 290 | + |
| 291 | + if reqErr != nil { |
| 292 | + log.Printf("[CRITAL]%s delete cynosdb audit service failed, reason:%+v", logId, reqErr) |
| 293 | + return reqErr |
| 294 | + } |
| 295 | + |
| 296 | + // wait |
| 297 | + waitReq := cynosdbv20190107.NewDescribeAuditInstanceListRequest() |
| 298 | + waitReq.Offset = helper.Uint64(0) |
| 299 | + waitReq.Limit = helper.Uint64(1) |
| 300 | + waitReq.Filters = []*cynosdbv20190107.AuditInstanceFilters{ |
| 301 | + { |
| 302 | + Name: helper.String("InstanceId"), |
| 303 | + ExactMatch: helper.Bool(true), |
| 304 | + Values: helper.Strings([]string{instanceId}), |
| 305 | + }, |
| 306 | + } |
| 307 | + |
| 308 | + reqErr = resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 309 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseCynosdbClient().DescribeAuditInstanceListWithContext(ctx, waitReq) |
| 310 | + if e != nil { |
| 311 | + return tccommon.RetryError(e) |
| 312 | + } else { |
| 313 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, waitReq.GetAction(), waitReq.ToJsonString(), result.ToJsonString()) |
| 314 | + } |
| 315 | + |
| 316 | + if result == nil || result.Response == nil || result.Response.Items == nil || len(result.Response.Items) == 0 { |
| 317 | + return resource.NonRetryableError(fmt.Errorf("Describe cynosdb audit service failed, Response is nil.")) |
| 318 | + } |
| 319 | + |
| 320 | + item := result.Response.Items[0] |
| 321 | + if item.AuditStatus == nil { |
| 322 | + return resource.NonRetryableError(fmt.Errorf("AuditStatus is nil.")) |
| 323 | + } |
| 324 | + |
| 325 | + if *item.AuditStatus == "OFF" { |
| 326 | + return nil |
| 327 | + } |
| 328 | + |
| 329 | + return resource.RetryableError(fmt.Errorf("cynosdb audit service is still running, audit status is %s.", *item.AuditStatus)) |
| 330 | + }) |
| 331 | + |
| 332 | + if reqErr != nil { |
| 333 | + log.Printf("[CRITAL]%s delete cynosdb audit service failed, reason:%+v", logId, reqErr) |
| 334 | + return reqErr |
| 335 | + } |
| 336 | + |
| 337 | + return nil |
| 338 | +} |
0 commit comments