Skip to content

Commit 175ed7b

Browse files
authored
feat(clb): [128236197] add new resource (#3584)
* add * add * add
1 parent c4ac917 commit 175ed7b

12 files changed

+445
-22
lines changed

.changelog/3584.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_clb_cls_log_attachment
3+
```
4+
5+
```release-note:enhancement
6+
resource/tencentcloud_clb_log_topic: support `status`
7+
```

tencentcloud/provider.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1448,6 +1448,8 @@ func Provider() *schema.Provider {
14481448
"tencentcloud_clb_instance_sla_config": clb.ResourceTencentCloudClbInstanceSlaConfig(),
14491449
"tencentcloud_clb_replace_cert_for_lbs": clb.ResourceTencentCloudClbReplaceCertForLbs(),
14501450
"tencentcloud_clb_security_group_attachment": clb.ResourceTencentCloudClbSecurityGroupAttachment(),
1451+
"tencentcloud_clb_target_group_attachments": clb.ResourceTencentCloudClbTargetGroupAttachments(),
1452+
"tencentcloud_clb_cls_log_attachment": clb.ResourceTencentCloudClbClsLogAttachment(),
14511453
"tencentcloud_gwlb_instance": gwlb.ResourceTencentCloudGwlbInstance(),
14521454
"tencentcloud_gwlb_target_group": gwlb.ResourceTencentCloudGwlbTargetGroup(),
14531455
"tencentcloud_gwlb_instance_associate_target_group": gwlb.ResourceTencentCloudGwlbInstanceAssociateTargetGroup(),
@@ -2413,7 +2415,6 @@ func Provider() *schema.Provider {
24132415
"tencentcloud_cdwpg_userhba": cdwpg.ResourceTencentCloudCdwpgUserhba(),
24142416
"tencentcloud_clickhouse_keyval_config": cdwch.ResourceTencentCloudClickhouseKeyvalConfig(),
24152417
"tencentcloud_clickhouse_xml_config": cdwch.ResourceTencentCloudClickhouseXmlConfig(),
2416-
"tencentcloud_clb_target_group_attachments": clb.ResourceTencentCloudClbTargetGroupAttachments(),
24172418
"tencentcloud_vpc_peer_connect_manager": vpc.ResourceTencentCloudVpcPeerConnectManager(),
24182419
"tencentcloud_vpc_peer_connect_accept_operation": vpc.ResourceTencentCloudVpcPeerConnectAcceptOperation(),
24192420
"tencentcloud_vpc_peer_connect_reject_operation": vpc.ResourceTencentCloudVpcPeerConnectRejectOperation(),

tencentcloud/provider.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ tencentcloud_clb_instance_sla_config
418418
tencentcloud_clb_instance_mix_ip_target_config
419419
tencentcloud_clb_replace_cert_for_lbs
420420
tencentcloud_clb_security_group_attachment
421+
tencentcloud_clb_cls_log_attachment
421422

422423
Cloud Object Storage(COS)
423424
Data Source
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
package clb
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"strings"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
clbv20180317 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb/v20180317"
12+
13+
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
14+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
15+
)
16+
17+
func ResourceTencentCloudClbClsLogAttachment() *schema.Resource {
18+
return &schema.Resource{
19+
Create: resourceTencentCloudClbClsLogAttachmentCreate,
20+
Read: resourceTencentCloudClbClsLogAttachmentRead,
21+
Delete: resourceTencentCloudClbClsLogAttachmentDelete,
22+
Importer: &schema.ResourceImporter{
23+
State: schema.ImportStatePassthrough,
24+
},
25+
Schema: map[string]*schema.Schema{
26+
"load_balancer_id": {
27+
Type: schema.TypeString,
28+
Required: true,
29+
ForceNew: true,
30+
Description: "CLB instance ID.",
31+
},
32+
33+
"log_set_id": {
34+
Type: schema.TypeString,
35+
Required: true,
36+
ForceNew: true,
37+
Description: "Logset ID of the Cloud Log Service (CLS).<li>When adding or updating a log topic, call the [DescribeLogsets](https://intl.cloud.tencent.com/document/product/614/58624?from_cn_redirect=1) API to obtain the logset ID.</li><li>When deleting a log topic, set this parameter to null.</li>.",
38+
},
39+
40+
"log_topic_id": {
41+
Type: schema.TypeString,
42+
Required: true,
43+
ForceNew: true,
44+
Description: "Log topic ID of the CLS.<li>When adding or updating a log topic, call the [DescribeTopics](https://intl.cloud.tencent.com/document/product/614/56454?from_cn_redirect=1) API to obtain the log topic ID.</li><li>When deleting a log topic, set this parameter to null.</li>.",
45+
},
46+
47+
// "log_type": {
48+
// Type: schema.TypeString,
49+
// Optional: true,
50+
// ForceNew: true,
51+
// Computed: true,
52+
// Description: "Log type:\n<li>`ACCESS`: access logs</li>\n<li>`HEALTH`: health check logs</li>\nDefault: `ACCESS`.",
53+
// },
54+
},
55+
}
56+
}
57+
58+
func resourceTencentCloudClbClsLogAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
59+
defer tccommon.LogElapsed("resource.tencentcloud_clb_cls_log_attachment.create")()
60+
defer tccommon.InconsistentCheck(d, meta)()
61+
62+
var (
63+
logId = tccommon.GetLogId(tccommon.ContextNil)
64+
ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
65+
request = clbv20180317.NewSetLoadBalancerClsLogRequest()
66+
loadBalancerId string
67+
logSetId string
68+
logTopicId string
69+
)
70+
71+
if v, ok := d.GetOk("load_balancer_id"); ok {
72+
request.LoadBalancerId = helper.String(v.(string))
73+
loadBalancerId = v.(string)
74+
}
75+
76+
if v, ok := d.GetOk("log_set_id"); ok {
77+
request.LogSetId = helper.String(v.(string))
78+
logSetId = v.(string)
79+
}
80+
81+
if v, ok := d.GetOk("log_topic_id"); ok {
82+
request.LogTopicId = helper.String(v.(string))
83+
logTopicId = v.(string)
84+
}
85+
86+
// if v, ok := d.GetOk("log_type"); ok {
87+
// request.LogType = helper.String(v.(string))
88+
// }
89+
90+
reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
91+
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseClbClient().SetLoadBalancerClsLogWithContext(ctx, request)
92+
if e != nil {
93+
return tccommon.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+
98+
return nil
99+
})
100+
101+
if reqErr != nil {
102+
log.Printf("[CRITAL]%s create clb cls log attachment failed, reason:%+v", logId, reqErr)
103+
return reqErr
104+
}
105+
106+
d.SetId(strings.Join([]string{loadBalancerId, logSetId, logTopicId}, tccommon.FILED_SP))
107+
return resourceTencentCloudClbClsLogAttachmentRead(d, meta)
108+
}
109+
110+
func resourceTencentCloudClbClsLogAttachmentRead(d *schema.ResourceData, meta interface{}) error {
111+
defer tccommon.LogElapsed("resource.tencentcloud_clb_cls_log_attachment.read")()
112+
defer tccommon.InconsistentCheck(d, meta)()
113+
114+
var (
115+
logId = tccommon.GetLogId(tccommon.ContextNil)
116+
ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
117+
service = ClbService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
118+
)
119+
120+
idSplit := strings.Split(d.Id(), tccommon.FILED_SP)
121+
if len(idSplit) != 3 {
122+
return fmt.Errorf("id is broken,%s", d.Id())
123+
}
124+
125+
loadBalancerId := idSplit[0]
126+
logSetId := idSplit[1]
127+
logTopicId := idSplit[2]
128+
129+
respData, err := service.DescribeClbClsLogAttachmentById(ctx, loadBalancerId, logSetId, logTopicId)
130+
if err != nil {
131+
return err
132+
}
133+
134+
if respData == nil {
135+
log.Printf("[WARN]%s resource `tencentcloud_clb_cls_log_attachment` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
136+
d.SetId("")
137+
return nil
138+
}
139+
140+
if respData.LoadBalancerId != nil {
141+
_ = d.Set("load_balancer_id", respData.LoadBalancerId)
142+
}
143+
144+
if respData.LogSetId != nil && respData.LogTopicId != nil {
145+
if *respData.LogSetId == logSetId && *respData.LogTopicId == logTopicId {
146+
_ = d.Set("log_set_id", respData.LogSetId)
147+
_ = d.Set("log_topic_id", respData.LogTopicId)
148+
// _ = d.Set("log_type", "ACCESS")
149+
}
150+
}
151+
152+
// if respData.HealthLogSetId != nil && respData.HealthLogTopicId != nil {
153+
// if *respData.HealthLogSetId == logSetId && *respData.HealthLogTopicId == logTopicId {
154+
// _ = d.Set("log_set_id", respData.HealthLogSetId)
155+
// _ = d.Set("log_topic_id", respData.HealthLogTopicId)
156+
// _ = d.Set("log_type", "HEALTH")
157+
// }
158+
// }
159+
160+
return nil
161+
}
162+
163+
func resourceTencentCloudClbClsLogAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
164+
defer tccommon.LogElapsed("resource.tencentcloud_clb_cls_log_attachment.delete")()
165+
defer tccommon.InconsistentCheck(d, meta)()
166+
167+
var (
168+
logId = tccommon.GetLogId(tccommon.ContextNil)
169+
ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
170+
request = clbv20180317.NewSetLoadBalancerClsLogRequest()
171+
)
172+
173+
idSplit := strings.Split(d.Id(), tccommon.FILED_SP)
174+
if len(idSplit) != 3 {
175+
return fmt.Errorf("id is broken,%s", d.Id())
176+
}
177+
178+
loadBalancerId := idSplit[0]
179+
180+
request.LoadBalancerId = &loadBalancerId
181+
request.LogSetId = helper.String("")
182+
request.LogTopicId = helper.String("")
183+
reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
184+
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseClbClient().SetLoadBalancerClsLogWithContext(ctx, request)
185+
if e != nil {
186+
return tccommon.RetryError(e)
187+
} else {
188+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
189+
}
190+
191+
return nil
192+
})
193+
194+
if reqErr != nil {
195+
log.Printf("[CRITAL]%s delete clb cls log attachment failed, reason:%+v", logId, reqErr)
196+
return reqErr
197+
}
198+
199+
return nil
200+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Provides a resource to create a CLB cls log attachment
2+
3+
Example Usage
4+
5+
```hcl
6+
resource "tencentcloud_clb_log_topic" "example" {
7+
log_set_id = "2ed70190-bf06-4777-980d-2d8a327a2554"
8+
topic_name = "tf-example"
9+
status = true
10+
}
11+
12+
resource "tencentcloud_clb_cls_log_attachment" "example" {
13+
load_balancer_id = "lb-n26tx0bm"
14+
log_set_id = "2ed70190-bf06-4777-980d-2d8a327a2554"
15+
log_topic_id = tencentcloud_clb_log_topic.example.id
16+
}
17+
```
18+
19+
Import
20+
21+
CLB cls log attachment can be imported using the loadBalancerId#logSetId#logTopicId, e.g.
22+
23+
```
24+
terraform import tencentcloud_clb_cls_log_attachment.example lb-n26tx0bm#2ed70190-bf06-4777-980d-2d8a327a2554#ac2fda28-3e79-4b51-b193-bfcf1aeece24
25+
```
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package clb_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
8+
tcacctest "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest"
9+
)
10+
11+
func TestAccTencentCloudClbClsLogAttachmentResource_basic(t *testing.T) {
12+
t.Parallel()
13+
resource.Test(t, resource.TestCase{
14+
PreCheck: func() {
15+
tcacctest.AccPreCheck(t)
16+
},
17+
Providers: tcacctest.AccProviders,
18+
Steps: []resource.TestStep{{
19+
Config: testAccClbClsLogAttachment,
20+
Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttrSet("tencentcloud_clb_cls_log_attachment.clb_cls_log_attachment", "id")),
21+
}, {
22+
ResourceName: "tencentcloud_clb_cls_log_attachment.clb_cls_log_attachment",
23+
ImportState: true,
24+
ImportStateVerify: true,
25+
}},
26+
})
27+
}
28+
29+
const testAccClbClsLogAttachment = `
30+
31+
resource "tencentcloud_clb_cls_log_attachment" "clb_cls_log_attachment" {
32+
}
33+
`

0 commit comments

Comments
 (0)