Skip to content

Commit f3ee0d5

Browse files
wqq578qiqiwwanglyu571
authored
add kms interface (#1949)
* add kms interface * add kms interfac * 1.modify doc 2.modify logic. * 1.add changelog. 2.rm import --------- Co-authored-by: qiqiwwang <qiqiwwang@tencent.com> Co-authored-by: nickyinluo <nickyinluo@tencent.com>
1 parent cd56456 commit f3ee0d5

File tree

14 files changed

+2112
-56
lines changed

14 files changed

+2112
-56
lines changed

.changelog/1949.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-resource
2+
tencentcloud_kubernetes_encryption_protection
3+
```

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ require (
8484
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tdmq v1.0.691
8585
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tem v1.0.578
8686
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/teo v1.0.529
87-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.644
87+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.691
8888
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tse v1.0.650
8989
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tsf v1.0.674
9090
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vod v1.0.199

go.sum

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,6 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.627/go.mod
832832
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.628/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
833833
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.633/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
834834
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.634/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
835-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.644/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
836835
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.650/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
837836
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.651/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
838837
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.652/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
@@ -940,8 +939,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tem v1.0.578 h1:vBpQhUr
940939
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tem v1.0.578/go.mod h1:UlojGQh/9wb7/uXPNi7PvMral1CNAskVDNgqJEV83l0=
941940
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/teo v1.0.529 h1:vWUgseUvHs1fW/Ok+x3ld9UIhrYRNO9Yr8ccX8wmkkY=
942941
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/teo v1.0.529/go.mod h1:vOd23iOVeQqm5LSEXUmE8773kiUCwGuoJnTO0po5D+Q=
943-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.644 h1:iyS55TcFRybmnn1SHR7HgLcdaSsxFmY+T1WORE0Znww=
944-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.644/go.mod h1:xJIrKYqmsIFTUovx1cwuH8GGu2arW5CDFvM6eqQGf7Q=
942+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.691 h1:D+a7bZnjLoqPAAHSPCOmil4eigXnGPkR1R5G3ybT/+c=
943+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.691/go.mod h1:hsc/CPzbCRXqblCYuE1HuvP3G06OWA9zhAGI6AtEe5U=
945944
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tse v1.0.650 h1:gQFdOULR78GKaUwPkHnupLTZO5Z3STZ1xSp/83xZMnA=
946945
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tse v1.0.650/go.mod h1:cNgQjHihEHq9Z2n6sSe/l7gG6pf7nhWue7e+Iu+wQCc=
947946
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tsf v1.0.674 h1:VsMV1/vsgVzespG7jUzraZS/AbAUllVQjmtVAlA9W/M=

tencentcloud/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,7 @@ Tencent Kubernetes Engine(TKE)
551551
tencentcloud_kubernetes_node_pool
552552
tencentcloud_kubernetes_serverless_node_pool
553553
tencentcloud_kubernetes_backup_storage_location
554+
tencentcloud_kubernetes_encryption_protection
554555
tencentcloud_kubernetes_auth_attachment
555556
tencentcloud_kubernetes_addon_attachment
556557
tencentcloud_kubernetes_cluster_endpoint
@@ -2317,6 +2318,7 @@ func Provider() *schema.Provider {
23172318
"tencentcloud_kubernetes_node_pool": resourceTencentCloudKubernetesNodePool(),
23182319
"tencentcloud_kubernetes_serverless_node_pool": resourceTkeServerLessNodePool(),
23192320
"tencentcloud_kubernetes_backup_storage_location": resourceTencentCloudTkeBackupStorageLocation(),
2321+
"tencentcloud_kubernetes_encryption_protection": resourceTencentCloudTkeEncryptionProtection(),
23202322
"tencentcloud_mysql_backup_policy": resourceTencentCloudMysqlBackupPolicy(),
23212323
"tencentcloud_mysql_account": resourceTencentCloudMysqlAccount(),
23222324
"tencentcloud_mysql_account_privilege": resourceTencentCloudMysqlAccountPrivilege(),

tencentcloud/resource_tc_kubernetes_cluster.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -934,8 +934,7 @@ func resourceTencentCloudTkeCluster() *schema.Resource {
934934
"cluster_version": {
935935
Type: schema.TypeString,
936936
Optional: true,
937-
Default: "1.10.5",
938-
Description: "Version of the cluster, Default is '1.10.5'. Use `tencentcloud_kubernetes_available_cluster_versions` to get the available versions.",
937+
Description: "Version of the cluster. Use `tencentcloud_kubernetes_available_cluster_versions` to get the upgradable cluster version.",
939938
},
940939
"upgrade_instances_follow_cluster": {
941940
Type: schema.TypeBool,
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
/*
2+
Provides a resource to create a tke encryption_protection
3+
4+
Example Usage
5+
6+
Enable tke encryption protection
7+
8+
```hcl
9+
variable "example_region" {
10+
default = "ap-guangzhou"
11+
}
12+
13+
variable "example_cluster_cidr" {
14+
default = "10.31.0.0/16"
15+
}
16+
17+
variable "availability_zone" {
18+
default = "ap-guangzhou-3"
19+
}
20+
21+
data "tencentcloud_vpc_subnets" "vpc" {
22+
is_default = true
23+
availability_zone = var.availability_zone
24+
}
25+
26+
resource "tencentcloud_kubernetes_cluster" "example" {
27+
vpc_id = data.tencentcloud_vpc_subnets.vpc.instance_list.0.vpc_id
28+
cluster_cidr = var.example_cluster_cidr
29+
cluster_max_pod_num = 32
30+
cluster_name = "tf_example_cluster"
31+
cluster_desc = "a tf example cluster for the kms test"
32+
cluster_max_service_num = 32
33+
cluster_internet = true
34+
cluster_version = "1.24.4"
35+
cluster_deploy_type = "MANAGED_CLUSTER"
36+
}
37+
38+
resource "tencentcloud_kms_key" "example" {
39+
alias = "tf-example-kms-key-ed-%s"
40+
description = "example of kms key instance"
41+
key_usage = "ENCRYPT_DECRYPT"
42+
is_enabled = true
43+
}
44+
45+
resource "tencentcloud_kubernetes_encryption_protection" "example" {
46+
cluster_id = tencentcloud_kubernetes_cluster.example.id
47+
kms_configuration {
48+
key_id = tencentcloud_kms_key.example.id
49+
kms_region = var.example_region
50+
}
51+
}
52+
```
53+
*/
54+
package tencentcloud
55+
56+
import (
57+
"context"
58+
"log"
59+
"time"
60+
61+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
62+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
63+
tke "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke/v20180525"
64+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
65+
)
66+
67+
func resourceTencentCloudTkeEncryptionProtection() *schema.Resource {
68+
return &schema.Resource{
69+
Create: resourceTencentCloudTkeEncryptionProtectionCreate,
70+
Read: resourceTencentCloudTkeEncryptionProtectionRead,
71+
Delete: resourceTencentCloudTkeEncryptionProtectionDelete,
72+
Schema: map[string]*schema.Schema{
73+
"cluster_id": {
74+
Required: true,
75+
ForceNew: true,
76+
Type: schema.TypeString,
77+
Description: "cluster id.",
78+
},
79+
80+
"kms_configuration": {
81+
Required: true,
82+
ForceNew: true,
83+
Type: schema.TypeList,
84+
MaxItems: 1,
85+
Description: "kms encryption configuration.",
86+
Elem: &schema.Resource{
87+
Schema: map[string]*schema.Schema{
88+
"key_id": {
89+
Type: schema.TypeString,
90+
Optional: true,
91+
Description: "kms id.",
92+
},
93+
"kms_region": {
94+
Type: schema.TypeString,
95+
Optional: true,
96+
Description: "kms region.",
97+
},
98+
},
99+
},
100+
},
101+
"status": {
102+
Computed: true,
103+
Type: schema.TypeString,
104+
Description: "kms encryption status.",
105+
},
106+
},
107+
}
108+
}
109+
110+
func resourceTencentCloudTkeEncryptionProtectionCreate(d *schema.ResourceData, meta interface{}) error {
111+
defer logElapsed("resource.tencentcloud_tke_encryption_protection.create")()
112+
defer inconsistentCheck(d, meta)()
113+
114+
logId := getLogId(contextNil)
115+
116+
var (
117+
request = tke.NewEnableEncryptionProtectionRequest()
118+
clusterId string
119+
)
120+
if v, ok := d.GetOk("cluster_id"); ok {
121+
request.ClusterId = helper.String(v.(string))
122+
clusterId = v.(string)
123+
}
124+
125+
if dMap, ok := helper.InterfacesHeadMap(d, "kms_configuration"); ok {
126+
kMSConfiguration := tke.KMSConfiguration{}
127+
if v, ok := dMap["key_id"]; ok {
128+
kMSConfiguration.KeyId = helper.String(v.(string))
129+
}
130+
if v, ok := dMap["kms_region"]; ok {
131+
kMSConfiguration.KmsRegion = helper.String(v.(string))
132+
}
133+
request.KMSConfiguration = &kMSConfiguration
134+
}
135+
136+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
137+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseTkeClient().EnableEncryptionProtection(request)
138+
if e != nil {
139+
return retryError(e)
140+
} else {
141+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
142+
}
143+
return nil
144+
})
145+
if err != nil {
146+
log.Printf("[CRITAL]%s create tke encryptionProtection failed, reason:%+v", logId, err)
147+
return err
148+
}
149+
150+
d.SetId(clusterId)
151+
152+
service := TkeService{client: meta.(*TencentCloudClient).apiV3Conn}
153+
154+
conf := BuildStateChangeConf([]string{}, []string{"Opened"}, 3*readRetryTimeout, time.Second, service.TkeEncryptionProtectionStateRefreshFunc(d.Id(), []string{}))
155+
156+
if _, e := conf.WaitForState(); e != nil {
157+
return e
158+
}
159+
160+
return resourceTencentCloudTkeEncryptionProtectionRead(d, meta)
161+
}
162+
163+
func resourceTencentCloudTkeEncryptionProtectionRead(d *schema.ResourceData, meta interface{}) error {
164+
defer logElapsed("resource.tencentcloud_tke_encryption_protection.read")()
165+
defer inconsistentCheck(d, meta)()
166+
167+
logId := getLogId(contextNil)
168+
169+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
170+
171+
service := TkeService{client: meta.(*TencentCloudClient).apiV3Conn}
172+
173+
encryptionProtectionId := d.Id()
174+
175+
encryptionProtection, err := service.DescribeTkeEncryptionProtectionById(ctx, encryptionProtectionId)
176+
if err != nil {
177+
return err
178+
}
179+
180+
if encryptionProtection == nil {
181+
d.SetId("")
182+
log.Printf("[WARN]%s resource `TkeEncryptionProtection` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
183+
return nil
184+
}
185+
186+
if encryptionProtection.Status != nil {
187+
_ = d.Set("status", encryptionProtection.Status)
188+
}
189+
190+
return nil
191+
}
192+
193+
func resourceTencentCloudTkeEncryptionProtectionDelete(d *schema.ResourceData, meta interface{}) error {
194+
defer logElapsed("resource.tencentcloud_tke_encryption_protection.delete")()
195+
defer inconsistentCheck(d, meta)()
196+
197+
logId := getLogId(contextNil)
198+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
199+
200+
service := TkeService{client: meta.(*TencentCloudClient).apiV3Conn}
201+
encryptionProtectionId := d.Id()
202+
203+
if err := service.DeleteTkeEncryptionProtectionById(ctx, encryptionProtectionId); err != nil {
204+
return err
205+
}
206+
207+
conf := BuildStateChangeConf([]string{}, []string{"Closed"}, 3*readRetryTimeout, time.Second, service.TkeEncryptionProtectionStateRefreshFunc(d.Id(), []string{}))
208+
209+
if _, e := conf.WaitForState(); e != nil {
210+
return e
211+
}
212+
213+
return nil
214+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package tencentcloud
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
)
10+
11+
func TestAccTencentCloudTkeEncryptionProtectionResource_basic(t *testing.T) {
12+
t.Parallel()
13+
rName := acctest.RandString(10)
14+
resource.Test(t, resource.TestCase{
15+
PreCheck: func() {
16+
testAccPreCheck(t)
17+
},
18+
Providers: testAccProviders,
19+
Steps: []resource.TestStep{
20+
{
21+
Config: fmt.Sprintf(testAccTkeEncryptionProtection, rName),
22+
Check: resource.ComposeTestCheckFunc(
23+
resource.TestCheckResourceAttrSet("tencentcloud_kubernetes_encryption_protection.example", "id"),
24+
resource.TestCheckResourceAttr("tencentcloud_kubernetes_encryption_protection.example", "cluster_id", defaultTkeClusterId),
25+
resource.TestCheckResourceAttrSet("tencentcloud_kubernetes_encryption_protection.example", "kms_configuration.#"),
26+
resource.TestCheckResourceAttrSet("tencentcloud_kubernetes_encryption_protection.example", "kms_configuration.0.key_id"),
27+
resource.TestCheckResourceAttr("tencentcloud_kubernetes_encryption_protection.example", "kms_configuration.0.kms_region", "ap-guangzhou"),
28+
resource.TestCheckResourceAttrSet("tencentcloud_kubernetes_encryption_protection.example", "status"),
29+
),
30+
},
31+
},
32+
})
33+
}
34+
35+
const testAccTkeEncryptionProtection = `
36+
37+
variable "example_region" {
38+
default = "ap-guangzhou"
39+
}
40+
41+
variable "example_cluster_cidr" {
42+
default = "10.31.0.0/16"
43+
}
44+
45+
variable "availability_zone" {
46+
default = "ap-guangzhou-3"
47+
}
48+
49+
data "tencentcloud_vpc_subnets" "vpc" {
50+
is_default = true
51+
availability_zone = var.availability_zone
52+
}
53+
54+
resource "tencentcloud_kubernetes_cluster" "example" {
55+
vpc_id = data.tencentcloud_vpc_subnets.vpc.instance_list.0.vpc_id
56+
cluster_cidr = var.example_cluster_cidr
57+
cluster_max_pod_num = 32
58+
cluster_name = "tf_example_cluster"
59+
cluster_desc = "a tf example cluster for the kms test"
60+
cluster_max_service_num = 32
61+
cluster_internet = true
62+
cluster_version = "1.24.4"
63+
cluster_deploy_type = "MANAGED_CLUSTER"
64+
}
65+
66+
resource "tencentcloud_kms_key" "example" {
67+
alias = "tf-example-%s"
68+
description = "example of kms key instance"
69+
key_usage = "ENCRYPT_DECRYPT"
70+
is_enabled = true
71+
}
72+
73+
resource "tencentcloud_kubernetes_encryption_protection" "example" {
74+
cluster_id = tencentcloud_kubernetes_cluster.example.id
75+
kms_configuration {
76+
key_id = tencentcloud_kms_key.example.id
77+
kms_region = var.example_region
78+
}
79+
}
80+
81+
`

0 commit comments

Comments
 (0)