Skip to content

Commit 16d2c14

Browse files
authored
Merge pull request #504 from cyberHermanwang/ckafka_topic
add ckafka topic resource and data_source file
2 parents 3ce447a + b2376d0 commit 16d2c14

14 files changed

+1352
-2
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
## 1.41.4 (Unreleased)
1+
## 1.42.0 (Unreleased)
2+
3+
FEATURES:
4+
5+
* **New Resource**: `tencentcloud_ckafka_topic`
6+
* **New Data Source**: `tencentcloud_ckafka_topics`
7+
28
## 1.41.3 (September 3, 2020)
39

410
ENHANCEMENTS:

examples/tencentcloud-ckafka/main.tf

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,25 @@ data "tencentcloud_ckafka_acls" "foo" {
2323
instance_id = tencentcloud_ckafka_acl.foo.instance_id
2424
resource_type = tencentcloud_ckafka_acl.foo.resource_type
2525
resource_name = tencentcloud_ckafka_acl.foo.resource_name
26+
}
27+
28+
resource "tencentcloud_ckafka_topic" "kafka_topic" {
29+
instance_id = "ckafka-f9ife4zz"
30+
topic_name = "ckafka-topic-tf-test"
31+
note = "this is test ckafka topic"
32+
replica_num = 2
33+
partition_num = 2
34+
enable_white_list = true
35+
ip_white_list = ["192.168.1.1"]
36+
clean_up_policy = "delete"
37+
sync_replica_min_num = 1
38+
unclean_leader_election_enable = false
39+
segment = 3600000
40+
retention = 60000
41+
max_message_bytes = 0
42+
}
43+
44+
data "tencentcloud_ckafka_topics" "kafka_topics" {
45+
instance_id = tencentcloud_ckafka_topic.kafka_topic.instance_id
46+
topic_name = tencentcloud_ckafka_topic.kafka_topic.topic_name
2647
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require (
1616
github.com/mitchellh/go-homedir v1.1.0
1717
github.com/pkg/errors v0.9.1
1818
github.com/tencentcloud/tencentcloud-sdk-go v3.0.229+incompatible
19+
github.com/terraform-providers/terraform-provider-tencentcloud v1.38.3 // indirect
1920
github.com/yangwenmai/ratelimit v0.0.0-20180104140304-44221c2292e1
2021
github.com/zclconf/go-cty v1.4.2 // indirect
2122
golang.org/x/sys v0.0.0-20200523222454-059865788121 // indirect

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,11 @@ github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s
458458
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
459459
github.com/tdakkota/asciicheck v0.0.0-20200416190851-d7f85be797a2 h1:Xr9gkxfOP0KQWXKNqmwe8vEeSUiUj4Rlee9CMVX2ZUQ=
460460
github.com/tdakkota/asciicheck v0.0.0-20200416190851-d7f85be797a2/go.mod h1:yHp0ai0Z9gUljN3o0xMhYJnH/IcvkdTBOX2fmJ93JEM=
461+
github.com/tencentcloud/tencentcloud-sdk-go v3.0.196+incompatible/go.mod h1:0PfYow01SHPMhKY31xa+EFz2RStxIqj6JFAJS+IkCi4=
461462
github.com/tencentcloud/tencentcloud-sdk-go v3.0.229+incompatible h1:ZlFfQuJYC+g2bEjlZUvIUQUifHhzWM69GTzFh0qzLl4=
462463
github.com/tencentcloud/tencentcloud-sdk-go v3.0.229+incompatible/go.mod h1:0PfYow01SHPMhKY31xa+EFz2RStxIqj6JFAJS+IkCi4=
464+
github.com/terraform-providers/terraform-provider-tencentcloud v1.38.3 h1:1RfGV7CiPmeyrG41LdvuJ3CmPao2QxHnEQnXKqVAxSk=
465+
github.com/terraform-providers/terraform-provider-tencentcloud v1.38.3/go.mod h1:5jcaioF+8fIC9h4RBZV0e32uanncMZr5c2/8p/CWCU8=
463466
github.com/tetafro/godot v0.3.7 h1:+mecr7RKrUKB5UQ1gwqEMn13sDKTyDR8KNIquB9mm+8=
464467
github.com/tetafro/godot v0.3.7/go.mod h1:/7NLHhv08H1+8DNj0MElpAACw1ajsCuf3TKNQxA5S+0=
465468
github.com/timakin/bodyclose v0.0.0-20190930140734-f7f2e9bca95e h1:RumXZ56IrCj4CL+g1b9OL/oH0QnsF976bC8xQFYUD5Q=
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
/*
2+
Use this data source to query detailed information of ckafka topic.
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_ckafka_topic" "foo" {
8+
instance_id = "ckafka-f9ife4zz"
9+
topic_name = "example"
10+
note = "topic note"
11+
replica_num = 2
12+
partition_num = 1
13+
enable_white_list = true
14+
ip_white_list = ["ip1","ip2"]
15+
clean_up_policy = "delete"
16+
sync_replica_min_num = 1
17+
unclean_leader_election_enable = false
18+
segment = 3600000
19+
retention = 60000
20+
max_message_bytes = 0
21+
}
22+
```
23+
*/
24+
package tencentcloud
25+
26+
import (
27+
"context"
28+
"log"
29+
30+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
31+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
32+
)
33+
34+
func dataSourceTencentCloudCkafkaTopics() *schema.Resource {
35+
return &schema.Resource{
36+
Read: dataSourceTencentCloudCkafkaTopicsRead,
37+
38+
Schema: map[string]*schema.Schema{
39+
"instance_id": {
40+
Type: schema.TypeString,
41+
Required: true,
42+
Description: "Ckafka instance ID.",
43+
},
44+
"topic_name": {
45+
Type: schema.TypeString,
46+
Optional: true,
47+
ValidateFunc: validateStringLengthInRange(1, 64),
48+
Description: "Name of the CKafka topic. It must start with a letter, the rest can contain letters, numbers and dashes(-). The length range is from 1 to 64.",
49+
},
50+
"result_output_file": {
51+
Type: schema.TypeString,
52+
Optional: true,
53+
Description: "Used to store results.",
54+
},
55+
"instance_list": {
56+
Type: schema.TypeList,
57+
Computed: true,
58+
Description: "A list of instances. Each element contains the following attributes.",
59+
Elem: &schema.Resource{
60+
Schema: map[string]*schema.Schema{
61+
"topic_id": {
62+
Type: schema.TypeString,
63+
Computed: true,
64+
Description: "Id of the CKafka topic.",
65+
},
66+
"topic_name": {
67+
Type: schema.TypeString,
68+
Computed: true,
69+
Description: "Name of the CKafka topic.",
70+
},
71+
"partition_num": {
72+
Type: schema.TypeInt,
73+
Computed: true,
74+
Description: "The number of partition.",
75+
},
76+
"replica_num": {
77+
Type: schema.TypeInt,
78+
Computed: true,
79+
Description: "The number of replica.",
80+
},
81+
"note": {
82+
Type: schema.TypeString,
83+
Computed: true,
84+
Description: "CKafka topic note description.",
85+
},
86+
"create_time": {
87+
Type: schema.TypeString,
88+
Computed: true,
89+
Description: "Create time of the CKafka topic.",
90+
},
91+
"enable_white_list": {
92+
Type: schema.TypeBool,
93+
Computed: true,
94+
Description: "Whether to open the IP Whitelist, true: open, false: close.",
95+
},
96+
"ip_white_list_count": {
97+
Type: schema.TypeInt,
98+
Computed: true,
99+
Description: "IP Whitelist count.",
100+
},
101+
"forward_interval": {
102+
Type: schema.TypeInt,
103+
Computed: true,
104+
Description: "Periodic frequency of data backup to cos.",
105+
},
106+
"forward_cos_bucket": {
107+
Type: schema.TypeString,
108+
Computed: true,
109+
Description: "Data backup cos bucket: the bucket address that is dumped to cos.",
110+
},
111+
"forward_status": {
112+
Type: schema.TypeInt,
113+
Computed: true,
114+
Description: "Data backup cos status. 1: do not open data backup, 0: open data backup.",
115+
},
116+
"retention": {
117+
Type: schema.TypeInt,
118+
Computed: true,
119+
Description: "Message can be selected. Retention time, unit ms.",
120+
},
121+
"sync_replica_min_num": {
122+
Type: schema.TypeInt,
123+
Computed: true,
124+
Description: "Min number of sync replicas.",
125+
},
126+
"clean_up_policy": {
127+
Type: schema.TypeString,
128+
Computed: true,
129+
Description: "Clear log policy, log clear mode. `delete`: logs are deleted according to the storage time, `compact`: logs are compressed according to the key, `compact, delete`: logs are compressed according to the key and will be deleted according to the storage time.",
130+
},
131+
"unclean_leader_election_enable": {
132+
Type: schema.TypeBool,
133+
Computed: true,
134+
Description: "Whether to allow unsynchronized replicas to be selected as leader, default is `false`, `true: `allowed, `false`: not allowed.",
135+
},
136+
"max_message_bytes": {
137+
Type: schema.TypeInt,
138+
Computed: true,
139+
Description: "Max message bytes.",
140+
},
141+
"segment": {
142+
Type: schema.TypeInt,
143+
Computed: true,
144+
Description: "Segment scrolling time, in ms.",
145+
},
146+
"segment_bytes": {
147+
Type: schema.TypeInt,
148+
Computed: true,
149+
Description: "Number of bytes rolled by shard.",
150+
},
151+
},
152+
},
153+
},
154+
},
155+
}
156+
}
157+
158+
func dataSourceTencentCloudCkafkaTopicsRead(d *schema.ResourceData, meta interface{}) error {
159+
defer logElapsed("data_source.tencentcloud_ckafka_topics.read")()
160+
161+
logId := getLogId(contextNil)
162+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
163+
instanceId := d.Get("instance_id").(string)
164+
topicName := d.Get("topic_name").(string)
165+
ckafkcService := CkafkaService{
166+
client: meta.(*TencentCloudClient).apiV3Conn,
167+
}
168+
topicDetails, err := ckafkcService.DescribeCkafkaTopics(ctx, instanceId, topicName)
169+
if err != nil {
170+
return err
171+
}
172+
173+
instanceList := make([]map[string]interface{}, 0, len(topicDetails))
174+
ids := make([]string, 0, len(topicDetails))
175+
176+
for _, topic := range topicDetails {
177+
var uncleanLeaderElectionEnable bool
178+
if topic.Config.UncleanLeaderElectionEnable != nil {
179+
uncleanLeaderElectionEnable = *topic.Config.UncleanLeaderElectionEnable != 0
180+
}
181+
instance := map[string]interface{}{
182+
"topic_name": topic.TopicName,
183+
"topic_id": topic.TopicId,
184+
"partition_num": topic.PartitionNum,
185+
"replica_num": topic.ReplicaNum,
186+
"note": topic.Note,
187+
"create_time": helper.FormatUnixTime(uint64(*topic.CreateTime)),
188+
"enable_white_list": topic.EnableWhiteList,
189+
"ip_white_list_count": topic.IpWhiteListCount,
190+
"forward_interval": topic.ForwardInterval,
191+
"forward_cos_bucket": topic.ForwardCosBucket,
192+
"forward_status": topic.ForwardStatus,
193+
"retention": topic.Config.Retention,
194+
"sync_replica_min_num": topic.Config.MinInsyncReplicas,
195+
"clean_up_policy": topic.Config.CleanUpPolicy,
196+
"unclean_leader_election_enable": uncleanLeaderElectionEnable,
197+
"max_message_bytes": topic.Config.MaxMessageBytes,
198+
"segment": topic.Config.SegmentMs,
199+
"segment_bytes": topic.Config.SegmentBytes,
200+
}
201+
resourceId := instanceId + FILED_SP + *topic.TopicName
202+
instanceList = append(instanceList, instance)
203+
ids = append(ids, resourceId)
204+
}
205+
206+
d.SetId(helper.DataResourceIdsHash(ids))
207+
if err = d.Set("instance_list", instanceList); err != nil {
208+
log.Printf("[CRITAL]%s provider set ckafka topic list fail, reason:%s\n ", logId, err.Error())
209+
return err
210+
}
211+
212+
output, ok := d.GetOk("result_output_file")
213+
if ok && output.(string) != "" {
214+
if err := writeToFile(output.(string), instanceList); err != nil {
215+
return err
216+
}
217+
}
218+
219+
return nil
220+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudCkafkaTopicDataSource(t *testing.T) {
10+
resource.Test(t, resource.TestCase{
11+
PreCheck: func() { testAccPreCheck(t) },
12+
Providers: testAccProviders,
13+
CheckDestroy: testAccTencentCloudKafkaTopicDestory,
14+
Steps: []resource.TestStep{
15+
{
16+
Config: testAccTencentCloudCkafkaTopicDataSourceConfig,
17+
Check: resource.ComposeTestCheckFunc(
18+
testAccCheckKafkaTopicInstanceExists("tencentcloud_ckafka_topic.kafka_topic"),
19+
resource.TestCheckResourceAttr("data.tencentcloud_ckafka_topics.kafka_topics", "instance_id", "ckafka-f9ife4zz"),
20+
resource.TestCheckResourceAttr("data.tencentcloud_ckafka_topics.kafka_topics", "instance_list.#", "1"),
21+
resource.TestCheckResourceAttr("data.tencentcloud_ckafka_topics.kafka_topics", "instance_list.0.topic_name", "ckafkaTopic-tf-test"),
22+
resource.TestCheckResourceAttr("data.tencentcloud_ckafka_topics.kafka_topics", "instance_list.0.partition_num", "1"),
23+
resource.TestCheckResourceAttr("data.tencentcloud_ckafka_topics.kafka_topics", "instance_list.0.replica_num", "2"),
24+
resource.TestCheckResourceAttrSet("data.tencentcloud_ckafka_topics.kafka_topics", "instance_list.0.create_time"),
25+
resource.TestCheckResourceAttr("data.tencentcloud_ckafka_topics.kafka_topics", "instance_list.0.note", "test topic"),
26+
resource.TestCheckResourceAttr("data.tencentcloud_ckafka_topics.kafka_topics", "instance_list.0.enable_white_list", "true"),
27+
resource.TestCheckResourceAttr("data.tencentcloud_ckafka_topics.kafka_topics", "instance_list.0.ip_white_list_count", "1"),
28+
resource.TestCheckResourceAttr("data.tencentcloud_ckafka_topics.kafka_topics", "instance_list.0.clean_up_policy", "delete"),
29+
resource.TestCheckResourceAttr("data.tencentcloud_ckafka_topics.kafka_topics", "instance_list.0.sync_replica_min_num", "1"),
30+
resource.TestCheckResourceAttrSet("data.tencentcloud_ckafka_topics.kafka_topics", "instance_list.0.unclean_leader_election_enable"),
31+
resource.TestCheckResourceAttr("data.tencentcloud_ckafka_topics.kafka_topics", "instance_list.0.segment", "3600000"),
32+
resource.TestCheckResourceAttr("data.tencentcloud_ckafka_topics.kafka_topics", "instance_list.0.retention", "60000"),
33+
resource.TestCheckResourceAttr("data.tencentcloud_ckafka_topics.foo", "instance_list.#", "2"),
34+
resource.TestCheckResourceAttrSet("data.tencentcloud_ckafka_topics.foo", "instance_list.1.partition_num"),
35+
resource.TestCheckResourceAttrSet("data.tencentcloud_ckafka_topics.foo", "instance_list.1.replica_num"),
36+
resource.TestCheckResourceAttrSet("data.tencentcloud_ckafka_topics.foo", "instance_list.1.create_time"),
37+
),
38+
},
39+
},
40+
})
41+
}
42+
43+
const testAccTencentCloudCkafkaTopicDataSourceConfig = `
44+
resource "tencentcloud_ckafka_topic" "kafka_topic" {
45+
instance_id = "ckafka-f9ife4zz"
46+
topic_name = "ckafkaTopic-tf-test"
47+
replica_num = 2
48+
partition_num = 1
49+
note = "test topic"
50+
enable_white_list = true
51+
ip_white_list = ["192.168.1.1"]
52+
clean_up_policy = "delete"
53+
sync_replica_min_num = 1
54+
unclean_leader_election_enable = false
55+
segment = 3600000
56+
retention = 60000
57+
}
58+
59+
data "tencentcloud_ckafka_topics" "kafka_topics" {
60+
instance_id = tencentcloud_ckafka_topic.kafka_topic.instance_id
61+
topic_name = tencentcloud_ckafka_topic.kafka_topic.topic_name
62+
}
63+
64+
data "tencentcloud_ckafka_topics" "foo" {
65+
instance_id = tencentcloud_ckafka_topic.kafka_topic.instance_id
66+
}
67+
`

tencentcloud/extension_ckafka.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,6 @@ var CKAFKA_PERMISSION_TYPE_TO_STRING = map[int64]string{
6767
2: "DENY",
6868
3: "ALLOW",
6969
}
70+
71+
//sdk ckafka not found error
72+
const CkafkaInstanceNotFound = "InvalidParameterValue.InstanceNotExist"

tencentcloud/provider.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,12 @@ Ckafka
8181
Data Source
8282
tencentcloud_ckafka_users
8383
tencentcloud_ckafka_acls
84+
tencentcloud_ckafka_topics
8485

8586
Resource
8687
tencentcloud_ckafka_user
8788
tencentcloud_ckafka_acl
89+
tencentcloud_ckafka_topic
8890

8991
Cloud Access Management(CAM)
9092
Data Source
@@ -636,6 +638,7 @@ func Provider() terraform.ResourceProvider {
636638
"tencentcloud_sqlserver_readonly_groups": dataSourceTencentCloudSqlserverReadonlyGroups(),
637639
"tencentcloud_ckafka_users": dataSourceTencentCloudCkafkaUsers(),
638640
"tencentcloud_ckafka_acls": dataSourceTencentCloudCkafkaAcls(),
641+
"tencentcloud_ckafka_topics": dataSourceTencentCloudCkafkaTopics(),
639642
},
640643

641644
ResourcesMap: map[string]*schema.Resource{
@@ -757,6 +760,7 @@ func Provider() terraform.ResourceProvider {
757760
"tencentcloud_sqlserver_readonly_instance": resourceTencentCloudSqlserverReadonlyInstance(),
758761
"tencentcloud_ckafka_user": resourceTencentCloudCkafkaUser(),
759762
"tencentcloud_ckafka_acl": resourceTencentCloudCkafkaAcl(),
763+
"tencentcloud_ckafka_topic": resourceTencentCloudCkafkaTopic(),
760764
},
761765

762766
ConfigureFunc: providerConfigure,

0 commit comments

Comments
 (0)