Skip to content

Commit c952140

Browse files
Modify the problems in ckafka topic code review
1 parent 2a9c131 commit c952140

8 files changed

+224
-202
lines changed

tencentcloud/data_source_tc_ckafka_topics.go

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ resource "tencentcloud_ckafka_topic" "foo" {
1010
note = "topic note"
1111
replica_num = 2
1212
partition_num = 1
13-
enable_white_list = 1
14-
ip_white_list = ["ip1","ip2"]
13+
enable_white_list = true
14+
ip_white_list = ["ip1","ip2"]
1515
clean_up_policy = "delete"
1616
sync_replica_min_num = 1
17-
unclean_leader_election_enable = false
17+
unclean_leader_election_enable = false
1818
segment = 3600000
1919
retention = 60000
2020
max_message_bytes = 0
@@ -33,7 +33,7 @@ import (
3333

3434
func dataSourceTencentCloudCkafkaTopics() *schema.Resource {
3535
return &schema.Resource{
36-
Read: dataSourceTencentCloudCkafkaTopicRead,
36+
Read: dataSourceTencentCloudCkafkaTopicsRead,
3737

3838
Schema: map[string]*schema.Schema{
3939
"instance_id": {
@@ -52,7 +52,6 @@ func dataSourceTencentCloudCkafkaTopics() *schema.Resource {
5252
Optional: true,
5353
Description: "Used to store results.",
5454
},
55-
// computed
5655
"instance_list": {
5756
Type: schema.TypeList,
5857
Computed: true,
@@ -82,17 +81,17 @@ func dataSourceTencentCloudCkafkaTopics() *schema.Resource {
8281
"note": {
8382
Type: schema.TypeString,
8483
Computed: true,
85-
Description: "Topic note description.",
84+
Description: "CKafka topic note description.",
8685
},
8786
"create_time": {
8887
Type: schema.TypeString,
8988
Computed: true,
90-
Description: "Create time of the topic instance.",
89+
Description: "Create time of the CKafka topic.",
9190
},
9291
"enable_white_list": {
9392
Type: schema.TypeBool,
9493
Computed: true,
95-
Description: "IP Whitelist switch, 1: open; 0: close.",
94+
Description: "Whether to open the IP Whitelist, true: open, false: close.",
9695
},
9796
"ip_white_list_count": {
9897
Type: schema.TypeInt,
@@ -112,7 +111,7 @@ func dataSourceTencentCloudCkafkaTopics() *schema.Resource {
112111
"forward_status": {
113112
Type: schema.TypeInt,
114113
Computed: true,
115-
Description: "Data backup cos status: 1 do not open data backup, 0 open data backup.",
114+
Description: "Data backup cos status. 1: do not open data backup, 0: open data backup.",
116115
},
117116
"retention": {
118117
Type: schema.TypeInt,
@@ -127,12 +126,12 @@ func dataSourceTencentCloudCkafkaTopics() *schema.Resource {
127126
"clean_up_policy": {
128127
Type: schema.TypeString,
129128
Computed: true,
130-
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.",
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.",
131130
},
132131
"unclean_leader_election_enable": {
133132
Type: schema.TypeBool,
134133
Computed: true,
135-
Description: "Whether to allow unsynchronized replicas to be selected as leader, false: not allowed, true: allowed, not allowed by default.",
134+
Description: "Whether to allow unsynchronized replicas to be selected as leader, default is `false`, `true: `allowed, `false`: not allowed.",
136135
},
137136
"max_message_bytes": {
138137
Type: schema.TypeInt,
@@ -156,19 +155,13 @@ func dataSourceTencentCloudCkafkaTopics() *schema.Resource {
156155
}
157156
}
158157

159-
func dataSourceTencentCloudCkafkaTopicRead(d *schema.ResourceData, meta interface{}) error {
160-
defer logElapsed("data_source.tencentcloud_ckafka_topic.read")()
158+
func dataSourceTencentCloudCkafkaTopicsRead(d *schema.ResourceData, meta interface{}) error {
159+
defer logElapsed("data_source.tencentcloud_ckafka_topics.read")()
161160

162161
logId := getLogId(contextNil)
163162
ctx := context.WithValue(context.TODO(), logIdKey, logId)
164-
165-
var instanceId, topicName string
166-
if v, ok := d.GetOk("instance_id"); ok {
167-
instanceId = v.(string)
168-
}
169-
if v, ok := d.GetOk("topic_name"); ok {
170-
topicName = v.(string)
171-
}
163+
instanceId := d.Get("instance_id").(string)
164+
topicName := d.Get("topic_name").(string)
172165
ckafkcService := CkafkaService{
173166
client: meta.(*TencentCloudClient).apiV3Conn,
174167
}
@@ -181,9 +174,9 @@ func dataSourceTencentCloudCkafkaTopicRead(d *schema.ResourceData, meta interfac
181174
ids := make([]string, 0, len(topicDetails))
182175

183176
for _, topic := range topicDetails {
184-
uncleanLeaderElectionEnable := false
185-
if *topic.Config.UncleanLeaderElectionEnable == int64(1) {
186-
uncleanLeaderElectionEnable = true
177+
var uncleanLeaderElectionEnable bool
178+
if topic.Config.UncleanLeaderElectionEnable != nil {
179+
uncleanLeaderElectionEnable = *topic.Config.UncleanLeaderElectionEnable == 1
187180
}
188181
instance := map[string]interface{}{
189182
"topic_name": topic.TopicName,
@@ -212,7 +205,7 @@ func dataSourceTencentCloudCkafkaTopicRead(d *schema.ResourceData, meta interfac
212205

213206
d.SetId(helper.DataResourceIdsHash(ids))
214207
if err = d.Set("instance_list", instanceList); err != nil {
215-
log.Printf("[CRITAL]%s provider set ckafka topic instance list fail, reason:%s\n ", logId, err.Error())
208+
log.Printf("[CRITAL]%s provider set ckafka topic list fail, reason:%s\n ", logId, err.Error())
216209
return err
217210
}
218211

tencentcloud/data_source_tc_ckafka_topics_test.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,17 @@ func TestAccTencentCloudCkafkaTopicDataSource(t *testing.T) {
2323
resource.TestCheckResourceAttr("data.tencentcloud_ckafka_topics.kafka_topics", "instance_list.0.replica_num", "2"),
2424
resource.TestCheckResourceAttrSet("data.tencentcloud_ckafka_topics.kafka_topics", "instance_list.0.create_time"),
2525
resource.TestCheckResourceAttr("data.tencentcloud_ckafka_topics.kafka_topics", "instance_list.0.note", "test topic"),
26-
resource.TestCheckResourceAttr("tencentcloud_ckafka_topic.kafka_topic", "enable_white_list", "1"),
27-
resource.TestCheckResourceAttr("tencentcloud_ckafka_topic.kafka_topic", "ip_white_list.#", "1"),
28-
resource.TestCheckResourceAttr("tencentcloud_ckafka_topic.kafka_topic", "ip_white_list.0", "192.168.1.1"),
29-
resource.TestCheckResourceAttr("tencentcloud_ckafka_topic.kafka_topic", "clean_up_policy", "delete"),
30-
resource.TestCheckResourceAttr("tencentcloud_ckafka_topic.kafka_topic", "sync_replica_min_num", "1"),
31-
resource.TestCheckResourceAttrSet("tencentcloud_ckafka_topic.kafka_topic", "unclean_leader_election_enable"),
32-
resource.TestCheckResourceAttr("tencentcloud_ckafka_topic.kafka_topic", "segment", "3600000"),
33-
resource.TestCheckResourceAttr("tencentcloud_ckafka_topic.kafka_topic", "retention", "60000"),
34-
resource.TestCheckResourceAttr("data.tencentcloud_ckafka_topics.foo", "instance_list.#", "1"),
35-
resource.TestCheckResourceAttr("data.tencentcloud_ckafka_topics.foo", "instance_list.0.partition_num", "1"),
36-
resource.TestCheckResourceAttr("data.tencentcloud_ckafka_topics.foo", "instance_list.0.replica_num", "2"),
37-
resource.TestCheckResourceAttrSet("data.tencentcloud_ckafka_topics.foo", "instance_list.0.create_time"),
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"),
3837
),
3938
},
4039
},
@@ -48,7 +47,7 @@ resource "tencentcloud_ckafka_topic" "kafka_topic" {
4847
replica_num = 2
4948
partition_num = 1
5049
note = "test topic"
51-
enable_white_list = 1
50+
enable_white_list = true
5251
ip_white_list = ["192.168.1.1"]
5352
clean_up_policy = "delete"
5453
sync_replica_min_num = 1

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"

0 commit comments

Comments
 (0)