Skip to content

Commit ef9b9c2

Browse files
authored
Merge pull request #471 from crab21/data_images
datasource images support snapshotSet
2 parents 9c70316 + 7d1697a commit ef9b9c2

File tree

5 files changed

+121
-1
lines changed

5 files changed

+121
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## 1.39.0 (Unreleased)
22

33
ENHANCEMENTS:
4+
5+
* Data Source: `tencentcloud_images` supports list of snapshots.
46
* Resource: `tencentcloud_kubernetes_cluster_attachment` add new argument `worker_config` to support config with existing instances.
57

68
## 1.38.2 (July 03, 2020)

tencentcloud/data_source_tc_images.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,35 @@ func dataSourceTencentCloudImages() *schema.Resource {
136136
Computed: true,
137137
Description: "Whether support cloud-init.",
138138
},
139+
"snapshots": {
140+
Type: schema.TypeList,
141+
Computed: true,
142+
Description: "List of snapshot details.",
143+
Elem: &schema.Resource{
144+
Schema: map[string]*schema.Schema{
145+
"snapshot_id": {
146+
Type: schema.TypeString,
147+
Computed: true,
148+
Description: "Snapshot ID.",
149+
},
150+
"snapshot_name": {
151+
Type: schema.TypeString,
152+
Computed: true,
153+
Description: "Snapshot name, the user-defined snapshot alias.",
154+
},
155+
"disk_usage": {
156+
Type: schema.TypeString,
157+
Computed: true,
158+
Description: "Type of the cloud disk used to create the snapshot.",
159+
},
160+
"disk_size": {
161+
Type: schema.TypeInt,
162+
Computed: true,
163+
Description: "Size of the cloud disk used to create the snapshot; unit: GB.",
164+
},
165+
},
166+
},
167+
},
139168
},
140169
},
141170
},
@@ -153,6 +182,10 @@ func dataSourceTencentCloudImagesRead(d *schema.ResourceData, meta interface{})
153182
client: meta.(*TencentCloudClient).apiV3Conn,
154183
}
155184

185+
cbsService := CbsService{
186+
client: meta.(*TencentCloudClient).apiV3Conn,
187+
}
188+
156189
var (
157190
imageId string
158191
imageType []string
@@ -234,6 +267,11 @@ func dataSourceTencentCloudImagesRead(d *schema.ResourceData, meta interface{})
234267
imageList := make([]map[string]interface{}, 0, len(results))
235268
ids := make([]string, 0, len(results))
236269
for _, image := range results {
270+
snapshots, err := imagesReadSnapshotByIds(ctx, cbsService, image)
271+
if err != nil {
272+
return err
273+
}
274+
237275
mapping := map[string]interface{}{
238276
"image_id": image.ImageId,
239277
"os_name": image.OsName,
@@ -249,6 +287,7 @@ func dataSourceTencentCloudImagesRead(d *schema.ResourceData, meta interface{})
249287
"image_source": image.ImageSource,
250288
"sync_percent": image.SyncPercent,
251289
"support_cloud_init": image.IsSupportCloudinit,
290+
"snapshots": snapshots,
252291
}
253292
imageList = append(imageList, mapping)
254293
ids = append(ids, *image.ImageId)
@@ -270,3 +309,32 @@ func dataSourceTencentCloudImagesRead(d *schema.ResourceData, meta interface{})
270309

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

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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"log"
66

7+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
78
cbs "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cbs/v20170312"
89
"github.com/terraform-providers/terraform-provider-tencentcloud/tencentcloud/connectivity"
910
"github.com/terraform-providers/terraform-provider-tencentcloud/tencentcloud/internal/helper"
@@ -226,6 +227,49 @@ func (me *CbsService) DescribeSnapshotById(ctx context.Context, snapshotId strin
226227
return
227228
}
228229

230+
func (me *CbsService) DescribeSnapshotByIds(ctx context.Context, snapshotIdsParam []*string) (snapshots []*cbs.Snapshot, errRet error) {
231+
if len(snapshotIdsParam) == 0 {
232+
return
233+
}
234+
235+
var (
236+
logId = getLogId(ctx)
237+
request = cbs.NewDescribeSnapshotsRequest()
238+
err error
239+
response *cbs.DescribeSnapshotsResponse
240+
offset, pageSize uint64 = 0, 100
241+
)
242+
request.SnapshotIds = snapshotIdsParam
243+
244+
for {
245+
request.Offset = &offset
246+
request.Limit = &pageSize
247+
248+
err = resource.Retry(readRetryTimeout, func() *resource.RetryError {
249+
ratelimit.Check(request.GetAction())
250+
response, err = me.client.UseCbsClient().DescribeSnapshots(request)
251+
if err != nil {
252+
return retryError(err, InternalError)
253+
}
254+
return nil
255+
})
256+
257+
if err != nil {
258+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
259+
logId, request.GetAction(), request.ToJsonString(), err.Error())
260+
errRet = err
261+
return
262+
}
263+
264+
snapshots = append(snapshots, response.Response.SnapshotSet...)
265+
if len(response.Response.SnapshotSet) < int(pageSize) {
266+
break
267+
}
268+
offset += pageSize
269+
}
270+
return
271+
}
272+
229273
func (me *CbsService) DescribeSnapshotsByFilter(ctx context.Context, params map[string]string) (snapshots []*cbs.Snapshot, errRet error) {
230274
logId := getLogId(ctx)
231275
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)