Skip to content

Commit 20595b1

Browse files
authored
Merge pull request #733 from tencentcloudstack/feat/cls-topic
Feat/cls topic
2 parents 8e5f91e + 10a1607 commit 20595b1

14 files changed

+394
-10
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ terraform.tfstate
1616
terraform.tfstate.backup
1717
terraform.tfstate.lock.info
1818
dist/
19-
gendoc/gendoc
19+
gendoc/gendoc
20+
.vscode/

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ require (
2828
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ckafka v1.0.199
2929
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb v1.0.283
3030
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cloudaudit v1.0.199
31-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cls v1.0.290
31+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cls v1.0.291
3232
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.264
3333
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.199
3434
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb v1.0.199

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb v1.0.283 h1:8pML0Ut
468468
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb v1.0.283/go.mod h1:zgARzAnsLzpLhdpAHrHTUilOXytH9aEJy5ssCdizVV0=
469469
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cloudaudit v1.0.199 h1:o41qFAFJGPDTLNWXs7nLw4fsDxFUCe5gkO2YXI9Ye6Q=
470470
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cloudaudit v1.0.199/go.mod h1:b7dNjabPys0/iLwRFd8MVE5EkJTNAh4qtaHQOOLchx4=
471+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cls v1.0.291 h1:r7GSk4v8od8srrBrpnsHz9Hxrq0fLNO2u1m4ABQVX5g=
472+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cls v1.0.291/go.mod h1:O5PQ2JoBWhmSNVils3vZyS1GPeRdXA8Lu23ZEjiUfZ8=
471473
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cls v1.0.290 h1:PJ/XwIXHUVIi/OwvtUKOSWB+x5qn42oJettG6bhHfNQ=
472474
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cls v1.0.290/go.mod h1:O5PQ2JoBWhmSNVils3vZyS1GPeRdXA8Lu23ZEjiUfZ8=
473475
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.194/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=

tencentcloud/connectivity/client.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,6 @@ func (me *TencentCloudClient) UseApiClient() *api.Client {
624624
return me.apiConn
625625
}
626626

627-
628627
// UseClsClient return CLS client for service
629628
func (me *TencentCloudClient) UseClsClient() *cls.Client {
630629
if me.clsConn != nil {
@@ -635,4 +634,4 @@ func (me *TencentCloudClient) UseClsClient() *cls.Client {
635634
me.clsConn.WithHttpTransport(&LogRoundTripper{})
636635

637636
return me.clsConn
638-
}
637+
}

tencentcloud/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ Cloud Load Balancer(CLB)
206206
207207
Resource
208208
tencentcloud_clb_instance
209+
tencentcloud_clb_log_topic
209210
tencentcloud_clb_listener
210211
tencentcloud_clb_listener_rule
211212
tencentcloud_clb_attachment
@@ -860,6 +861,7 @@ func Provider() terraform.ResourceProvider {
860861
"tencentcloud_lb": resourceTencentCloudLB(),
861862
"tencentcloud_alb_server_attachment": resourceTencentCloudAlbServerAttachment(),
862863
"tencentcloud_clb_instance": resourceTencentCloudClbInstance(),
864+
"tencentcloud_clb_log_topic": resourceTencentCloudClbLogTopic(),
863865
"tencentcloud_clb_listener": resourceTencentCloudClbListener(),
864866
"tencentcloud_clb_listener_rule": resourceTencentCloudClbListenerRule(),
865867
"tencentcloud_clb_attachment": resourceTencentCloudClbServerAttachment(),

tencentcloud/resource_tc_clb_instance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ func resourceTencentCloudClbInstanceCreate(d *schema.ResourceData, meta interfac
360360
request.SlaveZoneId = helper.String(v.(string))
361361
}
362362

363-
if v, ok := d.GetOk("load_balancer_pass_to_target"); ok{
363+
if v, ok := d.GetOk("load_balancer_pass_to_target"); ok {
364364
request.LoadBalancerPassToTarget = helper.Bool(v.(bool))
365365
}
366366
clbId := ""
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package tencentcloud
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-sdk/terraform"
10+
)
11+
12+
func TestAccTencentCloudClbInstanceTopic(t *testing.T) {
13+
14+
resource.Test(t, resource.TestCase{
15+
PreCheck: func() { testAccPreCheck(t) },
16+
Providers: testAccProviders,
17+
CheckDestroy: testAccCheckClbListenerRuleDestroy,
18+
Steps: []resource.TestStep{
19+
{
20+
Config: testAccClbInstanceTopic,
21+
Check: resource.ComposeTestCheckFunc(
22+
testAccCheckClbInstanceTopicExists("tencentcloud_clb_instances_topic.topic"),
23+
resource.TestCheckResourceAttr("tencentcloud_clb_instances_topic.topic", "topic_name", "clb-topic-test"),
24+
resource.TestCheckResourceAttr("tencentcloud_clb_instances_topic.topic", "partition_count", "3"),
25+
),
26+
},
27+
},
28+
})
29+
}
30+
31+
func testAccCheckClbInstanceTopicExists(n string) resource.TestCheckFunc {
32+
return func(s *terraform.State) error {
33+
logId := getLogId(contextNil)
34+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
35+
36+
rs, ok := s.RootModule().Resources[n]
37+
if !ok {
38+
return fmt.Errorf("[CHECK][CLB topic][Exists] check: CLB topic %s is not found", n)
39+
}
40+
if rs.Primary.ID == "" {
41+
return fmt.Errorf("[CHECK][CLB topic][Exists] check: CLB topic id is not set")
42+
}
43+
clsService := ClsService{
44+
client: testAccProvider.Meta().(*TencentCloudClient).apiV3Conn,
45+
}
46+
topicName := rs.Primary.Attributes["topic_name"]
47+
instance, err := clsService.DeleteTopicsByTopicName(ctx, topicName)
48+
if err != nil {
49+
return err
50+
}
51+
if instance == nil {
52+
return fmt.Errorf("[CHECK][CLB topic][Exists] id %s is not exist", rs.Primary.ID)
53+
}
54+
return nil
55+
}
56+
}
57+
58+
const testAccClbInstanceTopic = `
59+
resource "tencentcloud_clb_instances_topic" "topic" {
60+
topic_name="clb-topic-test"
61+
partition_count=3
62+
}
63+
`
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
Provides a resource to create a CLB instance topic.
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_clb_log_topic" "topic" {
8+
topic_name="clb-topic"
9+
partition_count=3
10+
}
11+
```
12+
13+
Import
14+
15+
CLB log topic can be imported using the id, e.g.
16+
17+
```
18+
$ terraform import tencentcloud_clb_log_topic.topic lb-7a0t6zqb
19+
*/
20+
package tencentcloud
21+
22+
import (
23+
"context"
24+
"log"
25+
"sync"
26+
27+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
28+
)
29+
30+
var clsActionMu = &sync.Mutex{}
31+
32+
func resourceTencentCloudClbLogTopic() *schema.Resource {
33+
return &schema.Resource{
34+
Create: resourceTencentCloudClbInstanceTopicCreate,
35+
Read: resourceTencentCloudClbInstanceTopicRead,
36+
Update: resourceTencentCloudClbInstanceTopicUpdate,
37+
Delete: resourceTencentCloudClbInstanceTopicDelete,
38+
Importer: &schema.ResourceImporter{
39+
State: schema.ImportStatePassthrough,
40+
},
41+
42+
Schema: map[string]*schema.Schema{
43+
"topic_name": {
44+
Type: schema.TypeString,
45+
Required: true,
46+
Description: "Log topic of CLB instance.",
47+
},
48+
"partition_count": {
49+
Type: schema.TypeInt,
50+
Optional: true,
51+
ValidateFunc: validateIntegerInRange(1, 10),
52+
Description: "Topic partition count of CLB instance.(Default 1).",
53+
},
54+
"limit": {
55+
Type: schema.TypeInt,
56+
Optional: true,
57+
Description: "Fetch topic info pagination limit.",
58+
},
59+
"offset": {
60+
Type: schema.TypeInt,
61+
Optional: true,
62+
Description: "Fetch topic info pagination offset.",
63+
},
64+
},
65+
}
66+
}
67+
68+
func resourceTencentCloudClbInstanceTopicRead(d *schema.ResourceData, meta interface{}) error {
69+
clsActionMu.Lock()
70+
defer clsActionMu.Unlock()
71+
logId := getLogId(contextNil)
72+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
73+
var topicName string
74+
if value, ok := d.GetOk("topic_name"); ok {
75+
topicName = value.(string)
76+
}
77+
clsService := ClsService{
78+
client: meta.(*TencentCloudClient).apiV3Conn,
79+
}
80+
res, ok := clsService.DescribeTopicsByTopicName(ctx, topicName)
81+
if ok != nil {
82+
return ok
83+
}
84+
_ = d.Set("logset_id", res.LogsetId)
85+
_ = d.Set("topic_id", res.TopicId)
86+
_ = d.Set("topic_name", res.TopicName)
87+
_ = d.Set("partition_count", res.PartitionCount)
88+
_ = d.Set("index", res.Index)
89+
_ = d.Set("create_time", res.CreateTime)
90+
_ = d.Set("status", res.Status)
91+
_ = d.Set("tags", res.Tags)
92+
_ = d.Set("auto_split", res.AutoSplit)
93+
_ = d.Set("max_split_partitions", res.MaxSplitPartitions)
94+
_ = d.Set("storage_type", res.StorageType)
95+
_ = d.Set("period", res.Period)
96+
return nil
97+
98+
}
99+
100+
func resourceTencentCloudClbInstanceTopicUpdate(d *schema.ResourceData, meta interface{}) error {
101+
return nil
102+
}
103+
104+
func resourceTencentCloudClbInstanceTopicDelete(d *schema.ResourceData, meta interface{}) error {
105+
clsActionMu.Lock()
106+
defer clsActionMu.Unlock()
107+
logId := getLogId(contextNil)
108+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
109+
var topicName string
110+
if value, ok := d.GetOk("topic_name"); ok {
111+
topicName = value.(string)
112+
}
113+
clsService := ClsService{
114+
client: meta.(*TencentCloudClient).apiV3Conn,
115+
}
116+
_, ok := clsService.DeleteTopicsByTopicName(ctx, topicName)
117+
if ok != nil {
118+
return ok
119+
}
120+
return nil
121+
}
122+
123+
func resourceTencentCloudClbInstanceTopicCreate(d *schema.ResourceData, meta interface{}) error {
124+
logId := getLogId(contextNil)
125+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
126+
params := make(map[string]interface{})
127+
if topicName, ok := d.GetOk("topic_name"); ok {
128+
params["topic_name"] = topicName
129+
}
130+
if partitionCount, ok := d.GetOk("partition_count"); ok {
131+
params["partition_count"] = partitionCount
132+
}
133+
clbService := ClbService{
134+
client: meta.(*TencentCloudClient).apiV3Conn,
135+
}
136+
resp, err := clbService.CreateTopic(ctx, params)
137+
d.SetId(*resp.Response.TopicId)
138+
if err != nil {
139+
log.Printf("[CRITAL]%s create CLB topic failed, reason:%+v", logId, err)
140+
return err
141+
}
142+
return resourceTencentCloudClbInstanceTopicRead(d, meta)
143+
}

tencentcloud/service_tencentcloud_clb.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
1111
"github.com/pkg/errors"
1212
clb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb/v20180317"
13+
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
1314
sdkErrors "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
1415
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/connectivity"
1516
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
@@ -1058,6 +1059,31 @@ func (me *ClbService) CreateTargetGroup(ctx context.Context, targetGroupName str
10581059
return
10591060
}
10601061

1062+
func (me *ClbService) CreateTopic(ctx context.Context, params map[string]interface{}) (response *clb.CreateTopicResponse, err error) {
1063+
1064+
request := clb.NewCreateTopicRequest()
1065+
1066+
if topicName, ok := params["topic_name"]; ok {
1067+
request.TopicName = common.StringPtr(topicName.(string))
1068+
}
1069+
1070+
if partitionCount, ok := params["partition_count"]; ok {
1071+
request.PartitionCount = common.Uint64Ptr((uint64)(partitionCount.(int)))
1072+
}
1073+
1074+
err = resource.Retry(writeRetryTimeout, func() *resource.RetryError {
1075+
ratelimit.Check(request.GetAction())
1076+
resp, ok := me.client.UseClbClient().CreateTopic(request)
1077+
if ok != nil {
1078+
err = ok
1079+
return retryError(ok)
1080+
}
1081+
response = resp
1082+
return nil
1083+
})
1084+
return
1085+
}
1086+
10611087
func (me *ClbService) ModifyTargetGroup(ctx context.Context, targetGroupId, targetGroupName string, port uint64) (err error) {
10621088
request := clb.NewModifyTargetGroupAttributeRequest()
10631089
request.TargetGroupId = &targetGroupId

0 commit comments

Comments
 (0)