|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of wedata rule templates |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_wedata_rule_templates" "rule_templates" { |
| 8 | + type = 2 |
| 9 | + source_object_type = 2 |
| 10 | + project_id = "1840731346428280832" |
| 11 | + source_engine_types = [2, 4, 16] |
| 12 | +} |
| 13 | +``` |
| 14 | +*/ |
| 15 | +package tencentcloud |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + |
| 20 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 21 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 22 | + wedata "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/wedata/v20210820" |
| 23 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 24 | +) |
| 25 | + |
| 26 | +func dataSourceTencentCloudWedataRuleTemplates() *schema.Resource { |
| 27 | + return &schema.Resource{ |
| 28 | + Read: dataSourceTencentCloudWedataRuleTemplatesRead, |
| 29 | + Schema: map[string]*schema.Schema{ |
| 30 | + "type": { |
| 31 | + Optional: true, |
| 32 | + Type: schema.TypeInt, |
| 33 | + Description: "Template type. `1` means System template, `2` means Custom template.", |
| 34 | + }, |
| 35 | + |
| 36 | + "source_object_type": { |
| 37 | + Optional: true, |
| 38 | + Type: schema.TypeInt, |
| 39 | + Description: "Source data object type. `1`: Constant, `2`: Offline table level, `3`: Offline field level.", |
| 40 | + }, |
| 41 | + |
| 42 | + "project_id": { |
| 43 | + Optional: true, |
| 44 | + Type: schema.TypeString, |
| 45 | + Description: "Project ID.", |
| 46 | + }, |
| 47 | + |
| 48 | + "source_engine_types": { |
| 49 | + Optional: true, |
| 50 | + Type: schema.TypeSet, |
| 51 | + Elem: &schema.Schema{ |
| 52 | + Type: schema.TypeInt, |
| 53 | + }, |
| 54 | + Description: "Applicable type of source data.", |
| 55 | + }, |
| 56 | + |
| 57 | + "data": { |
| 58 | + Computed: true, |
| 59 | + Type: schema.TypeList, |
| 60 | + Description: "rule template list.", |
| 61 | + Elem: &schema.Resource{ |
| 62 | + Schema: map[string]*schema.Schema{ |
| 63 | + "rule_template_id": { |
| 64 | + Type: schema.TypeInt, |
| 65 | + Computed: true, |
| 66 | + Description: "ID of rule template.", |
| 67 | + }, |
| 68 | + "name": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Computed: true, |
| 71 | + Description: "Name of rule template.", |
| 72 | + }, |
| 73 | + "description": { |
| 74 | + Type: schema.TypeString, |
| 75 | + Computed: true, |
| 76 | + Description: "Description of rule template.", |
| 77 | + }, |
| 78 | + "type": { |
| 79 | + Type: schema.TypeInt, |
| 80 | + Computed: true, |
| 81 | + Description: "Template type. `1` means System template, `2` means Custom template.", |
| 82 | + }, |
| 83 | + "source_object_type": { |
| 84 | + Type: schema.TypeInt, |
| 85 | + Computed: true, |
| 86 | + Description: "Source object type. `1`: Constant, `2`: Offline table level, `3`: Offline field level.", |
| 87 | + }, |
| 88 | + "source_object_data_type": { |
| 89 | + Type: schema.TypeInt, |
| 90 | + Computed: true, |
| 91 | + Description: "Source data object type. `1`: value, `2`: string.", |
| 92 | + }, |
| 93 | + "source_content": { |
| 94 | + Type: schema.TypeString, |
| 95 | + Computed: true, |
| 96 | + Description: "Content of rule template.", |
| 97 | + }, |
| 98 | + "source_engine_types": { |
| 99 | + Type: schema.TypeSet, |
| 100 | + Elem: &schema.Schema{ |
| 101 | + Type: schema.TypeInt, |
| 102 | + }, |
| 103 | + Computed: true, |
| 104 | + Description: "Applicable type of source data.", |
| 105 | + }, |
| 106 | + "quality_dim": { |
| 107 | + Type: schema.TypeInt, |
| 108 | + Computed: true, |
| 109 | + Description: "Quality inspection dimensions. `1`: Accuracy, `2`: Uniqueness, `3`: Completeness, `4`: Consistency, `5`: Timeliness, `6`: Effectiveness.", |
| 110 | + }, |
| 111 | + "compare_type": { |
| 112 | + Type: schema.TypeInt, |
| 113 | + Computed: true, |
| 114 | + Description: "The type of comparison method supported by the rule (1: fixed value comparison, greater than, less than, greater than or equal to, etc. 2: fluctuating value comparison, absolute value, rise, fall).", |
| 115 | + }, |
| 116 | + "citation_count": { |
| 117 | + Type: schema.TypeInt, |
| 118 | + Computed: true, |
| 119 | + Description: "Citations.", |
| 120 | + }, |
| 121 | + "user_id": { |
| 122 | + Type: schema.TypeInt, |
| 123 | + Computed: true, |
| 124 | + Description: "user id.", |
| 125 | + }, |
| 126 | + "user_name": { |
| 127 | + Type: schema.TypeString, |
| 128 | + Computed: true, |
| 129 | + Description: "user name.", |
| 130 | + }, |
| 131 | + "update_time": { |
| 132 | + Type: schema.TypeString, |
| 133 | + Computed: true, |
| 134 | + Description: "update time, like: yyyy-MM-dd HH:mm:ss.", |
| 135 | + }, |
| 136 | + "where_flag": { |
| 137 | + Type: schema.TypeBool, |
| 138 | + Computed: true, |
| 139 | + Description: "If add where.", |
| 140 | + }, |
| 141 | + "multi_source_flag": { |
| 142 | + Type: schema.TypeBool, |
| 143 | + Computed: true, |
| 144 | + Description: "Whether to associate other library tables.", |
| 145 | + }, |
| 146 | + "sql_expression": { |
| 147 | + Type: schema.TypeString, |
| 148 | + Computed: true, |
| 149 | + Description: "Sql Expression.", |
| 150 | + }, |
| 151 | + "sub_quality_dim": { |
| 152 | + Type: schema.TypeInt, |
| 153 | + Computed: true, |
| 154 | + Description: "Sub Quality inspection dimensions. `1`: Accuracy, `2`: Uniqueness, `3`: Completeness, `4`: Consistency, `5`: Timeliness, `6`: Effectiveness.", |
| 155 | + }, |
| 156 | + }, |
| 157 | + }, |
| 158 | + }, |
| 159 | + |
| 160 | + "result_output_file": { |
| 161 | + Type: schema.TypeString, |
| 162 | + Optional: true, |
| 163 | + Description: "Used to save results.", |
| 164 | + }, |
| 165 | + }, |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +func dataSourceTencentCloudWedataRuleTemplatesRead(d *schema.ResourceData, meta interface{}) error { |
| 170 | + defer logElapsed("data_source.tencentcloud_wedata_rule_templates.read")() |
| 171 | + defer inconsistentCheck(d, meta)() |
| 172 | + |
| 173 | + logId := getLogId(contextNil) |
| 174 | + |
| 175 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 176 | + |
| 177 | + paramMap := make(map[string]interface{}) |
| 178 | + if v, ok := d.GetOkExists("type"); ok { |
| 179 | + paramMap["Type"] = helper.IntUint64(v.(int)) |
| 180 | + } |
| 181 | + |
| 182 | + if v, ok := d.GetOkExists("source_object_type"); ok { |
| 183 | + paramMap["SourceObjectType"] = helper.IntUint64(v.(int)) |
| 184 | + } |
| 185 | + |
| 186 | + if v, ok := d.GetOk("project_id"); ok { |
| 187 | + paramMap["ProjectId"] = helper.String(v.(string)) |
| 188 | + } |
| 189 | + |
| 190 | + if v, ok := d.GetOk("source_engine_types"); ok { |
| 191 | + sourceEngineTypesSet := v.(*schema.Set).List() |
| 192 | + var sourceEngineTypes []*uint64 |
| 193 | + for i := range sourceEngineTypesSet { |
| 194 | + sourceEngineType := sourceEngineTypesSet[i].(int) |
| 195 | + sourceEngineTypes = append(sourceEngineTypes, helper.IntUint64(sourceEngineType)) |
| 196 | + } |
| 197 | + paramMap["SourceEngineTypes"] = sourceEngineTypes |
| 198 | + } |
| 199 | + |
| 200 | + service := WedataService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 201 | + |
| 202 | + var data []*wedata.RuleTemplate |
| 203 | + |
| 204 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 205 | + result, e := service.DescribeWedataRuleTemplatesByFilter(ctx, paramMap) |
| 206 | + if e != nil { |
| 207 | + return retryError(e) |
| 208 | + } |
| 209 | + data = result |
| 210 | + return nil |
| 211 | + }) |
| 212 | + if err != nil { |
| 213 | + return err |
| 214 | + } |
| 215 | + |
| 216 | + ids := make([]string, 0, len(data)) |
| 217 | + tmpList := make([]map[string]interface{}, 0, len(data)) |
| 218 | + |
| 219 | + if data != nil { |
| 220 | + for _, ruleTemplate := range data { |
| 221 | + ruleTemplateMap := map[string]interface{}{} |
| 222 | + |
| 223 | + if ruleTemplate.RuleTemplateId != nil { |
| 224 | + ruleTemplateMap["rule_template_id"] = ruleTemplate.RuleTemplateId |
| 225 | + } |
| 226 | + |
| 227 | + if ruleTemplate.Name != nil { |
| 228 | + ruleTemplateMap["name"] = ruleTemplate.Name |
| 229 | + } |
| 230 | + |
| 231 | + if ruleTemplate.Description != nil { |
| 232 | + ruleTemplateMap["description"] = ruleTemplate.Description |
| 233 | + } |
| 234 | + |
| 235 | + if ruleTemplate.Type != nil { |
| 236 | + ruleTemplateMap["type"] = ruleTemplate.Type |
| 237 | + } |
| 238 | + |
| 239 | + if ruleTemplate.SourceObjectType != nil { |
| 240 | + ruleTemplateMap["source_object_type"] = ruleTemplate.SourceObjectType |
| 241 | + } |
| 242 | + |
| 243 | + if ruleTemplate.SourceObjectDataType != nil { |
| 244 | + ruleTemplateMap["source_object_data_type"] = ruleTemplate.SourceObjectDataType |
| 245 | + } |
| 246 | + |
| 247 | + if ruleTemplate.SourceContent != nil { |
| 248 | + ruleTemplateMap["source_content"] = ruleTemplate.SourceContent |
| 249 | + } |
| 250 | + |
| 251 | + if ruleTemplate.SourceEngineTypes != nil { |
| 252 | + ruleTemplateMap["source_engine_types"] = ruleTemplate.SourceEngineTypes |
| 253 | + } |
| 254 | + |
| 255 | + if ruleTemplate.QualityDim != nil { |
| 256 | + ruleTemplateMap["quality_dim"] = ruleTemplate.QualityDim |
| 257 | + } |
| 258 | + |
| 259 | + if ruleTemplate.CompareType != nil { |
| 260 | + ruleTemplateMap["compare_type"] = ruleTemplate.CompareType |
| 261 | + } |
| 262 | + |
| 263 | + if ruleTemplate.CitationCount != nil { |
| 264 | + ruleTemplateMap["citation_count"] = ruleTemplate.CitationCount |
| 265 | + } |
| 266 | + |
| 267 | + if ruleTemplate.UserId != nil { |
| 268 | + ruleTemplateMap["user_id"] = ruleTemplate.UserId |
| 269 | + } |
| 270 | + |
| 271 | + if ruleTemplate.UserName != nil { |
| 272 | + ruleTemplateMap["user_name"] = ruleTemplate.UserName |
| 273 | + } |
| 274 | + |
| 275 | + if ruleTemplate.UpdateTime != nil { |
| 276 | + ruleTemplateMap["update_time"] = ruleTemplate.UpdateTime |
| 277 | + } |
| 278 | + |
| 279 | + if ruleTemplate.WhereFlag != nil { |
| 280 | + ruleTemplateMap["where_flag"] = ruleTemplate.WhereFlag |
| 281 | + } |
| 282 | + |
| 283 | + if ruleTemplate.MultiSourceFlag != nil { |
| 284 | + ruleTemplateMap["multi_source_flag"] = ruleTemplate.MultiSourceFlag |
| 285 | + } |
| 286 | + |
| 287 | + if ruleTemplate.SqlExpression != nil { |
| 288 | + ruleTemplateMap["sql_expression"] = ruleTemplate.SqlExpression |
| 289 | + } |
| 290 | + |
| 291 | + if ruleTemplate.SubQualityDim != nil { |
| 292 | + ruleTemplateMap["sub_quality_dim"] = ruleTemplate.SubQualityDim |
| 293 | + } |
| 294 | + |
| 295 | + ids = append(ids, helper.UInt64ToStr(*ruleTemplate.RuleTemplateId)) |
| 296 | + tmpList = append(tmpList, ruleTemplateMap) |
| 297 | + } |
| 298 | + |
| 299 | + _ = d.Set("data", tmpList) |
| 300 | + } |
| 301 | + |
| 302 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 303 | + output, ok := d.GetOk("result_output_file") |
| 304 | + if ok && output.(string) != "" { |
| 305 | + if e := writeToFile(output.(string), tmpList); e != nil { |
| 306 | + return e |
| 307 | + } |
| 308 | + } |
| 309 | + return nil |
| 310 | +} |
0 commit comments