This repository has been archived by the owner on Aug 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_opentelekomcloud_waf_preciseprotection_rule_v1.go
236 lines (208 loc) · 6.14 KB
/
resource_opentelekomcloud_waf_preciseprotection_rule_v1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package opentelekomcloud
import (
"fmt"
"log"
"strconv"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/openstack/waf/v1/preciseprotection_rules"
)
func resourceWafPreciseProtectionRuleV1() *schema.Resource {
return &schema.Resource{
Create: resourceWafPreciseProtectionRuleV1Create,
Read: resourceWafPreciseProtectionRuleV1Read,
Delete: resourceWafPreciseProtectionRuleV1Delete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
Schema: map[string]*schema.Schema{
"policy_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"time": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},
"start": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"end": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"conditions": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"category": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"index": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"logic": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
"contents": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
"action_category": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"priority": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
},
}
}
func getConditions(d *schema.ResourceData) []preciseprotection_rules.Condition {
var conditionOpts []preciseprotection_rules.Condition
conditions := d.Get("conditions").([]interface{})
for _, v := range conditions {
cond := v.(map[string]interface{})
contents_raw := cond["contents"].([]interface{})
contents := make([]string, len(contents_raw))
for i, v := range contents_raw {
contents[i] = v.(string)
}
condition := preciseprotection_rules.Condition{
Category: cond["category"].(string),
Index: cond["index"].(string),
Logic: cond["logic"].(int),
Contents: contents,
}
conditionOpts = append(conditionOpts, condition)
}
log.Printf("[DEBUG] getConditions: %#v", conditionOpts)
return conditionOpts
}
func getPreciseAction(d *schema.ResourceData) preciseprotection_rules.Action {
action := preciseprotection_rules.Action{
Category: d.Get("action_category").(string),
}
log.Printf("[DEBUG] getPreciseAction: %#v", action)
return action
}
func resourceWafPreciseProtectionRuleV1Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
wafClient, err := config.wafV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenTelekomcomCloud WAF Client: %s", err)
}
priority := d.Get("priority").(int)
createOpts := preciseprotection_rules.CreateOpts{
Name: d.Get("name").(string),
Time: d.Get("time").(bool),
Conditions: getConditions(d),
Action: getPreciseAction(d),
Priority: &priority,
}
if _, ok := d.GetOk("start"); ok {
start, err := strconv.ParseInt(d.Get("start").(string), 10, 64)
if err != nil {
return fmt.Errorf("Error converting start: %s", err)
}
createOpts.Start = start
}
if _, ok := d.GetOk("cache_control"); ok {
end, err := strconv.ParseInt(d.Get("end").(string), 10, 64)
if err != nil {
return fmt.Errorf("Error converting end: %s", err)
}
createOpts.End = end
}
policy_id := d.Get("policy_id").(string)
rule, err := preciseprotection_rules.Create(wafClient, policy_id, createOpts).Extract()
if err != nil {
return fmt.Errorf("Error creating OpenTelekomcomCloud WAF Precise Protection Rule: %s", err)
}
log.Printf("[DEBUG] Waf precise protection rule created: %#v", rule)
d.SetId(rule.Id)
return resourceWafPreciseProtectionRuleV1Read(d, meta)
}
func resourceWafPreciseProtectionRuleV1Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
wafClient, err := config.wafV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenTelekomCloud WAF client: %s", err)
}
policy_id := d.Get("policy_id").(string)
n, err := preciseprotection_rules.Get(wafClient, policy_id, d.Id()).Extract()
if err != nil {
if _, ok := err.(golangsdk.ErrDefault404); ok {
d.SetId("")
return nil
}
return fmt.Errorf("Error retrieving OpenTelekomCloud Waf Precise Protection Rule: %s", err)
}
d.SetId(n.Id)
d.Set("policy_id", n.PolicyID)
d.Set("name", n.Name)
d.Set("time", n.Time)
d.Set("start", strconv.FormatInt(n.Start, 10))
d.Set("end", strconv.FormatInt(n.End, 10))
conditions := make([]map[string]interface{}, len(n.Conditions))
for i, condition := range n.Conditions {
conditions[i] = make(map[string]interface{})
conditions[i]["category"] = condition.Category
conditions[i]["index"] = condition.Index
conditions[i]["logic"] = condition.Logic
conditions[i]["contents"] = condition.Contents
}
d.Set("conditions", conditions)
d.Set("action_category", n.Action.Category)
d.Set("priority", n.Priority)
return nil
}
func resourceWafPreciseProtectionRuleV1Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
wafClient, err := config.wafV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenTelekomCloud WAF client: %s", err)
}
policy_id := d.Get("policy_id").(string)
err = preciseprotection_rules.Delete(wafClient, policy_id, d.Id()).ExtractErr()
if err != nil {
return fmt.Errorf("Error deleting OpenTelekomCloud WAF Precise Protection Rule: %s", err)
}
d.SetId("")
return nil
}