diff --git a/.changelog/3078.txt b/.changelog/3078.txt new file mode 100644 index 0000000000..d4878d4156 --- /dev/null +++ b/.changelog/3078.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +datasource/tencentcloud_cbs_snapshots: update code +``` diff --git a/tencentcloud/services/cbs/data_source_tc_cbs_snapshots.go b/tencentcloud/services/cbs/data_source_tc_cbs_snapshots.go index e8caa23fe1..cf4af97394 100644 --- a/tencentcloud/services/cbs/data_source_tc_cbs_snapshots.go +++ b/tencentcloud/services/cbs/data_source_tc_cbs_snapshots.go @@ -36,10 +36,10 @@ func DataSourceTencentCloudCbsSnapshots() *schema.Resource { Type: schema.TypeString, Optional: true, ValidateFunc: tccommon.ValidateAllowedStringValue(CBS_STORAGE_USAGE), - Description: "Types of CBS which this snapshot created from, and available values include SYSTEM_DISK and DATA_DISK.", + Description: "Types of CBS which this snapshot created from, and available values include `SYSTEM_DISK` and `DATA_DISK`.", }, "project_id": { - Type: schema.TypeInt, + Type: schema.TypeString, Optional: true, Description: "ID of the project within the snapshot.", }, @@ -119,38 +119,43 @@ func DataSourceTencentCloudCbsSnapshots() *schema.Resource { func dataSourceTencentCloudCbsSnapshotsRead(d *schema.ResourceData, meta interface{}) error { defer tccommon.LogElapsed("data_source.tencentcloud_cbs_snapshots.read")() - logId := tccommon.GetLogId(tccommon.ContextNil) - ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) + var ( + logId = tccommon.GetLogId(tccommon.ContextNil) + ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId) + cbsService = CbsService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} + ) params := make(map[string]string) if v, ok := d.GetOk("snapshot_id"); ok { params["snapshot-id"] = v.(string) } + if v, ok := d.GetOk("snapshot_name"); ok { params["snapshot-name"] = v.(string) } + if v, ok := d.GetOk("storage_id"); ok { params["disk-id"] = v.(string) } + if v, ok := d.GetOk("storage_usage"); ok { params["disk-usage"] = v.(string) } - if v, ok := d.GetOkExists("project_id"); ok { + + if v, ok := d.GetOk("project_id"); ok { params["project-id"] = v.(string) } + if v, ok := d.GetOk("availability_zone"); ok { params["zone"] = v.(string) } - cbsService := CbsService{ - client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(), - } - err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError { snapshots, e := cbsService.DescribeSnapshotsByFilter(ctx, params) if e != nil { return tccommon.RetryError(e) } + ids := make([]string, 0, len(snapshots)) snapshotList := make([]map[string]interface{}, 0, len(snapshots)) for _, snapshot := range snapshots { @@ -166,6 +171,7 @@ func dataSourceTencentCloudCbsSnapshotsRead(d *schema.ResourceData, meta interfa "create_time": *snapshot.CreateTime, "encrypt": *snapshot.Encrypt, } + snapshotList = append(snapshotList, mapping) ids = append(ids, *snapshot.SnapshotId) } @@ -185,6 +191,7 @@ func dataSourceTencentCloudCbsSnapshotsRead(d *schema.ResourceData, meta interfa return nil }) + if err != nil { log.Printf("[CRITAL]%s read cbs snapshots failed, reason:%s\n ", logId, err.Error()) return err diff --git a/tencentcloud/services/cbs/data_source_tc_cbs_snapshots.md b/tencentcloud/services/cbs/data_source_tc_cbs_snapshots.md index 3196d6e0b8..fff91c9936 100644 --- a/tencentcloud/services/cbs/data_source_tc_cbs_snapshots.md +++ b/tencentcloud/services/cbs/data_source_tc_cbs_snapshots.md @@ -2,9 +2,37 @@ Use this data source to query detailed information of CBS snapshots. Example Usage +Query all snapshots + ```hcl +data "tencentcloud_cbs_snapshots" "snapshots" {} +``` + +Query snapshots by filters + +```hcl +data "tencentcloud_cbs_snapshots" "snapshots" { + snapshot_id = "snap-hibh08s3" + result_output_file = "my_snapshots" +} + +data "tencentcloud_cbs_snapshots" "snapshots" { + snapshot_name = "tf-example" +} + +data "tencentcloud_cbs_snapshots" "snapshots" { + storage_id = "disk-12j0fk1w" +} + +data "tencentcloud_cbs_snapshots" "snapshots" { + storage_usage = "SYSTEM_DISK" +} + +data "tencentcloud_cbs_snapshots" "snapshots" { + project_id = "0" +} + data "tencentcloud_cbs_snapshots" "snapshots" { - snapshot_id = "snap-f3io7adt" - result_output_file = "mytestpath" + availability_zone = "ap-guangzhou-4" } ``` \ No newline at end of file diff --git a/website/docs/d/cbs_snapshots.html.markdown b/website/docs/d/cbs_snapshots.html.markdown index d8b84f59ba..81b0891c8c 100644 --- a/website/docs/d/cbs_snapshots.html.markdown +++ b/website/docs/d/cbs_snapshots.html.markdown @@ -13,10 +13,38 @@ Use this data source to query detailed information of CBS snapshots. ## Example Usage +### Query all snapshots + +```hcl +data "tencentcloud_cbs_snapshots" "snapshots" {} +``` + +### Query snapshots by filters + ```hcl data "tencentcloud_cbs_snapshots" "snapshots" { - snapshot_id = "snap-f3io7adt" - result_output_file = "mytestpath" + snapshot_id = "snap-hibh08s3" + result_output_file = "my_snapshots" +} + +data "tencentcloud_cbs_snapshots" "snapshots" { + snapshot_name = "tf-example" +} + +data "tencentcloud_cbs_snapshots" "snapshots" { + storage_id = "disk-12j0fk1w" +} + +data "tencentcloud_cbs_snapshots" "snapshots" { + storage_usage = "SYSTEM_DISK" +} + +data "tencentcloud_cbs_snapshots" "snapshots" { + project_id = "0" +} + +data "tencentcloud_cbs_snapshots" "snapshots" { + availability_zone = "ap-guangzhou-4" } ``` @@ -25,12 +53,12 @@ data "tencentcloud_cbs_snapshots" "snapshots" { The following arguments are supported: * `availability_zone` - (Optional, String) The available zone that the CBS instance locates at. -* `project_id` - (Optional, Int) ID of the project within the snapshot. +* `project_id` - (Optional, String) ID of the project within the snapshot. * `result_output_file` - (Optional, String) Used to save results. * `snapshot_id` - (Optional, String) ID of the snapshot to be queried. * `snapshot_name` - (Optional, String) Name of the snapshot to be queried. * `storage_id` - (Optional, String) ID of the the CBS which this snapshot created from. -* `storage_usage` - (Optional, String) Types of CBS which this snapshot created from, and available values include SYSTEM_DISK and DATA_DISK. +* `storage_usage` - (Optional, String) Types of CBS which this snapshot created from, and available values include `SYSTEM_DISK` and `DATA_DISK`. ## Attributes Reference