Skip to content

Commit bd46bf1

Browse files
tongyimingmikatong
andauthored
Feat/ckafka route (#1983)
* support ckafka route * update * add changelog && update doc --------- Co-authored-by: mikatong <mikatong@tencent.com>
1 parent 419bbb4 commit bd46bf1

File tree

13 files changed

+1046
-66
lines changed

13 files changed

+1046
-66
lines changed

.changelog/1983.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-resource
2+
tencentcloud_ckafka_route
3+
```

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ require (
3838
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cfs v1.0.627
3939
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/chdfs v1.0.600
4040
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ciam v1.0.695
41-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ckafka v1.0.667
41+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ckafka v1.0.706
4242
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb v1.0.693
4343
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cloudaudit v1.0.544
4444
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cls v1.0.663

go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ciam v1.0.695 h1:FGwsF1
800800
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ciam v1.0.695/go.mod h1:HAasVoWz8ed6kAg7Q/DTg+8uZXiOgW7lmJeAGGrquEQ=
801801
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ckafka v1.0.667 h1:cecrsRRZ5bvKC/3DHvngMkkHe52+Run+73kLy/I/xLU=
802802
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ckafka v1.0.667/go.mod h1:oMEFOM7AR1K/5pJyhQ9x+TXWX3AsxCqYACdqZLxH/kA=
803+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ckafka v1.0.706 h1:m1CDOlYmPBuYh8vTcyfxyrRglbQ1CzKyGkJsjYcm31o=
804+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ckafka v1.0.706/go.mod h1:X78AG97YgOyUBW4ODoeOcTB5RW7RVjISDyheYQDEB3Y=
803805
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb v1.0.693 h1:3eI2Ciq3NOXgoxeW/eY6vgCzi8vcqEZv+FzHww6K9oU=
804806
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb v1.0.693/go.mod h1:WhFqrn14QuRHB01xA5Q9sC+xw73BpqSE5+ZnqEznCW8=
805807
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cloudaudit v1.0.544 h1:ApY6rS7I9otgujOdAFy0+Epno1PNVCQmsOoWQxx724Y=
@@ -851,10 +853,6 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.691/go.mod
851853
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.692/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
852854
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.693/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
853855
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.695/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
854-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.696/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
855-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.698/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
856-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.699/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
857-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.701/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
858856
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.706 h1:NjpSF2Fi/fz1KQrM8NqCeHMTBNWG1MHAw3BxvN2QZM4=
859857
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.706/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
860858
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.624 h1:nEZqsoqt1pEoaP9JjkHQy3/H00suCfzlHW1qOm2nYD8=

tencentcloud/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ Cloud Kafka(ckafka)
196196
tencentcloud_ckafka_consumer_group
197197
tencentcloud_ckafka_consumer_group_modify_offset
198198
tencentcloud_ckafka_datahub_task
199+
tencentcloud_ckafka_route
199200
200201
Cloud Access Management(CAM)
201202
Data Source
@@ -2572,6 +2573,7 @@ func Provider() *schema.Provider {
25722573
"tencentcloud_ckafka_consumer_group": resourceTencentCloudCkafkaConsumerGroup(),
25732574
"tencentcloud_ckafka_consumer_group_modify_offset": resourceTencentCloudCkafkaConsumerGroupModifyOffset(),
25742575
"tencentcloud_ckafka_datahub_task": resourceTencentCloudCkafkaDatahubTask(),
2576+
"tencentcloud_ckafka_route": resourceTencentCloudCkafkaRoute(),
25752577
"tencentcloud_audit": resourceTencentCloudAudit(),
25762578
"tencentcloud_audit_track": resourceTencentCloudAuditTrack(),
25772579
"tencentcloud_image": resourceTencentCloudImage(),
Lines changed: 332 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,332 @@
1+
/*
2+
Provides a resource to create a ckafka route
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_ckafka_route" "route" {
8+
instance_id = "ckafka-xxxxxx"
9+
vip_type = 3
10+
vpc_id = "vpc-xxxxxx"
11+
subnet_id = "subnet-xxxxxx"
12+
access_type = 0
13+
public_network = 3
14+
}
15+
```
16+
*/
17+
package tencentcloud
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"log"
23+
"strconv"
24+
"strings"
25+
"time"
26+
27+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
28+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
29+
ckafka "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ckafka/v20190819"
30+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
31+
)
32+
33+
func resourceTencentCloudCkafkaRoute() *schema.Resource {
34+
return &schema.Resource{
35+
Create: resourceTencentCloudCkafkaRouteCreate,
36+
Read: resourceTencentCloudCkafkaRouteRead,
37+
Update: resourceTencentCloudCkafkaRouteUpdate,
38+
Delete: resourceTencentCloudCkafkaRouteDelete,
39+
Importer: &schema.ResourceImporter{
40+
State: schema.ImportStatePassthrough,
41+
},
42+
Schema: map[string]*schema.Schema{
43+
"instance_id": {
44+
Required: true,
45+
Type: schema.TypeString,
46+
Description: "Instance id.",
47+
},
48+
49+
"vip_type": {
50+
Required: true,
51+
Type: schema.TypeInt,
52+
Description: "Routing network type (3:vpc routing; 4: standard support routing; 7: professional support routing).",
53+
},
54+
55+
"vpc_id": {
56+
Optional: true,
57+
Computed: true,
58+
Type: schema.TypeString,
59+
Description: "Vpc id.",
60+
},
61+
62+
"subnet_id": {
63+
Optional: true,
64+
Computed: true,
65+
Type: schema.TypeString,
66+
Description: "Subnet id.",
67+
},
68+
69+
"access_type": {
70+
Optional: true,
71+
Computed: true,
72+
Type: schema.TypeInt,
73+
Description: "Access type. Valid values:\n" +
74+
"- 0: PLAINTEXT (in clear text, supported by both the old version and the community version without user information)\n" +
75+
"- 1: SASL_PLAINTEXT (in clear text, but at the beginning of the data, authentication will be logged in through SASL, which is only supported by the community version)\n" +
76+
"- 2: SSL (SSL encrypted communication without user information, supported by both older and community versions)\n" +
77+
"- 3: SASL_SSL (SSL encrypted communication. When the data starts, authentication will be logged in through SASL. Only the community version supports it).",
78+
},
79+
80+
"auth_flag": {
81+
Optional: true,
82+
Type: schema.TypeInt,
83+
Description: "Auth flag.",
84+
},
85+
86+
"caller_appid": {
87+
Optional: true,
88+
Type: schema.TypeInt,
89+
Description: "Caller appid.",
90+
},
91+
92+
"public_network": {
93+
Optional: true,
94+
Type: schema.TypeInt,
95+
Description: "Public network.",
96+
},
97+
98+
"ip": {
99+
Optional: true,
100+
Computed: true,
101+
Type: schema.TypeString,
102+
Description: "Ip.",
103+
},
104+
"vip_list": {
105+
Type: schema.TypeList,
106+
Computed: true,
107+
Description: "Virtual IP list.",
108+
Elem: &schema.Resource{
109+
Schema: map[string]*schema.Schema{
110+
"vip": {
111+
Type: schema.TypeString,
112+
Computed: true,
113+
Description: "Virtual IP.",
114+
},
115+
"vport": {
116+
Type: schema.TypeString,
117+
Computed: true,
118+
Description: "Virtual port.",
119+
},
120+
},
121+
},
122+
},
123+
"broker_vip_list": {
124+
Type: schema.TypeList,
125+
Computed: true,
126+
Description: "Virtual IP list (1 to 1 broker nodes).",
127+
Elem: &schema.Resource{
128+
Schema: map[string]*schema.Schema{
129+
"vip": {
130+
Type: schema.TypeString,
131+
Computed: true,
132+
Description: "Virtual IP.",
133+
},
134+
"vport": {
135+
Type: schema.TypeString,
136+
Computed: true,
137+
Description: "Virtual port.",
138+
},
139+
},
140+
},
141+
},
142+
},
143+
}
144+
}
145+
146+
func resourceTencentCloudCkafkaRouteCreate(d *schema.ResourceData, meta interface{}) error {
147+
defer logElapsed("resource.tencentcloud_ckafka_route.create")()
148+
defer inconsistentCheck(d, meta)()
149+
150+
logId := getLogId(contextNil)
151+
152+
var (
153+
request = ckafka.NewCreateRouteRequest()
154+
response = ckafka.NewCreateRouteResponse()
155+
instanceId string
156+
)
157+
if v, ok := d.GetOk("instance_id"); ok {
158+
instanceId = v.(string)
159+
request.InstanceId = helper.String(instanceId)
160+
}
161+
162+
if v, ok := d.GetOkExists("vip_type"); ok {
163+
request.VipType = helper.IntInt64(v.(int))
164+
}
165+
166+
if v, ok := d.GetOk("vpc_id"); ok {
167+
request.VpcId = helper.String(v.(string))
168+
}
169+
170+
if v, ok := d.GetOk("subnet_id"); ok {
171+
request.SubnetId = helper.String(v.(string))
172+
}
173+
174+
if v, ok := d.GetOkExists("access_type"); ok {
175+
request.AccessType = helper.IntInt64(v.(int))
176+
}
177+
178+
if v, ok := d.GetOkExists("auth_flag"); ok {
179+
request.AuthFlag = helper.IntInt64(v.(int))
180+
}
181+
182+
if v, ok := d.GetOkExists("caller_appid"); ok {
183+
request.CallerAppid = helper.IntInt64(v.(int))
184+
}
185+
186+
if v, ok := d.GetOkExists("public_network"); ok {
187+
request.PublicNetwork = helper.IntInt64(v.(int))
188+
}
189+
190+
if v, ok := d.GetOk("ip"); ok {
191+
request.Ip = helper.String(v.(string))
192+
}
193+
194+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
195+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseCkafkaClient().CreateRoute(request)
196+
if e != nil {
197+
return retryError(e)
198+
} else {
199+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
200+
}
201+
response = result
202+
return nil
203+
})
204+
if err != nil {
205+
log.Printf("[CRITAL]%s create ckafka route failed, reason:%+v", logId, err)
206+
return err
207+
}
208+
log.Printf("Result.Data: %+v", response.Response.Result.Data)
209+
routeIdInt64 := *response.Response.Result.Data.RouteDTO.RouteId
210+
flowIdInt64 := *response.Response.Result.Data.FlowId
211+
reouteIdString := strconv.FormatInt(routeIdInt64, 10)
212+
d.SetId(instanceId + FILED_SP + reouteIdString)
213+
214+
service := CkafkaService{client: meta.(*TencentCloudClient).apiV3Conn}
215+
216+
conf := BuildStateChangeConf([]string{}, []string{"0"}, 1*readRetryTimeout, time.Second, service.CkafkaRouteStateRefreshFunc(flowIdInt64, []string{}))
217+
218+
if _, e := conf.WaitForState(); e != nil {
219+
return e
220+
}
221+
222+
return resourceTencentCloudCkafkaRouteRead(d, meta)
223+
}
224+
225+
func resourceTencentCloudCkafkaRouteRead(d *schema.ResourceData, meta interface{}) error {
226+
defer logElapsed("resource.tencentcloud_ckafka_route.read")()
227+
defer inconsistentCheck(d, meta)()
228+
229+
logId := getLogId(contextNil)
230+
231+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
232+
233+
service := CkafkaService{client: meta.(*TencentCloudClient).apiV3Conn}
234+
235+
items := strings.Split(d.Id(), FILED_SP)
236+
if len(items) < 2 {
237+
return fmt.Errorf("id is broken,%s", d.Id())
238+
}
239+
instanceId := items[0]
240+
routeId := items[1]
241+
routeIdInt64, err := strconv.ParseInt(routeId, 10, 64)
242+
if err != nil {
243+
return err
244+
}
245+
route, err := service.DescribeCkafkaRouteById(ctx, instanceId, routeIdInt64)
246+
if err != nil {
247+
return err
248+
}
249+
_ = d.Set("instance_id", instanceId)
250+
if route == nil {
251+
d.SetId("")
252+
log.Printf("[WARN]%s resource `CkafkaRoute` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
253+
return nil
254+
}
255+
256+
if route.VipType != nil {
257+
_ = d.Set("vip_type", route.VipType)
258+
}
259+
260+
if route.VpcId != nil {
261+
_ = d.Set("vpc_id", route.VpcId)
262+
}
263+
264+
if route.Subnet != nil {
265+
_ = d.Set("subnet_id", route.Subnet)
266+
}
267+
268+
if route.AccessType != nil {
269+
_ = d.Set("access_type", route.AccessType)
270+
}
271+
272+
if len(route.VipList) > 0 {
273+
_ = d.Set("ip", route.VipList[0].Vip)
274+
}
275+
vipList := make([]map[string]interface{}, 0)
276+
for _, vip := range route.VipList {
277+
vipList = append(vipList, map[string]interface{}{
278+
"vip": vip.Vip,
279+
"vport": vip.Vport,
280+
})
281+
}
282+
_ = d.Set("vip_list", vipList)
283+
brokerVipList := make([]map[string]interface{}, 0)
284+
for _, brokerVip := range route.BrokerVipList {
285+
brokerVipList = append(brokerVipList, map[string]interface{}{
286+
"vip": brokerVip.Vip,
287+
"vport": brokerVip.Vport,
288+
})
289+
}
290+
_ = d.Set("broker_vip_list", brokerVipList)
291+
292+
return nil
293+
}
294+
295+
func resourceTencentCloudCkafkaRouteUpdate(d *schema.ResourceData, meta interface{}) error {
296+
defer logElapsed("resource.tencentcloud_ckafka_route.update")()
297+
defer inconsistentCheck(d, meta)()
298+
299+
immutableArgs := []string{"instance_id", "vip_type", "vpc_id", "subnet_id", "access_type", "auth_flag", "caller_appid", "public_network", "ip"}
300+
301+
for _, v := range immutableArgs {
302+
if d.HasChange(v) {
303+
return fmt.Errorf("argument `%s` cannot be changed", v)
304+
}
305+
}
306+
return resourceTencentCloudCkafkaRouteRead(d, meta)
307+
}
308+
309+
func resourceTencentCloudCkafkaRouteDelete(d *schema.ResourceData, meta interface{}) error {
310+
defer logElapsed("resource.tencentcloud_ckafka_route.delete")()
311+
defer inconsistentCheck(d, meta)()
312+
313+
logId := getLogId(contextNil)
314+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
315+
316+
service := CkafkaService{client: meta.(*TencentCloudClient).apiV3Conn}
317+
items := strings.Split(d.Id(), FILED_SP)
318+
if len(items) < 2 {
319+
return fmt.Errorf("id is broken,%s", d.Id())
320+
}
321+
instanceId := items[0]
322+
routeId := items[1]
323+
routeIdInt64, err := strconv.ParseInt(routeId, 10, 64)
324+
if err != nil {
325+
return err
326+
}
327+
if err := service.DeleteCkafkaRouteById(ctx, instanceId, routeIdInt64); err != nil {
328+
return err
329+
}
330+
331+
return nil
332+
}

0 commit comments

Comments
 (0)