Skip to content

fix(cbs): [121657802] tencentcloud_cbs_snapshots update code #3078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 14, 2025
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
3 changes: 3 additions & 0 deletions .changelog/3078.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
datasource/tencentcloud_cbs_snapshots: update code
```
25 changes: 16 additions & 9 deletions tencentcloud/services/cbs/data_source_tc_cbs_snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
},
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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
Expand Down
32 changes: 30 additions & 2 deletions tencentcloud/services/cbs/data_source_tc_cbs_snapshots.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
```
36 changes: 32 additions & 4 deletions website/docs/d/cbs_snapshots.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
```

Expand All @@ -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

Expand Down
Loading