Skip to content

Commit cc6e325

Browse files
committed
fix error push
1 parent f7806f0 commit cc6e325

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

tencentcloud/data_source_tc_images.go

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
/*
22
Use this data source to query images.
3-
43
Example Usage
5-
64
```hcl
75
data "tencentcloud_images" "foo" {
86
image_type = ["PUBLIC_IMAGE"]
@@ -136,6 +134,35 @@ func dataSourceTencentCloudImages() *schema.Resource {
136134
Computed: true,
137135
Description: "Whether support cloud-init.",
138136
},
137+
"snapshots": {
138+
Type: schema.TypeList,
139+
Computed: true,
140+
Description: "List of snapshot details.",
141+
Elem: &schema.Resource{
142+
Schema: map[string]*schema.Schema{
143+
"snapshot_id": {
144+
Type: schema.TypeString,
145+
Computed: true,
146+
Description: "Snapshot ID.",
147+
},
148+
"snapshot_name": {
149+
Type: schema.TypeString,
150+
Computed: true,
151+
Description: "Snapshot name, the user-defined snapshot alias.",
152+
},
153+
"disk_usage": {
154+
Type: schema.TypeString,
155+
Computed: true,
156+
Description: "Type of the cloud disk used to create the snapshot.",
157+
},
158+
"disk_size": {
159+
Type: schema.TypeInt,
160+
Computed: true,
161+
Description: "Size of the cloud disk used to create the snapshot; unit: GB.",
162+
},
163+
},
164+
},
165+
},
139166
},
140167
},
141168
},
@@ -153,6 +180,10 @@ func dataSourceTencentCloudImagesRead(d *schema.ResourceData, meta interface{})
153180
client: meta.(*TencentCloudClient).apiV3Conn,
154181
}
155182

183+
cbsService := CbsService{
184+
client: meta.(*TencentCloudClient).apiV3Conn,
185+
}
186+
156187
var (
157188
imageId string
158189
imageType []string
@@ -234,6 +265,11 @@ func dataSourceTencentCloudImagesRead(d *schema.ResourceData, meta interface{})
234265
imageList := make([]map[string]interface{}, 0, len(results))
235266
ids := make([]string, 0, len(results))
236267
for _, image := range results {
268+
snapshots, err := imagesReadSnapshotByIds(ctx, cbsService, image)
269+
if err != nil {
270+
return err
271+
}
272+
237273
mapping := map[string]interface{}{
238274
"image_id": image.ImageId,
239275
"os_name": image.OsName,
@@ -249,6 +285,7 @@ func dataSourceTencentCloudImagesRead(d *schema.ResourceData, meta interface{})
249285
"image_source": image.ImageSource,
250286
"sync_percent": image.SyncPercent,
251287
"support_cloud_init": image.IsSupportCloudinit,
288+
"snapshots": snapshots,
252289
}
253290
imageList = append(imageList, mapping)
254291
ids = append(ids, *image.ImageId)
@@ -270,3 +307,32 @@ func dataSourceTencentCloudImagesRead(d *schema.ResourceData, meta interface{})
270307

271308
return nil
272309
}
310+
311+
func imagesReadSnapshotByIds(ctx context.Context, cbsService CbsService, image *cvm.Image) (snapshotResults []map[string]interface{}, errRet error) {
312+
if len(image.SnapshotSet) == 0 {
313+
return
314+
}
315+
316+
snapshotByIds := make([]*string, 0, len(image.SnapshotSet))
317+
for _, snapshot := range image.SnapshotSet {
318+
snapshotByIds = append(snapshotByIds, snapshot.SnapshotId)
319+
}
320+
321+
snapshots, errRet := cbsService.DescribeSnapshotByIds(ctx, snapshotByIds)
322+
if errRet != nil {
323+
return
324+
}
325+
326+
snapshotResults = make([]map[string]interface{}, 0, len(snapshots))
327+
for _, snapshot := range snapshots {
328+
snapshotMap := make(map[string]interface{}, 4)
329+
snapshotMap["snapshot_id"] = snapshot.SnapshotId
330+
snapshotMap["disk_usage"] = snapshot.DiskUsage
331+
snapshotMap["disk_size"] = snapshot.DiskSize
332+
snapshotMap["snapshot_name"] = snapshot.SnapshotName
333+
334+
snapshotResults = append(snapshotResults, snapshotMap)
335+
}
336+
337+
return
338+
}

tencentcloud/data_source_tc_images_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,13 @@ func TestAccTencentCloudDataSourceImagesBase(t *testing.T) {
4545

4646
const testAccTencentCloudDataSourceImagesBase = `
4747
data "tencentcloud_images" "foo" {
48+
result_output_file = "data_source_tc_images_test.txt"
4849
}
4950
`
5051

5152
const testAccTencentCloudDataSourceImagesBaseWithFilter = `
5253
data "tencentcloud_images" "foo" {
53-
image_type = ["PUBLIC_IMAGE"]
54+
image_type = ["PRIVATE_IMAGE"]
5455
}
5556
`
5657

0 commit comments

Comments
 (0)