From bda46fe55a8a1a8207db06f5f2e97b8645a80752 Mon Sep 17 00:00:00 2001 From: SevenEarth <391613297@qq.com> Date: Thu, 23 Jan 2025 18:56:30 +0800 Subject: [PATCH 1/2] add --- .../ckafka/resource_tc_ckafka_topic.go | 310 +++++++++++++----- .../ckafka/resource_tc_ckafka_topic.md | 30 +- website/docs/r/ckafka_topic.html.markdown | 20 +- 3 files changed, 244 insertions(+), 116 deletions(-) diff --git a/tencentcloud/services/ckafka/resource_tc_ckafka_topic.go b/tencentcloud/services/ckafka/resource_tc_ckafka_topic.go index 509a2841ea..b43a1dc785 100644 --- a/tencentcloud/services/ckafka/resource_tc_ckafka_topic.go +++ b/tencentcloud/services/ckafka/resource_tc_ckafka_topic.go @@ -138,78 +138,111 @@ func ResourceTencentCloudCkafkaTopic() *schema.Resource { func resourceTencentCloudCkafkaTopicCreate(d *schema.ResourceData, meta interface{}) error { defer tccommon.LogElapsed("resource.tencentcloud_ckafka_topic.create")() var ( - logId = tccommon.GetLogId(tccommon.ContextNil) - ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId) - ckafkcService = CkafkaService{ - client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(), - } - request = ckafka.NewCreateTopicRequest() - instanceId = d.Get("instance_id").(string) - topicName = d.Get("topic_name").(string) - note string - ipWhiteLists = d.Get("ip_white_list").([]interface{}) - ipWhiteList = make([]*string, 0, len(ipWhiteLists)) - syncReplicaMinNum = int64(d.Get("sync_replica_min_num").(int)) - uncleanLeaderElection = d.Get("unclean_leader_election_enable").(bool) - cleanUpPolicy = d.Get("clean_up_policy").(string) - whiteListSwitch = d.Get("enable_white_list").(bool) - retention = d.Get("retention").(int) + logId = tccommon.GetLogId(tccommon.ContextNil) + ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId) + ckafkcService = CkafkaService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} + request = ckafka.NewCreateTopicRequest() + instanceId string + topicName string + whiteListSwitch bool + // ipWhiteLists = d.Get("ip_white_list").([]interface{}) + // ipWhiteList = make([]*string, 0, len(ipWhiteLists)) ) - for _, value := range ipWhiteLists { - ipWhiteList = append(ipWhiteList, helper.String(value.(string))) + + if v, ok := d.GetOk("instance_id"); ok { + request.InstanceId = helper.String(v.(string)) + instanceId = v.(string) } - request.EnableWhiteList = helper.BoolToInt64Ptr(whiteListSwitch) - if whiteListSwitch { - if len(ipWhiteList) == 0 { - return fmt.Errorf("this Topic %s Create Failed, reason: ip whitelist switch is on, ip whitelist cannot be empty", topicName) + + if v, ok := d.GetOk("topic_name"); ok { + request.TopicName = helper.String(v.(string)) + topicName = v.(string) + } + + if v, ok := d.GetOkExists("partition_num"); ok { + request.PartitionNum = helper.IntInt64(v.(int)) + } + + if v, ok := d.GetOkExists("replica_num"); ok { + request.ReplicaNum = helper.IntInt64(v.(int)) + } + + if v, ok := d.GetOkExists("enable_white_list"); ok { + request.EnableWhiteList = helper.BoolToInt64Ptr(v.(bool)) + whiteListSwitch = v.(bool) + } + + if v, ok := d.GetOk("ip_white_list"); ok { + for _, item := range v.([]interface{}) { + request.IpWhiteList = append(request.IpWhiteList, helper.String(item.(string))) } - request.IpWhiteList = ipWhiteList } + if v, ok := d.GetOk("note"); ok { - note = v.(string) - request.Note = ¬e + request.Note = helper.String(v.(string)) } - if v, ok := d.GetOk("segment"); ok { - if v.(int) != 0 { - request.SegmentMs = helper.IntInt64(v.(int)) - } + + if v, ok := d.GetOkExists("retention"); ok { + request.RetentionMs = helper.IntInt64(v.(int)) + } + + if v, ok := d.GetOkExists("sync_replica_min_num"); ok { + request.MinInsyncReplicas = helper.IntInt64(v.(int)) + } + + if v, ok := d.GetOk("clean_up_policy"); ok { + request.CleanUpPolicy = helper.String(v.(string)) + } + + if v, ok := d.GetOkExists("unclean_leader_election_enable"); ok { + request.UncleanLeaderElectionEnable = helper.BoolToInt64Ptr(v.(bool)) } - if v, ok := d.GetOk("max_message_bytes"); ok { + + if v, ok := d.GetOkExists("max_message_bytes"); ok { request.MaxMessageBytes = helper.IntInt64(v.(int)) } - request.InstanceId = &instanceId - request.TopicName = &topicName - request.PartitionNum = helper.IntInt64(d.Get("partition_num").(int)) - request.ReplicaNum = helper.IntInt64(d.Get("replica_num").(int)) - request.MinInsyncReplicas = &syncReplicaMinNum - request.UncleanLeaderElectionEnable = helper.BoolToInt64Ptr(uncleanLeaderElection) - request.CleanUpPolicy = &cleanUpPolicy - request.RetentionMs = helper.IntInt64(retention) + + if v, ok := d.GetOkExists("segment"); ok { + request.SegmentMs = helper.IntInt64(v.(int)) + } + + if whiteListSwitch { + if len(request.IpWhiteList) == 0 { + return fmt.Errorf("this Topic %s Create Failed, reason: ip whitelist switch is on, ip whitelist cannot be empty", topicName) + } + } + //Before create topic,Check if kafka exists _, has, error := ckafkcService.DescribeInstanceById(ctx, instanceId) if error != nil { return error } + if !has { return fmt.Errorf("ckafka %s does not exist", instanceId) } + errCreate := ckafkcService.CreateCkafkaTopic(ctx, request) if errCreate != nil { return errCreate } + _, hasExist, err := ckafkcService.DescribeCkafkaTopicByName(ctx, instanceId, topicName) if err != nil { return err } + if !hasExist { return fmt.Errorf("this Topic %s Create Failed", topicName) } - if len(ipWhiteList) > 0 && whiteListSwitch { - err = ckafkcService.AddCkafkaTopicIpWhiteList(ctx, instanceId, topicName, ipWhiteList) + + if len(request.IpWhiteList) > 0 && whiteListSwitch { + err = ckafkcService.AddCkafkaTopicIpWhiteList(ctx, instanceId, topicName, request.IpWhiteList) if err != nil { return err } } + resourceId := instanceId + tccommon.FILED_SP + topicName d.SetId(resourceId) return resourceTencentCloudCkafkaTopicRead(d, meta) @@ -218,21 +251,26 @@ func resourceTencentCloudCkafkaTopicCreate(d *schema.ResourceData, meta interfac func resourceTencentCloudCkafkaTopicRead(d *schema.ResourceData, meta interface{}) error { defer tccommon.LogElapsed("resource.tencentcloud_ckafka_topic.read")() defer tccommon.InconsistentCheck(d, meta)() - logId := tccommon.GetLogId(tccommon.ContextNil) - ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) - ckafkcService := CkafkaService{ - client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(), - } + + var ( + logId = tccommon.GetLogId(tccommon.ContextNil) + ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId) + ckafkcService = CkafkaService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} + ) + items := strings.Split(d.Id(), tccommon.FILED_SP) if len(items) < 2 { return fmt.Errorf("id is broken,%s", d.Id()) } + instanceId := items[0] topicName := items[1] + topicListInfo, hasExist, e := ckafkcService.DescribeCkafkaTopicByName(ctx, instanceId, topicName) if e != nil { return e } + if !hasExist { d.SetId("") return nil @@ -244,80 +282,161 @@ func resourceTencentCloudCkafkaTopicRead(d *schema.ResourceData, meta interface{ if e != nil { return tccommon.RetryError(e) } + if topicDetail == nil { d.SetId("") return nil } + topicinfo = topicDetail return nil }) + if errInfo != nil { return fmt.Errorf("[API]Describe kafka topic fail,reason:%s", errInfo.Error()) } + _ = d.Set("instance_id", instanceId) - _ = d.Set("note", topicinfo.Note) - _ = d.Set("ip_white_list", topicinfo.IpWhiteList) - _ = d.Set("enable_white_list", *topicinfo.EnableWhiteList == 1) - _ = d.Set("replica_num", topicListInfo.ReplicaNum) - _ = d.Set("create_time", helper.FormatUnixTime(uint64(*topicinfo.CreateTime))) - _ = d.Set("partition_num", topicinfo.PartitionNum) - _ = d.Set("topic_name", topicListInfo.TopicName) - _ = d.Set("forward_interval", topicListInfo.ForwardInterval) - _ = d.Set("forward_cos_bucket", topicListInfo.ForwardCosBucket) - _ = d.Set("forward_status", topicListInfo.ForwardStatus) - _ = d.Set("clean_up_policy", topicinfo.Config.CleanUpPolicy) - _ = d.Set("max_message_bytes", topicinfo.Config.MaxMessageBytes) - _ = d.Set("sync_replica_min_num", topicinfo.Config.MinInsyncReplicas) - _ = d.Set("retention", topicinfo.Config.Retention) - _ = d.Set("segment_bytes", topicinfo.Config.SegmentBytes) - _ = d.Set("segment", topicinfo.Config.SegmentMs) - if topicinfo.Config.UncleanLeaderElectionEnable != nil { - _ = d.Set("unclean_leader_election_enable", *topicinfo.Config.UncleanLeaderElectionEnable == 1) + + if topicListInfo.TopicName != nil { + _ = d.Set("topic_name", topicListInfo.TopicName) + } + + if topicinfo.PartitionNum != nil { + _ = d.Set("partition_num", topicinfo.PartitionNum) + } + + if topicListInfo.ReplicaNum != nil { + _ = d.Set("replica_num", topicListInfo.ReplicaNum) + } + + if topicinfo.EnableWhiteList != nil { + if *topicinfo.EnableWhiteList == 1 { + _ = d.Set("enable_white_list", true) + } else { + _ = d.Set("enable_white_list", false) + } + } + + if topicinfo.IpWhiteList != nil { + _ = d.Set("ip_white_list", topicinfo.IpWhiteList) } + + if topicinfo.Note != nil { + _ = d.Set("note", topicinfo.Note) + } + + if topicinfo.Config != nil { + if topicinfo.Config.Retention != nil { + _ = d.Set("retention", topicinfo.Config.Retention) + } + + if topicinfo.Config.MinInsyncReplicas != nil { + _ = d.Set("sync_replica_min_num", topicinfo.Config.MinInsyncReplicas) + } + + if topicinfo.Config.CleanUpPolicy != nil { + _ = d.Set("clean_up_policy", topicinfo.Config.CleanUpPolicy) + } + + if topicinfo.Config.UncleanLeaderElectionEnable != nil { + if *topicinfo.Config.UncleanLeaderElectionEnable == 1 { + _ = d.Set("unclean_leader_election_enable", true) + } else { + _ = d.Set("unclean_leader_election_enable", false) + } + } + + if topicinfo.Config.MaxMessageBytes != nil { + _ = d.Set("max_message_bytes", topicinfo.Config.MaxMessageBytes) + } + + if topicinfo.Config.SegmentMs != nil { + _ = d.Set("segment", topicinfo.Config.SegmentMs) + } + + if topicinfo.Config.SegmentBytes != nil { + _ = d.Set("segment_bytes", topicinfo.Config.SegmentBytes) + } + } + + if topicinfo.CreateTime != nil { + _ = d.Set("create_time", helper.FormatUnixTime(uint64(*topicinfo.CreateTime))) + } + + if topicListInfo.ForwardInterval != nil { + _ = d.Set("forward_interval", topicListInfo.ForwardInterval) + } + + if topicListInfo.ForwardCosBucket != nil { + _ = d.Set("forward_cos_bucket", topicListInfo.ForwardCosBucket) + } + + if topicListInfo.ForwardStatus != nil { + _ = d.Set("forward_status", topicListInfo.ForwardStatus) + } + return nil } func resourceTencentCloudCkafkaTopicUpdate(d *schema.ResourceData, meta interface{}) error { defer tccommon.LogElapsed("resource.tencentcloud_ckafka_topic.update")() - logId := tccommon.GetLogId(tccommon.ContextNil) - ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) - ckafkcService := CkafkaService{ - client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(), - } + + var ( + logId = tccommon.GetLogId(tccommon.ContextNil) + ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId) + ckafkcService = CkafkaService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} + whiteListSwitch bool + ) items := strings.Split(d.Id(), tccommon.FILED_SP) if len(items) < 2 { return fmt.Errorf("id is broken,%s", d.Id()) } + instanceId := items[0] topicName := items[1] request := ckafka.NewModifyTopicAttributesRequest() - replicaNum := d.Get("replica_num").(int) - whiteListSwitch := d.Get("enable_white_list").(bool) - cleanUpPolicy := d.Get("clean_up_policy").(string) - retention := d.Get("retention").(int) - var note string + request.InstanceId = &instanceId + request.TopicName = &topicName + if v, ok := d.GetOkExists("replica_num"); ok { + request.ReplicaNum = helper.IntInt64(v.(int)) + } + + if v, ok := d.GetOkExists("enable_white_list"); ok { + request.EnableWhiteList = helper.BoolToInt64Ptr(v.(bool)) + whiteListSwitch = v.(bool) + } + + if v, ok := d.GetOk("clean_up_policy"); ok { + request.CleanUpPolicy = helper.String(v.(string)) + } + + if v, ok := d.GetOkExists("retention"); ok { + request.RetentionMs = helper.IntInt64(v.(int)) + } + if v, ok := d.GetOk("note"); ok { - note = v.(string) - request.Note = ¬e + request.Note = helper.String(v.(string)) } + if v, ok := d.GetOk("segment"); ok { - if v.(int) != 0 { - request.SegmentMs = helper.IntInt64(v.(int)) - } + request.SegmentMs = helper.IntInt64(v.(int)) } - request.InstanceId = &instanceId - request.TopicName = &topicName - request.ReplicaNum = helper.IntInt64(replicaNum) - request.EnableWhiteList = helper.BoolToInt64Ptr(whiteListSwitch) - request.MinInsyncReplicas = helper.IntInt64(d.Get("sync_replica_min_num").(int)) - request.UncleanLeaderElectionEnable = helper.BoolToInt64Ptr(d.Get("unclean_leader_election_enable").(bool)) - request.CleanUpPolicy = &cleanUpPolicy - request.RetentionMs = helper.IntInt64(retention) + + if v, ok := d.GetOkExists("sync_replica_min_num"); ok { + request.MinInsyncReplicas = helper.IntInt64(v.(int)) + } + + if v, ok := d.GetOkExists("unclean_leader_election_enable"); ok { + request.UncleanLeaderElectionEnable = helper.BoolToInt64Ptr(v.(bool)) + } + if d.Get("max_message_bytes").(int) != 0 { request.MaxMessageBytes = helper.IntInt64(d.Get("max_message_bytes").(int)) } + //Update ip white List if whiteListSwitch { _, newInterface := d.GetChange("ip_white_list") @@ -326,9 +445,11 @@ func resourceTencentCloudCkafkaTopicUpdate(d *schema.ResourceData, meta interfac for _, value := range newIpWhiteListInterface { newIpWhiteList = append(newIpWhiteList, helper.String(value.(string))) } + if len(newIpWhiteList) == 0 { return fmt.Errorf("this Topic %s Create Failed, reason: ip whitelist switch is on, ip whitelist cannot be empty", topicName) } + request.IpWhiteList = newIpWhiteList } else { //IP whiteList Switch not turned on, and the ip whitelist cannot be modified @@ -336,6 +457,7 @@ func resourceTencentCloudCkafkaTopicUpdate(d *schema.ResourceData, meta interfac return fmt.Errorf("this Topic %s IP whitelist Modification failed, reason: The Ip Whitelist Switch is not turned on", topicName) } } + //Update partition num oldPartitionNum, newPartitionNum := d.GetChange("partition_num") if newPartitionNum.(int) < oldPartitionNum.(int) { @@ -348,14 +470,17 @@ func resourceTencentCloudCkafkaTopicUpdate(d *schema.ResourceData, meta interfac } } } + err := ckafkcService.ModifyCkafkaTopicAttribute(ctx, request) if err != nil { return err } + _, has, errDes := ckafkcService.DescribeCkafkaTopicByName(ctx, instanceId, topicName) if errDes != nil { return errDes } + if !has { errDes = fmt.Errorf("this Topic %s Update Failed", topicName) return errDes @@ -366,15 +491,18 @@ func resourceTencentCloudCkafkaTopicUpdate(d *schema.ResourceData, meta interfac func resourceTencentCLoudCkafkaTopicDelete(d *schema.ResourceData, meta interface{}) error { defer tccommon.LogElapsed("resource.tencentcloud_ckafka_topic.delete")() - logId := tccommon.GetLogId(tccommon.ContextNil) - ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId) - ckafkcService := CkafkaService{ - client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(), - } + + var ( + logId = tccommon.GetLogId(tccommon.ContextNil) + ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId) + ckafkcService = CkafkaService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} + ) + items := strings.Split(d.Id(), tccommon.FILED_SP) if len(items) < 2 { return fmt.Errorf("id is broken,%s", d.Id()) } + instanceId := items[0] topicName := items[1] diff --git a/tencentcloud/services/ckafka/resource_tc_ckafka_topic.md b/tencentcloud/services/ckafka/resource_tc_ckafka_topic.md index c764a46583..0e41da2a3b 100644 --- a/tencentcloud/services/ckafka/resource_tc_ckafka_topic.md +++ b/tencentcloud/services/ckafka/resource_tc_ckafka_topic.md @@ -3,20 +3,20 @@ Use this resource to create ckafka topic. Example Usage ```hcl -resource "tencentcloud_ckafka_topic" "foo" { - instance_id = "ckafka-f9ife4zz" - topic_name = "example" - note = "topic note" - replica_num = 2 - partition_num = 1 - enable_white_list = true - ip_white_list = ["ip1","ip2"] - clean_up_policy = "delete" - sync_replica_min_num = 1 - unclean_leader_election_enable = false - segment = 3600000 - retention = 60000 - max_message_bytes = 0 +resource "tencentcloud_ckafka_topic" "example" { + instance_id = "ckafka-bzmjpavn" + topic_name = "tf-example" + note = "topic note" + replica_num = 4 + partition_num = 2 + enable_white_list = true + ip_white_list = ["1.1.1.1", "2.2.2.2"] + clean_up_policy = "delete" + sync_replica_min_num = 2 + unclean_leader_election_enable = false + segment = 86400000 + retention = 60000 + max_message_bytes = 4096 } ``` @@ -25,5 +25,5 @@ Import ckafka topic can be imported using the instance_id#topic_name, e.g. ``` -$ terraform import tencentcloud_ckafka_topic.foo ckafka-f9ife4zz#example +$ terraform import tencentcloud_ckafka_topic.example ckafka-f9ife4zz#tf-example ``` \ No newline at end of file diff --git a/website/docs/r/ckafka_topic.html.markdown b/website/docs/r/ckafka_topic.html.markdown index 0003195063..51521681de 100644 --- a/website/docs/r/ckafka_topic.html.markdown +++ b/website/docs/r/ckafka_topic.html.markdown @@ -14,20 +14,20 @@ Use this resource to create ckafka topic. ## Example Usage ```hcl -resource "tencentcloud_ckafka_topic" "foo" { - instance_id = "ckafka-f9ife4zz" - topic_name = "example" +resource "tencentcloud_ckafka_topic" "example" { + instance_id = "ckafka-bzmjpavn" + topic_name = "tf-example" note = "topic note" - replica_num = 2 - partition_num = 1 + replica_num = 4 + partition_num = 2 enable_white_list = true - ip_white_list = ["ip1", "ip2"] + ip_white_list = ["1.1.1.1", "2.2.2.2"] clean_up_policy = "delete" - sync_replica_min_num = 1 + sync_replica_min_num = 2 unclean_leader_election_enable = false - segment = 3600000 + segment = 86400000 retention = 60000 - max_message_bytes = 0 + max_message_bytes = 4096 } ``` @@ -67,6 +67,6 @@ In addition to all arguments above, the following attributes are exported: ckafka topic can be imported using the instance_id#topic_name, e.g. ``` -$ terraform import tencentcloud_ckafka_topic.foo ckafka-f9ife4zz#example +$ terraform import tencentcloud_ckafka_topic.example ckafka-f9ife4zz#tf-example ``` From 90ed0d70037352906f480af2154cfe5b03e2205f Mon Sep 17 00:00:00 2001 From: SevenEarth <391613297@qq.com> Date: Thu, 23 Jan 2025 18:58:10 +0800 Subject: [PATCH 2/2] add --- .changelog/3107.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/3107.txt diff --git a/.changelog/3107.txt b/.changelog/3107.txt new file mode 100644 index 0000000000..2a39966c8f --- /dev/null +++ b/.changelog/3107.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/tencentcloud_ckafka_topic: fix the issue with default values for params +```