Skip to content

Commit 969bf8e

Browse files
committed
datasource images support snapshotSet
1 parent 9c70316 commit 969bf8e

File tree

4 files changed

+132
-1
lines changed

4 files changed

+132
-1
lines changed

tencentcloud/data_source_tc_images.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

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

tencentcloud/service_tencentcloud_cbs.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,44 @@ func (me *CbsService) DescribeSnapshotById(ctx context.Context, snapshotId strin
226226
return
227227
}
228228

229+
func (me *CbsService) DescribeSnapshotByIds(ctx context.Context, snapshotIdsParam []*string) (snapshots []*cbs.Snapshot, errRet error) {
230+
logId := getLogId(ctx)
231+
request := cbs.NewDescribeSnapshotsRequest()
232+
if len(snapshotIdsParam) == 0 {
233+
return
234+
}
235+
request.SnapshotIds = snapshotIdsParam
236+
237+
offset := 0
238+
pageSize := 100
239+
for {
240+
request.Offset = helper.IntUint64(offset)
241+
request.Limit = helper.IntUint64(pageSize)
242+
ratelimit.Check(request.GetAction())
243+
response, err := me.client.UseCbsClient().DescribeSnapshots(request)
244+
if err != nil {
245+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
246+
logId, request.GetAction(), request.ToJsonString(), err.Error())
247+
errRet = err
248+
return
249+
}
250+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
251+
logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
252+
253+
if response == nil || len(response.Response.SnapshotSet) < 1 {
254+
break
255+
}
256+
257+
snapshots = append(snapshots, response.Response.SnapshotSet...)
258+
259+
if len(response.Response.SnapshotSet) < pageSize {
260+
break
261+
}
262+
offset += pageSize
263+
}
264+
return
265+
}
266+
229267
func (me *CbsService) DescribeSnapshotsByFilter(ctx context.Context, params map[string]string) (snapshots []*cbs.Snapshot, errRet error) {
230268
logId := getLogId(ctx)
231269
request := cbs.NewDescribeSnapshotsRequest()

website/docs/d/images.html.markdown

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ In addition to all arguments above, the following attributes are exported:
4646
* `image_type` - Type of the image.
4747
* `os_name` - OS name of the image.
4848
* `platform` - Platform of the image.
49+
* `snapshots` - List of snapshot details.
50+
* `disk_size` - Size of the cloud disk used to create the snapshot; unit: GB.
51+
* `disk_usage` - Type of the cloud disk used to create the snapshot.
52+
* `snapshot_id` - Snapshot ID.
53+
* `snapshot_name` - Snapshot name, the user-defined snapshot alias.
4954
* `support_cloud_init` - Whether support cloud-init.
5055
* `sync_percent` - Sync percent of the image.
5156

0 commit comments

Comments
 (0)