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_falsealarmmasking_rule_v1.go
113 lines (95 loc) · 2.97 KB
/
resource_opentelekomcloud_waf_falsealarmmasking_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
package opentelekomcloud
import (
"fmt"
"log"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/huaweicloud/golangsdk/openstack/waf/v1/falsealarmmasking_rules"
)
func resourceWafFalseAlarmMaskingRuleV1() *schema.Resource {
return &schema.Resource{
Create: resourceWafFalseAlarmMaskingRuleV1Create,
Read: resourceWafFalseAlarmMaskingRuleV1Read,
Delete: resourceWafFalseAlarmMaskingRuleV1Delete,
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,
},
"url": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"rule": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceWafFalseAlarmMaskingRuleV1Create(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)
}
createOpts := falsealarmmasking_rules.CreateOpts{
Url: d.Get("url").(string),
Rule: d.Get("rule").(string),
}
policy_id := d.Get("policy_id").(string)
rule, err := falsealarmmasking_rules.Create(wafClient, policy_id, createOpts).Extract()
if err != nil {
return fmt.Errorf("Error creating OpenTelekomcomCloud WAF False Alarm Masking Rule: %s", err)
}
log.Printf("[DEBUG] Waf falsealarmmasking rule created: %#v", rule)
d.SetId(rule.Id)
return resourceWafFalseAlarmMaskingRuleV1Read(d, meta)
}
func resourceWafFalseAlarmMaskingRuleV1Read(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)
rules, err := falsealarmmasking_rules.List(wafClient, policy_id).Extract()
if err != nil {
return fmt.Errorf("Error retrieving OpenTelekomCloud Waf False Alarm Masking Rule: %s", err)
}
for _, r := range rules {
if r.Id == d.Id() {
d.SetId(r.Id)
d.Set("url", r.Url)
d.Set("rule", r.Rule)
d.Set("policy_id", r.PolicyID)
return nil
}
}
d.SetId("")
return nil
}
func resourceWafFalseAlarmMaskingRuleV1Delete(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 = falsealarmmasking_rules.Delete(wafClient, policy_id, d.Id()).ExtractErr()
if err != nil {
return fmt.Errorf("Error deleting OpenTelekomCloud WAF False Alarm Masking Rule: %s", err)
}
d.SetId("")
return nil
}