@@ -24,25 +24,33 @@ func ResourceTencentCloudCbsSnapshot() *schema.Resource {
2424 Importer : & schema.ResourceImporter {
2525 State : schema .ImportStatePassthrough ,
2626 },
27-
2827 Schema : map [string ]* schema.Schema {
28+ "storage_id" : {
29+ Type : schema .TypeString ,
30+ Required : true ,
31+ ForceNew : true ,
32+ Description : "ID of the the CBS which this snapshot created from." ,
33+ },
2934 "snapshot_name" : {
3035 Type : schema .TypeString ,
3136 Required : true ,
3237 ValidateFunc : tccommon .ValidateStringLengthInRange (2 , 60 ),
3338 Description : "Name of the snapshot." ,
3439 },
35- "storage_id" : {
36- Type : schema .TypeString ,
37- Required : true ,
38- ForceNew : true ,
39- Description : "ID of the the CBS which this snapshot created from." ,
40+ "disk_usage" : {
41+ Type : schema .TypeString ,
42+ Optional : true ,
43+ Computed : true ,
44+ ForceNew : true ,
45+ ValidateFunc : tccommon .ValidateAllowedStringValue ([]string {"SYSTEM_DISK" , "DATA_DISK" }),
46+ Description : "The type of cloud disk associated with the snapshot: SYSTEM_DISK: system disk; DATA_DISK: data disk. If not filled in, the snapshot type will be consistent with the cloud disk type. This parameter is used in some scenarios where users need to create a data disk snapshot from the system disk for shared use." ,
4047 },
4148 "tags" : {
4249 Type : schema .TypeMap ,
4350 Optional : true ,
4451 Description : "The available tags within this CBS Snapshot." ,
4552 },
53+ // computed
4654 "storage_size" : {
4755 Type : schema .TypeInt ,
4856 Computed : true ,
@@ -56,6 +64,7 @@ func ResourceTencentCloudCbsSnapshot() *schema.Resource {
5664 "disk_type" : {
5765 Type : schema .TypeString ,
5866 Computed : true ,
67+ Deprecated : "It has been deprecated from version 1.82.14. Please use `disk_usage` instead." ,
5968 Description : "Types of CBS which this snapshot created from." ,
6069 },
6170 "percent" : {
@@ -75,44 +84,77 @@ func ResourceTencentCloudCbsSnapshot() *schema.Resource {
7584func resourceTencentCloudCbsSnapshotCreate (d * schema.ResourceData , meta interface {}) error {
7685 defer tccommon .LogElapsed ("resource.tencentcloud_cbs_snapshot.create" )()
7786
78- logId := tccommon .GetLogId (tccommon .ContextNil )
79- ctx := context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
87+ var (
88+ logId = tccommon .GetLogId (tccommon .ContextNil )
89+ ctx = context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
90+ cbsService = CbsService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
91+ request = cbs .NewCreateSnapshotRequest ()
92+ response = cbs .NewCreateSnapshotResponse ()
93+ snapshotId string
94+ )
95+
96+ if v , ok := d .GetOk ("storage_id" ); ok {
97+ request .DiskId = helper .String (v .(string ))
98+ }
8099
81- storageId := d .Get ("storage_id" ).(string )
82- snapshotName := d .Get ("snapshot_name" ).(string )
83- cbsService := CbsService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
100+ if v , ok := d .GetOk ("snapshot_name" ); ok {
101+ request .SnapshotName = helper .String (v .(string ))
102+ }
103+
104+ if v , ok := d .GetOk ("disk_usage" ); ok {
105+ request .DiskUsage = helper .String (v .(string ))
106+ }
84107
85- snapshotId := ""
86108 err := resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
87- var e error
88- snapshotId , e = cbsService .CreateSnapshot (ctx , storageId , snapshotName )
109+ result , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UseCbsClient ().CreateSnapshot (request )
89110 if e != nil {
90- return tccommon .RetryError (e )
111+ return tccommon .RetryError (e , tccommon .InternalError )
112+ } else {
113+ log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " , logId , request .GetAction (), request .ToJsonString (), result .ToJsonString ())
114+ }
115+
116+ if result == nil || result .Response == nil {
117+ return resource .NonRetryableError (fmt .Errorf ("Create cbs snapshot, Response is nil." ))
91118 }
92- d .SetId (snapshotId )
119+
120+ response = result
93121 return nil
94122 })
123+
95124 if err != nil {
96125 log .Printf ("[CRITAL]%s create cbs snapshot failed, reason:%s\n " , logId , err .Error ())
97126 return err
98127 }
99128
129+ if response .Response .SnapshotId == nil {
130+ return fmt .Errorf ("SnapshotId is nil." )
131+ }
132+
133+ snapshotId = * response .Response .SnapshotId
134+ d .SetId (snapshotId )
135+
136+ // wait
100137 err = resource .Retry (20 * tccommon .ReadRetryTimeout , func () * resource.RetryError {
101138 snapshot , e := cbsService .DescribeSnapshotById (ctx , snapshotId )
102139 if e != nil {
103140 return tccommon .RetryError (e )
104141 }
142+
105143 if snapshot == nil {
106144 return resource .RetryableError (fmt .Errorf ("cbs snapshot is nil" ))
107145 }
146+
108147 if * snapshot .SnapshotState == CBS_SNAPSHOT_STATUS_CREATING {
109148 return resource .RetryableError (fmt .Errorf ("cbs snapshot status is still %s" , * snapshot .SnapshotState ))
110149 }
150+
111151 if * snapshot .SnapshotState == CBS_SNAPSHOT_STATUS_NORMAL {
112152 return nil
113153 }
154+
114155 return resource .NonRetryableError (fmt .Errorf ("cbs snapshot status is %s, we won't wait for it finish." , * snapshot .SnapshotState ))
115156 })
157+
116158 if err != nil {
117159 log .Printf ("[CRITAL]%s create cbs snapshot failed, reason:%s\n " , logId , err .Error ())
118160 return err
@@ -134,83 +176,108 @@ func resourceTencentCloudCbsSnapshotRead(d *schema.ResourceData, meta interface{
134176 defer tccommon .LogElapsed ("resource.tencentcloud_cbs_snapshot.read" )()
135177 defer tccommon .InconsistentCheck (d , meta )()
136178
137- logId := tccommon .GetLogId (tccommon .ContextNil )
138- ctx := context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
139-
140- snapshotId := d .Id ()
141- cbsService := CbsService {
142- client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn (),
143- }
179+ var (
180+ logId = tccommon .GetLogId (tccommon .ContextNil )
181+ ctx = context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
182+ tcClient = meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()
183+ cbsService = CbsService {client : tcClient }
184+ snapshotId = d .Id ()
185+ snapshot * cbs.Snapshot
186+ e error
187+ )
144188
145- var snapshot * cbs.Snapshot
146- var e error
147189 err := resource .Retry (tccommon .ReadRetryTimeout , func () * resource.RetryError {
148190 snapshot , e = cbsService .DescribeSnapshotById (ctx , snapshotId )
149191 if e != nil {
150192 return tccommon .RetryError (e )
151193 }
194+
152195 return nil
153196 })
197+
154198 if err != nil {
155199 log .Printf ("[CRITAL]%s read cbs snapshot failed, reason:%s\n " , logId , err .Error ())
156200 return err
157201 }
202+
158203 if snapshot == nil {
159204 d .SetId ("" )
160205 return nil
161206 }
162207
163- _ = d .Set ("disk_type" , snapshot .DiskUsage )
164- _ = d .Set ("percent" , snapshot .Percent )
165- _ = d .Set ("storage_size" , snapshot .DiskSize )
166- _ = d .Set ("storage_id" , snapshot .DiskId )
167- _ = d .Set ("snapshot_name" , snapshot .SnapshotName )
168- _ = d .Set ("snapshot_status" , snapshot .SnapshotState )
208+ if snapshot .DiskId != nil {
209+ _ = d .Set ("storage_id" , snapshot .DiskId )
210+ }
211+
212+ if snapshot .SnapshotName != nil {
213+ _ = d .Set ("snapshot_name" , snapshot .SnapshotName )
214+ }
215+
216+ if snapshot .DiskUsage != nil {
217+ _ = d .Set ("disk_usage" , snapshot .DiskUsage )
218+ }
219+
220+ if snapshot .DiskSize != nil {
221+ _ = d .Set ("storage_size" , snapshot .DiskSize )
222+ }
223+
224+ if snapshot .SnapshotState != nil {
225+ _ = d .Set ("snapshot_status" , snapshot .SnapshotState )
226+ }
227+
228+ if snapshot .DiskUsage != nil {
229+ _ = d .Set ("disk_type" , snapshot .DiskUsage )
230+ }
231+
232+ if snapshot .Percent != nil {
233+ _ = d .Set ("percent" , snapshot .Percent )
234+ }
235+
236+ if snapshot .CreateTime != nil {
237+ _ = d .Set ("create_time" , snapshot .CreateTime )
238+ }
169239
170- tcClient := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()
171240 tagService := svctag .NewTagService (tcClient )
172241 tags , err := tagService .DescribeResourceTags (ctx , "cvm" , "volume" , tcClient .Region , d .Id ())
173242 if err != nil {
174243 return err
175244 }
176245
177246 _ = d .Set ("tags" , tags )
178-
179247 return nil
180248}
181249
182250func resourceTencentCloudCbsSnapshotUpdate (d * schema.ResourceData , meta interface {}) error {
183251 defer tccommon .LogElapsed ("resource.tencentcloud_cbs_snapshot.update" )()
184252
185- logId := tccommon .GetLogId (tccommon .ContextNil )
186- ctx := context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
187-
188- snapshotId := d .Id ()
253+ var (
254+ logId = tccommon .GetLogId (tccommon .ContextNil )
255+ ctx = context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
256+ tcClient = meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()
257+ snapshotId = d .Id ()
258+ )
189259
190260 if d .HasChange ("snapshot_name" ) {
191261 snapshotName := d .Get ("snapshot_name" ).(string )
192- cbsService := CbsService {
193- client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn (),
194- }
262+ cbsService := CbsService {client : tcClient }
195263 err := resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
196264 e := cbsService .ModifySnapshotName (ctx , snapshotId , snapshotName )
197265 if e != nil {
198266 return tccommon .RetryError (e )
199267 }
268+
200269 return nil
201270 })
271+
202272 if err != nil {
203- log .Printf ("[CRITAL]%s update cbs snapshot failed, reason:%s\n " , logId , err .Error ())
273+ log .Printf ("[CRITAL]%s update cbs snapshot name failed, reason:%s\n " , logId , err .Error ())
204274 return err
205275 }
206276 }
207277
208278 if d .HasChange ("tags" ) {
209-
210279 oldValue , newValue := d .GetChange ("tags" )
211280 replaceTags , deleteTags := svctag .DiffTags (oldValue .(map [string ]interface {}), newValue .(map [string ]interface {}))
212-
213- tcClient := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()
214281 tagService := svctag .NewTagService (tcClient )
215282 resourceName := tccommon .BuildTagResourceName ("cvm" , "volume" , tcClient .Region , d .Id ())
216283 err := tagService .ModifyTags (ctx , resourceName , replaceTags , deleteTags )
@@ -225,21 +292,22 @@ func resourceTencentCloudCbsSnapshotUpdate(d *schema.ResourceData, meta interfac
225292func resourceTencentCloudCbsSnapshotDelete (d * schema.ResourceData , meta interface {}) error {
226293 defer tccommon .LogElapsed ("resource.tencentcloud_cbs_snapshot.delete" )()
227294
228- logId := tccommon .GetLogId (tccommon .ContextNil )
229- ctx := context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
230-
231- snapshotId := d .Id ()
232- cbsService := CbsService {
233- client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn (),
234- }
295+ var (
296+ logId = tccommon .GetLogId (tccommon .ContextNil )
297+ ctx = context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
298+ cbsService = CbsService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
299+ snapshotId = d .Id ()
300+ )
235301
236302 err := resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
237303 e := cbsService .DeleteSnapshot (ctx , snapshotId )
238304 if e != nil {
239305 return tccommon .RetryError (e )
240306 }
307+
241308 return nil
242309 })
310+
243311 if err != nil {
244312 log .Printf ("[CRITAL]%s delete cbs snapshot failed, reason:%s\n " , logId , err .Error ())
245313 return err
0 commit comments