Skip to content

Commit 9c892a7

Browse files
authored
cls support import (#1815)
* cls support import * cls support import * fix lint error
1 parent 744dc16 commit 9c892a7

11 files changed

+957
-57
lines changed

.changelog/1815.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
```release-note:enhancement
2+
resource/tencentcloud_cls_config: support import function
3+
```
4+
5+
```release-note:enhancement
6+
resource/tencentcloud_cls_config_extra: support import function
7+
```
8+
9+
```release-note:enhancement
10+
resource/tencentcloud_cls_config_attachment: support import function
11+
```

tencentcloud/resource_tc_cls_config.go

Lines changed: 302 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ resource "tencentcloud_cls_config" "config" {
3333
# user_define_rule = ""
3434
}
3535
```
36+
37+
Import
38+
39+
cls config can be imported using the id, e.g.
40+
41+
```
42+
terraform import tencentcloud_cls_config.config config_id
43+
```
3644
*/
3745
package tencentcloud
3846

@@ -53,6 +61,9 @@ func resourceTencentCloudClsConfig() *schema.Resource {
5361
Read: resourceTencentCloudClsConfigRead,
5462
Delete: resourceTencentCloudClsConfigDelete,
5563
Update: resourceTencentCloudClsConfigUpdate,
64+
Importer: &schema.ResourceImporter{
65+
State: schema.ImportStatePassthrough,
66+
},
5667
Schema: map[string]*schema.Schema{
5768
"name": {
5869
Type: schema.TypeString,
@@ -147,6 +158,60 @@ func resourceTencentCloudClsConfig() *schema.Resource {
147158
Optional: true,
148159
Description: "Size of the data to be rewound in incremental collection mode. Default value: -1 (full collection).",
149160
},
161+
"is_gbk": {
162+
Type: schema.TypeInt,
163+
Optional: true,
164+
Description: "GBK encoding. Default 0.",
165+
},
166+
"json_standard": {
167+
Type: schema.TypeInt,
168+
Optional: true,
169+
Description: "standard json. Default 0.",
170+
},
171+
"protocol": {
172+
Type: schema.TypeString,
173+
Optional: true,
174+
Description: "syslog protocol, tcp or udp.",
175+
},
176+
"address": {
177+
Type: schema.TypeString,
178+
Optional: true,
179+
Description: "syslog system log collection specifies the address and port that the collector listens to.",
180+
},
181+
"parse_protocol": {
182+
Type: schema.TypeString,
183+
Optional: true,
184+
Description: "parse protocol.",
185+
},
186+
"metadata_type": {
187+
Type: schema.TypeInt,
188+
Optional: true,
189+
Description: "metadata type.",
190+
},
191+
"path_regex": {
192+
Type: schema.TypeString,
193+
Optional: true,
194+
Description: "metadata path regex.",
195+
},
196+
"meta_tags": {
197+
Type: schema.TypeList,
198+
Optional: true,
199+
Description: "metadata tags.",
200+
Elem: &schema.Resource{
201+
Schema: map[string]*schema.Schema{
202+
"key": {
203+
Type: schema.TypeString,
204+
Optional: true,
205+
Description: "tag key.",
206+
},
207+
"value": {
208+
Type: schema.TypeString,
209+
Optional: true,
210+
Description: "tag value.",
211+
},
212+
},
213+
},
214+
},
150215
},
151216
},
152217
},
@@ -252,6 +317,40 @@ func resourceTencentCloudClsConfigCreate(d *schema.ResourceData, meta interface{
252317
if v, ok := dMap["backtracking"]; ok {
253318
extractRule.Backtracking = helper.IntInt64(v.(int))
254319
}
320+
if v, ok := dMap["is_gbk"]; ok {
321+
extractRule.IsGBK = helper.IntInt64(v.(int))
322+
}
323+
if v, ok := dMap["json_standard"]; ok {
324+
extractRule.JsonStandard = helper.IntInt64(v.(int))
325+
}
326+
if v, ok := dMap["protocol"]; ok {
327+
extractRule.Protocol = helper.String(v.(string))
328+
}
329+
if v, ok := dMap["address"]; ok {
330+
extractRule.Address = helper.String(v.(string))
331+
}
332+
if v, ok := dMap["parse_protocol"]; ok {
333+
extractRule.ParseProtocol = helper.String(v.(string))
334+
}
335+
if v, ok := dMap["metadata_type"]; ok {
336+
extractRule.MetadataType = helper.IntInt64(v.(int))
337+
}
338+
if v, ok := dMap["path_regex"]; ok {
339+
extractRule.PathRegex = helper.String(v.(string))
340+
}
341+
if v, ok := dMap["meta_tags"]; ok {
342+
for _, item := range v.([]interface{}) {
343+
metaTagsMap := item.(map[string]interface{})
344+
metaTagInfo := cls.MetaTagInfo{}
345+
if v, ok := metaTagsMap["key"]; ok {
346+
metaTagInfo.Key = helper.String(v.(string))
347+
}
348+
if v, ok := metaTagsMap["value"]; ok {
349+
metaTagInfo.Value = helper.String(v.(string))
350+
}
351+
extractRule.MetaTags = append(extractRule.MetaTags, &metaTagInfo)
352+
}
353+
}
255354
extractRules = append(extractRules, &extractRule)
256355
request.ExtractRule = extractRules[0]
257356
}
@@ -293,11 +392,178 @@ func resourceTencentCloudClsConfigCreate(d *schema.ResourceData, meta interface{
293392

294393
id := *response.Response.ConfigId
295394
d.SetId(id)
296-
return nil
395+
return resourceTencentCloudClsConfigRead(d, meta)
297396
}
298397

299398
func resourceTencentCloudClsConfigRead(d *schema.ResourceData, meta interface{}) error {
300399
defer logElapsed("resource.tencentcloud_cls_config.read")()
400+
defer inconsistentCheck(d, meta)()
401+
402+
logId := getLogId(contextNil)
403+
404+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
405+
406+
service := ClsService{client: meta.(*TencentCloudClient).apiV3Conn}
407+
408+
configId := d.Id()
409+
410+
config, err := service.DescribeClsConfigById(ctx, configId)
411+
if err != nil {
412+
return err
413+
}
414+
415+
if config == nil {
416+
d.SetId("")
417+
log.Printf("[WARN]%s resource `ClsConfig` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
418+
return nil
419+
}
420+
421+
if config.Name != nil {
422+
_ = d.Set("name", config.Name)
423+
}
424+
425+
if config.Output != nil {
426+
_ = d.Set("output", config.Output)
427+
}
428+
429+
if config.Path != nil {
430+
_ = d.Set("path", config.Path)
431+
}
432+
433+
if config.LogType != nil {
434+
_ = d.Set("log_type", config.LogType)
435+
}
436+
437+
if config.ExtractRule != nil {
438+
extractRuleMap := map[string]interface{}{}
439+
440+
if config.ExtractRule.TimeKey != nil {
441+
extractRuleMap["time_key"] = config.ExtractRule.TimeKey
442+
}
443+
444+
if config.ExtractRule.TimeFormat != nil {
445+
extractRuleMap["time_format"] = config.ExtractRule.TimeFormat
446+
}
447+
448+
if config.ExtractRule.Delimiter != nil {
449+
extractRuleMap["delimiter"] = config.ExtractRule.Delimiter
450+
}
451+
452+
if config.ExtractRule.LogRegex != nil {
453+
extractRuleMap["log_regex"] = config.ExtractRule.LogRegex
454+
}
455+
456+
if config.ExtractRule.BeginRegex != nil {
457+
extractRuleMap["begin_regex"] = config.ExtractRule.BeginRegex
458+
}
459+
460+
if config.ExtractRule.Keys != nil {
461+
extractRuleMap["keys"] = config.ExtractRule.Keys
462+
}
463+
464+
if config.ExtractRule.FilterKeyRegex != nil {
465+
filterKeyRegexList := []interface{}{}
466+
for _, filterKeyRegex := range config.ExtractRule.FilterKeyRegex {
467+
filterKeyRegexMap := map[string]interface{}{}
468+
469+
if filterKeyRegex.Key != nil {
470+
filterKeyRegexMap["key"] = filterKeyRegex.Key
471+
}
472+
473+
if filterKeyRegex.Regex != nil {
474+
filterKeyRegexMap["regex"] = filterKeyRegex.Regex
475+
}
476+
477+
filterKeyRegexList = append(filterKeyRegexList, filterKeyRegexMap)
478+
}
479+
480+
extractRuleMap["filter_key_regex"] = filterKeyRegexList
481+
}
482+
483+
if config.ExtractRule.UnMatchUpLoadSwitch != nil {
484+
extractRuleMap["un_match_up_load_switch"] = config.ExtractRule.UnMatchUpLoadSwitch
485+
}
486+
487+
if config.ExtractRule.UnMatchLogKey != nil {
488+
extractRuleMap["un_match_log_key"] = config.ExtractRule.UnMatchLogKey
489+
}
490+
491+
if config.ExtractRule.Backtracking != nil {
492+
extractRuleMap["backtracking"] = config.ExtractRule.Backtracking
493+
}
494+
495+
if config.ExtractRule.IsGBK != nil {
496+
extractRuleMap["is_gbk"] = config.ExtractRule.IsGBK
497+
}
498+
499+
if config.ExtractRule.JsonStandard != nil {
500+
extractRuleMap["json_standard"] = config.ExtractRule.JsonStandard
501+
}
502+
503+
if config.ExtractRule.Protocol != nil {
504+
extractRuleMap["protocol"] = config.ExtractRule.Protocol
505+
}
506+
507+
if config.ExtractRule.Address != nil {
508+
extractRuleMap["address"] = config.ExtractRule.Address
509+
}
510+
511+
if config.ExtractRule.ParseProtocol != nil {
512+
extractRuleMap["parse_protocol"] = config.ExtractRule.ParseProtocol
513+
}
514+
515+
if config.ExtractRule.MetadataType != nil {
516+
extractRuleMap["metadata_type"] = config.ExtractRule.MetadataType
517+
}
518+
519+
if config.ExtractRule.PathRegex != nil {
520+
extractRuleMap["path_regex"] = config.ExtractRule.PathRegex
521+
}
522+
523+
if config.ExtractRule.MetaTags != nil {
524+
metaTagsList := []interface{}{}
525+
for _, metaTags := range config.ExtractRule.MetaTags {
526+
metaTagsMap := map[string]interface{}{}
527+
528+
if metaTags.Key != nil {
529+
metaTagsMap["key"] = metaTags.Key
530+
}
531+
532+
if metaTags.Value != nil {
533+
metaTagsMap["value"] = metaTags.Value
534+
}
535+
536+
metaTagsList = append(metaTagsList, metaTagsMap)
537+
}
538+
539+
extractRuleMap["meta_tags"] = metaTagsList
540+
}
541+
542+
_ = d.Set("extract_rule", []interface{}{extractRuleMap})
543+
}
544+
545+
if config.ExcludePaths != nil {
546+
excludePathsList := []interface{}{}
547+
for _, excludePath := range config.ExcludePaths {
548+
excludePathsMap := map[string]interface{}{}
549+
550+
if excludePath.Type != nil {
551+
excludePathsMap["type"] = excludePath.Type
552+
}
553+
554+
if excludePath.Value != nil {
555+
excludePathsMap["value"] = excludePath.Value
556+
}
557+
558+
excludePathsList = append(excludePathsList, excludePathsMap)
559+
}
560+
561+
_ = d.Set("exclude_paths", excludePathsList)
562+
}
563+
564+
if config.UserDefineRule != nil {
565+
_ = d.Set("user_define_rule", config.UserDefineRule)
566+
}
301567

302568
return nil
303569
}
@@ -380,6 +646,40 @@ func resourceTencentCloudClsConfigUpdate(d *schema.ResourceData, meta interface{
380646
if v, ok := dMap["backtracking"]; ok {
381647
extractRule.Backtracking = helper.IntInt64(v.(int))
382648
}
649+
if v, ok := dMap["is_gbk"]; ok {
650+
extractRule.IsGBK = helper.IntInt64(v.(int))
651+
}
652+
if v, ok := dMap["json_standard"]; ok {
653+
extractRule.JsonStandard = helper.IntInt64(v.(int))
654+
}
655+
if v, ok := dMap["protocol"]; ok {
656+
extractRule.Protocol = helper.String(v.(string))
657+
}
658+
if v, ok := dMap["address"]; ok {
659+
extractRule.Address = helper.String(v.(string))
660+
}
661+
if v, ok := dMap["parse_protocol"]; ok {
662+
extractRule.ParseProtocol = helper.String(v.(string))
663+
}
664+
if v, ok := dMap["metadata_type"]; ok {
665+
extractRule.MetadataType = helper.IntInt64(v.(int))
666+
}
667+
if v, ok := dMap["path_regex"]; ok {
668+
extractRule.PathRegex = helper.String(v.(string))
669+
}
670+
if v, ok := dMap["meta_tags"]; ok {
671+
for _, item := range v.([]interface{}) {
672+
metaTagsMap := item.(map[string]interface{})
673+
metaTagInfo := cls.MetaTagInfo{}
674+
if v, ok := metaTagsMap["key"]; ok {
675+
metaTagInfo.Key = helper.String(v.(string))
676+
}
677+
if v, ok := metaTagsMap["value"]; ok {
678+
metaTagInfo.Value = helper.String(v.(string))
679+
}
680+
extractRule.MetaTags = append(extractRule.MetaTags, &metaTagInfo)
681+
}
682+
}
383683
extractRules = append(extractRules, &extractRule)
384684
request.ExtractRule = extractRules[0]
385685
}
@@ -423,7 +723,7 @@ func resourceTencentCloudClsConfigUpdate(d *schema.ResourceData, meta interface{
423723
return err
424724
}
425725

426-
return nil
726+
return resourceTencentCloudClsConfigRead(d, meta)
427727
}
428728

429729
func resourceTencentCloudClsConfigDelete(d *schema.ResourceData, meta interface{}) error {

0 commit comments

Comments
 (0)