|
| 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 | +} |
0 commit comments