|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of eb event_rules |
| 3 | +Example Usage |
| 4 | +```hcl |
| 5 | +resource "tencentcloud_eb_event_bus" "foo" { |
| 6 | + event_bus_name = "tf-event_bus_rule" |
| 7 | + description = "event bus desc" |
| 8 | + enable_store = false |
| 9 | + save_days = 1 |
| 10 | + tags = { |
| 11 | + "createdBy" = "terraform" |
| 12 | + } |
| 13 | +} |
| 14 | +resource "tencentcloud_eb_event_rule" "event_rule" { |
| 15 | + event_bus_id = tencentcloud_eb_event_bus.foo.id |
| 16 | + rule_name = "tf-event_rule" |
| 17 | + description = "event rule desc" |
| 18 | + enable = true |
| 19 | + event_pattern = jsonencode( |
| 20 | + { |
| 21 | + source = "apigw.cloud.tencent" |
| 22 | + type = [ |
| 23 | + "connector:apigw", |
| 24 | + ] |
| 25 | + } |
| 26 | + ) |
| 27 | + tags = { |
| 28 | + "createdBy" = "terraform" |
| 29 | + } |
| 30 | +} |
| 31 | +data "tencentcloud_eb_event_rules" "event_rules" { |
| 32 | + event_bus_id = tencentcloud_eb_event_bus.foo.id |
| 33 | + order_by = "AddTime" |
| 34 | + order = "DESC" |
| 35 | + depends_on = [tencentcloud_eb_event_rule.event_rule] |
| 36 | +} |
| 37 | +``` |
| 38 | +*/ |
| 39 | +package tencentcloud |
| 40 | + |
| 41 | +import ( |
| 42 | + "context" |
| 43 | + |
| 44 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 45 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 46 | + eb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/eb/v20210416" |
| 47 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 48 | +) |
| 49 | + |
| 50 | +func dataSourceTencentCloudEbEventRules() *schema.Resource { |
| 51 | + return &schema.Resource{ |
| 52 | + Read: dataSourceTencentCloudEbEventRulesRead, |
| 53 | + Schema: map[string]*schema.Schema{ |
| 54 | + "event_bus_id": { |
| 55 | + Required: true, |
| 56 | + Type: schema.TypeString, |
| 57 | + Description: "event bus Id.", |
| 58 | + }, |
| 59 | + |
| 60 | + "order_by": { |
| 61 | + Optional: true, |
| 62 | + Type: schema.TypeString, |
| 63 | + Description: "According to which field to sort the returned results, the following fields are supported: AddTime (creation time), ModTime (modification time).", |
| 64 | + }, |
| 65 | + |
| 66 | + "order": { |
| 67 | + Optional: true, |
| 68 | + Type: schema.TypeString, |
| 69 | + Description: "Return results in ascending or descending order, optional values ASC (ascending) and DESC (descending).", |
| 70 | + }, |
| 71 | + |
| 72 | + "rules": { |
| 73 | + Computed: true, |
| 74 | + Type: schema.TypeList, |
| 75 | + Description: "Event rule information.", |
| 76 | + Elem: &schema.Resource{ |
| 77 | + Schema: map[string]*schema.Schema{ |
| 78 | + "status": { |
| 79 | + Type: schema.TypeString, |
| 80 | + Computed: true, |
| 81 | + Description: "Status.", |
| 82 | + }, |
| 83 | + "mod_time": { |
| 84 | + Type: schema.TypeString, |
| 85 | + Computed: true, |
| 86 | + Description: "modify time.", |
| 87 | + }, |
| 88 | + "enable": { |
| 89 | + Type: schema.TypeBool, |
| 90 | + Computed: true, |
| 91 | + Description: "enable switch.", |
| 92 | + }, |
| 93 | + "description": { |
| 94 | + Type: schema.TypeString, |
| 95 | + Computed: true, |
| 96 | + Description: "description.", |
| 97 | + }, |
| 98 | + "rule_id": { |
| 99 | + Type: schema.TypeString, |
| 100 | + Computed: true, |
| 101 | + Description: "rule Id.", |
| 102 | + }, |
| 103 | + "add_time": { |
| 104 | + Type: schema.TypeString, |
| 105 | + Computed: true, |
| 106 | + Description: "create time.", |
| 107 | + }, |
| 108 | + "event_bus_id": { |
| 109 | + Type: schema.TypeString, |
| 110 | + Computed: true, |
| 111 | + Description: "event bus Id.", |
| 112 | + }, |
| 113 | + "rule_name": { |
| 114 | + Type: schema.TypeString, |
| 115 | + Computed: true, |
| 116 | + Description: "rule name.", |
| 117 | + }, |
| 118 | + "targets": { |
| 119 | + Type: schema.TypeList, |
| 120 | + Computed: true, |
| 121 | + Description: "Target brief information, note: this field may return null, indicating that no valid value can be obtained.", |
| 122 | + Elem: &schema.Resource{ |
| 123 | + Schema: map[string]*schema.Schema{ |
| 124 | + "target_id": { |
| 125 | + Type: schema.TypeString, |
| 126 | + Computed: true, |
| 127 | + Description: "target Id.", |
| 128 | + }, |
| 129 | + "type": { |
| 130 | + Type: schema.TypeString, |
| 131 | + Computed: true, |
| 132 | + Description: "target type.", |
| 133 | + }, |
| 134 | + }, |
| 135 | + }, |
| 136 | + }, |
| 137 | + "dead_letter_config": { |
| 138 | + Type: schema.TypeList, |
| 139 | + Computed: true, |
| 140 | + Description: "The dlq rule set by rule. It may be null. Note: this field may return null, indicating that no valid value can be obtained.", |
| 141 | + Elem: &schema.Resource{ |
| 142 | + Schema: map[string]*schema.Schema{ |
| 143 | + "dispose_method": { |
| 144 | + Type: schema.TypeString, |
| 145 | + Computed: true, |
| 146 | + Description: "Support three modes of dlq, discarding, ignoring errors and continuing to pass, corresponding to: DLQ, DROP, IGNORE_ERROR.", |
| 147 | + }, |
| 148 | + "ckafka_delivery_params": { |
| 149 | + Type: schema.TypeList, |
| 150 | + Computed: true, |
| 151 | + Description: "After setting the DLQ mode, this option is required. The error message will be delivered to the corresponding kafka topic Note: This field may return null, indicating that no valid value can be obtained.", |
| 152 | + Elem: &schema.Resource{ |
| 153 | + Schema: map[string]*schema.Schema{ |
| 154 | + "topic_name": { |
| 155 | + Type: schema.TypeString, |
| 156 | + Computed: true, |
| 157 | + Description: "ckafka topic name.", |
| 158 | + }, |
| 159 | + "resource_description": { |
| 160 | + Type: schema.TypeString, |
| 161 | + Computed: true, |
| 162 | + Description: "ckafka resource qcs six-segment.", |
| 163 | + }, |
| 164 | + }, |
| 165 | + }, |
| 166 | + }, |
| 167 | + }, |
| 168 | + }, |
| 169 | + }, |
| 170 | + }, |
| 171 | + }, |
| 172 | + }, |
| 173 | + |
| 174 | + "result_output_file": { |
| 175 | + Type: schema.TypeString, |
| 176 | + Optional: true, |
| 177 | + Description: "Used to save results.", |
| 178 | + }, |
| 179 | + }, |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +func dataSourceTencentCloudEbEventRulesRead(d *schema.ResourceData, meta interface{}) error { |
| 184 | + defer logElapsed("data_source.tencentcloud_eb_event_rules.read")() |
| 185 | + defer inconsistentCheck(d, meta)() |
| 186 | + |
| 187 | + logId := getLogId(contextNil) |
| 188 | + |
| 189 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 190 | + |
| 191 | + paramMap := make(map[string]interface{}) |
| 192 | + if v, ok := d.GetOk("event_bus_id"); ok { |
| 193 | + paramMap["EventBusId"] = helper.String(v.(string)) |
| 194 | + } |
| 195 | + |
| 196 | + if v, ok := d.GetOk("order_by"); ok { |
| 197 | + paramMap["OrderBy"] = helper.String(v.(string)) |
| 198 | + } |
| 199 | + |
| 200 | + if v, ok := d.GetOk("order"); ok { |
| 201 | + paramMap["Order"] = helper.String(v.(string)) |
| 202 | + } |
| 203 | + |
| 204 | + service := EbService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 205 | + |
| 206 | + var rules []*eb.Rule |
| 207 | + |
| 208 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 209 | + result, e := service.DescribeEbEventRulesByFilter(ctx, paramMap) |
| 210 | + if e != nil { |
| 211 | + return retryError(e) |
| 212 | + } |
| 213 | + rules = result |
| 214 | + return nil |
| 215 | + }) |
| 216 | + if err != nil { |
| 217 | + return err |
| 218 | + } |
| 219 | + |
| 220 | + ids := make([]string, 0, len(rules)) |
| 221 | + tmpList := make([]map[string]interface{}, 0, len(rules)) |
| 222 | + |
| 223 | + if rules != nil { |
| 224 | + for _, rule := range rules { |
| 225 | + ruleMap := map[string]interface{}{} |
| 226 | + |
| 227 | + if rule.Status != nil { |
| 228 | + ruleMap["status"] = rule.Status |
| 229 | + } |
| 230 | + |
| 231 | + if rule.ModTime != nil { |
| 232 | + ruleMap["mod_time"] = rule.ModTime |
| 233 | + } |
| 234 | + |
| 235 | + if rule.Enable != nil { |
| 236 | + ruleMap["enable"] = rule.Enable |
| 237 | + } |
| 238 | + |
| 239 | + if rule.Description != nil { |
| 240 | + ruleMap["description"] = rule.Description |
| 241 | + } |
| 242 | + |
| 243 | + if rule.RuleId != nil { |
| 244 | + ruleMap["rule_id"] = rule.RuleId |
| 245 | + } |
| 246 | + |
| 247 | + if rule.AddTime != nil { |
| 248 | + ruleMap["add_time"] = rule.AddTime |
| 249 | + } |
| 250 | + |
| 251 | + if rule.EventBusId != nil { |
| 252 | + ruleMap["event_bus_id"] = rule.EventBusId |
| 253 | + } |
| 254 | + |
| 255 | + if rule.RuleName != nil { |
| 256 | + ruleMap["rule_name"] = rule.RuleName |
| 257 | + } |
| 258 | + |
| 259 | + if rule.Targets != nil { |
| 260 | + targetsList := []interface{}{} |
| 261 | + for _, targets := range rule.Targets { |
| 262 | + targetsMap := map[string]interface{}{} |
| 263 | + |
| 264 | + if targets.TargetId != nil { |
| 265 | + targetsMap["target_id"] = targets.TargetId |
| 266 | + } |
| 267 | + |
| 268 | + if targets.Type != nil { |
| 269 | + targetsMap["type"] = targets.Type |
| 270 | + } |
| 271 | + |
| 272 | + targetsList = append(targetsList, targetsMap) |
| 273 | + } |
| 274 | + |
| 275 | + ruleMap["targets"] = []interface{}{targetsList} |
| 276 | + } |
| 277 | + |
| 278 | + if rule.DeadLetterConfig != nil { |
| 279 | + deadLetterConfigMap := map[string]interface{}{} |
| 280 | + |
| 281 | + if rule.DeadLetterConfig.DisposeMethod != nil { |
| 282 | + deadLetterConfigMap["dispose_method"] = rule.DeadLetterConfig.DisposeMethod |
| 283 | + } |
| 284 | + |
| 285 | + if rule.DeadLetterConfig.CkafkaDeliveryParams != nil { |
| 286 | + ckafkaDeliveryParamsMap := map[string]interface{}{} |
| 287 | + |
| 288 | + if rule.DeadLetterConfig.CkafkaDeliveryParams.TopicName != nil { |
| 289 | + ckafkaDeliveryParamsMap["topic_name"] = rule.DeadLetterConfig.CkafkaDeliveryParams.TopicName |
| 290 | + } |
| 291 | + |
| 292 | + if rule.DeadLetterConfig.CkafkaDeliveryParams.ResourceDescription != nil { |
| 293 | + ckafkaDeliveryParamsMap["resource_description"] = rule.DeadLetterConfig.CkafkaDeliveryParams.ResourceDescription |
| 294 | + } |
| 295 | + |
| 296 | + deadLetterConfigMap["ckafka_delivery_params"] = []interface{}{ckafkaDeliveryParamsMap} |
| 297 | + } |
| 298 | + |
| 299 | + ruleMap["dead_letter_config"] = []interface{}{deadLetterConfigMap} |
| 300 | + } |
| 301 | + |
| 302 | + ids = append(ids, *rule.EventBusId) |
| 303 | + tmpList = append(tmpList, ruleMap) |
| 304 | + } |
| 305 | + |
| 306 | + _ = d.Set("rules", tmpList) |
| 307 | + } |
| 308 | + |
| 309 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 310 | + output, ok := d.GetOk("result_output_file") |
| 311 | + if ok && output.(string) != "" { |
| 312 | + if e := writeToFile(output.(string), tmpList); e != nil { |
| 313 | + return e |
| 314 | + } |
| 315 | + } |
| 316 | + return nil |
| 317 | +} |
0 commit comments