Skip to content

Commit 878d2eb

Browse files
authored
fix(cbs): [125536409] tencentcloud_cbs_snapshot_policy_attachment support storage_ids and import (#3433)
* add * add
1 parent cf8d535 commit 878d2eb

File tree

5 files changed

+381
-54
lines changed

5 files changed

+381
-54
lines changed

.changelog/3433.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/tencentcloud_cbs_snapshot_policy_attachment: support `storage_ids` and `import`
3+
```

tencentcloud/services/cbs/resource_tc_cbs_snapshot_policy_attachment.go

Lines changed: 158 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,28 @@ func ResourceTencentCloudCbsSnapshotPolicyAttachment() *schema.Resource {
1818
Create: resourceTencentCloudCbsSnapshotPolicyAttachmentCreate,
1919
Read: resourceTencentCloudCbsSnapshotPolicyAttachmentRead,
2020
Delete: resourceTencentCloudCbsSnapshotPolicyAttachmentDelete,
21-
21+
Importer: &schema.ResourceImporter{
22+
State: schema.ImportStatePassthrough,
23+
},
2224
Schema: map[string]*schema.Schema{
2325
"storage_id": {
24-
Type: schema.TypeString,
25-
Required: true,
26-
ForceNew: true,
27-
Description: "ID of CBS.",
26+
Type: schema.TypeString,
27+
Optional: true,
28+
ForceNew: true,
29+
ExactlyOneOf: []string{"storage_ids"},
30+
Description: "ID of CBS.",
31+
},
32+
33+
"storage_ids": {
34+
Type: schema.TypeSet,
35+
Optional: true,
36+
ForceNew: true,
37+
MinItems: 2,
38+
ExactlyOneOf: []string{"storage_id"},
39+
Description: "IDs of CBS.",
40+
Elem: &schema.Schema{Type: schema.TypeString},
2841
},
42+
2943
"snapshot_policy_id": {
3044
Type: schema.TypeString,
3145
Required: true,
@@ -38,96 +52,199 @@ func ResourceTencentCloudCbsSnapshotPolicyAttachment() *schema.Resource {
3852

3953
func resourceTencentCloudCbsSnapshotPolicyAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
4054
defer tccommon.LogElapsed("resource.tencentcloud_cbs_snapshot_policy_attachment.create")()
41-
logId := tccommon.GetLogId(tccommon.ContextNil)
42-
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
4355

44-
storageId := d.Get("storage_id").(string)
45-
policyId := d.Get("snapshot_policy_id").(string)
46-
cbsService := CbsService{
47-
client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(),
56+
var (
57+
logId = tccommon.GetLogId(tccommon.ContextNil)
58+
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
59+
cbsService = CbsService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
60+
storageId string
61+
storageIds []string
62+
policyId string
63+
)
64+
65+
if v, ok := d.GetOk("storage_id"); ok {
66+
storageId = v.(string)
67+
}
68+
69+
if v, ok := d.GetOk("storage_ids"); ok {
70+
for _, item := range v.(*schema.Set).List() {
71+
if storageId, ok := item.(string); ok && storageId != "" {
72+
storageIds = append(storageIds, storageId)
73+
}
74+
}
4875
}
76+
77+
if v, ok := d.GetOk("snapshot_policy_id"); ok {
78+
policyId = v.(string)
79+
}
80+
4981
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
50-
errRet := cbsService.AttachSnapshotPolicy(ctx, storageId, policyId)
82+
errRet := cbsService.AttachSnapshotPolicy(ctx, storageId, storageIds, policyId)
5183
if errRet != nil {
5284
return tccommon.RetryError(errRet)
5385
}
86+
5487
return nil
5588
})
89+
5690
if err != nil {
5791
log.Printf("[CRITAL]%s cbs storage policy attach failed, reason:%s\n ", logId, err.Error())
5892
return err
5993
}
6094

61-
d.SetId(storageId + tccommon.FILED_SP + policyId)
95+
if storageId != "" {
96+
d.SetId(strings.Join([]string{storageId, policyId}, tccommon.FILED_SP))
97+
} else {
98+
storageIdsStr := strings.Join(storageIds, tccommon.COMMA_SP)
99+
d.SetId(strings.Join([]string{storageIdsStr, policyId}, tccommon.FILED_SP))
100+
}
101+
62102
return resourceTencentCloudCbsSnapshotPolicyAttachmentRead(d, meta)
63103
}
64104

65105
func resourceTencentCloudCbsSnapshotPolicyAttachmentRead(d *schema.ResourceData, meta interface{}) error {
66106
defer tccommon.LogElapsed("resource.tencentcloud_cbs_snapshot_policy_attachment.read")()
67107
defer tccommon.InconsistentCheck(d, meta)()
68108

69-
logId := tccommon.GetLogId(tccommon.ContextNil)
70-
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
109+
var (
110+
logId = tccommon.GetLogId(tccommon.ContextNil)
111+
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
112+
cbsService = CbsService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
113+
id = d.Id()
114+
storageId string
115+
storageIds []string
116+
)
71117

72-
id := d.Id()
73118
idSplit := strings.Split(id, tccommon.FILED_SP)
74119
if len(idSplit) != 2 {
75120
return fmt.Errorf("tencentcloud_cbs_snapshot_policy_attachment id is illegal: %s", id)
76121
}
77-
storageId := idSplit[0]
122+
123+
storageIdObj := idSplit[0]
78124
policyId := idSplit[1]
79-
cbsService := CbsService{
80-
client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(),
125+
126+
storageIdObjSplit := strings.Split(storageIdObj, tccommon.COMMA_SP)
127+
if len(storageIdObjSplit) == 1 {
128+
storageId = storageIdObjSplit[0]
129+
} else {
130+
storageIds = storageIdObjSplit
81131
}
82-
var policy *cbs.AutoSnapshotPolicy
83-
var errRet error
84-
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
85-
policy, errRet = cbsService.DescribeAttachedSnapshotPolicy(ctx, storageId, policyId)
86-
if errRet != nil {
87-
return tccommon.RetryError(errRet, tccommon.InternalError)
132+
133+
var (
134+
policy *cbs.AutoSnapshotPolicy
135+
errRet error
136+
)
137+
138+
if storageId != "" {
139+
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
140+
policy, errRet = cbsService.DescribeAttachedSnapshotPolicy(ctx, storageId, policyId)
141+
if errRet != nil {
142+
return tccommon.RetryError(errRet, tccommon.InternalError)
143+
}
144+
145+
return nil
146+
})
147+
148+
if err != nil {
149+
log.Printf("[CRITAL]%s cbs storage policy attach failed, reason:%s\n ", logId, err.Error())
150+
return err
88151
}
89-
return nil
90-
})
91-
if err != nil {
92-
log.Printf("[CRITAL]%s cbs storage policy attach failed, reason:%s\n ", logId, err.Error())
93-
return err
152+
153+
if policy == nil {
154+
d.SetId("")
155+
return nil
156+
}
157+
158+
_ = d.Set("storage_id", storageId)
94159
}
95-
if policy == nil {
96-
d.SetId("")
97-
return nil
160+
161+
if len(storageIds) > 0 {
162+
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
163+
policy, errRet = cbsService.DescribeAttachedSnapshotPolicyDisksById(ctx, policyId)
164+
if errRet != nil {
165+
return tccommon.RetryError(errRet, tccommon.InternalError)
166+
}
167+
168+
return nil
169+
})
170+
171+
if err != nil {
172+
log.Printf("[CRITAL]%s cbs storage policy attach failed, reason:%s\n ", logId, err.Error())
173+
return err
174+
}
175+
176+
if policy == nil || policy.DiskIdSet == nil || len(policy.DiskIdSet) < 1 {
177+
d.SetId("")
178+
return nil
179+
}
180+
181+
tmpList := GetDiskIds(storageIds, policy.DiskIdSet)
182+
_ = d.Set("storage_ids", tmpList)
98183
}
99-
_ = d.Set("storage_id", storageId)
184+
100185
_ = d.Set("snapshot_policy_id", policyId)
101186

102187
return nil
103188
}
104189

105190
func resourceTencentCloudCbsSnapshotPolicyAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
106191
defer tccommon.LogElapsed("resource.tencentcloud_cbs_snapshot_policy_attachment.delete")()
107-
logId := tccommon.GetLogId(tccommon.ContextNil)
108-
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
109192

110-
id := d.Id()
193+
var (
194+
logId = tccommon.GetLogId(tccommon.ContextNil)
195+
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
196+
cbsService = CbsService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
197+
id = d.Id()
198+
storageId string
199+
storageIds []string
200+
)
201+
111202
idSplit := strings.Split(id, tccommon.FILED_SP)
112203
if len(idSplit) != 2 {
113204
return fmt.Errorf("tencentcloud_cbs_snapshot_policy_attachment id is illegal: %s", id)
114205
}
115-
storageId := idSplit[0]
206+
207+
storageIdObj := idSplit[0]
116208
policyId := idSplit[1]
117-
cbsService := CbsService{
118-
client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(),
209+
210+
storageIdObjSplit := strings.Split(storageIdObj, tccommon.COMMA_SP)
211+
if len(storageIdObjSplit) == 1 {
212+
storageId = storageIdObjSplit[0]
213+
} else {
214+
storageIds = storageIdObjSplit
119215
}
216+
120217
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
121-
errRet := cbsService.UnattachSnapshotPolicy(ctx, storageId, policyId)
218+
errRet := cbsService.UnattachSnapshotPolicy(ctx, storageId, storageIds, policyId)
122219
if errRet != nil {
123220
return tccommon.RetryError(errRet)
124221
}
222+
125223
return nil
126224
})
225+
127226
if err != nil {
128227
log.Printf("[CRITAL]%s cbs storage policy unattach failed, reason:%s\n ", logId, err.Error())
129228
return err
130229
}
131230

132231
return nil
133232
}
233+
234+
func GetDiskIds(A []string, B []*string) []string {
235+
set := make(map[string]bool, len(B))
236+
for _, ptr := range B {
237+
if ptr != nil {
238+
set[*ptr] = true
239+
}
240+
}
241+
242+
var tmpList []string
243+
for _, s := range A {
244+
if set[s] {
245+
tmpList = append(tmpList, s)
246+
}
247+
}
248+
249+
return tmpList
250+
}
Lines changed: 89 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,95 @@
11
Provides a CBS snapshot policy attachment resource.
22

3+
~> **NOTE:** To distinguish between `storage_id` and `storage_id`, use `storage_id` when there is only one diskId, otherwise use `storage_ids`.
4+
35
Example Usage
46

7+
Attachment CBS snapshot policy by storage_id
8+
59
```hcl
6-
resource "tencentcloud_cbs_snapshot_policy_attachment" "foo" {
7-
storage_id = tencentcloud_cbs_storage.foo.id
8-
snapshot_policy_id = tencentcloud_cbs_snapshot_policy.policy.id
10+
resource "tencentcloud_cbs_storage" "example" {
11+
storage_name = "tf-example"
12+
storage_type = "CLOUD_SSD"
13+
storage_size = 60
14+
availability_zone = "ap-guangzhou-6"
15+
project_id = 0
16+
encrypt = false
17+
18+
tags = {
19+
createBy = "Terraform"
20+
}
21+
}
22+
23+
resource "tencentcloud_cbs_snapshot_policy" "example" {
24+
snapshot_policy_name = "tf-example"
25+
repeat_weekdays = [1, 4]
26+
repeat_hours = [1]
27+
retention_days = 7
28+
}
29+
30+
resource "tencentcloud_cbs_snapshot_policy_attachment" "example" {
31+
storage_id = tencentcloud_cbs_storage.example.id
32+
snapshot_policy_id = tencentcloud_cbs_snapshot_policy.example.id
933
}
10-
```
34+
```
35+
36+
Attachment CBS snapshot policy by storage_ids
37+
38+
```hcl
39+
resource "tencentcloud_cbs_storage" "example1" {
40+
storage_name = "tf-example1"
41+
storage_type = "CLOUD_SSD"
42+
storage_size = 60
43+
availability_zone = "ap-guangzhou-6"
44+
project_id = 0
45+
encrypt = false
46+
47+
tags = {
48+
createBy = "Terraform"
49+
}
50+
}
51+
52+
resource "tencentcloud_cbs_storage" "example2" {
53+
storage_name = "tf-example2"
54+
storage_type = "CLOUD_SSD"
55+
storage_size = 60
56+
availability_zone = "ap-guangzhou-6"
57+
project_id = 0
58+
encrypt = false
59+
60+
tags = {
61+
createBy = "Terraform"
62+
}
63+
}
64+
65+
resource "tencentcloud_cbs_snapshot_policy" "example" {
66+
snapshot_policy_name = "tf-example"
67+
repeat_weekdays = [1, 4]
68+
repeat_hours = [1]
69+
retention_days = 7
70+
}
71+
72+
resource "tencentcloud_cbs_snapshot_policy_attachment" "example" {
73+
storage_ids = [
74+
tencentcloud_cbs_storage.example1.id,
75+
tencentcloud_cbs_storage.example2.id,
76+
]
77+
snapshot_policy_id = tencentcloud_cbs_snapshot_policy.example.id
78+
}
79+
```
80+
81+
Import
82+
83+
CBS snapshot policy attachment can be imported using the id, e.g.
84+
85+
If use storage_id
86+
87+
```
88+
$ terraform import tencentcloud_cbs_snapshot_policy_attachment.example disk-fesgc43m#asp-8abupspr
89+
```
90+
91+
If use storage_ids
92+
93+
```
94+
$ terraform import tencentcloud_cbs_snapshot_policy_attachment.example disk-ghylus9y,disk-0tm61hla#asp-ng87uf4t
95+
```

0 commit comments

Comments
 (0)