@@ -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
3953func 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
65105func 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
105190func 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+ }
0 commit comments