Skip to content

fix(cbs): [121606495] tencentcloud_cbs_snapshot update code and support tags #3070

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 2 commits into from
Jan 9, 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/3070.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/tencentcloud_cbs_snapshot: update code and support `tags`
```
31 changes: 11 additions & 20 deletions tencentcloud/services/cbs/resource_tc_cbs_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"tags": {
Type: schema.TypeMap,
Optional: true,
Deprecated: "cbs snapshot do not support tag now.",
Description: "The available tags within this CBS Snapshot.",
},
"storage_size": {
Expand Down Expand Up @@ -81,20 +80,12 @@

storageId := d.Get("storage_id").(string)
snapshotName := d.Get("snapshot_name").(string)

var tags map[string]string

if temp := helper.GetTags(d, "tags"); len(temp) > 0 {
tags = temp
}
cbsService := CbsService{
client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(),
}
cbsService := CbsService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}

snapshotId := ""
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
var e error
snapshotId, e = cbsService.CreateSnapshot(ctx, storageId, snapshotName, tags)
snapshotId, e = cbsService.CreateSnapshot(ctx, storageId, snapshotName)
if e != nil {
return tccommon.RetryError(e)
}
Expand All @@ -106,15 +97,6 @@
return err
}

if tags := helper.GetTags(d, "tags"); len(tags) > 0 {
tcClient := meta.(tccommon.ProviderMeta).GetAPIV3Conn()
tagService := svctag.NewTagService(tcClient)
resourceName := tccommon.BuildTagResourceName("cvm", "volume", tcClient.Region, d.Id())
if err := tagService.ModifyTags(ctx, resourceName, tags, nil); err != nil {
return err
}
}

err = resource.Retry(20*tccommon.ReadRetryTimeout, func() *resource.RetryError {
snapshot, e := cbsService.DescribeSnapshotById(ctx, snapshotId)
if e != nil {
Expand All @@ -123,10 +105,10 @@
if snapshot == nil {
return resource.RetryableError(fmt.Errorf("cbs snapshot is nil"))
}
if *snapshot.SnapshotState == CBS_SNAPSHOT_STATUS_CREATING {

Check failure on line 108 in tencentcloud/services/cbs/resource_tc_cbs_snapshot.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: CBS_SNAPSHOT_STATUS_CREATING (typecheck)
return resource.RetryableError(fmt.Errorf("cbs snapshot status is still %s", *snapshot.SnapshotState))
}
if *snapshot.SnapshotState == CBS_SNAPSHOT_STATUS_NORMAL {

Check failure on line 111 in tencentcloud/services/cbs/resource_tc_cbs_snapshot.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: CBS_SNAPSHOT_STATUS_NORMAL (typecheck)
return nil
}
return resource.NonRetryableError(fmt.Errorf("cbs snapshot status is %s, we won't wait for it finish.", *snapshot.SnapshotState))
Expand All @@ -136,6 +118,15 @@
return err
}

if tags := helper.GetTags(d, "tags"); len(tags) > 0 {
tcClient := meta.(tccommon.ProviderMeta).GetAPIV3Conn()
tagService := svctag.NewTagService(tcClient)
resourceName := tccommon.BuildTagResourceName("cvm", "volume", tcClient.Region, d.Id())
if err := tagService.ModifyTags(ctx, resourceName, tags, nil); err != nil {
return err
}
}

return resourceTencentCloudCbsSnapshotRead(d, meta)
}

Expand Down
11 changes: 7 additions & 4 deletions tencentcloud/services/cbs/resource_tc_cbs_snapshot.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ Provides a resource to create a CBS snapshot.
Example Usage

```hcl
resource "tencentcloud_cbs_snapshot" "snapshot" {
snapshot_name = "unnamed"
storage_id = "disk-kdt0sq6m"
resource "tencentcloud_cbs_snapshot" "example" {
snapshot_name = "tf-example"
storage_id = "disk-alc1r5sw"
tags = {
createBy = "Terraform"
}
}
```

Expand All @@ -14,5 +17,5 @@ Import
CBS snapshot can be imported using the id, e.g.

```
$ terraform import tencentcloud_cbs_snapshot.snapshot snap-3sa3f39b
$ terraform import tencentcloud_cbs_snapshot.example snap-3sa3f39b
```
17 changes: 7 additions & 10 deletions tencentcloud/services/cbs/service_tencentcloud_cbs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cbs

import (
"context"
"fmt"
"log"
"strings"
"sync"
Expand Down Expand Up @@ -387,20 +388,11 @@ func (me *CbsService) DetachDisk(ctx context.Context, diskId, instanceId string)
return nil
}

func (me *CbsService) CreateSnapshot(ctx context.Context, diskId, snapshotName string, tags map[string]string) (snapshotId string, errRet error) {
func (me *CbsService) CreateSnapshot(ctx context.Context, diskId, snapshotName string) (snapshotId string, errRet error) {
logId := tccommon.GetLogId(ctx)
request := cbs.NewCreateSnapshotRequest()
request.DiskId = &diskId
request.SnapshotName = &snapshotName
if len(tags) > 0 {
for tagKey, tagValue := range tags {
tag := cbs.Tag{
Key: helper.String(tagKey),
Value: helper.String(tagValue),
}
request.Tags = append(request.Tags, &tag)
}
}
ratelimit.Check(request.GetAction())
response, err := me.client.UseCbsClient().CreateSnapshot(request)
if err != nil {
Expand All @@ -412,6 +404,11 @@ func (me *CbsService) CreateSnapshot(ctx context.Context, diskId, snapshotName s
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())

if response == nil || response.Response == nil || response.Response.SnapshotId == nil {
errRet = fmt.Errorf("CreateSnapshot response is nil.")
return
}

snapshotId = *response.Response.SnapshotId
return
}
Expand Down
13 changes: 8 additions & 5 deletions website/docs/r/cbs_snapshot.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ Provides a resource to create a CBS snapshot.
## Example Usage

```hcl
resource "tencentcloud_cbs_snapshot" "snapshot" {
snapshot_name = "unnamed"
storage_id = "disk-kdt0sq6m"
resource "tencentcloud_cbs_snapshot" "example" {
snapshot_name = "tf-example"
storage_id = "disk-alc1r5sw"
tags = {
createBy = "Terraform"
}
}
```

Expand All @@ -26,7 +29,7 @@ The following arguments are supported:

* `snapshot_name` - (Required, String) Name of the snapshot.
* `storage_id` - (Required, String, ForceNew) ID of the the CBS which this snapshot created from.
* `tags` - (Optional, Map, **Deprecated**) cbs snapshot do not support tag now. The available tags within this CBS Snapshot.
* `tags` - (Optional, Map) The available tags within this CBS Snapshot.

## Attributes Reference

Expand All @@ -45,6 +48,6 @@ In addition to all arguments above, the following attributes are exported:
CBS snapshot can be imported using the id, e.g.

```
$ terraform import tencentcloud_cbs_snapshot.snapshot snap-3sa3f39b
$ terraform import tencentcloud_cbs_snapshot.example snap-3sa3f39b
```

Loading