@@ -21,6 +21,7 @@ import (
2121
2222 "github.com/hashicorp/terraform-plugin-sdk/helper/resource"
2323 "github.com/hashicorp/terraform-plugin-sdk/helper/schema"
24+ cbs "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cbs/v20170312"
2425 cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
2526 "github.com/terraform-providers/terraform-provider-tencentcloud/tencentcloud/internal/helper"
2627)
@@ -136,6 +137,35 @@ func dataSourceTencentCloudImages() *schema.Resource {
136137 Computed : true ,
137138 Description : "Whether support cloud-init." ,
138139 },
140+ "snapshots" : {
141+ Type : schema .TypeSet ,
142+ Computed : true ,
143+ Description : "List of snapshot details." ,
144+ Elem : & schema.Resource {
145+ Schema : map [string ]* schema.Schema {
146+ "snapshot_id" : {
147+ Type : schema .TypeString ,
148+ Computed : true ,
149+ Description : "Snapshot ID." ,
150+ },
151+ "snapshot_name" : {
152+ Type : schema .TypeString ,
153+ Computed : true ,
154+ Description : "Snapshot name, the user-defined snapshot alias." ,
155+ },
156+ "disk_usage" : {
157+ Type : schema .TypeString ,
158+ Computed : true ,
159+ Description : "Type of the cloud disk used to create the snapshot." ,
160+ },
161+ "disk_size" : {
162+ Type : schema .TypeInt ,
163+ Computed : true ,
164+ Description : "Size of the cloud disk used to create the snapshot; unit: GB." ,
165+ },
166+ },
167+ },
168+ },
139169 },
140170 },
141171 },
@@ -153,6 +183,10 @@ func dataSourceTencentCloudImagesRead(d *schema.ResourceData, meta interface{})
153183 client : meta .(* TencentCloudClient ).apiV3Conn ,
154184 }
155185
186+ cbsService := CbsService {
187+ client : meta .(* TencentCloudClient ).apiV3Conn ,
188+ }
189+
156190 var (
157191 imageId string
158192 imageType []string
@@ -234,6 +268,11 @@ func dataSourceTencentCloudImagesRead(d *schema.ResourceData, meta interface{})
234268 imageList := make ([]map [string ]interface {}, 0 , len (results ))
235269 ids := make ([]string , 0 , len (results ))
236270 for _ , image := range results {
271+ snapshots , err := imagesReadSnapshotByIds (image , cbsService , ctx )
272+ if err != nil {
273+ return err
274+ }
275+
237276 mapping := map [string ]interface {}{
238277 "image_id" : image .ImageId ,
239278 "os_name" : image .OsName ,
@@ -249,6 +288,7 @@ func dataSourceTencentCloudImagesRead(d *schema.ResourceData, meta interface{})
249288 "image_source" : image .ImageSource ,
250289 "sync_percent" : image .SyncPercent ,
251290 "support_cloud_init" : image .IsSupportCloudinit ,
291+ "snapshots" : snapshots ,
252292 }
253293 imageList = append (imageList , mapping )
254294 ids = append (ids , * image .ImageId )
@@ -270,3 +310,50 @@ func dataSourceTencentCloudImagesRead(d *schema.ResourceData, meta interface{})
270310
271311 return nil
272312}
313+
314+ func imagesReadSnapshotByIds (image * cvm.Image , cbsService CbsService , ctx context.Context ) (snapshotResults []map [string ]interface {}, errSnap error ) {
315+ if len (image .SnapshotSet ) == 0 {
316+ return
317+ }
318+
319+ snapshotSetMap := map [string ]interface {}{}
320+ snapshotByIds := make ([]* string , 0 , len (image .SnapshotSet ))
321+ for _ , snapshot := range image .SnapshotSet {
322+ snapshotByIds = append (snapshotByIds , snapshot .SnapshotId )
323+ }
324+
325+ var snapshots []* cbs.Snapshot
326+ errSnap = resource .Retry (readRetryTimeout , func () * resource.RetryError {
327+ snapshots , errSnap = cbsService .DescribeSnapshotByIds (ctx , snapshotByIds )
328+ if errSnap != nil {
329+ return retryError (errSnap , InternalError )
330+ }
331+ return nil
332+ })
333+ if errSnap != nil {
334+ return
335+ }
336+
337+ for _ , snapshot := range snapshots {
338+ snapshotSetMap [* snapshot .SnapshotId ] = snapshot
339+ }
340+
341+ snapshotSet := image .SnapshotSet
342+ snapshotResults = make ([]map [string ]interface {}, 0 , len (snapshotSet ))
343+ if len (snapshotSet ) > 0 {
344+ for _ , snapshot := range snapshotSet {
345+ snapshotMap := make (map [string ]interface {}, 4 )
346+ id := snapshot .SnapshotId
347+ snapshotMap ["snapshot_id" ] = * id
348+ snapshotMap ["disk_usage" ] = snapshot .DiskUsage
349+ snapshotMap ["disk_size" ] = snapshot .DiskSize
350+
351+ if v , ok := snapshotSetMap [* id ]; ok {
352+ snapshotMap ["snapshot_name" ] = v .(* cbs.Snapshot ).SnapshotName
353+ }
354+ snapshotResults = append (snapshotResults , snapshotMap )
355+ }
356+ }
357+
358+ return
359+ }
0 commit comments