|
| 1 | +/* |
| 2 | +Provides a resource to create a mysql time_window |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_mysql_time_window" "time_window" { |
| 8 | + instance_id = "cdb-lw71b6ar" |
| 9 | + max_delay_time = 10 |
| 10 | + time_ranges = [ |
| 11 | + "01:00-02:01" |
| 12 | + ] |
| 13 | + weekdays = [ |
| 14 | + "friday", |
| 15 | + "monday", |
| 16 | + "saturday", |
| 17 | + "thursday", |
| 18 | + "tuesday", |
| 19 | + "wednesday", |
| 20 | + ] |
| 21 | +} |
| 22 | +``` |
| 23 | +
|
| 24 | +Import |
| 25 | +
|
| 26 | +mysql time_window can be imported using the id, e.g. |
| 27 | +
|
| 28 | +``` |
| 29 | +terraform import tencentcloud_mysql_time_window.time_window instanceId |
| 30 | +``` |
| 31 | +*/ |
| 32 | +package tencentcloud |
| 33 | + |
| 34 | +import ( |
| 35 | + "context" |
| 36 | + "log" |
| 37 | + |
| 38 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 39 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 40 | + mysql "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdb/v20170320" |
| 41 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 42 | +) |
| 43 | + |
| 44 | +func resourceTencentCloudMysqlTimeWindow() *schema.Resource { |
| 45 | + return &schema.Resource{ |
| 46 | + Create: resourceTencentCloudMysqlTimeWindowCreate, |
| 47 | + Read: resourceTencentCloudMysqlTimeWindowRead, |
| 48 | + Update: resourceTencentCloudMysqlTimeWindowUpdate, |
| 49 | + Delete: resourceTencentCloudMysqlTimeWindowDelete, |
| 50 | + Importer: &schema.ResourceImporter{ |
| 51 | + State: schema.ImportStatePassthrough, |
| 52 | + }, |
| 53 | + Schema: map[string]*schema.Schema{ |
| 54 | + "instance_id": { |
| 55 | + Required: true, |
| 56 | + Type: schema.TypeString, |
| 57 | + Description: "Instance ID in the format of cdb-c1nl9rpv or cdbro-c1nl9rpv. It is the same as the instance ID displayed on the TencentDB Console page.", |
| 58 | + }, |
| 59 | + |
| 60 | + "time_ranges": { |
| 61 | + Required: true, |
| 62 | + Type: schema.TypeSet, |
| 63 | + Elem: &schema.Schema{ |
| 64 | + Type: schema.TypeString, |
| 65 | + }, |
| 66 | + Description: "Time period available for maintenance after modification in the format of 10:00-12:00. Each period lasts from half an hour to three hours, with the start time and end time aligned by half-hour. Up to two time periods can be set. Start and end time range: [00:00, 24:00].", |
| 67 | + }, |
| 68 | + |
| 69 | + "weekdays": { |
| 70 | + Optional: true, |
| 71 | + Type: schema.TypeSet, |
| 72 | + Elem: &schema.Schema{ |
| 73 | + Type: schema.TypeString, |
| 74 | + }, |
| 75 | + Description: "Specifies for which day to modify the time period. Value range: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday. If it is not specified or is left blank, the time period will be modified for every day by default.", |
| 76 | + }, |
| 77 | + |
| 78 | + "max_delay_time": { |
| 79 | + Optional: true, |
| 80 | + Type: schema.TypeInt, |
| 81 | + Description: "Data delay threshold. It takes effect only for source instance and disaster recovery instance. Default value: 10.", |
| 82 | + }, |
| 83 | + }, |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func resourceTencentCloudMysqlTimeWindowCreate(d *schema.ResourceData, meta interface{}) error { |
| 88 | + defer logElapsed("resource.tencentcloud_mysql_time_window.create")() |
| 89 | + defer inconsistentCheck(d, meta)() |
| 90 | + |
| 91 | + d.SetId(d.Get("instance_id").(string)) |
| 92 | + |
| 93 | + return resourceTencentCloudMysqlTimeWindowUpdate(d, meta) |
| 94 | +} |
| 95 | + |
| 96 | +func resourceTencentCloudMysqlTimeWindowRead(d *schema.ResourceData, meta interface{}) error { |
| 97 | + defer logElapsed("resource.tencentcloud_mysql_time_window.read")() |
| 98 | + defer inconsistentCheck(d, meta)() |
| 99 | + |
| 100 | + logId := getLogId(contextNil) |
| 101 | + |
| 102 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 103 | + |
| 104 | + service := MysqlService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 105 | + |
| 106 | + instanceId := d.Id() |
| 107 | + |
| 108 | + timeWindow, err := service.DescribeMysqlTimeWindowById(ctx, instanceId) |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + |
| 113 | + if timeWindow == nil { |
| 114 | + d.SetId("") |
| 115 | + log.Printf("[WARN]%s resource `tencentcloud_mysql_time_window` [%s] not found, please check if it has been deleted.", |
| 116 | + logId, instanceId, |
| 117 | + ) |
| 118 | + return nil |
| 119 | + } |
| 120 | + |
| 121 | + var timeRanges []*string |
| 122 | + var weekdays []*string |
| 123 | + |
| 124 | + _ = d.Set("instance_id", instanceId) |
| 125 | + |
| 126 | + if *timeWindow.Response.Monday[0] != "00:00-00:00" { |
| 127 | + timeRanges = timeWindow.Response.Monday |
| 128 | + weekdays = append(weekdays, helper.String("monday")) |
| 129 | + } |
| 130 | + if *timeWindow.Response.Tuesday[0] != "00:00-00:00" { |
| 131 | + timeRanges = timeWindow.Response.Tuesday |
| 132 | + weekdays = append(weekdays, helper.String("tuesday")) |
| 133 | + } |
| 134 | + if *timeWindow.Response.Wednesday[0] != "00:00-00:00" { |
| 135 | + timeRanges = timeWindow.Response.Wednesday |
| 136 | + weekdays = append(weekdays, helper.String("wednesday")) |
| 137 | + } |
| 138 | + if *timeWindow.Response.Thursday[0] != "00:00-00:00" { |
| 139 | + timeRanges = timeWindow.Response.Thursday |
| 140 | + weekdays = append(weekdays, helper.String("thursday")) |
| 141 | + } |
| 142 | + if *timeWindow.Response.Friday[0] != "00:00-00:00" { |
| 143 | + timeRanges = timeWindow.Response.Friday |
| 144 | + weekdays = append(weekdays, helper.String("friday")) |
| 145 | + } |
| 146 | + if *timeWindow.Response.Saturday[0] != "00:00-00:00" { |
| 147 | + timeRanges = timeWindow.Response.Saturday |
| 148 | + weekdays = append(weekdays, helper.String("saturday")) |
| 149 | + } |
| 150 | + if *timeWindow.Response.Wednesday[0] != "00:00-00:00" { |
| 151 | + timeRanges = timeWindow.Response.Wednesday |
| 152 | + weekdays = append(weekdays, helper.String("wednesday")) |
| 153 | + } |
| 154 | + |
| 155 | + if timeRanges != nil { |
| 156 | + _ = d.Set("time_ranges", timeRanges) |
| 157 | + } |
| 158 | + |
| 159 | + if weekdays != nil { |
| 160 | + _ = d.Set("weekdays", weekdays) |
| 161 | + } |
| 162 | + |
| 163 | + if timeWindow.Response.MaxDelayTime != nil { |
| 164 | + _ = d.Set("max_delay_time", timeWindow.Response.MaxDelayTime) |
| 165 | + } |
| 166 | + |
| 167 | + return nil |
| 168 | +} |
| 169 | + |
| 170 | +func resourceTencentCloudMysqlTimeWindowUpdate(d *schema.ResourceData, meta interface{}) error { |
| 171 | + defer logElapsed("resource.tencentcloud_mysql_time_window.update")() |
| 172 | + defer inconsistentCheck(d, meta)() |
| 173 | + |
| 174 | + logId := getLogId(contextNil) |
| 175 | + |
| 176 | + request := mysql.NewModifyTimeWindowRequest() |
| 177 | + |
| 178 | + instanceId := d.Id() |
| 179 | + |
| 180 | + request.InstanceId = &instanceId |
| 181 | + |
| 182 | + if v, ok := d.GetOk("time_ranges"); ok { |
| 183 | + timeRangesSet := v.(*schema.Set).List() |
| 184 | + for i := range timeRangesSet { |
| 185 | + timeRange := timeRangesSet[i].(string) |
| 186 | + request.TimeRanges = append(request.TimeRanges, &timeRange) |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + if v, ok := d.GetOk("weekdays"); ok { |
| 191 | + weekdaysSet := v.(*schema.Set).List() |
| 192 | + for i := range weekdaysSet { |
| 193 | + weekday := weekdaysSet[i].(string) |
| 194 | + request.Weekdays = append(request.Weekdays, &weekday) |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + if d.HasChange("max_delay_time") { |
| 199 | + if v, _ := d.GetOk("max_delay_time"); v != nil { |
| 200 | + request.MaxDelayTime = helper.IntUint64(v.(int)) |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 205 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseMysqlClient().ModifyTimeWindow(request) |
| 206 | + if e != nil { |
| 207 | + return retryError(e) |
| 208 | + } else { |
| 209 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 210 | + } |
| 211 | + return nil |
| 212 | + }) |
| 213 | + if err != nil { |
| 214 | + d.SetId("") |
| 215 | + log.Printf("[CRITAL]%s update mysql timeWindow failed, reason:%+v", logId, err) |
| 216 | + return err |
| 217 | + } |
| 218 | + |
| 219 | + return resourceTencentCloudMysqlTimeWindowRead(d, meta) |
| 220 | +} |
| 221 | + |
| 222 | +func resourceTencentCloudMysqlTimeWindowDelete(d *schema.ResourceData, meta interface{}) error { |
| 223 | + defer logElapsed("resource.tencentcloud_mysql_time_window.delete")() |
| 224 | + defer inconsistentCheck(d, meta)() |
| 225 | + |
| 226 | + logId := getLogId(contextNil) |
| 227 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 228 | + |
| 229 | + service := MysqlService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 230 | + instanceId := d.Id() |
| 231 | + |
| 232 | + if err := service.DeleteMysqlTimeWindowById(ctx, instanceId); err != nil { |
| 233 | + return err |
| 234 | + } |
| 235 | + |
| 236 | + return nil |
| 237 | +} |
0 commit comments