|
| 1 | +/* |
| 2 | +Provides a resource to create a cfs auto_snapshot_policy |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_cfs_auto_snapshot_policy" "auto_snapshot_policy" { |
| 8 | + day_of_week = "1,2" |
| 9 | + hour = "2,3" |
| 10 | + policy_name = "policy_name" |
| 11 | + alive_days = 7 |
| 12 | +} |
| 13 | +``` |
| 14 | +
|
| 15 | +Import |
| 16 | +
|
| 17 | +cfs auto_snapshot_policy can be imported using the id, e.g. |
| 18 | +
|
| 19 | +``` |
| 20 | +terraform import tencentcloud_cfs_auto_snapshot_policy.auto_snapshot_policy auto_snapshot_policy_id |
| 21 | +``` |
| 22 | +*/ |
| 23 | +package tencentcloud |
| 24 | + |
| 25 | +import ( |
| 26 | + "context" |
| 27 | + "fmt" |
| 28 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 29 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 30 | + cfs "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cfs/v20190719" |
| 31 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 32 | + "log" |
| 33 | +) |
| 34 | + |
| 35 | +func resourceTencentCloudCfsAutoSnapshotPolicy() *schema.Resource { |
| 36 | + return &schema.Resource{ |
| 37 | + Create: resourceTencentCloudCfsAutoSnapshotPolicyCreate, |
| 38 | + Read: resourceTencentCloudCfsAutoSnapshotPolicyRead, |
| 39 | + Update: resourceTencentCloudCfsAutoSnapshotPolicyUpdate, |
| 40 | + Delete: resourceTencentCloudCfsAutoSnapshotPolicyDelete, |
| 41 | + Importer: &schema.ResourceImporter{ |
| 42 | + State: schema.ImportStatePassthrough, |
| 43 | + }, |
| 44 | + Schema: map[string]*schema.Schema{ |
| 45 | + "day_of_week": { |
| 46 | + Required: true, |
| 47 | + Type: schema.TypeString, |
| 48 | + Description: "The day of the week on which to repeat the snapshot operation.", |
| 49 | + }, |
| 50 | + |
| 51 | + "hour": { |
| 52 | + Required: true, |
| 53 | + Type: schema.TypeString, |
| 54 | + Description: "The time point when to repeat the snapshot operation.", |
| 55 | + }, |
| 56 | + |
| 57 | + "policy_name": { |
| 58 | + Optional: true, |
| 59 | + Type: schema.TypeString, |
| 60 | + Description: "Policy name.", |
| 61 | + }, |
| 62 | + |
| 63 | + "alive_days": { |
| 64 | + Optional: true, |
| 65 | + Type: schema.TypeInt, |
| 66 | + Description: "Snapshot retention period.", |
| 67 | + }, |
| 68 | + }, |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func resourceTencentCloudCfsAutoSnapshotPolicyCreate(d *schema.ResourceData, meta interface{}) error { |
| 73 | + defer logElapsed("resource.tencentcloud_cfs_auto_snapshot_policy.create")() |
| 74 | + defer inconsistentCheck(d, meta)() |
| 75 | + |
| 76 | + logId := getLogId(contextNil) |
| 77 | + |
| 78 | + var ( |
| 79 | + request = cfs.NewCreateAutoSnapshotPolicyRequest() |
| 80 | + response = cfs.NewCreateAutoSnapshotPolicyResponse() |
| 81 | + autoSnapshotPolicyId string |
| 82 | + ) |
| 83 | + if v, ok := d.GetOk("day_of_week"); ok { |
| 84 | + request.DayOfWeek = helper.String(v.(string)) |
| 85 | + } |
| 86 | + |
| 87 | + if v, ok := d.GetOk("hour"); ok { |
| 88 | + request.Hour = helper.String(v.(string)) |
| 89 | + } |
| 90 | + |
| 91 | + if v, ok := d.GetOk("policy_name"); ok { |
| 92 | + request.PolicyName = helper.String(v.(string)) |
| 93 | + } |
| 94 | + |
| 95 | + if v, _ := d.GetOk("alive_days"); v != nil { |
| 96 | + request.AliveDays = helper.IntUint64(v.(int)) |
| 97 | + } |
| 98 | + |
| 99 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 100 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseCfsClient().CreateAutoSnapshotPolicy(request) |
| 101 | + if e != nil { |
| 102 | + return 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 | + response = result |
| 107 | + return nil |
| 108 | + }) |
| 109 | + if err != nil { |
| 110 | + log.Printf("[CRITAL]%s create cfs autoSnapshotPolicy failed, reason:%+v", logId, err) |
| 111 | + return err |
| 112 | + } |
| 113 | + |
| 114 | + autoSnapshotPolicyId = *response.Response.AutoSnapshotPolicyId |
| 115 | + d.SetId(autoSnapshotPolicyId) |
| 116 | + |
| 117 | + return resourceTencentCloudCfsAutoSnapshotPolicyRead(d, meta) |
| 118 | +} |
| 119 | + |
| 120 | +func resourceTencentCloudCfsAutoSnapshotPolicyRead(d *schema.ResourceData, meta interface{}) error { |
| 121 | + defer logElapsed("resource.tencentcloud_cfs_auto_snapshot_policy.read")() |
| 122 | + defer inconsistentCheck(d, meta)() |
| 123 | + |
| 124 | + logId := getLogId(contextNil) |
| 125 | + |
| 126 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 127 | + |
| 128 | + service := CfsService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 129 | + |
| 130 | + autoSnapshotPolicyId := d.Id() |
| 131 | + |
| 132 | + autoSnapshotPolicy, err := service.DescribeCfsAutoSnapshotPolicyById(ctx, autoSnapshotPolicyId) |
| 133 | + if err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + |
| 137 | + if autoSnapshotPolicy == nil { |
| 138 | + d.SetId("") |
| 139 | + return fmt.Errorf("resource `track` %s does not exist", d.Id()) |
| 140 | + } |
| 141 | + |
| 142 | + if autoSnapshotPolicy.DayOfWeek != nil { |
| 143 | + _ = d.Set("day_of_week", autoSnapshotPolicy.DayOfWeek) |
| 144 | + } |
| 145 | + |
| 146 | + if autoSnapshotPolicy.Hour != nil { |
| 147 | + _ = d.Set("hour", autoSnapshotPolicy.Hour) |
| 148 | + } |
| 149 | + |
| 150 | + if autoSnapshotPolicy.PolicyName != nil { |
| 151 | + _ = d.Set("policy_name", autoSnapshotPolicy.PolicyName) |
| 152 | + } |
| 153 | + |
| 154 | + if autoSnapshotPolicy.AliveDays != nil { |
| 155 | + _ = d.Set("alive_days", autoSnapshotPolicy.AliveDays) |
| 156 | + } |
| 157 | + |
| 158 | + return nil |
| 159 | +} |
| 160 | + |
| 161 | +func resourceTencentCloudCfsAutoSnapshotPolicyUpdate(d *schema.ResourceData, meta interface{}) error { |
| 162 | + defer logElapsed("resource.tencentcloud_cfs_auto_snapshot_policy.update")() |
| 163 | + defer inconsistentCheck(d, meta)() |
| 164 | + |
| 165 | + logId := getLogId(contextNil) |
| 166 | + |
| 167 | + request := cfs.NewUpdateAutoSnapshotPolicyRequest() |
| 168 | + |
| 169 | + autoSnapshotPolicyId := d.Id() |
| 170 | + |
| 171 | + request.AutoSnapshotPolicyId = &autoSnapshotPolicyId |
| 172 | + if d.HasChange("day_of_week") { |
| 173 | + if v, ok := d.GetOk("day_of_week"); ok { |
| 174 | + request.DayOfWeek = helper.String(v.(string)) |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + if d.HasChange("hour") { |
| 179 | + if v, ok := d.GetOk("hour"); ok { |
| 180 | + request.Hour = helper.String(v.(string)) |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + if d.HasChange("policy_name") { |
| 185 | + if v, ok := d.GetOk("policy_name"); ok { |
| 186 | + request.PolicyName = helper.String(v.(string)) |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + if d.HasChange("alive_days") { |
| 191 | + if v, _ := d.GetOk("alive_days"); v != nil { |
| 192 | + request.AliveDays = helper.IntUint64(v.(int)) |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 197 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseCfsClient().UpdateAutoSnapshotPolicy(request) |
| 198 | + if e != nil { |
| 199 | + return retryError(e) |
| 200 | + } else { |
| 201 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 202 | + } |
| 203 | + return nil |
| 204 | + }) |
| 205 | + if err != nil { |
| 206 | + log.Printf("[CRITAL]%s create cfs autoSnapshotPolicy failed, reason:%+v", logId, err) |
| 207 | + return err |
| 208 | + } |
| 209 | + |
| 210 | + return resourceTencentCloudCfsAutoSnapshotPolicyRead(d, meta) |
| 211 | +} |
| 212 | + |
| 213 | +func resourceTencentCloudCfsAutoSnapshotPolicyDelete(d *schema.ResourceData, meta interface{}) error { |
| 214 | + defer logElapsed("resource.tencentcloud_cfs_auto_snapshot_policy.delete")() |
| 215 | + defer inconsistentCheck(d, meta)() |
| 216 | + |
| 217 | + logId := getLogId(contextNil) |
| 218 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 219 | + |
| 220 | + service := CfsService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 221 | + autoSnapshotPolicyId := d.Id() |
| 222 | + |
| 223 | + if err := service.DeleteCfsAutoSnapshotPolicyById(ctx, autoSnapshotPolicyId); err != nil { |
| 224 | + return err |
| 225 | + } |
| 226 | + |
| 227 | + return nil |
| 228 | +} |
0 commit comments