diff --git a/.changelog/3070.txt b/.changelog/3070.txt new file mode 100644 index 0000000000..b3df066bcf --- /dev/null +++ b/.changelog/3070.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/tencentcloud_cbs_snapshot: update code and support `tags` +``` diff --git a/tencentcloud/services/cbs/resource_tc_cbs_snapshot.go b/tencentcloud/services/cbs/resource_tc_cbs_snapshot.go index ab4115e9de..5d73d7e6ce 100644 --- a/tencentcloud/services/cbs/resource_tc_cbs_snapshot.go +++ b/tencentcloud/services/cbs/resource_tc_cbs_snapshot.go @@ -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": { @@ -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) } @@ -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 { @@ -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) } diff --git a/tencentcloud/services/cbs/resource_tc_cbs_snapshot.md b/tencentcloud/services/cbs/resource_tc_cbs_snapshot.md index 7aa7ffd516..386b4e3bcf 100644 --- a/tencentcloud/services/cbs/resource_tc_cbs_snapshot.md +++ b/tencentcloud/services/cbs/resource_tc_cbs_snapshot.md @@ -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" + } } ``` @@ -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 ``` \ No newline at end of file diff --git a/tencentcloud/services/cbs/service_tencentcloud_cbs.go b/tencentcloud/services/cbs/service_tencentcloud_cbs.go index 72dcf7e017..520392a7ac 100644 --- a/tencentcloud/services/cbs/service_tencentcloud_cbs.go +++ b/tencentcloud/services/cbs/service_tencentcloud_cbs.go @@ -2,6 +2,7 @@ package cbs import ( "context" + "fmt" "log" "strings" "sync" @@ -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 { @@ -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 } diff --git a/website/docs/r/cbs_snapshot.html.markdown b/website/docs/r/cbs_snapshot.html.markdown index 595973998a..5183b4ea6d 100644 --- a/website/docs/r/cbs_snapshot.html.markdown +++ b/website/docs/r/cbs_snapshot.html.markdown @@ -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" + } } ``` @@ -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 @@ -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 ```