|
| 1 | +package mongodb |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 8 | + mongodb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mongodb/v20190725" |
| 9 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 10 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 11 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/ratelimit" |
| 12 | +) |
| 13 | + |
| 14 | +func ResourceTencentCloudMongodbInstanceBackupRule() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Create: resourceTencentCloudMongodbInstanceBackupRuleCreate, |
| 17 | + Read: resourceTencentCloudMongodbInstanceBackupRuleRead, |
| 18 | + Update: resourceTencentCloudMongodbInstanceBackupRuleUpdate, |
| 19 | + Delete: resourceTencentCloudMongodbInstanceBackupRuleDelete, |
| 20 | + Importer: &schema.ResourceImporter{ |
| 21 | + State: schema.ImportStatePassthrough, |
| 22 | + }, |
| 23 | + Schema: map[string]*schema.Schema{ |
| 24 | + "instance_id": { |
| 25 | + Required: true, |
| 26 | + ForceNew: true, |
| 27 | + Type: schema.TypeString, |
| 28 | + Description: "Instance id.", |
| 29 | + }, |
| 30 | + |
| 31 | + "backup_method": { |
| 32 | + Required: true, |
| 33 | + Type: schema.TypeInt, |
| 34 | + Description: "Set automatic backup method. Valid values:\n" + |
| 35 | + "- 0: Logical backup;\n" + |
| 36 | + "- 1: Physical backup;\n" + |
| 37 | + "- 3: Snapshot backup (supported only in cloud disk version).", |
| 38 | + }, |
| 39 | + |
| 40 | + "backup_time": { |
| 41 | + Required: true, |
| 42 | + Type: schema.TypeInt, |
| 43 | + Description: "Set the start time for automatic backup. The value range is: [0,23]. For example, setting this parameter to 2 means that backup starts at 02:00.", |
| 44 | + }, |
| 45 | + |
| 46 | + "backup_retention_period": { |
| 47 | + Optional: true, |
| 48 | + Computed: true, |
| 49 | + Type: schema.TypeInt, |
| 50 | + Description: "Specify the number of days to save backup data. The default is 7 days, and the support settings are 7, 30, 90, 180, 365.", |
| 51 | + }, |
| 52 | + }, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func resourceTencentCloudMongodbInstanceBackupRuleCreate(d *schema.ResourceData, meta interface{}) error { |
| 57 | + defer tccommon.LogElapsed("resource.tencentcloud_mongodb_instance_backup_rule.create")() |
| 58 | + defer tccommon.InconsistentCheck(d, meta)() |
| 59 | + |
| 60 | + logId := tccommon.GetLogId(tccommon.ContextNil) |
| 61 | + var ( |
| 62 | + request = mongodb.NewSetBackupRulesRequest() |
| 63 | + ) |
| 64 | + instanceId := d.Get("instance_id").(string) |
| 65 | + request.InstanceId = helper.String(instanceId) |
| 66 | + |
| 67 | + if v, _ := d.GetOk("backup_method"); v != nil { |
| 68 | + request.BackupMethod = helper.IntUint64(v.(int)) |
| 69 | + } |
| 70 | + |
| 71 | + if v, _ := d.GetOk("backup_time"); v != nil { |
| 72 | + request.BackupTime = helper.IntUint64(v.(int)) |
| 73 | + } |
| 74 | + |
| 75 | + if v, ok := d.GetOkExists("backup_retention_period"); ok { |
| 76 | + request.BackupRetentionPeriod = helper.IntUint64(v.(int)) |
| 77 | + } |
| 78 | + |
| 79 | + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 80 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseMongodbClient().SetBackupRules(request) |
| 81 | + if e != nil { |
| 82 | + return tccommon.RetryError(e) |
| 83 | + } else { |
| 84 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 85 | + } |
| 86 | + return nil |
| 87 | + }) |
| 88 | + if err != nil { |
| 89 | + log.Printf("[CRITAL]%s operate mongodb backupRule failed, reason:%+v", logId, err) |
| 90 | + return err |
| 91 | + } |
| 92 | + |
| 93 | + d.SetId(instanceId) |
| 94 | + |
| 95 | + return resourceTencentCloudMongodbInstanceBackupRuleRead(d, meta) |
| 96 | +} |
| 97 | + |
| 98 | +func resourceTencentCloudMongodbInstanceBackupRuleRead(d *schema.ResourceData, meta interface{}) error { |
| 99 | + defer tccommon.LogElapsed("resource.tencentcloud_mongodb_instance_backup_rule.read")() |
| 100 | + defer tccommon.InconsistentCheck(d, meta)() |
| 101 | + |
| 102 | + request := mongodb.NewDescribeBackupRulesRequest() |
| 103 | + request.InstanceId = helper.String(d.Id()) |
| 104 | + ratelimit.Check(request.GetAction()) |
| 105 | + response, err := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseMongodbClient().DescribeBackupRules(request) |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + _ = d.Set("instance_id", d.Id()) |
| 110 | + _ = d.Set("backup_method", response.Response.BackupMethod) |
| 111 | + _ = d.Set("backup_time", response.Response.BackupTime) |
| 112 | + _ = d.Set("backup_retention_period", response.Response.BackupSaveTime) |
| 113 | + return nil |
| 114 | +} |
| 115 | + |
| 116 | +func resourceTencentCloudMongodbInstanceBackupRuleUpdate(d *schema.ResourceData, meta interface{}) error { |
| 117 | + defer tccommon.LogElapsed("resource.tencentcloud_mongodb_instance_transparent_data_encryption.update")() |
| 118 | + defer tccommon.InconsistentCheck(d, meta)() |
| 119 | + |
| 120 | + return resourceTencentCloudMongodbInstanceBackupRuleCreate(d, meta) |
| 121 | +} |
| 122 | + |
| 123 | +func resourceTencentCloudMongodbInstanceBackupRuleDelete(d *schema.ResourceData, meta interface{}) error { |
| 124 | + defer tccommon.LogElapsed("resource.tencentcloud_mongodb_instance_backup_rule.delete")() |
| 125 | + defer tccommon.InconsistentCheck(d, meta)() |
| 126 | + |
| 127 | + return nil |
| 128 | +} |
0 commit comments