Skip to content

Commit 48c0a77

Browse files
committed
New Data Source: alicloud_esa_network_optimizations
1 parent 4f1056b commit 48c0a77

3 files changed

Lines changed: 441 additions & 0 deletions

File tree

Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
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 dataSourceAliCloudEsaNetworkOptimizations() *schema.Resource {
16+
return &schema.Resource{
17+
Read: dataSourceAliCloudEsaNetworkOptimizationRead,
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+
"config_id": {
35+
Type: schema.TypeString,
36+
Optional: true,
37+
},
38+
"config_type": {
39+
Type: schema.TypeString,
40+
Optional: true,
41+
},
42+
"rule_name": {
43+
Type: schema.TypeString,
44+
Optional: true,
45+
},
46+
"site_version": {
47+
Type: schema.TypeInt,
48+
Optional: true,
49+
},
50+
"output_file": {
51+
Type: schema.TypeString,
52+
Optional: true,
53+
},
54+
"names": {
55+
Type: schema.TypeList,
56+
Computed: true,
57+
Elem: &schema.Schema{Type: schema.TypeString},
58+
},
59+
"optimizations": {
60+
Type: schema.TypeList,
61+
Computed: true,
62+
Elem: &schema.Resource{
63+
Schema: map[string]*schema.Schema{
64+
"id": {
65+
Type: schema.TypeString,
66+
Computed: true,
67+
},
68+
"config_id": {
69+
Type: schema.TypeString,
70+
Computed: true,
71+
},
72+
"config_type": {
73+
Type: schema.TypeString,
74+
Computed: true,
75+
},
76+
"site_version": {
77+
Type: schema.TypeInt,
78+
Computed: true,
79+
},
80+
"rule_enable": {
81+
Type: schema.TypeString,
82+
Computed: true,
83+
},
84+
"rule_name": {
85+
Type: schema.TypeString,
86+
Computed: true,
87+
},
88+
"rule": {
89+
Type: schema.TypeString,
90+
Computed: true,
91+
},
92+
"sequence": {
93+
Type: schema.TypeInt,
94+
Computed: true,
95+
},
96+
"smart_routing": {
97+
Type: schema.TypeString,
98+
Computed: true,
99+
},
100+
"grpc": {
101+
Type: schema.TypeString,
102+
Computed: true,
103+
},
104+
"http2_origin": {
105+
Type: schema.TypeString,
106+
Computed: true,
107+
},
108+
"websocket": {
109+
Type: schema.TypeString,
110+
Computed: true,
111+
},
112+
"upload_max_filesize": {
113+
Type: schema.TypeString,
114+
Computed: true,
115+
},
116+
},
117+
},
118+
},
119+
},
120+
}
121+
}
122+
123+
func dataSourceAliCloudEsaNetworkOptimizationRead(d *schema.ResourceData, meta interface{}) error {
124+
client := meta.(*connectivity.AliyunClient)
125+
126+
action := "ListNetworkOptimizations"
127+
request := make(map[string]interface{})
128+
request["RegionId"] = client.RegionId
129+
request["PageSize"] = PageSizeLarge
130+
request["PageNumber"] = 1
131+
132+
request["SiteId"] = d.Get("site_id")
133+
134+
if v, ok := d.GetOk("config_id"); ok {
135+
request["ConfigId"] = v
136+
}
137+
138+
if v, ok := d.GetOk("config_type"); ok {
139+
request["ConfigType"] = v
140+
}
141+
142+
if v, ok := d.GetOk("rule_name"); ok {
143+
request["RuleName"] = v
144+
}
145+
146+
if v, ok := d.GetOkExists("site_version"); ok {
147+
request["SiteVersion"] = v
148+
}
149+
150+
var objects []map[string]interface{}
151+
152+
idsMap := make(map[string]string)
153+
if v, ok := d.GetOk("ids"); ok {
154+
for _, vv := range v.([]interface{}) {
155+
if vv == nil {
156+
continue
157+
}
158+
idsMap[vv.(string)] = vv.(string)
159+
}
160+
}
161+
162+
var networkOptimizationNameRegex *regexp.Regexp
163+
if v, ok := d.GetOk("name_regex"); ok {
164+
r, err := regexp.Compile(v.(string))
165+
if err != nil {
166+
return WrapError(err)
167+
}
168+
169+
networkOptimizationNameRegex = r
170+
}
171+
172+
var response map[string]interface{}
173+
var err error
174+
175+
for {
176+
wait := incrementalWait(3*time.Second, 5*time.Second)
177+
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
178+
response, err = client.RpcGet("ESA", "2024-09-10", action, request, nil)
179+
if err != nil {
180+
if IsExpectedErrors(err, []string{"Site.ServiceBusy", "TooManyRequests"}) || NeedRetry(err) {
181+
wait()
182+
return resource.RetryableError(err)
183+
}
184+
return resource.NonRetryableError(err)
185+
}
186+
return nil
187+
})
188+
addDebug(action, response, request)
189+
190+
if err != nil {
191+
return WrapErrorf(err, DataDefaultErrorMsg, "alicloud_esa_network_optimizations", action, AlibabaCloudSdkGoERROR)
192+
}
193+
194+
resp, err := jsonpath.Get("$.Configs", response)
195+
if err != nil {
196+
return WrapErrorf(err, FailedGetAttributeMsg, action, "$.Configs", response)
197+
}
198+
199+
result, _ := resp.([]interface{})
200+
for _, v := range result {
201+
item := v.(map[string]interface{})
202+
if len(idsMap) > 0 {
203+
if _, ok := idsMap[fmt.Sprintf("%v:%v", request["SiteId"], item["ConfigId"])]; !ok {
204+
continue
205+
}
206+
}
207+
208+
if networkOptimizationNameRegex != nil {
209+
if !networkOptimizationNameRegex.MatchString(fmt.Sprint(item["RuleName"])) {
210+
continue
211+
}
212+
}
213+
214+
objects = append(objects, item)
215+
}
216+
217+
if len(result) < PageSizeLarge {
218+
break
219+
}
220+
221+
request["PageNumber"] = request["PageNumber"].(int) + 1
222+
}
223+
224+
ids := make([]string, 0)
225+
names := make([]interface{}, 0)
226+
s := make([]map[string]interface{}, 0)
227+
for _, object := range objects {
228+
mapping := map[string]interface{}{
229+
"id": fmt.Sprintf("%v:%v", request["SiteId"], object["ConfigId"]),
230+
"config_id": fmt.Sprint(object["ConfigId"]),
231+
"config_type": object["ConfigType"],
232+
"site_version": object["SiteVersion"],
233+
"rule_enable": object["RuleEnable"],
234+
"rule_name": object["RuleName"],
235+
"rule": object["Rule"],
236+
"sequence": object["Sequence"],
237+
"smart_routing": object["SmartRouting"],
238+
"grpc": object["Grpc"],
239+
"http2_origin": object["Http2Origin"],
240+
"websocket": object["Websocket"],
241+
"upload_max_filesize": object["UploadMaxFilesize"],
242+
}
243+
244+
ids = append(ids, fmt.Sprint(mapping["id"]))
245+
names = append(names, object["RuleName"])
246+
s = append(s, mapping)
247+
}
248+
249+
d.SetId(dataResourceIdHash(ids))
250+
251+
if err := d.Set("ids", ids); err != nil {
252+
return WrapError(err)
253+
}
254+
255+
if err := d.Set("names", names); err != nil {
256+
return WrapError(err)
257+
}
258+
259+
if err := d.Set("optimizations", s); err != nil {
260+
return WrapError(err)
261+
}
262+
263+
if output, ok := d.GetOk("output_file"); ok && output.(string) != "" {
264+
writeToFile(output.(string), s)
265+
}
266+
267+
return nil
268+
}

0 commit comments

Comments
 (0)