Skip to content

Commit

Permalink
fix(cbs): [121606495] tencentcloud_cbs_snapshot update code and sup…
Browse files Browse the repository at this point in the history
…port `tags` (#3070)

* add

* add
  • Loading branch information
SevenEarth authored Jan 9, 2025
1 parent 3422f72 commit dee9ff4
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 39 deletions.
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 @@ func ResourceTencentCloudCbsSnapshot() *schema.Resource {
"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 @@ func resourceTencentCloudCbsSnapshotCreate(d *schema.ResourceData, meta interfac

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 @@ func resourceTencentCloudCbsSnapshotCreate(d *schema.ResourceData, meta interfac
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 @@ -136,6 +118,15 @@ func resourceTencentCloudCbsSnapshotCreate(d *schema.ResourceData, meta interfac
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
```

0 comments on commit dee9ff4

Please sign in to comment.