Skip to content

Commit f584ed8

Browse files
committed
add snapshot attach
1 parent c24160e commit f584ed8

8 files changed

+341
-1
lines changed

.changelog/1481.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```release-note:new-resource
2+
tencentcloud_cfs_auto_snapshot_policy
3+
```
4+
5+
```release-note:new-resource
6+
tencentcloud_cfs_auto_snapshot_policy_attachment
7+
```

tencentcloud/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ Cloud File Storage(CFS)
224224
tencentcloud_cfs_access_group
225225
tencentcloud_cfs_access_rule
226226
tencentcloud_cfs_auto_snapshot_policy
227+
tencentcloud_cfs_auto_snapshot_policy_attachment
227228
228229
Container Cluster
229230
Data Source
@@ -1298,6 +1299,7 @@ func Provider() terraform.ResourceProvider {
12981299
"tencentcloud_cfs_access_group": resourceTencentCloudCfsAccessGroup(),
12991300
"tencentcloud_cfs_access_rule": resourceTencentCloudCfsAccessRule(),
13001301
"tencentcloud_cfs_auto_snapshot_policy": resourceTencentCloudCfsAutoSnapshotPolicy(),
1302+
"tencentcloud_cfs_auto_snapshot_policy_attachment": resourceTencentCloudCfsAutoSnapshotPolicyAttachment(),
13011303
"tencentcloud_redis_instance": resourceTencentCloudRedisInstance(),
13021304
"tencentcloud_redis_backup_config": resourceTencentCloudRedisBackupConfig(),
13031305
"tencentcloud_redis_param_template": resourceTencentCloudRedisParamTemplate(),

tencentcloud/resource_tc_cfs_auto_snapshot_policy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func resourceTencentCloudCfsAutoSnapshotPolicyRead(d *schema.ResourceData, meta
136136

137137
if autoSnapshotPolicy == nil {
138138
d.SetId("")
139-
return fmt.Errorf("resource `track` %s does not exist", d.Id())
139+
return fmt.Errorf("resource `tencentcloud_cfs_auto_snapshot_policy` %s does not exist", d.Id())
140140
}
141141

142142
if autoSnapshotPolicy.DayOfWeek != nil {
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*
2+
Provides a resource to create a cfs auto_snapshot_policy_attachment
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_cfs_auto_snapshot_policy_attachment" "auto_snapshot_policy_attachment" {
8+
auto_snapshot_policy_id = "asp-basic"
9+
file_system_ids = "cfs-4xzkct19,cfs-iobiaxtj"
10+
}
11+
```
12+
13+
Import
14+
15+
cfs auto_snapshot_policy_attachment can be imported using the id, e.g.
16+
17+
```
18+
terraform import tencentcloud_cfs_auto_snapshot_policy_attachment.auto_snapshot_policy_attachment auto_snapshot_policy_id#file_system_ids
19+
```
20+
*/
21+
package tencentcloud
22+
23+
import (
24+
"context"
25+
"fmt"
26+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
27+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
28+
cfs "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cfs/v20190719"
29+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
30+
"log"
31+
"strings"
32+
)
33+
34+
func resourceTencentCloudCfsAutoSnapshotPolicyAttachment() *schema.Resource {
35+
return &schema.Resource{
36+
Create: resourceTencentCloudCfsAutoSnapshotPolicyAttachmentCreate,
37+
Read: resourceTencentCloudCfsAutoSnapshotPolicyAttachmentRead,
38+
Delete: resourceTencentCloudCfsAutoSnapshotPolicyAttachmentDelete,
39+
Importer: &schema.ResourceImporter{
40+
State: schema.ImportStatePassthrough,
41+
},
42+
Schema: map[string]*schema.Schema{
43+
"auto_snapshot_policy_id": {
44+
Required: true,
45+
ForceNew: true,
46+
Type: schema.TypeString,
47+
Description: "ID of the snapshot to be unbound.",
48+
},
49+
50+
"file_system_ids": {
51+
Required: true,
52+
ForceNew: true,
53+
Type: schema.TypeString,
54+
Description: "List of IDs of the file systems to be unbound, separated by comma.",
55+
},
56+
},
57+
}
58+
}
59+
60+
func resourceTencentCloudCfsAutoSnapshotPolicyAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
61+
defer logElapsed("resource.tencentcloud_cfs_auto_snapshot_policy_attachment.create")()
62+
defer inconsistentCheck(d, meta)()
63+
64+
logId := getLogId(contextNil)
65+
66+
var (
67+
request = cfs.NewBindAutoSnapshotPolicyRequest()
68+
autoSnapshotPolicyId string
69+
fileSystemIds string
70+
)
71+
if v, ok := d.GetOk("auto_snapshot_policy_id"); ok {
72+
autoSnapshotPolicyId = v.(string)
73+
request.AutoSnapshotPolicyId = helper.String(v.(string))
74+
}
75+
76+
if v, ok := d.GetOk("file_system_ids"); ok {
77+
fileSystemIds = v.(string)
78+
request.FileSystemIds = helper.String(v.(string))
79+
}
80+
81+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
82+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseCfsClient().BindAutoSnapshotPolicy(request)
83+
if e != nil {
84+
return retryError(e)
85+
} else {
86+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
87+
}
88+
return nil
89+
})
90+
if err != nil {
91+
log.Printf("[CRITAL]%s create cfs autoSnapshotPolicyAttachment failed, reason:%+v", logId, err)
92+
return err
93+
}
94+
95+
d.SetId(autoSnapshotPolicyId + FILED_SP + fileSystemIds)
96+
97+
return resourceTencentCloudCfsAutoSnapshotPolicyAttachmentRead(d, meta)
98+
}
99+
100+
func resourceTencentCloudCfsAutoSnapshotPolicyAttachmentRead(d *schema.ResourceData, meta interface{}) error {
101+
defer logElapsed("resource.tencentcloud_cfs_auto_snapshot_policy_attachment.read")()
102+
defer inconsistentCheck(d, meta)()
103+
104+
logId := getLogId(contextNil)
105+
106+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
107+
108+
service := CfsService{client: meta.(*TencentCloudClient).apiV3Conn}
109+
110+
idSplit := strings.Split(d.Id(), FILED_SP)
111+
if len(idSplit) != 2 {
112+
return fmt.Errorf("id is broken,%s", d.Id())
113+
}
114+
autoSnapshotPolicyId := idSplit[0]
115+
fileSystemIds := idSplit[1]
116+
117+
autoSnapshotPolicyAttachment, err := service.DescribeCfsAutoSnapshotPolicyAttachmentById(ctx, autoSnapshotPolicyId, fileSystemIds)
118+
if err != nil {
119+
return err
120+
}
121+
122+
if autoSnapshotPolicyAttachment == nil {
123+
d.SetId("")
124+
return fmt.Errorf("resource `tencentcloud_cfs_auto_snapshot_policy_attachment` %s does not exist", d.Id())
125+
}
126+
127+
if autoSnapshotPolicyAttachment.AutoSnapshotPolicyId != nil {
128+
_ = d.Set("auto_snapshot_policy_id", autoSnapshotPolicyId)
129+
}
130+
131+
if autoSnapshotPolicyAttachment.FileSystems != nil {
132+
_ = d.Set("file_system_ids", fileSystemIds)
133+
}
134+
135+
return nil
136+
}
137+
138+
func resourceTencentCloudCfsAutoSnapshotPolicyAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
139+
defer logElapsed("resource.tencentcloud_cfs_auto_snapshot_policy_attachment.delete")()
140+
defer inconsistentCheck(d, meta)()
141+
142+
logId := getLogId(contextNil)
143+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
144+
145+
service := CfsService{client: meta.(*TencentCloudClient).apiV3Conn}
146+
idSplit := strings.Split(d.Id(), FILED_SP)
147+
if len(idSplit) != 2 {
148+
return fmt.Errorf("id is broken,%s", d.Id())
149+
}
150+
autoSnapshotPolicyId := idSplit[0]
151+
fileSystemIds := idSplit[1]
152+
153+
if err := service.DeleteCfsAutoSnapshotPolicyAttachmentById(ctx, autoSnapshotPolicyId, fileSystemIds); err != nil {
154+
return err
155+
}
156+
157+
return nil
158+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package tencentcloud
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
5+
"testing"
6+
)
7+
8+
func TestAccTencentCloudCfsAutoSnapshotPolicyAttachmentResource_basic(t *testing.T) {
9+
t.Parallel()
10+
resource.Test(t, resource.TestCase{
11+
PreCheck: func() {
12+
testAccPreCheck(t)
13+
},
14+
Providers: testAccProviders,
15+
Steps: []resource.TestStep{
16+
{
17+
Config: testAccCfsAutoSnapshotPolicyAttachment,
18+
Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttrSet("tencentcloud_cfs_auto_snapshot_policy_attachment.auto_snapshot_policy_attachment", "id")),
19+
},
20+
{
21+
ResourceName: "tencentcloud_cfs_auto_snapshot_policy_attachment.auto_snapshot_policy_attachment",
22+
ImportState: true,
23+
ImportStateVerify: true,
24+
},
25+
},
26+
})
27+
}
28+
29+
const testAccCfsAutoSnapshotPolicyAttachment = `
30+
31+
resource "tencentcloud_cfs_auto_snapshot_policy_attachment" "auto_snapshot_policy_attachment" {
32+
auto_snapshot_policy_id = "asp-basic"
33+
file_system_ids = "cfs-iobiaxtj"
34+
}
35+
36+
`

tencentcloud/service_tencentcloud_cfs.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import (
44
"context"
55
"fmt"
66
"log"
7+
"reflect"
8+
"sort"
9+
"strings"
710

811
cfs "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cfs/v20190719"
912
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
@@ -308,3 +311,89 @@ func (me *CfsService) DeleteCfsAutoSnapshotPolicyById(ctx context.Context, autoS
308311

309312
return
310313
}
314+
315+
func (me *CfsService) DescribeCfsAutoSnapshotPolicyAttachmentById(ctx context.Context, autoSnapshotPolicyId string, fileSystemIds string) (autoSnapshotPolicyAttachment *cfs.AutoSnapshotPolicyInfo, errRet error) {
316+
logId := getLogId(ctx)
317+
318+
request := cfs.NewDescribeAutoSnapshotPoliciesRequest()
319+
request.AutoSnapshotPolicyId = &autoSnapshotPolicyId
320+
321+
defer func() {
322+
if errRet != nil {
323+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error())
324+
}
325+
}()
326+
327+
ratelimit.Check(request.GetAction())
328+
329+
var (
330+
offset uint64 = 0
331+
limit uint64 = 20
332+
)
333+
instances := make([]*cfs.AutoSnapshotPolicyInfo, 0)
334+
for {
335+
request.Offset = &offset
336+
request.Limit = &limit
337+
response, err := me.client.UseCfsClient().DescribeAutoSnapshotPolicies(request)
338+
if err != nil {
339+
errRet = err
340+
return
341+
}
342+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
343+
344+
if response == nil || len(response.Response.AutoSnapshotPolicies) < 1 {
345+
break
346+
}
347+
instances = append(instances, response.Response.AutoSnapshotPolicies...)
348+
if len(response.Response.AutoSnapshotPolicies) < int(limit) {
349+
break
350+
}
351+
352+
offset += limit
353+
}
354+
355+
if len(instances) < 1 {
356+
return
357+
}
358+
var fileSystemIdsList []string
359+
autoSnapshotPolicy := instances[0]
360+
361+
for _, fileSystem := range autoSnapshotPolicy.FileSystems {
362+
fileSystemIdsList = append(fileSystemIdsList, *fileSystem.FileSystemId)
363+
}
364+
365+
res := strings.Split(fileSystemIds, ",")
366+
sort.Strings(res)
367+
sort.Strings(fileSystemIdsList)
368+
369+
if reflect.DeepEqual(res, fileSystemIdsList) {
370+
autoSnapshotPolicyAttachment = autoSnapshotPolicy
371+
}
372+
373+
return
374+
}
375+
376+
func (me *CfsService) DeleteCfsAutoSnapshotPolicyAttachmentById(ctx context.Context, autoSnapshotPolicyId string, fileSystemIds string) (errRet error) {
377+
logId := getLogId(ctx)
378+
379+
request := cfs.NewUnbindAutoSnapshotPolicyRequest()
380+
request.AutoSnapshotPolicyId = &autoSnapshotPolicyId
381+
request.FileSystemIds = &fileSystemIds
382+
383+
defer func() {
384+
if errRet != nil {
385+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error())
386+
}
387+
}()
388+
389+
ratelimit.Check(request.GetAction())
390+
391+
response, err := me.client.UseCfsClient().UnbindAutoSnapshotPolicy(request)
392+
if err != nil {
393+
errRet = err
394+
return
395+
}
396+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
397+
398+
return
399+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
subcategory: "Cloud File Storage(CFS)"
3+
layout: "tencentcloud"
4+
page_title: "TencentCloud: tencentcloud_cfs_auto_snapshot_policy_attachment"
5+
sidebar_current: "docs-tencentcloud-resource-cfs_auto_snapshot_policy_attachment"
6+
description: |-
7+
Provides a resource to create a cfs auto_snapshot_policy_attachment
8+
---
9+
10+
# tencentcloud_cfs_auto_snapshot_policy_attachment
11+
12+
Provides a resource to create a cfs auto_snapshot_policy_attachment
13+
14+
## Example Usage
15+
16+
```hcl
17+
resource "tencentcloud_cfs_auto_snapshot_policy_attachment" "auto_snapshot_policy_attachment" {
18+
auto_snapshot_policy_id = "asp-basic"
19+
file_system_ids = "cfs-4xzkct19,cfs-iobiaxtj"
20+
}
21+
```
22+
23+
## Argument Reference
24+
25+
The following arguments are supported:
26+
27+
* `auto_snapshot_policy_id` - (Required, String, ForceNew) ID of the snapshot to be unbound.
28+
* `file_system_ids` - (Required, String, ForceNew) List of IDs of the file systems to be unbound, separated by comma.
29+
30+
## Attributes Reference
31+
32+
In addition to all arguments above, the following attributes are exported:
33+
34+
* `id` - ID of the resource.
35+
36+
37+
38+
## Import
39+
40+
cfs auto_snapshot_policy_attachment can be imported using the id, e.g.
41+
42+
```
43+
terraform import tencentcloud_cfs_auto_snapshot_policy_attachment.auto_snapshot_policy_attachment auto_snapshot_policy_id#file_system_ids
44+
```
45+

website/tencentcloud.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,9 @@
512512
<li>
513513
<a href="/docs/providers/tencentcloud/r/cfs_auto_snapshot_policy.html">tencentcloud_cfs_auto_snapshot_policy</a>
514514
</li>
515+
<li>
516+
<a href="/docs/providers/tencentcloud/r/cfs_auto_snapshot_policy_attachment.html">tencentcloud_cfs_auto_snapshot_policy_attachment</a>
517+
</li>
515518
<li>
516519
<a href="/docs/providers/tencentcloud/r/cfs_file_system.html">tencentcloud_cfs_file_system</a>
517520
</li>

0 commit comments

Comments
 (0)