|
| 1 | +/* |
| 2 | +Provides a resource to create a mysql backup_encryption_status |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_mysql_backup_encryption_status" "backup_encryption_status" { |
| 8 | + instance_id = "cdb-c1nl9rpv" |
| 9 | + encryption_status = "on" |
| 10 | +} |
| 11 | +``` |
| 12 | +
|
| 13 | +Import |
| 14 | +
|
| 15 | +mysql backup_encryption_status can be imported using the id, e.g. |
| 16 | +
|
| 17 | +``` |
| 18 | +terraform import tencentcloud_mysql_backup_encryption_status.backup_encryption_status backup_encryption_status_id |
| 19 | +``` |
| 20 | +*/ |
| 21 | +package tencentcloud |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "log" |
| 26 | + |
| 27 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 28 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 29 | + mysql "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdb/v20170320" |
| 30 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 31 | +) |
| 32 | + |
| 33 | +func resourceTencentCloudMysqlBackupEncryptionStatus() *schema.Resource { |
| 34 | + return &schema.Resource{ |
| 35 | + Create: resourceTencentCloudMysqlBackupEncryptionStatusCreate, |
| 36 | + Read: resourceTencentCloudMysqlBackupEncryptionStatusRead, |
| 37 | + Update: resourceTencentCloudMysqlBackupEncryptionStatusUpdate, |
| 38 | + Delete: resourceTencentCloudMysqlBackupEncryptionStatusDelete, |
| 39 | + Importer: &schema.ResourceImporter{ |
| 40 | + State: schema.ImportStatePassthrough, |
| 41 | + }, |
| 42 | + Schema: map[string]*schema.Schema{ |
| 43 | + "instance_id": { |
| 44 | + Required: true, |
| 45 | + Type: schema.TypeString, |
| 46 | + Description: "Instance ID, in the format: cdb-XXXX. Same instance ID as displayed in the ApsaraDB for Console page.", |
| 47 | + }, |
| 48 | + |
| 49 | + "encryption_status": { |
| 50 | + Required: true, |
| 51 | + Type: schema.TypeString, |
| 52 | + Description: "Whether physical backup encryption is enabled for the instance. Possible values are `on`, `off`.", |
| 53 | + }, |
| 54 | + }, |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func resourceTencentCloudMysqlBackupEncryptionStatusCreate(d *schema.ResourceData, meta interface{}) error { |
| 59 | + defer logElapsed("resource.tencentcloud_mysql_backup_encryption_status.create")() |
| 60 | + defer inconsistentCheck(d, meta)() |
| 61 | + |
| 62 | + var instanceId string |
| 63 | + if v, ok := d.GetOk("instance_id"); ok { |
| 64 | + instanceId = v.(string) |
| 65 | + } |
| 66 | + |
| 67 | + d.SetId(instanceId) |
| 68 | + |
| 69 | + return resourceTencentCloudMysqlBackupEncryptionStatusUpdate(d, meta) |
| 70 | +} |
| 71 | + |
| 72 | +func resourceTencentCloudMysqlBackupEncryptionStatusRead(d *schema.ResourceData, meta interface{}) error { |
| 73 | + defer logElapsed("resource.tencentcloud_mysql_backup_encryption_status.read")() |
| 74 | + defer inconsistentCheck(d, meta)() |
| 75 | + |
| 76 | + logId := getLogId(contextNil) |
| 77 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 78 | + |
| 79 | + service := MysqlService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 80 | + |
| 81 | + instanceId := d.Id() |
| 82 | + |
| 83 | + backupEncryptionStatus, err := service.DescribeMysqlBackupEncryptionStatusById(ctx, instanceId) |
| 84 | + if err != nil { |
| 85 | + return err |
| 86 | + } |
| 87 | + |
| 88 | + if backupEncryptionStatus == nil { |
| 89 | + d.SetId("") |
| 90 | + log.Printf("[WARN]%s resource `MysqlBackupEncryptionStatus` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 91 | + return nil |
| 92 | + } |
| 93 | + |
| 94 | + _ = d.Set("instance_id", instanceId) |
| 95 | + |
| 96 | + if backupEncryptionStatus.EncryptionStatus != nil { |
| 97 | + _ = d.Set("encryption_status", backupEncryptionStatus.EncryptionStatus) |
| 98 | + } |
| 99 | + |
| 100 | + return nil |
| 101 | +} |
| 102 | + |
| 103 | +func resourceTencentCloudMysqlBackupEncryptionStatusUpdate(d *schema.ResourceData, meta interface{}) error { |
| 104 | + defer logElapsed("resource.tencentcloud_mysql_backup_encryption_status.update")() |
| 105 | + defer inconsistentCheck(d, meta)() |
| 106 | + |
| 107 | + logId := getLogId(contextNil) |
| 108 | + request := mysql.NewModifyBackupEncryptionStatusRequest() |
| 109 | + |
| 110 | + instanceId := d.Id() |
| 111 | + |
| 112 | + request.InstanceId = &instanceId |
| 113 | + |
| 114 | + if v, ok := d.GetOk("encryption_status"); ok { |
| 115 | + request.EncryptionStatus = helper.String(v.(string)) |
| 116 | + } |
| 117 | + |
| 118 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 119 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseMysqlClient().ModifyBackupEncryptionStatus(request) |
| 120 | + if e != nil { |
| 121 | + return retryError(e) |
| 122 | + } else { |
| 123 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 124 | + } |
| 125 | + return nil |
| 126 | + }) |
| 127 | + if err != nil { |
| 128 | + log.Printf("[CRITAL]%s update mysql backupEncryptionStatus failed, reason:%+v", logId, err) |
| 129 | + return err |
| 130 | + } |
| 131 | + |
| 132 | + return resourceTencentCloudMysqlBackupEncryptionStatusRead(d, meta) |
| 133 | +} |
| 134 | + |
| 135 | +func resourceTencentCloudMysqlBackupEncryptionStatusDelete(d *schema.ResourceData, meta interface{}) error { |
| 136 | + defer logElapsed("resource.tencentcloud_mysql_backup_encryption_status.delete")() |
| 137 | + defer inconsistentCheck(d, meta)() |
| 138 | + |
| 139 | + return nil |
| 140 | +} |
0 commit comments