Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## 1.39.0 (Unreleased)

ENHANCEMENTS:

* Data Source: `tencentcloud_images` supports list of snapshots.
* Resource: `tencentcloud_kubernetes_cluster_attachment` add new argument `worker_config` to support config with existing instances.

## 1.38.2 (July 03, 2020)
Expand Down
68 changes: 68 additions & 0 deletions tencentcloud/data_source_tc_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,35 @@ func dataSourceTencentCloudImages() *schema.Resource {
Computed: true,
Description: "Whether support cloud-init.",
},
"snapshots": {
Type: schema.TypeList,
Computed: true,
Description: "List of snapshot details.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"snapshot_id": {
Type: schema.TypeString,
Computed: true,
Description: "Snapshot ID.",
},
"snapshot_name": {
Type: schema.TypeString,
Computed: true,
Description: "Snapshot name, the user-defined snapshot alias.",
},
"disk_usage": {
Type: schema.TypeString,
Computed: true,
Description: "Type of the cloud disk used to create the snapshot.",
},
"disk_size": {
Type: schema.TypeInt,
Computed: true,
Description: "Size of the cloud disk used to create the snapshot; unit: GB.",
},
},
},
},
},
},
},
Expand All @@ -153,6 +182,10 @@ func dataSourceTencentCloudImagesRead(d *schema.ResourceData, meta interface{})
client: meta.(*TencentCloudClient).apiV3Conn,
}

cbsService := CbsService{
client: meta.(*TencentCloudClient).apiV3Conn,
}

var (
imageId string
imageType []string
Expand Down Expand Up @@ -234,6 +267,11 @@ func dataSourceTencentCloudImagesRead(d *schema.ResourceData, meta interface{})
imageList := make([]map[string]interface{}, 0, len(results))
ids := make([]string, 0, len(results))
for _, image := range results {
snapshots, err := imagesReadSnapshotByIds(ctx, cbsService, image)
if err != nil {
return err
}

mapping := map[string]interface{}{
"image_id": image.ImageId,
"os_name": image.OsName,
Expand All @@ -249,6 +287,7 @@ func dataSourceTencentCloudImagesRead(d *schema.ResourceData, meta interface{})
"image_source": image.ImageSource,
"sync_percent": image.SyncPercent,
"support_cloud_init": image.IsSupportCloudinit,
"snapshots": snapshots,
}
imageList = append(imageList, mapping)
ids = append(ids, *image.ImageId)
Expand All @@ -270,3 +309,32 @@ func dataSourceTencentCloudImagesRead(d *schema.ResourceData, meta interface{})

return nil
}

func imagesReadSnapshotByIds(ctx context.Context, cbsService CbsService, image *cvm.Image) (snapshotResults []map[string]interface{}, errRet error) {
if len(image.SnapshotSet) == 0 {
return
}

snapshotByIds := make([]*string, 0, len(image.SnapshotSet))
for _, snapshot := range image.SnapshotSet {
snapshotByIds = append(snapshotByIds, snapshot.SnapshotId)
}

snapshots, errRet := cbsService.DescribeSnapshotByIds(ctx, snapshotByIds)
if errRet != nil {
return
}

snapshotResults = make([]map[string]interface{}, 0, len(snapshots))
for _, snapshot := range snapshots {
snapshotMap := make(map[string]interface{}, 4)
snapshotMap["snapshot_id"] = snapshot.SnapshotId
snapshotMap["disk_usage"] = snapshot.DiskUsage
snapshotMap["disk_size"] = snapshot.DiskSize
snapshotMap["snapshot_name"] = snapshot.SnapshotName

snapshotResults = append(snapshotResults, snapshotMap)
}

return
}
3 changes: 2 additions & 1 deletion tencentcloud/data_source_tc_images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ func TestAccTencentCloudDataSourceImagesBase(t *testing.T) {

const testAccTencentCloudDataSourceImagesBase = `
data "tencentcloud_images" "foo" {
result_output_file = "data_source_tc_images_test.txt"
}
`

const testAccTencentCloudDataSourceImagesBaseWithFilter = `
data "tencentcloud_images" "foo" {
image_type = ["PUBLIC_IMAGE"]
image_type = ["PRIVATE_IMAGE"]
}
`

Expand Down
44 changes: 44 additions & 0 deletions tencentcloud/service_tencentcloud_cbs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"log"

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

func (me *CbsService) DescribeSnapshotByIds(ctx context.Context, snapshotIdsParam []*string) (snapshots []*cbs.Snapshot, errRet error) {
if len(snapshotIdsParam) == 0 {
return
}

var (
logId = getLogId(ctx)
request = cbs.NewDescribeSnapshotsRequest()
err error
response *cbs.DescribeSnapshotsResponse
offset, pageSize uint64 = 0, 100
)
request.SnapshotIds = snapshotIdsParam

for {
request.Offset = &offset
request.Limit = &pageSize

err = resource.Retry(readRetryTimeout, func() *resource.RetryError {
ratelimit.Check(request.GetAction())
response, err = me.client.UseCbsClient().DescribeSnapshots(request)
if err != nil {
return retryError(err, InternalError)
}
return nil
})

if err != nil {
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
logId, request.GetAction(), request.ToJsonString(), err.Error())
errRet = err
return
}

snapshots = append(snapshots, response.Response.SnapshotSet...)
if len(response.Response.SnapshotSet) < int(pageSize) {
break
}
offset += pageSize
}
return
}

func (me *CbsService) DescribeSnapshotsByFilter(ctx context.Context, params map[string]string) (snapshots []*cbs.Snapshot, errRet error) {
logId := getLogId(ctx)
request := cbs.NewDescribeSnapshotsRequest()
Expand Down
5 changes: 5 additions & 0 deletions website/docs/d/images.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ In addition to all arguments above, the following attributes are exported:
* `image_type` - Type of the image.
* `os_name` - OS name of the image.
* `platform` - Platform of the image.
* `snapshots` - List of snapshot details.
* `disk_size` - Size of the cloud disk used to create the snapshot; unit: GB.
* `disk_usage` - Type of the cloud disk used to create the snapshot.
* `snapshot_id` - Snapshot ID.
* `snapshot_name` - Snapshot name, the user-defined snapshot alias.
* `support_cloud_init` - Whether support cloud-init.
* `sync_percent` - Sync percent of the image.

Expand Down