-
Notifications
You must be signed in to change notification settings - Fork 134
/
resource_tc_waf_module_status.go
202 lines (165 loc) · 5.85 KB
/
resource_tc_waf_module_status.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
package waf
import (
"context"
"fmt"
"log"
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
waf "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/waf/v20180125"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func ResourceTencentCloudWafModuleStatus() *schema.Resource {
return &schema.Resource{
Create: resourceTencentCloudWafModuleStatusCreate,
Read: resourceTencentCloudWafModuleStatusRead,
Update: resourceTencentCloudWafModuleStatusUpdate,
Delete: resourceTencentCloudWafModuleStatusDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"domain": {
Required: true,
Type: schema.TypeString,
Description: "Domain.",
},
"web_security": {
Required: true,
Type: schema.TypeInt,
ValidateFunc: tccommon.ValidateAllowedIntValue([]int{0, 1}),
Description: "WEB security module status, 0:closed, 1:opened.",
},
"access_control": {
Required: true,
Type: schema.TypeInt,
ValidateFunc: tccommon.ValidateAllowedIntValue([]int{0, 1}),
Description: "ACL module status, 0:closed, 1:opened.",
},
"cc_protection": {
Required: true,
Type: schema.TypeInt,
ValidateFunc: tccommon.ValidateAllowedIntValue([]int{0, 1}),
Description: "CC module status, 0:closed, 1:opened.",
},
"api_protection": {
Required: true,
Type: schema.TypeInt,
ValidateFunc: tccommon.ValidateAllowedIntValue([]int{0, 1}),
Description: "API security module status, 0:closed, 1:opened.",
},
"anti_tamper": {
Optional: true,
Type: schema.TypeInt,
ValidateFunc: tccommon.ValidateAllowedIntValue([]int{0, 1}),
Description: "Anti tamper module status, 0:closed, 1:opened.",
},
"anti_leakage": {
Optional: true,
Type: schema.TypeInt,
ValidateFunc: tccommon.ValidateAllowedIntValue([]int{0, 1}),
Description: "Anti leakage module status, 0:closed, 1:opened.",
},
},
}
}
func resourceTencentCloudWafModuleStatusCreate(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_waf_module_status.create")()
defer tccommon.InconsistentCheck(d, meta)()
var domain string
if v, ok := d.GetOk("domain"); ok {
domain = v.(string)
}
d.SetId(domain)
return resourceTencentCloudWafModuleStatusUpdate(d, meta)
}
func resourceTencentCloudWafModuleStatusRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_waf_module_status.read")()
defer tccommon.InconsistentCheck(d, meta)()
var (
logId = tccommon.GetLogId(tccommon.ContextNil)
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
service = WafService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
domain = d.Id()
)
moduleStatus, err := service.DescribeWafModuleStatusById(ctx, domain)
if err != nil {
return err
}
if moduleStatus == nil {
d.SetId("")
log.Printf("[WARN]%s resource `WafModuleStatus` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
return nil
}
_ = d.Set("domain", domain)
if moduleStatus.WebSecurity != nil {
_ = d.Set("web_security", moduleStatus.WebSecurity)
}
if moduleStatus.AccessControl != nil {
_ = d.Set("access_control", moduleStatus.AccessControl)
}
if moduleStatus.CcProtection != nil {
_ = d.Set("cc_protection", moduleStatus.CcProtection)
}
if moduleStatus.ApiProtection != nil {
_ = d.Set("api_protection", moduleStatus.ApiProtection)
}
if moduleStatus.AntiTamper != nil {
_ = d.Set("anti_tamper", moduleStatus.AntiTamper)
}
if moduleStatus.AntiLeakage != nil {
_ = d.Set("anti_leakage", moduleStatus.AntiLeakage)
}
return nil
}
func resourceTencentCloudWafModuleStatusUpdate(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_waf_module_status.update")()
defer tccommon.InconsistentCheck(d, meta)()
var (
logId = tccommon.GetLogId(tccommon.ContextNil)
request = waf.NewModifyModuleStatusRequest()
domain = d.Id()
)
request.Domain = &domain
if v, ok := d.GetOkExists("web_security"); ok {
request.WebSecurity = helper.IntUint64(v.(int))
}
if v, ok := d.GetOkExists("access_control"); ok {
request.AccessControl = helper.IntUint64(v.(int))
}
if v, ok := d.GetOkExists("cc_protection"); ok {
request.CcProtection = helper.IntUint64(v.(int))
}
if v, ok := d.GetOkExists("api_protection"); ok {
request.ApiProtection = helper.IntUint64(v.(int))
}
if v, ok := d.GetOkExists("anti_tamper"); ok {
request.AntiTamper = helper.IntUint64(v.(int))
}
if v, ok := d.GetOkExists("anti_leakage"); ok {
request.AntiLeakage = helper.IntUint64(v.(int))
}
err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseWafClient().ModifyModuleStatus(request)
if e != nil {
return tccommon.RetryError(e)
} else {
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
}
if result == nil {
e = fmt.Errorf("waf moduleStatus version not exists")
return resource.NonRetryableError(e)
}
return nil
})
if err != nil {
log.Printf("[CRITAL]%s modify waf moduleStatus failed, reason:%+v", logId, err)
return err
}
return resourceTencentCloudWafModuleStatusRead(d, meta)
}
func resourceTencentCloudWafModuleStatusDelete(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_waf_module_status.delete")()
defer tccommon.InconsistentCheck(d, meta)()
return nil
}