Skip to content

Commit 50d954b

Browse files
authored
add resource cbs_attach_set (#1135)
1 parent 0c44e46 commit 50d954b

File tree

4 files changed

+195
-0
lines changed

4 files changed

+195
-0
lines changed

tencentcloud/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ Cloud Block Storage(CBS)
183183
tencentcloud_cbs_storage
184184
tencentcloud_cbs_storage_set
185185
tencentcloud_cbs_storage_attachment
186+
tencentcloud_cbs_storage_set_attachment
186187
tencentcloud_cbs_snapshot
187188
tencentcloud_cbs_snapshot_policy
188189
tencentcloud_cbs_snapshot_policy_attachment
@@ -932,6 +933,7 @@ func Provider() terraform.ResourceProvider {
932933
"tencentcloud_cbs_storage": resourceTencentCloudCbsStorage(),
933934
"tencentcloud_cbs_storage_set": resourceTencentCloudCbsStorageSet(),
934935
"tencentcloud_cbs_storage_attachment": resourceTencentCloudCbsStorageAttachment(),
936+
"tencentcloud_cbs_storage_set_attachment": resourceTencentCloudCbsStorageSetAttachment(),
935937
"tencentcloud_cbs_snapshot_policy_attachment": resourceTencentCloudCbsSnapshotPolicyAttachment(),
936938
"tencentcloud_vpc": resourceTencentCloudVpcInstance(),
937939
"tencentcloud_vpc_acl": resourceTencentCloudVpcACL(),
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
Provides a CBS storage set attachment resource.
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_cbs_storage_set_attachment" "attachment" {
8+
storage_id = "disk-kdt0sq6m"
9+
instance_id = "ins-jqlegd42"
10+
}
11+
```
12+
13+
*/
14+
package tencentcloud
15+
16+
import (
17+
"context"
18+
"fmt"
19+
"log"
20+
"time"
21+
22+
sdkErrors "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
23+
24+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
25+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
26+
cbs "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cbs/v20170312"
27+
)
28+
29+
func resourceTencentCloudCbsStorageSetAttachment() *schema.Resource {
30+
return &schema.Resource{
31+
Create: resourceTencentCloudCbsStorageSetAttachmentCreate,
32+
Read: resourceTencentCloudCbsStorageSetAttachmentRead,
33+
Delete: resourceTencentCloudCbsStorageSetAttachmentDelete,
34+
Importer: &schema.ResourceImporter{
35+
State: schema.ImportStatePassthrough,
36+
},
37+
38+
Schema: map[string]*schema.Schema{
39+
"storage_id": {
40+
Type: schema.TypeString,
41+
Required: true,
42+
ForceNew: true,
43+
Description: "ID of the mounted CBS.",
44+
},
45+
"instance_id": {
46+
Type: schema.TypeString,
47+
Required: true,
48+
ForceNew: true,
49+
Description: "ID of the CVM instance.",
50+
},
51+
},
52+
}
53+
}
54+
55+
func resourceTencentCloudCbsStorageSetAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
56+
defer logElapsed("resource.tencentcloud_cbs_storage_set_attachment.create")()
57+
58+
logId := getLogId(contextNil)
59+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
60+
61+
storageId := d.Get("storage_id").(string)
62+
instanceId := d.Get("instance_id").(string)
63+
64+
cbsService := CbsService{
65+
client: meta.(*TencentCloudClient).apiV3Conn,
66+
}
67+
68+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
69+
e := cbsService.AttachDisk(ctx, storageId, instanceId)
70+
if e != nil {
71+
ee, ok := e.(*sdkErrors.TencentCloudSDKError)
72+
if ok && IsContains(CVM_RETRYABLE_ERROR, ee.Code) {
73+
time.Sleep(1 * time.Second) // 需要重试的话,等待1s进行重试
74+
return resource.RetryableError(fmt.Errorf("cbs attach error: %s, retrying", ee.Error()))
75+
}
76+
return resource.NonRetryableError(ee)
77+
}
78+
return nil
79+
})
80+
if err != nil {
81+
log.Printf("[CRITAL]%s cbs storage attach failed, reason:%s\n ", logId, err.Error())
82+
return err
83+
}
84+
85+
d.SetId(storageId)
86+
87+
return nil
88+
}
89+
90+
func resourceTencentCloudCbsStorageSetAttachmentRead(d *schema.ResourceData, meta interface{}) error {
91+
defer logElapsed("resource.tencentcloud_cbs_storage_set_attachment.read")()
92+
defer inconsistentCheck(d, meta)()
93+
94+
logId := getLogId(contextNil)
95+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
96+
97+
storageId := d.Id()
98+
cbsService := CbsService{
99+
client: meta.(*TencentCloudClient).apiV3Conn,
100+
}
101+
102+
var storage *cbs.Disk
103+
var errRet error
104+
storage, errRet = cbsService.DescribeDiskById(ctx, storageId)
105+
if errRet != nil {
106+
log.Printf("[CRITAL]%s describe cbs storage attach failed, reason:%s\n ", logId, errRet.Error())
107+
return errRet
108+
}
109+
110+
if storage == nil || !*storage.Attached {
111+
d.SetId("")
112+
return nil
113+
}
114+
_ = d.Set("storage_id", storage.DiskId)
115+
_ = d.Set("instance_id", storage.InstanceId)
116+
117+
return nil
118+
}
119+
120+
func resourceTencentCloudCbsStorageSetAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
121+
defer logElapsed("resource.tencentcloud_cbs_storage_set_attachment.delete")()
122+
123+
logId := getLogId(contextNil)
124+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
125+
126+
cbsService := CbsService{
127+
client: meta.(*TencentCloudClient).apiV3Conn,
128+
}
129+
130+
storageId := d.Id()
131+
instanceId := d.Get("instance_id").(string)
132+
133+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
134+
errRet := cbsService.DetachDisk(ctx, storageId, instanceId)
135+
if errRet != nil {
136+
log.Printf("[CRITAL][detach disk]%s api[%s] fail, reason[%s]\n",
137+
logId, "detach", errRet.Error())
138+
e, ok := errRet.(*sdkErrors.TencentCloudSDKError)
139+
if ok && IsContains(CVM_RETRYABLE_ERROR, e.Code) {
140+
time.Sleep(1 * time.Second) // 需要重试的话,等待1s进行重试
141+
return resource.RetryableError(fmt.Errorf("[detach]disk detach error: %s, retrying", e.Error()))
142+
}
143+
return resource.NonRetryableError(errRet)
144+
}
145+
return nil
146+
})
147+
if err != nil {
148+
log.Printf("[CRITAL]%s cbs storage detach failed, reason:%s\n ", logId, err.Error())
149+
return err
150+
}
151+
152+
return nil
153+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
subcategory: "Cloud Block Storage(CBS)"
3+
layout: "tencentcloud"
4+
page_title: "TencentCloud: tencentcloud_cbs_storage_set_attachment"
5+
sidebar_current: "docs-tencentcloud-resource-cbs_storage_set_attachment"
6+
description: |-
7+
Provides a CBS storage set attachment resource.
8+
---
9+
10+
# tencentcloud_cbs_storage_set_attachment
11+
12+
Provides a CBS storage set attachment resource.
13+
14+
## Example Usage
15+
16+
```hcl
17+
resource "tencentcloud_cbs_storage_set_attachment" "attachment" {
18+
storage_id = "disk-kdt0sq6m"
19+
instance_id = "ins-jqlegd42"
20+
}
21+
```
22+
23+
## Argument Reference
24+
25+
The following arguments are supported:
26+
27+
* `instance_id` - (Required, ForceNew) ID of the CVM instance.
28+
* `storage_id` - (Required, ForceNew) ID of the mounted CBS.
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+

website/tencentcloud.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,9 @@
478478
<li>
479479
<a href="/docs/providers/tencentcloud/r/cbs_storage_set.html">tencentcloud_cbs_storage_set</a>
480480
</li>
481+
<li>
482+
<a href="/docs/providers/tencentcloud/r/cbs_storage_set_attachment.html">tencentcloud_cbs_storage_set_attachment</a>
483+
</li>
481484
</ul>
482485
</li>
483486
</ul>

0 commit comments

Comments
 (0)