|
| 1 | +package waf |
| 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 | + wafv20180125 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/waf/v20180125" |
| 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 DataSourceTencentCloudWafOwaspRuleTypes() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Read: dataSourceTencentCloudWafOwaspRuleTypesRead, |
| 17 | + Schema: map[string]*schema.Schema{ |
| 18 | + "domain": { |
| 19 | + Type: schema.TypeString, |
| 20 | + Required: true, |
| 21 | + Description: "Domain names to be queried.", |
| 22 | + }, |
| 23 | + |
| 24 | + "filters": { |
| 25 | + Type: schema.TypeList, |
| 26 | + Optional: true, |
| 27 | + Description: "Filter conditions. supports RuleId, CveID, and Desc.", |
| 28 | + Elem: &schema.Resource{ |
| 29 | + Schema: map[string]*schema.Schema{ |
| 30 | + "name": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Required: true, |
| 33 | + Description: "Field name, used for filtering\nFilter the sub-order number (value) by DealName.", |
| 34 | + }, |
| 35 | + "values": { |
| 36 | + Type: schema.TypeSet, |
| 37 | + Required: true, |
| 38 | + Description: "Values after filtering.", |
| 39 | + Elem: &schema.Schema{ |
| 40 | + Type: schema.TypeString, |
| 41 | + }, |
| 42 | + }, |
| 43 | + "exact_match": { |
| 44 | + Type: schema.TypeBool, |
| 45 | + Required: true, |
| 46 | + Description: "Exact search or not.", |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + |
| 52 | + "list": { |
| 53 | + Type: schema.TypeList, |
| 54 | + Computed: true, |
| 55 | + Description: "Rule type list and information.", |
| 56 | + Elem: &schema.Resource{ |
| 57 | + Schema: map[string]*schema.Schema{ |
| 58 | + "type_id": { |
| 59 | + Type: schema.TypeInt, |
| 60 | + Computed: true, |
| 61 | + Description: "Type ID.", |
| 62 | + }, |
| 63 | + "type_name": { |
| 64 | + Type: schema.TypeString, |
| 65 | + Computed: true, |
| 66 | + Description: "Type name.", |
| 67 | + }, |
| 68 | + "description": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Computed: true, |
| 71 | + Description: "Type description.", |
| 72 | + }, |
| 73 | + "classification": { |
| 74 | + Type: schema.TypeString, |
| 75 | + Computed: true, |
| 76 | + Description: "Data type category.", |
| 77 | + }, |
| 78 | + "action": { |
| 79 | + Type: schema.TypeInt, |
| 80 | + Computed: true, |
| 81 | + Description: "Protection mode of the rule type. valid values: 0 (observation), 1 (intercept).", |
| 82 | + }, |
| 83 | + "level": { |
| 84 | + Type: schema.TypeInt, |
| 85 | + Computed: true, |
| 86 | + Description: "Protection level of the rule type. valid values: 100 (loose), 200 (normal), 300 (strict), 400 (ultra-strict).", |
| 87 | + }, |
| 88 | + "status": { |
| 89 | + Type: schema.TypeInt, |
| 90 | + Computed: true, |
| 91 | + Description: "The switch status of the rule type. valid values: 0 (disabled), 1 (enabled).", |
| 92 | + }, |
| 93 | + "total_rule": { |
| 94 | + Type: schema.TypeInt, |
| 95 | + Computed: true, |
| 96 | + Description: "Specifies all rules under the rule type. always.", |
| 97 | + }, |
| 98 | + "active_rule": { |
| 99 | + Type: schema.TypeInt, |
| 100 | + Computed: true, |
| 101 | + Description: "Indicates the total number of rules enabled under the rule type.", |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + |
| 107 | + "result_output_file": { |
| 108 | + Type: schema.TypeString, |
| 109 | + Optional: true, |
| 110 | + Description: "Used to save results.", |
| 111 | + }, |
| 112 | + }, |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func dataSourceTencentCloudWafOwaspRuleTypesRead(d *schema.ResourceData, meta interface{}) error { |
| 117 | + defer tccommon.LogElapsed("data_source.tencentcloud_waf_owasp_rule_types.read")() |
| 118 | + defer tccommon.InconsistentCheck(d, meta)() |
| 119 | + |
| 120 | + var ( |
| 121 | + logId = tccommon.GetLogId(nil) |
| 122 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 123 | + service = WafService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 124 | + domain string |
| 125 | + ) |
| 126 | + |
| 127 | + paramMap := make(map[string]interface{}) |
| 128 | + if v, ok := d.GetOk("domain"); ok { |
| 129 | + paramMap["Domain"] = helper.String(v.(string)) |
| 130 | + domain = v.(string) |
| 131 | + } |
| 132 | + |
| 133 | + if v, ok := d.GetOk("filters"); ok { |
| 134 | + filtersSet := v.([]interface{}) |
| 135 | + tmpSet := make([]*wafv20180125.FiltersItemNew, 0, len(filtersSet)) |
| 136 | + for _, item := range filtersSet { |
| 137 | + filtersMap := item.(map[string]interface{}) |
| 138 | + filtersItemNew := wafv20180125.FiltersItemNew{} |
| 139 | + if v, ok := filtersMap["name"].(string); ok && v != "" { |
| 140 | + filtersItemNew.Name = helper.String(v) |
| 141 | + } |
| 142 | + |
| 143 | + if v, ok := filtersMap["values"]; ok { |
| 144 | + valuesSet := v.(*schema.Set).List() |
| 145 | + for i := range valuesSet { |
| 146 | + values := valuesSet[i].(string) |
| 147 | + filtersItemNew.Values = append(filtersItemNew.Values, helper.String(values)) |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + if v, ok := filtersMap["exact_match"].(bool); ok { |
| 152 | + filtersItemNew.ExactMatch = helper.Bool(v) |
| 153 | + } |
| 154 | + |
| 155 | + tmpSet = append(tmpSet, &filtersItemNew) |
| 156 | + } |
| 157 | + |
| 158 | + paramMap["Filters"] = tmpSet |
| 159 | + } |
| 160 | + |
| 161 | + var respData []*wafv20180125.OwaspRuleType |
| 162 | + reqErr := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError { |
| 163 | + result, e := service.DescribeWafOwaspRuleTypesByFilter(ctx, paramMap) |
| 164 | + if e != nil { |
| 165 | + return tccommon.RetryError(e) |
| 166 | + } |
| 167 | + |
| 168 | + respData = result |
| 169 | + return nil |
| 170 | + }) |
| 171 | + |
| 172 | + if reqErr != nil { |
| 173 | + return reqErr |
| 174 | + } |
| 175 | + |
| 176 | + listList := make([]map[string]interface{}, 0, len(respData)) |
| 177 | + for _, list := range respData { |
| 178 | + listMap := map[string]interface{}{} |
| 179 | + if list.TypeId != nil { |
| 180 | + listMap["type_id"] = list.TypeId |
| 181 | + } |
| 182 | + |
| 183 | + if list.TypeName != nil { |
| 184 | + listMap["type_name"] = list.TypeName |
| 185 | + } |
| 186 | + |
| 187 | + if list.Description != nil { |
| 188 | + listMap["description"] = list.Description |
| 189 | + } |
| 190 | + |
| 191 | + if list.Classification != nil { |
| 192 | + listMap["classification"] = list.Classification |
| 193 | + } |
| 194 | + |
| 195 | + if list.Action != nil { |
| 196 | + listMap["action"] = list.Action |
| 197 | + } |
| 198 | + |
| 199 | + if list.Level != nil { |
| 200 | + listMap["level"] = list.Level |
| 201 | + } |
| 202 | + |
| 203 | + if list.Status != nil { |
| 204 | + listMap["status"] = list.Status |
| 205 | + } |
| 206 | + |
| 207 | + if list.TotalRule != nil { |
| 208 | + listMap["total_rule"] = list.TotalRule |
| 209 | + } |
| 210 | + |
| 211 | + if list.ActiveRule != nil { |
| 212 | + listMap["active_rule"] = list.ActiveRule |
| 213 | + } |
| 214 | + |
| 215 | + listList = append(listList, listMap) |
| 216 | + } |
| 217 | + |
| 218 | + _ = d.Set("list", listList) |
| 219 | + d.SetId(domain) |
| 220 | + output, ok := d.GetOk("result_output_file") |
| 221 | + if ok && output.(string) != "" { |
| 222 | + if e := tccommon.WriteToFile(output.(string), d); e != nil { |
| 223 | + return e |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + return nil |
| 228 | +} |
0 commit comments