|
| 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 | +} |
0 commit comments