|
| 1 | +/* |
| 2 | +Provides a resource to create a sqlserver full_backup_migration |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_sqlserver_full_backup_migration" "full_backup_migration" { |
| 8 | + instance_id = "mssql-i1z41iwd" |
| 9 | + recovery_type = "FULL" |
| 10 | + upload_type = "COS_URL" |
| 11 | + migration_name = "test_migration" |
| 12 | + backup_files = [] |
| 13 | +} |
| 14 | +``` |
| 15 | +
|
| 16 | +Import |
| 17 | +
|
| 18 | +sqlserver full_backup_migration can be imported using the id, e.g. |
| 19 | +
|
| 20 | +``` |
| 21 | +terraform import tencentcloud_sqlserver_full_backup_migration.full_backup_migration full_backup_migration_id |
| 22 | +``` |
| 23 | +*/ |
| 24 | +package tencentcloud |
| 25 | + |
| 26 | +import ( |
| 27 | + "context" |
| 28 | + "fmt" |
| 29 | + "log" |
| 30 | + |
| 31 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 32 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 33 | + sqlserver "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sqlserver/v20180328" |
| 34 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 35 | +) |
| 36 | + |
| 37 | +func resourceTencentCloudSqlserverFullBackupMigration() *schema.Resource { |
| 38 | + return &schema.Resource{ |
| 39 | + Create: resourceTencentCloudSqlserverFullBackupMigrationCreate, |
| 40 | + Read: resourceTencentCloudSqlserverFullBackupMigrationRead, |
| 41 | + Update: resourceTencentCloudSqlserverFullBackupMigrationUpdate, |
| 42 | + Delete: resourceTencentCloudSqlserverFullBackupMigrationDelete, |
| 43 | + Importer: &schema.ResourceImporter{ |
| 44 | + State: schema.ImportStatePassthrough, |
| 45 | + }, |
| 46 | + Schema: map[string]*schema.Schema{ |
| 47 | + "instance_id": { |
| 48 | + Required: true, |
| 49 | + Type: schema.TypeString, |
| 50 | + Description: "ID of imported target instance.", |
| 51 | + }, |
| 52 | + "recovery_type": { |
| 53 | + Required: true, |
| 54 | + Type: schema.TypeString, |
| 55 | + Description: "Migration task restoration type. FULL: full backup restoration, FULL_LOG: full backup and transaction log restoration, FULL_DIFF: full backup and differential backup restoration.", |
| 56 | + }, |
| 57 | + "upload_type": { |
| 58 | + Required: true, |
| 59 | + Type: schema.TypeString, |
| 60 | + Description: "Backup upload type. COS_URL: the backup is stored in users Cloud Object Storage, with URL provided. COS_UPLOAD: the backup is stored in the application Cloud Object Storage and needs to be uploaded by the user.", |
| 61 | + }, |
| 62 | + "migration_name": { |
| 63 | + Required: true, |
| 64 | + Type: schema.TypeString, |
| 65 | + Description: "Task name.", |
| 66 | + }, |
| 67 | + "backup_files": { |
| 68 | + Optional: true, |
| 69 | + Type: schema.TypeList, |
| 70 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 71 | + Description: "If the UploadType is COS_URL, fill in the URL here. If the UploadType is COS_UPLOAD, fill in the name of the backup file here. Only 1 backup file is supported, but a backup file can involve multiple databases.", |
| 72 | + }, |
| 73 | + "backup_migration_id": { |
| 74 | + Computed: true, |
| 75 | + Type: schema.TypeString, |
| 76 | + Description: "migration id.", |
| 77 | + }, |
| 78 | + "backup_migration_set": { |
| 79 | + Computed: true, |
| 80 | + Type: schema.TypeList, |
| 81 | + Description: "backup migration set.", |
| 82 | + Elem: &schema.Resource{ |
| 83 | + Schema: map[string]*schema.Schema{ |
| 84 | + "app_id": { |
| 85 | + Type: schema.TypeInt, |
| 86 | + Computed: true, |
| 87 | + Description: "app id.", |
| 88 | + }, |
| 89 | + "backup_files": { |
| 90 | + Type: schema.TypeList, |
| 91 | + Computed: true, |
| 92 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 93 | + Description: "backup files list.", |
| 94 | + }, |
| 95 | + "create_time": { |
| 96 | + Type: schema.TypeString, |
| 97 | + Computed: true, |
| 98 | + Description: "create time.", |
| 99 | + }, |
| 100 | + "end_time": { |
| 101 | + Type: schema.TypeString, |
| 102 | + Computed: true, |
| 103 | + Description: "end time.", |
| 104 | + }, |
| 105 | + "instance_id": { |
| 106 | + Type: schema.TypeString, |
| 107 | + Computed: true, |
| 108 | + Description: "instance id.", |
| 109 | + }, |
| 110 | + "is_recovery": { |
| 111 | + Type: schema.TypeString, |
| 112 | + Computed: true, |
| 113 | + Description: "Whether it is the final recovery, the field of the full import task is empty.", |
| 114 | + }, |
| 115 | + "message": { |
| 116 | + Type: schema.TypeString, |
| 117 | + Computed: true, |
| 118 | + Description: "msg.", |
| 119 | + }, |
| 120 | + "migration_id": { |
| 121 | + Type: schema.TypeString, |
| 122 | + Computed: true, |
| 123 | + Description: "migration id.", |
| 124 | + }, |
| 125 | + "migration_name": { |
| 126 | + Type: schema.TypeString, |
| 127 | + Computed: true, |
| 128 | + Description: "migration name.", |
| 129 | + }, |
| 130 | + "recovery_type": { |
| 131 | + Type: schema.TypeString, |
| 132 | + Computed: true, |
| 133 | + Description: "recovery type.", |
| 134 | + }, |
| 135 | + "region": { |
| 136 | + Type: schema.TypeString, |
| 137 | + Computed: true, |
| 138 | + Description: "region.", |
| 139 | + }, |
| 140 | + "start_time": { |
| 141 | + Type: schema.TypeString, |
| 142 | + Computed: true, |
| 143 | + Description: "start time.", |
| 144 | + }, |
| 145 | + "status": { |
| 146 | + Type: schema.TypeInt, |
| 147 | + Computed: true, |
| 148 | + Description: "Migration task status, 2-created, 7-full import, 8-waiting for increment, 9-import successful, 10-import failed, 12-incremental import.", |
| 149 | + }, |
| 150 | + "upload_type": { |
| 151 | + Type: schema.TypeString, |
| 152 | + Computed: true, |
| 153 | + Description: "Backup user upload type, COS_URL - the backup is placed on the user's object storage, and the URL is provided. COS_UPLOAD-The backup is placed on the object storage of the business, and the user uploads it.", |
| 154 | + }, |
| 155 | + }, |
| 156 | + }, |
| 157 | + }, |
| 158 | + }, |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +func resourceTencentCloudSqlserverFullBackupMigrationCreate(d *schema.ResourceData, meta interface{}) error { |
| 163 | + defer logElapsed("resource.tencentcloud_sqlserver_full_backup_migration.create")() |
| 164 | + defer inconsistentCheck(d, meta)() |
| 165 | + |
| 166 | + var ( |
| 167 | + logId = getLogId(contextNil) |
| 168 | + request = sqlserver.NewCreateBackupMigrationRequest() |
| 169 | + response = sqlserver.NewCreateBackupMigrationResponse() |
| 170 | + instanceId string |
| 171 | + backupMigrationId string |
| 172 | + ) |
| 173 | + |
| 174 | + if v, ok := d.GetOk("instance_id"); ok { |
| 175 | + request.InstanceId = helper.String(v.(string)) |
| 176 | + instanceId = v.(string) |
| 177 | + } |
| 178 | + |
| 179 | + if v, ok := d.GetOk("recovery_type"); ok { |
| 180 | + request.RecoveryType = helper.String(v.(string)) |
| 181 | + } |
| 182 | + |
| 183 | + if v, ok := d.GetOk("upload_type"); ok { |
| 184 | + request.UploadType = helper.String(v.(string)) |
| 185 | + } |
| 186 | + |
| 187 | + if v, ok := d.GetOk("migration_name"); ok { |
| 188 | + request.MigrationName = helper.String(v.(string)) |
| 189 | + } |
| 190 | + |
| 191 | + if v, ok := d.GetOk("backup_files"); ok { |
| 192 | + backupFilesSet := v.(*schema.Set).List() |
| 193 | + for i := range backupFilesSet { |
| 194 | + backupFiles := backupFilesSet[i].(string) |
| 195 | + request.BackupFiles = append(request.BackupFiles, &backupFiles) |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 200 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseSqlserverClient().CreateBackupMigration(request) |
| 201 | + if e != nil { |
| 202 | + return retryError(e) |
| 203 | + } else { |
| 204 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 205 | + } |
| 206 | + |
| 207 | + if result == nil { |
| 208 | + e = fmt.Errorf("sqlserver full backup migration %s not exists", instanceId) |
| 209 | + return resource.NonRetryableError(e) |
| 210 | + } |
| 211 | + |
| 212 | + response = result |
| 213 | + return nil |
| 214 | + }) |
| 215 | + |
| 216 | + if err != nil { |
| 217 | + log.Printf("[CRITAL]%s create sqlserver fullBackupMigration failed, reason:%+v", logId, err) |
| 218 | + return err |
| 219 | + } |
| 220 | + |
| 221 | + backupMigrationId = *response.Response.BackupMigrationId |
| 222 | + d.SetId(instanceId) |
| 223 | + _ = d.Set("backup_migration_id", backupMigrationId) |
| 224 | + |
| 225 | + return resourceTencentCloudSqlserverFullBackupMigrationRead(d, meta) |
| 226 | +} |
| 227 | + |
| 228 | +func resourceTencentCloudSqlserverFullBackupMigrationRead(d *schema.ResourceData, meta interface{}) error { |
| 229 | + defer logElapsed("resource.tencentcloud_sqlserver_full_backup_migration.read")() |
| 230 | + defer inconsistentCheck(d, meta)() |
| 231 | + |
| 232 | + var ( |
| 233 | + logId = getLogId(contextNil) |
| 234 | + ctx = context.WithValue(context.TODO(), logIdKey, logId) |
| 235 | + service = SqlserverService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 236 | + instanceId = d.Id() |
| 237 | + backupMigrationId = d.Get("backup_migration_id").(string) |
| 238 | + ) |
| 239 | + |
| 240 | + fullBackupMigration, err := service.DescribeSqlserverFullBackupMigrationById(ctx, instanceId, backupMigrationId) |
| 241 | + if err != nil { |
| 242 | + return err |
| 243 | + } |
| 244 | + |
| 245 | + if fullBackupMigration == nil { |
| 246 | + d.SetId("") |
| 247 | + log.Printf("[WARN]%s resource `SqlserverFullBackupMigration` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 248 | + return nil |
| 249 | + } |
| 250 | + |
| 251 | + list := make([]map[string]interface{}, 0) |
| 252 | + var infoMap = map[string]interface{}{} |
| 253 | + if fullBackupMigration.AppId != nil { |
| 254 | + infoMap["app_id"] = fullBackupMigration.AppId |
| 255 | + } |
| 256 | + if fullBackupMigration.BackupFiles != nil { |
| 257 | + infoMap["backup_files"] = fullBackupMigration.BackupFiles |
| 258 | + } |
| 259 | + if fullBackupMigration.CreateTime != nil { |
| 260 | + infoMap["create_time"] = fullBackupMigration.CreateTime |
| 261 | + } |
| 262 | + if fullBackupMigration.EndTime != nil { |
| 263 | + infoMap["end_time"] = fullBackupMigration.EndTime |
| 264 | + } |
| 265 | + if fullBackupMigration.InstanceId != nil { |
| 266 | + infoMap["instance_id"] = fullBackupMigration.InstanceId |
| 267 | + } |
| 268 | + if fullBackupMigration.IsRecovery != nil { |
| 269 | + infoMap["is_recovery"] = fullBackupMigration.IsRecovery |
| 270 | + } |
| 271 | + if fullBackupMigration.Message != nil { |
| 272 | + infoMap["message"] = fullBackupMigration.Message |
| 273 | + } |
| 274 | + if fullBackupMigration.MigrationId != nil { |
| 275 | + infoMap["migration_id"] = fullBackupMigration.MigrationId |
| 276 | + } |
| 277 | + if fullBackupMigration.MigrationName != nil { |
| 278 | + infoMap["migration_name"] = fullBackupMigration.MigrationName |
| 279 | + } |
| 280 | + if fullBackupMigration.RecoveryType != nil { |
| 281 | + infoMap["recovery_type"] = fullBackupMigration.RecoveryType |
| 282 | + } |
| 283 | + if fullBackupMigration.Region != nil { |
| 284 | + infoMap["region"] = fullBackupMigration.Region |
| 285 | + } |
| 286 | + if fullBackupMigration.StartTime != nil { |
| 287 | + infoMap["start_time"] = fullBackupMigration.StartTime |
| 288 | + } |
| 289 | + if fullBackupMigration.Status != nil { |
| 290 | + infoMap["status"] = fullBackupMigration.Status |
| 291 | + } |
| 292 | + if fullBackupMigration.UploadType != nil { |
| 293 | + infoMap["upload_type"] = fullBackupMigration.UploadType |
| 294 | + } |
| 295 | + |
| 296 | + list = append(list, infoMap) |
| 297 | + _ = d.Set("backup_migration_set", list) |
| 298 | + |
| 299 | + return nil |
| 300 | +} |
| 301 | + |
| 302 | +func resourceTencentCloudSqlserverFullBackupMigrationUpdate(d *schema.ResourceData, meta interface{}) error { |
| 303 | + defer logElapsed("resource.tencentcloud_sqlserver_full_backup_migration.update")() |
| 304 | + defer inconsistentCheck(d, meta)() |
| 305 | + |
| 306 | + var ( |
| 307 | + logId = getLogId(contextNil) |
| 308 | + request = sqlserver.NewModifyBackupMigrationRequest() |
| 309 | + instanceId = d.Id() |
| 310 | + backupMigrationId = d.Get("backup_migration_id").(string) |
| 311 | + ) |
| 312 | + |
| 313 | + request.InstanceId = &instanceId |
| 314 | + request.BackupMigrationId = &backupMigrationId |
| 315 | + |
| 316 | + if d.HasChange("migration_name") { |
| 317 | + if v, ok := d.GetOk("migration_name"); ok { |
| 318 | + request.MigrationName = helper.String(v.(string)) |
| 319 | + } |
| 320 | + } |
| 321 | + |
| 322 | + if d.HasChange("recovery_type") { |
| 323 | + if v, ok := d.GetOk("recovery_type"); ok { |
| 324 | + request.RecoveryType = helper.String(v.(string)) |
| 325 | + } |
| 326 | + } |
| 327 | + |
| 328 | + if d.HasChange("upload_type") { |
| 329 | + if v, ok := d.GetOk("upload_type"); ok { |
| 330 | + request.UploadType = helper.String(v.(string)) |
| 331 | + } |
| 332 | + } |
| 333 | + |
| 334 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 335 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseSqlserverClient().ModifyBackupMigration(request) |
| 336 | + if e != nil { |
| 337 | + return retryError(e) |
| 338 | + } else { |
| 339 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 340 | + } |
| 341 | + |
| 342 | + if result == nil { |
| 343 | + e = fmt.Errorf("update sqlserver full backup migration %s not exists", instanceId) |
| 344 | + return resource.NonRetryableError(e) |
| 345 | + } |
| 346 | + |
| 347 | + return nil |
| 348 | + }) |
| 349 | + |
| 350 | + if err != nil { |
| 351 | + log.Printf("[CRITAL]%s update sqlserver fullBackupMigration failed, reason:%+v", logId, err) |
| 352 | + return err |
| 353 | + } |
| 354 | + |
| 355 | + return resourceTencentCloudSqlserverFullBackupMigrationRead(d, meta) |
| 356 | +} |
| 357 | + |
| 358 | +func resourceTencentCloudSqlserverFullBackupMigrationDelete(d *schema.ResourceData, meta interface{}) error { |
| 359 | + defer logElapsed("resource.tencentcloud_sqlserver_full_backup_migration.delete")() |
| 360 | + defer inconsistentCheck(d, meta)() |
| 361 | + |
| 362 | + var ( |
| 363 | + logId = getLogId(contextNil) |
| 364 | + ctx = context.WithValue(context.TODO(), logIdKey, logId) |
| 365 | + service = SqlserverService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 366 | + instanceId = d.Id() |
| 367 | + backupMigrationId = d.Get("backup_migration_id").(string) |
| 368 | + ) |
| 369 | + |
| 370 | + if err := service.DeleteSqlserverFullBackupMigrationById(ctx, instanceId, backupMigrationId); err != nil { |
| 371 | + return err |
| 372 | + } |
| 373 | + |
| 374 | + return nil |
| 375 | +} |
0 commit comments