Skip to content

Commit 04fa344

Browse files
authored
feat(image): [128740503] support encrypt (#3639)
* add encrypt * add encrypt
1 parent 0185788 commit 04fa344

File tree

4 files changed

+53
-3
lines changed

4 files changed

+53
-3
lines changed

.changelog/3639.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/tencentcloud_cvm_sync_image: support `encrypt` and `kms_key_id`
3+
```

tencentcloud/services/cvm/resource_tc_cvm_sync_image.go

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cvm
22

33
import (
4+
"fmt"
45
"log"
56
"time"
67

@@ -57,6 +58,29 @@ func ResourceTencentCloudCvmSyncImage() *schema.Resource {
5758
Type: schema.TypeBool,
5859
Description: "Whether to return the ID of image created in the destination region.",
5960
},
61+
62+
"encrypt": {
63+
Optional: true,
64+
ForceNew: true,
65+
Type: schema.TypeBool,
66+
Description: "Whether to synchronize as an encrypted custom image. Default value is `false`. Synchronization to an encrypted custom image is only supported within the same region.",
67+
},
68+
69+
"kms_key_id": {
70+
Optional: true,
71+
ForceNew: true,
72+
Type: schema.TypeString,
73+
Description: "KMS key ID used when synchronizing to an encrypted custom image. This parameter is valid only synchronizing to an encrypted image. If KmsKeyId is not specified, the default CBS cloud product KMS key is used.",
74+
},
75+
76+
"image_set": {
77+
Computed: true,
78+
Type: schema.TypeSet,
79+
Elem: &schema.Schema{
80+
Type: schema.TypeString,
81+
},
82+
Description: "ID of the image created in the destination region.",
83+
},
6084
},
6185
}
6286
}
@@ -67,6 +91,7 @@ func resourceTencentCloudCvmSyncImageCreate(d *schema.ResourceData, meta interfa
6791

6892
logId := tccommon.GetLogId(tccommon.ContextNil)
6993
request := cvm.NewSyncImagesRequest()
94+
response := cvm.NewSyncImagesResponse()
7095
imageId := d.Get("image_id").(string)
7196
request.ImageIds = []*string{&imageId}
7297

@@ -78,34 +103,50 @@ func resourceTencentCloudCvmSyncImageCreate(d *schema.ResourceData, meta interfa
78103
}
79104
}
80105

81-
if v, _ := d.GetOk("dry_run"); v != nil {
106+
if v, ok := d.GetOkExists("dry_run"); ok {
82107
request.DryRun = helper.Bool(v.(bool))
83108
}
84109

85110
if v, ok := d.GetOk("image_name"); ok {
86111
request.ImageName = helper.String(v.(string))
87112
}
88113

89-
if v, _ := d.GetOk("image_set_required"); v != nil {
114+
if v, ok := d.GetOkExists("image_set_required"); ok {
90115
request.ImageSetRequired = helper.Bool(v.(bool))
91116
}
92117

118+
if v, ok := d.GetOkExists("encrypt"); ok {
119+
request.Encrypt = helper.Bool(v.(bool))
120+
}
121+
122+
if v, ok := d.GetOk("kms_key_id"); ok {
123+
request.KmsKeyId = helper.String(v.(string))
124+
}
125+
93126
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
94127
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseCvmClient().SyncImages(request)
95128
if e != nil {
96129
return tccommon.RetryError(e)
97130
} else {
98131
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
99132
}
133+
response = result
100134
return nil
101135
})
102136
if err != nil {
103137
log.Printf("[CRITAL]%s operate cvm syncImages failed, reason:%+v", logId, err)
104138
return err
105139
}
106140

141+
if response == nil || response.Response == nil || response.Response.ImageSet == nil {
142+
err = fmt.Errorf("Response is nil")
143+
return err
144+
}
145+
107146
d.SetId(imageId)
108147

148+
_ = d.Set("image_set", response.Response.ImageSet)
149+
109150
service := CvmService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
110151

111152
conf := tccommon.BuildStateChangeConf([]string{}, []string{"NORMAL"}, 20*tccommon.ReadRetryTimeout, time.Second, service.CvmSyncImagesStateRefreshFunc(d.Id(), []string{}))

tencentcloud/services/cvm/resource_tc_cvm_sync_image.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ data "tencentcloud_images" "example" {
1111
resource "tencentcloud_cvm_sync_image" "example" {
1212
image_id = data.tencentcloud_images.example.images.0.image_id
1313
destination_regions = ["ap-guangzhou", "ap-shanghai"]
14+
encrypt = true
15+
kms_key_id = "f063c18b-654b-11ef-9d9f-525400d3a886"
1416
}
1517
```

website/docs/r/cvm_sync_image.html.markdown

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ data "tencentcloud_images" "example" {
2222
resource "tencentcloud_cvm_sync_image" "example" {
2323
image_id = data.tencentcloud_images.example.images.0.image_id
2424
destination_regions = ["ap-guangzhou", "ap-shanghai"]
25+
encrypt = true
26+
kms_key_id = "f063c18b-654b-11ef-9d9f-525400d3a886"
2527
}
2628
```
2729

@@ -32,14 +34,16 @@ The following arguments are supported:
3234
* `destination_regions` - (Required, Set: [`String`], ForceNew) List of destination regions for synchronization. Limits: It must be a valid region. For a custom image, the destination region cannot be the source region. For a shared image, the destination region must be the source region, which indicates to create a copy of the image as a custom image in the same region.
3335
* `image_id` - (Required, String, ForceNew) Image ID. The specified image must meet the following requirement: the images must be in the `NORMAL` state.
3436
* `dry_run` - (Optional, Bool, ForceNew) Checks whether image synchronization can be initiated.
37+
* `encrypt` - (Optional, Bool, ForceNew) Whether to synchronize as an encrypted custom image. Default value is `false`. Synchronization to an encrypted custom image is only supported within the same region.
3538
* `image_name` - (Optional, String, ForceNew) Destination image name.
3639
* `image_set_required` - (Optional, Bool, ForceNew) Whether to return the ID of image created in the destination region.
40+
* `kms_key_id` - (Optional, String, ForceNew) KMS key ID used when synchronizing to an encrypted custom image. This parameter is valid only synchronizing to an encrypted image. If KmsKeyId is not specified, the default CBS cloud product KMS key is used.
3741

3842
## Attributes Reference
3943

4044
In addition to all arguments above, the following attributes are exported:
4145

4246
* `id` - ID of the resource.
43-
47+
* `image_set` - ID of the image created in the destination region.
4448

4549

0 commit comments

Comments
 (0)