|
| 1 | +package alicloud |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "regexp" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/PaesslerAG/jsonpath" |
| 9 | + "github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/helper/validation" |
| 13 | +) |
| 14 | + |
| 15 | +func dataSourceAliCloudEsaWafRuleSets() *schema.Resource { |
| 16 | + return &schema.Resource{ |
| 17 | + Read: dataSourceAliCloudEsaWafRuleSetRead, |
| 18 | + Schema: map[string]*schema.Schema{ |
| 19 | + "ids": { |
| 20 | + Type: schema.TypeList, |
| 21 | + Optional: true, |
| 22 | + Computed: true, |
| 23 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 24 | + }, |
| 25 | + "name_regex": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Optional: true, |
| 28 | + ValidateFunc: validation.StringIsValidRegExp, |
| 29 | + }, |
| 30 | + "site_id": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Required: true, |
| 33 | + }, |
| 34 | + "phase": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Required: true, |
| 37 | + }, |
| 38 | + "site_version": { |
| 39 | + Type: schema.TypeInt, |
| 40 | + Required: true, |
| 41 | + }, |
| 42 | + "status": { |
| 43 | + Type: schema.TypeString, |
| 44 | + Optional: true, |
| 45 | + ValidateFunc: StringInSlice([]string{"on", "off"}, true), |
| 46 | + }, |
| 47 | + "query_args": { |
| 48 | + Type: schema.TypeList, |
| 49 | + Optional: true, |
| 50 | + ForceNew: true, |
| 51 | + MaxItems: 1, |
| 52 | + Elem: &schema.Resource{ |
| 53 | + Schema: map[string]*schema.Schema{ |
| 54 | + "any_like": { |
| 55 | + Type: schema.TypeString, |
| 56 | + Optional: true, |
| 57 | + }, |
| 58 | + "name_like": { |
| 59 | + Type: schema.TypeString, |
| 60 | + Optional: true, |
| 61 | + }, |
| 62 | + "order_by": { |
| 63 | + Type: schema.TypeString, |
| 64 | + Optional: true, |
| 65 | + }, |
| 66 | + "desc": { |
| 67 | + Type: schema.TypeBool, |
| 68 | + Optional: true, |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + "output_file": { |
| 74 | + Type: schema.TypeString, |
| 75 | + Optional: true, |
| 76 | + }, |
| 77 | + "names": { |
| 78 | + Type: schema.TypeList, |
| 79 | + Computed: true, |
| 80 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 81 | + }, |
| 82 | + "sets": { |
| 83 | + Type: schema.TypeList, |
| 84 | + Computed: true, |
| 85 | + Elem: &schema.Resource{ |
| 86 | + Schema: map[string]*schema.Schema{ |
| 87 | + "id": { |
| 88 | + Type: schema.TypeString, |
| 89 | + Computed: true, |
| 90 | + }, |
| 91 | + "ruleset_id": { |
| 92 | + Type: schema.TypeString, |
| 93 | + Computed: true, |
| 94 | + }, |
| 95 | + "phase": { |
| 96 | + Type: schema.TypeString, |
| 97 | + Computed: true, |
| 98 | + }, |
| 99 | + "name": { |
| 100 | + Type: schema.TypeString, |
| 101 | + Computed: true, |
| 102 | + }, |
| 103 | + "target": { |
| 104 | + Type: schema.TypeString, |
| 105 | + Computed: true, |
| 106 | + }, |
| 107 | + "status": { |
| 108 | + Type: schema.TypeString, |
| 109 | + Computed: true, |
| 110 | + }, |
| 111 | + "update_time": { |
| 112 | + Type: schema.TypeString, |
| 113 | + Computed: true, |
| 114 | + }, |
| 115 | + "types": { |
| 116 | + Type: schema.TypeList, |
| 117 | + Computed: true, |
| 118 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 119 | + }, |
| 120 | + "fields": { |
| 121 | + Type: schema.TypeList, |
| 122 | + Computed: true, |
| 123 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 124 | + }, |
| 125 | + }, |
| 126 | + }, |
| 127 | + }, |
| 128 | + }, |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +func dataSourceAliCloudEsaWafRuleSetRead(d *schema.ResourceData, meta interface{}) error { |
| 133 | + client := meta.(*connectivity.AliyunClient) |
| 134 | + |
| 135 | + action := "ListWafRulesets" |
| 136 | + request := make(map[string]interface{}) |
| 137 | + request["RegionId"] = client.RegionId |
| 138 | + request["PageSize"] = PageSizeLarge |
| 139 | + request["PageNumber"] = 1 |
| 140 | + |
| 141 | + request["SiteId"] = d.Get("site_id") |
| 142 | + request["Phase"] = d.Get("phase") |
| 143 | + request["SiteVersion"] = d.Get("site_version") |
| 144 | + |
| 145 | + if v, ok := d.GetOk("query_args"); ok { |
| 146 | + queryArgsMap := map[string]interface{}{} |
| 147 | + for _, queryArgsList := range v.([]interface{}) { |
| 148 | + queryArgsArg := queryArgsList.(map[string]interface{}) |
| 149 | + |
| 150 | + if anyLike, ok := queryArgsArg["any_like"]; ok { |
| 151 | + queryArgsMap["AnyLike"] = anyLike |
| 152 | + } |
| 153 | + |
| 154 | + if nameLike, ok := queryArgsArg["name_like"]; ok { |
| 155 | + queryArgsMap["NameLike"] = nameLike |
| 156 | + } |
| 157 | + |
| 158 | + if orderBy, ok := queryArgsArg["order_by"]; ok { |
| 159 | + queryArgsMap["OrderBy"] = orderBy |
| 160 | + } |
| 161 | + |
| 162 | + if desc, ok := d.GetOkExists("query_args.0.desc"); ok { |
| 163 | + queryArgsMap["Desc"] = desc |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + request["QueryArgs"] = convertObjectToJsonString(queryArgsMap) |
| 168 | + } |
| 169 | + |
| 170 | + status, statusOk := d.GetOk("status") |
| 171 | + |
| 172 | + var objects []map[string]interface{} |
| 173 | + |
| 174 | + idsMap := make(map[string]string) |
| 175 | + if v, ok := d.GetOk("ids"); ok { |
| 176 | + for _, vv := range v.([]interface{}) { |
| 177 | + if vv == nil { |
| 178 | + continue |
| 179 | + } |
| 180 | + idsMap[vv.(string)] = vv.(string) |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + var wafRuleSetNameRegex *regexp.Regexp |
| 185 | + if v, ok := d.GetOk("name_regex"); ok { |
| 186 | + r, err := regexp.Compile(v.(string)) |
| 187 | + if err != nil { |
| 188 | + return WrapError(err) |
| 189 | + } |
| 190 | + |
| 191 | + wafRuleSetNameRegex = r |
| 192 | + } |
| 193 | + |
| 194 | + var response map[string]interface{} |
| 195 | + var err error |
| 196 | + |
| 197 | + for { |
| 198 | + wait := incrementalWait(3*time.Second, 5*time.Second) |
| 199 | + err = resource.Retry(5*time.Minute, func() *resource.RetryError { |
| 200 | + response, err = client.RpcPost("ESA", "2024-09-10", action, nil, request, true) |
| 201 | + if err != nil { |
| 202 | + if IsExpectedErrors(err, []string{"Site.ServiceBusy", "TooManyRequests"}) || NeedRetry(err) { |
| 203 | + wait() |
| 204 | + return resource.RetryableError(err) |
| 205 | + } |
| 206 | + return resource.NonRetryableError(err) |
| 207 | + } |
| 208 | + return nil |
| 209 | + }) |
| 210 | + addDebug(action, response, request) |
| 211 | + |
| 212 | + if err != nil { |
| 213 | + return WrapErrorf(err, DataDefaultErrorMsg, "alicloud_esa_waf_rulesets", action, AlibabaCloudSdkGoERROR) |
| 214 | + } |
| 215 | + |
| 216 | + resp, err := jsonpath.Get("$.Rulesets", response) |
| 217 | + if err != nil { |
| 218 | + return WrapErrorf(err, FailedGetAttributeMsg, action, "$.Rulesets", response) |
| 219 | + } |
| 220 | + |
| 221 | + result, _ := resp.([]interface{}) |
| 222 | + for _, v := range result { |
| 223 | + item := v.(map[string]interface{}) |
| 224 | + if len(idsMap) > 0 { |
| 225 | + if _, ok := idsMap[fmt.Sprintf("%v:%v", item["Id"], request["SiteId"])]; !ok { |
| 226 | + continue |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + if wafRuleSetNameRegex != nil { |
| 231 | + if !wafRuleSetNameRegex.MatchString(fmt.Sprint(item["Name"])) { |
| 232 | + continue |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + if statusOk && status.(string) != "" && status.(string) != item["Status"].(string) { |
| 237 | + continue |
| 238 | + } |
| 239 | + |
| 240 | + objects = append(objects, item) |
| 241 | + } |
| 242 | + |
| 243 | + if len(result) < PageSizeLarge { |
| 244 | + break |
| 245 | + } |
| 246 | + |
| 247 | + request["PageNumber"] = request["PageNumber"].(int) + 1 |
| 248 | + } |
| 249 | + |
| 250 | + ids := make([]string, 0) |
| 251 | + names := make([]interface{}, 0) |
| 252 | + s := make([]map[string]interface{}, 0) |
| 253 | + for _, object := range objects { |
| 254 | + mapping := map[string]interface{}{ |
| 255 | + "id": fmt.Sprintf("%v:%v", object["Id"], request["SiteId"]), |
| 256 | + "ruleset_id": fmt.Sprint(object["Id"]), |
| 257 | + "phase": object["Phase"], |
| 258 | + "name": object["Name"], |
| 259 | + "target": object["Target"], |
| 260 | + "status": object["Status"], |
| 261 | + "update_time": object["UpdateTime"], |
| 262 | + "types": object["Types"], |
| 263 | + "fields": object["Fields"], |
| 264 | + } |
| 265 | + |
| 266 | + ids = append(ids, fmt.Sprint(mapping["id"])) |
| 267 | + names = append(names, object["Name"]) |
| 268 | + s = append(s, mapping) |
| 269 | + } |
| 270 | + |
| 271 | + d.SetId(dataResourceIdHash(ids)) |
| 272 | + |
| 273 | + if err := d.Set("ids", ids); err != nil { |
| 274 | + return WrapError(err) |
| 275 | + } |
| 276 | + |
| 277 | + if err := d.Set("names", names); err != nil { |
| 278 | + return WrapError(err) |
| 279 | + } |
| 280 | + |
| 281 | + if err := d.Set("sets", s); err != nil { |
| 282 | + return WrapError(err) |
| 283 | + } |
| 284 | + |
| 285 | + if output, ok := d.GetOk("output_file"); ok && output.(string) != "" { |
| 286 | + writeToFile(output.(string), s) |
| 287 | + } |
| 288 | + |
| 289 | + return nil |
| 290 | +} |
0 commit comments