@@ -18,14 +18,28 @@ func ResourceTencentCloudCbsSnapshotPolicyAttachment() *schema.Resource {
18
18
Create : resourceTencentCloudCbsSnapshotPolicyAttachmentCreate ,
19
19
Read : resourceTencentCloudCbsSnapshotPolicyAttachmentRead ,
20
20
Delete : resourceTencentCloudCbsSnapshotPolicyAttachmentDelete ,
21
-
21
+ Importer : & schema.ResourceImporter {
22
+ State : schema .ImportStatePassthrough ,
23
+ },
22
24
Schema : map [string ]* schema.Schema {
23
25
"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 },
28
41
},
42
+
29
43
"snapshot_policy_id" : {
30
44
Type : schema .TypeString ,
31
45
Required : true ,
@@ -38,96 +52,199 @@ func ResourceTencentCloudCbsSnapshotPolicyAttachment() *schema.Resource {
38
52
39
53
func resourceTencentCloudCbsSnapshotPolicyAttachmentCreate (d * schema.ResourceData , meta interface {}) error {
40
54
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 )
43
55
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
+ }
48
75
}
76
+
77
+ if v , ok := d .GetOk ("snapshot_policy_id" ); ok {
78
+ policyId = v .(string )
79
+ }
80
+
49
81
err := resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
50
- errRet := cbsService .AttachSnapshotPolicy (ctx , storageId , policyId )
82
+ errRet := cbsService .AttachSnapshotPolicy (ctx , storageId , storageIds , policyId )
51
83
if errRet != nil {
52
84
return tccommon .RetryError (errRet )
53
85
}
86
+
54
87
return nil
55
88
})
89
+
56
90
if err != nil {
57
91
log .Printf ("[CRITAL]%s cbs storage policy attach failed, reason:%s\n " , logId , err .Error ())
58
92
return err
59
93
}
60
94
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
+
62
102
return resourceTencentCloudCbsSnapshotPolicyAttachmentRead (d , meta )
63
103
}
64
104
65
105
func resourceTencentCloudCbsSnapshotPolicyAttachmentRead (d * schema.ResourceData , meta interface {}) error {
66
106
defer tccommon .LogElapsed ("resource.tencentcloud_cbs_snapshot_policy_attachment.read" )()
67
107
defer tccommon .InconsistentCheck (d , meta )()
68
108
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
+ )
71
117
72
- id := d .Id ()
73
118
idSplit := strings .Split (id , tccommon .FILED_SP )
74
119
if len (idSplit ) != 2 {
75
120
return fmt .Errorf ("tencentcloud_cbs_snapshot_policy_attachment id is illegal: %s" , id )
76
121
}
77
- storageId := idSplit [0 ]
122
+
123
+ storageIdObj := idSplit [0 ]
78
124
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
81
131
}
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
88
151
}
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 )
94
159
}
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 )
98
183
}
99
- _ = d . Set ( "storage_id" , storageId )
184
+
100
185
_ = d .Set ("snapshot_policy_id" , policyId )
101
186
102
187
return nil
103
188
}
104
189
105
190
func resourceTencentCloudCbsSnapshotPolicyAttachmentDelete (d * schema.ResourceData , meta interface {}) error {
106
191
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 )
109
192
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
+
111
202
idSplit := strings .Split (id , tccommon .FILED_SP )
112
203
if len (idSplit ) != 2 {
113
204
return fmt .Errorf ("tencentcloud_cbs_snapshot_policy_attachment id is illegal: %s" , id )
114
205
}
115
- storageId := idSplit [0 ]
206
+
207
+ storageIdObj := idSplit [0 ]
116
208
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
119
215
}
216
+
120
217
err := resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
121
- errRet := cbsService .UnattachSnapshotPolicy (ctx , storageId , policyId )
218
+ errRet := cbsService .UnattachSnapshotPolicy (ctx , storageId , storageIds , policyId )
122
219
if errRet != nil {
123
220
return tccommon .RetryError (errRet )
124
221
}
222
+
125
223
return nil
126
224
})
225
+
127
226
if err != nil {
128
227
log .Printf ("[CRITAL]%s cbs storage policy unattach failed, reason:%s\n " , logId , err .Error ())
129
228
return err
130
229
}
131
230
132
231
return nil
133
232
}
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
+ }
0 commit comments