|
| 1 | +package cls |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 8 | + clsv20201016 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cls/v20201016" |
| 9 | + |
| 10 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 11 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 12 | +) |
| 13 | + |
| 14 | +func DataSourceTencentCloudClsTopics() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Read: dataSourceTencentCloudClsTopicsRead, |
| 17 | + Schema: map[string]*schema.Schema{ |
| 18 | + "filters": { |
| 19 | + Type: schema.TypeList, |
| 20 | + Optional: true, |
| 21 | + Description: "<li>topicName: Filter by **log topic name**. Fuzzy match is implemented by default. You can use the `PreciseSearch` parameter to set exact match. Type: String. Required. No. <br><li>logsetName: Filter by **logset name**. Fuzzy match is implemented by default. You can use the `PreciseSearch` parameter to set exact match. Type: String. Required: No. <br><li>topicId: Filter by **log topic ID**. Type: String. Required: No. <br><li>logsetId: Filter by **logset ID**. You can call `DescribeLogsets` to query the list of created logsets or log in to the console to view them. You can also call `CreateLogset` to create a logset. Type: String. Required: No. <br><li>tagKey: Filter by **tag key**. Type: String. Required: No. <br><li>tag:tagKey: Filter by **tag key-value pair**. The `tagKey` should be replaced with a specified tag key, such as `tag:exampleKey`. Type: String. Required: No. <br><li>storageType: Filter by **log topic storage type**. Valid values: `hot` (standard storage) and `cold` (IA storage). Type: String. Required: No. Each request can have up to 10 `Filters` and 100 `Filter.Values`.", |
| 22 | + Elem: &schema.Resource{ |
| 23 | + Schema: map[string]*schema.Schema{ |
| 24 | + "key": { |
| 25 | + Type: schema.TypeString, |
| 26 | + Required: true, |
| 27 | + Description: "Field to be filtered.", |
| 28 | + }, |
| 29 | + "values": { |
| 30 | + Type: schema.TypeSet, |
| 31 | + Required: true, |
| 32 | + Description: "Value to be filtered.", |
| 33 | + Elem: &schema.Schema{ |
| 34 | + Type: schema.TypeString, |
| 35 | + }, |
| 36 | + }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + }, |
| 40 | + |
| 41 | + "precise_search": { |
| 42 | + Type: schema.TypeInt, |
| 43 | + Optional: true, |
| 44 | + Description: "Match mode for `Filters` fields.\n- 0: Fuzzy match for `topicName` and `logsetName`. This is the default value.\n- 1: Exact match for `topicName`.\n- 2: Exact match for `logsetName`.\n- 3: Exact match for `topicName` and `logsetName`.", |
| 45 | + }, |
| 46 | + |
| 47 | + "biz_type": { |
| 48 | + Type: schema.TypeInt, |
| 49 | + Optional: true, |
| 50 | + Description: "Topic type\n- 0 (default): Log topic.\n- 1: Metric topic.", |
| 51 | + }, |
| 52 | + |
| 53 | + "topics": { |
| 54 | + Type: schema.TypeList, |
| 55 | + Computed: true, |
| 56 | + Description: "Log topic list.", |
| 57 | + Elem: &schema.Resource{ |
| 58 | + Schema: map[string]*schema.Schema{ |
| 59 | + "logset_id": { |
| 60 | + Type: schema.TypeString, |
| 61 | + Required: true, |
| 62 | + Description: "Logset ID.", |
| 63 | + }, |
| 64 | + "topic_id": { |
| 65 | + Type: schema.TypeString, |
| 66 | + Required: true, |
| 67 | + Description: "Topic ID.", |
| 68 | + }, |
| 69 | + "topic_name": { |
| 70 | + Type: schema.TypeString, |
| 71 | + Required: true, |
| 72 | + Description: "Topic Name.", |
| 73 | + }, |
| 74 | + "partition_count": { |
| 75 | + Type: schema.TypeInt, |
| 76 | + Required: true, |
| 77 | + Description: "Number of topic partitions.", |
| 78 | + }, |
| 79 | + "index": { |
| 80 | + Type: schema.TypeBool, |
| 81 | + Required: true, |
| 82 | + Description: "Whether the topic has indexing enabled (the topic type must be log topic).", |
| 83 | + }, |
| 84 | + "assumer_name": { |
| 85 | + Type: schema.TypeString, |
| 86 | + Required: true, |
| 87 | + Description: "Cloud product identifier. When the topic is created by other cloud products, this field displays the name of the cloud product, such as CDN, TKE.Note: This field may return null, indicating that no valid values can be obtained.", |
| 88 | + }, |
| 89 | + "create_time": { |
| 90 | + Type: schema.TypeString, |
| 91 | + Required: true, |
| 92 | + Description: "Creation time.", |
| 93 | + }, |
| 94 | + "status": { |
| 95 | + Type: schema.TypeBool, |
| 96 | + Required: true, |
| 97 | + Description: "Whether the topic has log collection enabled. true: collection enabled; false: collection disabled.Log collection is enabled by default when creating a log topic, and this field can be modified by calling ModifyTopic through the SDK.The console currently does not support modifying this parameter.", |
| 98 | + }, |
| 99 | + "tags": { |
| 100 | + Type: schema.TypeList, |
| 101 | + Required: true, |
| 102 | + Description: "Tag information bound to the topicNote: This field may return null, indicating that no valid values can be obtained.", |
| 103 | + Elem: &schema.Resource{ |
| 104 | + Schema: map[string]*schema.Schema{ |
| 105 | + "key": { |
| 106 | + Type: schema.TypeString, |
| 107 | + Required: true, |
| 108 | + Description: "The tag key.\nNote: This field may return null, indicating that no valid values can be obtained.", |
| 109 | + }, |
| 110 | + "value": { |
| 111 | + Type: schema.TypeString, |
| 112 | + Required: true, |
| 113 | + Description: "The tag value.\nNote: This field may return null, indicating that no valid values can be obtained.", |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + }, |
| 118 | + "auto_split": { |
| 119 | + Type: schema.TypeBool, |
| 120 | + Required: true, |
| 121 | + Description: "Whether automatic split is enabled for this topic\nNote: this field may return `null`, indicating that no valid values can be obtained.", |
| 122 | + }, |
| 123 | + "max_split_partitions": { |
| 124 | + Type: schema.TypeInt, |
| 125 | + Required: true, |
| 126 | + Description: "Maximum number of partitions to split into for this topic if automatic split is enabled\nNote: this field may return `null`, indicating that no valid values can be obtained.", |
| 127 | + }, |
| 128 | + "storage_type": { |
| 129 | + Type: schema.TypeString, |
| 130 | + Required: true, |
| 131 | + Description: "Storage type of the topicNote: This field may return null, indicating that no valid values can be obtained.", |
| 132 | + }, |
| 133 | + "period": { |
| 134 | + Type: schema.TypeInt, |
| 135 | + Required: true, |
| 136 | + Description: "Lifecycle in days. Value range: 1-3600 (3640 indicates permanent retention)\nNote: This field may return `null`, indicating that no valid value was found.", |
| 137 | + }, |
| 138 | + "sub_assumer_name": { |
| 139 | + Type: schema.TypeString, |
| 140 | + Required: true, |
| 141 | + Description: "Cloud product sub-identifier. If the log topic is created by another cloud product, this field returns the name of the cloud product and its log type, such as `TKE-Audit` or `TKE-Event`. Some products only return the cloud product identifier (`AssumerName`), without this field.\nNote: This field may return null, indicating that no valid values can be obtained.", |
| 142 | + }, |
| 143 | + "describes": { |
| 144 | + Type: schema.TypeString, |
| 145 | + Required: true, |
| 146 | + Description: "Topic description\nNote: This field may return null, indicating that no valid values can be obtained.", |
| 147 | + }, |
| 148 | + "hot_period": { |
| 149 | + Type: schema.TypeInt, |
| 150 | + Required: true, |
| 151 | + Description: "Enable log sinking, with the lifecycle of standard storage, where hotPeriod < Period.For standard storage, hotPeriod is used, and for infrequent access storage, it is Period-hotPeriod. (The topic type must be a log topic)HotPeriod=0 indicates that log sinking is not enabled.\nNote: This field may return null, indicating that no valid values can be obtained.", |
| 152 | + }, |
| 153 | + "biz_type": { |
| 154 | + Type: schema.TypeInt, |
| 155 | + Required: true, |
| 156 | + Description: "Topic type.\n- 0: log Topic \n- 1: Metric Topic\nNote: This field may return null, indicating that no valid values can be obtained.", |
| 157 | + }, |
| 158 | + "is_web_tracking": { |
| 159 | + Type: schema.TypeBool, |
| 160 | + Required: true, |
| 161 | + Description: "Free authentication switch. false: disabled; true: enabled.After enabling, anonymous access to the log topic will be supported for specified operations. For details, please refer to Log Topic (https://intl.cloud.tencent.com/document/product/614/41035?from_cn_redirect=1).Note: This field may return null, indicating that no valid values can be obtained.", |
| 162 | + }, |
| 163 | + }, |
| 164 | + }, |
| 165 | + }, |
| 166 | + |
| 167 | + "result_output_file": { |
| 168 | + Type: schema.TypeString, |
| 169 | + Optional: true, |
| 170 | + Description: "Used to save results.", |
| 171 | + }, |
| 172 | + }, |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +func dataSourceTencentCloudClsTopicsRead(d *schema.ResourceData, meta interface{}) error { |
| 177 | + defer tccommon.LogElapsed("data_source.tencentcloud_cls_topics.read")() |
| 178 | + defer tccommon.InconsistentCheck(d, meta)() |
| 179 | + |
| 180 | + var ( |
| 181 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 182 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 183 | + service = ClsService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 184 | + ) |
| 185 | + |
| 186 | + paramMap := make(map[string]interface{}) |
| 187 | + if v, ok := d.GetOk("filters"); ok { |
| 188 | + filtersSet := v.([]interface{}) |
| 189 | + tmpSet := make([]*clsv20201016.Filter, 0, len(filtersSet)) |
| 190 | + for _, item := range filtersSet { |
| 191 | + filtersMap := item.(map[string]interface{}) |
| 192 | + filter := clsv20201016.Filter{} |
| 193 | + if v, ok := filtersMap["key"].(string); ok && v != "" { |
| 194 | + filter.Key = helper.String(v) |
| 195 | + } |
| 196 | + |
| 197 | + if v, ok := filtersMap["values"]; ok { |
| 198 | + valuesSet := v.(*schema.Set).List() |
| 199 | + for i := range valuesSet { |
| 200 | + values := valuesSet[i].(string) |
| 201 | + filter.Values = append(filter.Values, helper.String(values)) |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + tmpSet = append(tmpSet, &filter) |
| 206 | + } |
| 207 | + |
| 208 | + paramMap["Filters"] = tmpSet |
| 209 | + } |
| 210 | + |
| 211 | + if v, ok := d.GetOkExists("precise_search"); ok { |
| 212 | + paramMap["PreciseSearch"] = helper.IntUint64(v.(int)) |
| 213 | + } |
| 214 | + |
| 215 | + if v, ok := d.GetOkExists("biz_type"); ok { |
| 216 | + paramMap["BizType"] = helper.IntUint64(v.(int)) |
| 217 | + } |
| 218 | + |
| 219 | + var respData []*clsv20201016.TopicInfo |
| 220 | + reqErr := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError { |
| 221 | + result, e := service.DescribeClsTopicsByFilter(ctx, paramMap) |
| 222 | + if e != nil { |
| 223 | + return tccommon.RetryError(e) |
| 224 | + } |
| 225 | + |
| 226 | + respData = result |
| 227 | + return nil |
| 228 | + }) |
| 229 | + |
| 230 | + if reqErr != nil { |
| 231 | + return reqErr |
| 232 | + } |
| 233 | + |
| 234 | + ids := make([]string, 0, len(respData)) |
| 235 | + topicsList := make([]map[string]interface{}, 0, len(respData)) |
| 236 | + if respData != nil { |
| 237 | + for _, topics := range respData { |
| 238 | + topicsMap := map[string]interface{}{} |
| 239 | + if topics.LogsetId != nil { |
| 240 | + topicsMap["logset_id"] = topics.LogsetId |
| 241 | + } |
| 242 | + |
| 243 | + if topics.TopicId != nil { |
| 244 | + topicsMap["topic_id"] = topics.TopicId |
| 245 | + ids = append(ids, *topics.TopicId) |
| 246 | + } |
| 247 | + |
| 248 | + if topics.TopicName != nil { |
| 249 | + topicsMap["topic_name"] = topics.TopicName |
| 250 | + } |
| 251 | + |
| 252 | + if topics.PartitionCount != nil { |
| 253 | + topicsMap["partition_count"] = topics.PartitionCount |
| 254 | + } |
| 255 | + |
| 256 | + if topics.Index != nil { |
| 257 | + topicsMap["index"] = topics.Index |
| 258 | + } |
| 259 | + |
| 260 | + if topics.AssumerName != nil { |
| 261 | + topicsMap["assumer_name"] = topics.AssumerName |
| 262 | + } |
| 263 | + |
| 264 | + if topics.CreateTime != nil { |
| 265 | + topicsMap["create_time"] = topics.CreateTime |
| 266 | + } |
| 267 | + |
| 268 | + if topics.Status != nil { |
| 269 | + topicsMap["status"] = topics.Status |
| 270 | + } |
| 271 | + |
| 272 | + tagsList := make([]map[string]interface{}, 0, len(topics.Tags)) |
| 273 | + if topics.Tags != nil { |
| 274 | + for _, tags := range topics.Tags { |
| 275 | + tagsMap := map[string]interface{}{} |
| 276 | + if tags.Key != nil { |
| 277 | + tagsMap["key"] = tags.Key |
| 278 | + } |
| 279 | + |
| 280 | + if tags.Value != nil { |
| 281 | + tagsMap["value"] = tags.Value |
| 282 | + } |
| 283 | + |
| 284 | + tagsList = append(tagsList, tagsMap) |
| 285 | + } |
| 286 | + |
| 287 | + topicsMap["tags"] = tagsList |
| 288 | + } |
| 289 | + |
| 290 | + if topics.AutoSplit != nil { |
| 291 | + topicsMap["auto_split"] = topics.AutoSplit |
| 292 | + } |
| 293 | + |
| 294 | + if topics.MaxSplitPartitions != nil { |
| 295 | + topicsMap["max_split_partitions"] = topics.MaxSplitPartitions |
| 296 | + } |
| 297 | + |
| 298 | + if topics.StorageType != nil { |
| 299 | + topicsMap["storage_type"] = topics.StorageType |
| 300 | + } |
| 301 | + |
| 302 | + if topics.Period != nil { |
| 303 | + topicsMap["period"] = topics.Period |
| 304 | + } |
| 305 | + |
| 306 | + if topics.SubAssumerName != nil { |
| 307 | + topicsMap["sub_assumer_name"] = topics.SubAssumerName |
| 308 | + } |
| 309 | + |
| 310 | + if topics.Describes != nil { |
| 311 | + topicsMap["describes"] = topics.Describes |
| 312 | + } |
| 313 | + |
| 314 | + if topics.HotPeriod != nil { |
| 315 | + topicsMap["hot_period"] = topics.HotPeriod |
| 316 | + } |
| 317 | + |
| 318 | + if topics.BizType != nil { |
| 319 | + topicsMap["biz_type"] = topics.BizType |
| 320 | + } |
| 321 | + |
| 322 | + if topics.IsWebTracking != nil { |
| 323 | + topicsMap["is_web_tracking"] = topics.IsWebTracking |
| 324 | + } |
| 325 | + |
| 326 | + topicsList = append(topicsList, topicsMap) |
| 327 | + } |
| 328 | + |
| 329 | + _ = d.Set("topics", topicsList) |
| 330 | + } |
| 331 | + |
| 332 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 333 | + output, ok := d.GetOk("result_output_file") |
| 334 | + if ok && output.(string) != "" { |
| 335 | + if e := tccommon.WriteToFile(output.(string), topicsList); e != nil { |
| 336 | + return e |
| 337 | + } |
| 338 | + } |
| 339 | + |
| 340 | + return nil |
| 341 | +} |
0 commit comments