Skip to content

Commit 6fcc17a

Browse files
authored
Feat/cdwch support (#2377)
* add cdwch resource * make doc * update cdwch resource hcl * add changelog 2377.txt * xml_config support terraform import * update markdown
1 parent f98c188 commit 6fcc17a

13 files changed

+881
-0
lines changed

.changelog/2377.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```release-note:new-resource
2+
tencentcloud_clickhouse_keyval_config
3+
```
4+
5+
```release-note:new-resource
6+
tencentcloud_clickhouse_xml_config
7+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Provides a resource to create a clickhouse keyval_config
2+
3+
Example Usage
4+
5+
```hcl
6+
resource "tencentcloud_clickhouse_keyval_config" "keyval_config" {
7+
instance_id = "cdwch-datuhk3z"
8+
items {
9+
conf_key = "max_open_files"
10+
conf_value = "50000"
11+
}
12+
}
13+
```
14+
15+
Import
16+
17+
clickhouse config can be imported using the id, e.g.
18+
19+
```
20+
terraform import tencentcloud_clickhouse_keyval_config.config cdwch-datuhk3z#max_open_files#50000
21+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Provides a resource to create a clickhouse xml_config
2+
3+
Example Usage
4+
5+
```hcl
6+
resource "tencentcloud_clickhouse_xml_config" "xml_config" {
7+
instance_id = "cdwch-datuhk3z"
8+
modify_conf_context {
9+
file_name = "metrika.xml"
10+
new_conf_value = "PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPHlhbmRleD4KICAgIDx6b29rZWVwZXItc2VydmVycz4KICAgIDwvem9va2VlcGVyLXNlcnZlcnM+CjwveWFuZGV4Pgo="
11+
file_path = "/etc/clickhouse-server"
12+
}
13+
}
14+
```
15+
16+
Import
17+
18+
clickhouse xml_config can be imported using the id, e.g.
19+
20+
```
21+
terraform import tencentcloud_clickhouse_xml_config.xml_config cdwch-datuhk3z#metrika.xml
22+
```

tencentcloud/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,6 +1826,8 @@ func Provider() *schema.Provider {
18261826
"tencentcloud_bi_embed_token_apply": resourceTencentCloudBiEmbedTokenApply(),
18271827
"tencentcloud_bi_embed_interval_apply": resourceTencentCloudBiEmbedIntervalApply(),
18281828
"tencentcloud_cdwpg_instance": resourceTencentCloudCdwpgInstance(),
1829+
"tencentcloud_clickhouse_keyval_config": resourceTencentCloudClickhouseKeyvalConfig(),
1830+
"tencentcloud_clickhouse_xml_config": resourceTencentCloudClickhouseXmlConfig(),
18291831
},
18301832

18311833
ConfigureFunc: providerConfigure,

tencentcloud/provider.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,6 +1922,8 @@ ClickHouse(CDWCH)
19221922
tencentcloud_clickhouse_delete_backup_data
19231923
tencentcloud_clickhouse_account
19241924
tencentcloud_clickhouse_account_permission
1925+
tencentcloud_clickhouse_keyval_config
1926+
tencentcloud_clickhouse_xml_config
19251927

19261928
Tag
19271929
Resource
Lines changed: 282 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,282 @@
1+
package tencentcloud
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"strings"
8+
"time"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
cdwch "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdwch/v20200915"
13+
)
14+
15+
func resourceTencentCloudClickhouseKeyvalConfig() *schema.Resource {
16+
return &schema.Resource{
17+
Create: resourceTencentCloudClickhouseKeyvalConfigCreate,
18+
Read: resourceTencentCloudClickhouseKeyvalConfigRead,
19+
Update: resourceTencentCloudClickhouseKeyvalConfigUpdate,
20+
Delete: resourceTencentCloudClickhouseKeyvalConfigDelete,
21+
Importer: &schema.ResourceImporter{
22+
State: schema.ImportStatePassthrough,
23+
},
24+
Schema: map[string]*schema.Schema{
25+
"instance_id": {
26+
Required: true,
27+
ForceNew: true,
28+
Type: schema.TypeString,
29+
Description: "Instance ID.",
30+
},
31+
32+
"items": {
33+
Required: true,
34+
Type: schema.TypeList,
35+
MaxItems: 1,
36+
Description: "configuration list.",
37+
Elem: &schema.Resource{
38+
Schema: map[string]*schema.Schema{
39+
"conf_key": {
40+
Type: schema.TypeString,
41+
Required: true,
42+
Description: "Instance config key.",
43+
},
44+
"conf_value": {
45+
Type: schema.TypeString,
46+
Required: true,
47+
Description: "Instance config value.",
48+
},
49+
},
50+
},
51+
},
52+
},
53+
}
54+
}
55+
56+
func resourceTencentCloudClickhouseKeyvalConfigCreate(d *schema.ResourceData, meta interface{}) error {
57+
defer logElapsed("resource.tencentcloud_clickhouse_keyval_config.create")()
58+
defer inconsistentCheck(d, meta)()
59+
60+
logId := getLogId(contextNil)
61+
62+
request := cdwch.NewModifyInstanceKeyValConfigsRequest()
63+
64+
var ids []string
65+
var instanceId string
66+
if v, ok := d.GetOk("instance_id"); ok {
67+
instanceId = v.(string)
68+
}
69+
ids = append(ids, instanceId)
70+
71+
var addItems []*cdwch.InstanceConfigItem
72+
if row, ok := d.GetOk("items"); ok {
73+
items := row.([]interface{})
74+
for _, v := range items {
75+
value := v.(map[string]interface{})
76+
configKey := value["conf_key"].(string)
77+
configValue := value["conf_value"].(string)
78+
79+
addItems = append(addItems, &cdwch.InstanceConfigItem{
80+
ConfKey: &configKey,
81+
ConfValue: &configValue,
82+
})
83+
ids = append(ids, configKey, configValue)
84+
}
85+
}
86+
87+
request.InstanceId = &instanceId
88+
request.AddItems = addItems
89+
90+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
91+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseCdwchClient().ModifyInstanceKeyValConfigs(request)
92+
if e != nil {
93+
return retryError(e)
94+
} else {
95+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
96+
}
97+
return nil
98+
})
99+
if err != nil {
100+
log.Printf("[CRITAL]%s update cdwch config failed, reason:%+v", logId, err)
101+
return err
102+
}
103+
104+
service := CdwchService{client: meta.(*TencentCloudClient).apiV3Conn}
105+
conf := BuildStateChangeConf([]string{}, []string{"Serving"}, 10*readRetryTimeout, time.Second, service.InstanceStateRefreshFunc(instanceId))
106+
107+
if _, e := conf.WaitForState(); e != nil {
108+
return e
109+
}
110+
111+
d.SetId(strings.Join(ids, FILED_SP))
112+
113+
return resourceTencentCloudClickhouseKeyvalConfigRead(d, meta)
114+
}
115+
116+
func resourceTencentCloudClickhouseKeyvalConfigRead(d *schema.ResourceData, meta interface{}) error {
117+
defer logElapsed("resource.tencentcloud_clickhouse_keyval_config.read")()
118+
defer inconsistentCheck(d, meta)()
119+
120+
logId := getLogId(contextNil)
121+
122+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
123+
124+
service := CdwchService{client: meta.(*TencentCloudClient).apiV3Conn}
125+
126+
idSplit := strings.Split(d.Id(), FILED_SP)
127+
if len(idSplit) != 3 {
128+
return fmt.Errorf("id is broken,%s", d.Id())
129+
}
130+
instanceId := idSplit[0]
131+
configKey := idSplit[1]
132+
133+
_ = d.Set("instance_id", instanceId)
134+
135+
config, err := service.DescribeClickhouseKeyvalConfigById(ctx, instanceId)
136+
if err != nil {
137+
return err
138+
}
139+
if config == nil {
140+
d.SetId("")
141+
log.Printf("[WARN]%s resource `ClickhouseKeyvalConfig` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
142+
return nil
143+
}
144+
145+
var items []*cdwch.InstanceConfigInfo
146+
items = append(items, config.ConfigItems...)
147+
items = append(items, config.UnConfigItems...)
148+
149+
resultMap := make(map[string]*cdwch.InstanceConfigInfo)
150+
for _, item := range items {
151+
if item.ConfKey != nil {
152+
resultMap[*item.ConfKey] = item
153+
}
154+
}
155+
156+
var itemsList []interface{}
157+
item := resultMap[configKey]
158+
if item != nil {
159+
itemsMap := map[string]interface{}{}
160+
itemsMap["conf_key"] = item.ConfKey
161+
itemsMap["conf_value"] = item.ConfValue
162+
itemsList = append(itemsList, itemsMap)
163+
}
164+
_ = d.Set("items", itemsList)
165+
166+
return nil
167+
}
168+
169+
func resourceTencentCloudClickhouseKeyvalConfigUpdate(d *schema.ResourceData, meta interface{}) error {
170+
defer logElapsed("resource.tencentcloud_clickhouse_keyval_config.update")()
171+
defer inconsistentCheck(d, meta)()
172+
173+
logId := getLogId(contextNil)
174+
175+
request := cdwch.NewModifyInstanceKeyValConfigsRequest()
176+
177+
idSplit := strings.Split(d.Id(), FILED_SP)
178+
if len(idSplit) != 3 {
179+
return fmt.Errorf("id is broken,%s", d.Id())
180+
}
181+
instanceId := idSplit[0]
182+
configKey := idSplit[1]
183+
184+
var ids []string
185+
ids = append(ids, instanceId)
186+
187+
var updateItems []*cdwch.InstanceConfigItem
188+
if d.HasChange("items") {
189+
items := d.Get("items").([]interface{})
190+
for _, v := range items {
191+
value := v.(map[string]interface{})
192+
newConfigKey := value["conf_key"].(string)
193+
newConfigValue := value["conf_value"].(string)
194+
195+
if configKey != newConfigKey {
196+
return fmt.Errorf("`conf_key` is not allowed to be modified when updating the configuration list")
197+
}
198+
updateItems = append(updateItems, &cdwch.InstanceConfigItem{
199+
ConfKey: &newConfigKey,
200+
ConfValue: &newConfigValue,
201+
})
202+
ids = append(ids, newConfigKey, newConfigValue)
203+
}
204+
}
205+
206+
request.InstanceId = &instanceId
207+
request.UpdateItems = updateItems
208+
209+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
210+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseCdwchClient().ModifyInstanceKeyValConfigs(request)
211+
if e != nil {
212+
return retryError(e)
213+
} else {
214+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
215+
}
216+
return nil
217+
})
218+
if err != nil {
219+
log.Printf("[CRITAL]%s update cdwch config failed, reason:%+v", logId, err)
220+
return err
221+
}
222+
223+
service := CdwchService{client: meta.(*TencentCloudClient).apiV3Conn}
224+
conf := BuildStateChangeConf([]string{}, []string{"Serving"}, 10*readRetryTimeout, time.Second, service.InstanceStateRefreshFunc(instanceId))
225+
226+
if _, e := conf.WaitForState(); e != nil {
227+
return e
228+
}
229+
230+
d.SetId(strings.Join(ids, FILED_SP))
231+
232+
return resourceTencentCloudClickhouseKeyvalConfigRead(d, meta)
233+
}
234+
235+
func resourceTencentCloudClickhouseKeyvalConfigDelete(d *schema.ResourceData, meta interface{}) error {
236+
defer logElapsed("resource.tencentcloud_clickhouse_keyval_config.delete")()
237+
defer inconsistentCheck(d, meta)()
238+
239+
logId := getLogId(contextNil)
240+
241+
request := cdwch.NewModifyInstanceKeyValConfigsRequest()
242+
243+
idSplit := strings.Split(d.Id(), FILED_SP)
244+
if len(idSplit) != 3 {
245+
return fmt.Errorf("id is broken,%s", d.Id())
246+
}
247+
instanceId := idSplit[0]
248+
configKey := idSplit[1]
249+
configValue := idSplit[2]
250+
251+
var delItems []*cdwch.InstanceConfigItem
252+
delItems = append(delItems, &cdwch.InstanceConfigItem{
253+
ConfKey: &configKey,
254+
ConfValue: &configValue,
255+
})
256+
257+
request.InstanceId = &instanceId
258+
request.DelItems = delItems
259+
260+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
261+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseCdwchClient().ModifyInstanceKeyValConfigs(request)
262+
if e != nil {
263+
return retryError(e)
264+
} else {
265+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
266+
}
267+
return nil
268+
})
269+
if err != nil {
270+
log.Printf("[CRITAL]%s update cdwch config failed, reason:%+v", logId, err)
271+
return err
272+
}
273+
274+
service := CdwchService{client: meta.(*TencentCloudClient).apiV3Conn}
275+
conf := BuildStateChangeConf([]string{}, []string{"Serving"}, 10*readRetryTimeout, time.Second, service.InstanceStateRefreshFunc(instanceId))
276+
277+
if _, e := conf.WaitForState(); e != nil {
278+
return e
279+
}
280+
281+
return nil
282+
}

0 commit comments

Comments
 (0)