-
Notifications
You must be signed in to change notification settings - Fork 2
/
rate_rules_client.go
158 lines (140 loc) · 4.37 KB
/
rate_rules_client.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
// Copyright 2022 Edgecast Inc., Licensed under the terms of the Apache 2.0
// license. See LICENSE file in project root for terms.
package rate
/*
This file contains operations and types specific to WAF Rate Rules.
Rate Rules restricts the flow of site traffic with the intention of:
- Diverting malicious or inadvertent DDoS traffic.
- Preventing a customer origin server from being overloaded.
- Requests that exceed the rate limit may be dropped, redirected to another
URL, or sent a custom response.
For detailed information about Rate Rules in WAF, please refer to:
https://docs.edgecast.com/cdn/#Web-Security/Rate-Rules.htm
*/
import (
"fmt"
"github.com/EdgeCast/ec-sdk-go/edgecast/internal/ecclient"
)
// New creates a new instance of the Rate Rule Client Service
func New(c ecclient.APIClient, baseAPIURL string) ClientService {
return Client{c, baseAPIURL}
}
// Client is the Rate Rules client.
type Client struct {
client ecclient.APIClient
baseAPIURL string
}
// ClientService is the interface for Client methods.
type ClientService interface {
AddRateRule(
params AddRateRuleParams,
) (string, error)
GetRateRule(
params GetRateRuleParams,
) (*RateRuleGetOK, error)
UpdateRateRule(
params UpdateRateRuleParams,
) error
DeleteRateRule(
params DeleteRateRuleParams,
) error
GetAllRateRules(
params GetAllRateRulesParams,
) (*[]RateRuleGetAllOK, error)
}
// AddRateRule creates a rate rule for the provided account number
// and returns the new rule's system-generated ID
func (c Client) AddRateRule(
params AddRateRuleParams,
) (string, error) {
parsedResponse := &RateRuleAddOK{}
_, err := c.client.SubmitRequest(ecclient.SubmitRequestParams{
Method: ecclient.Post,
Path: "/v2/mcc/customers/{account_number}/waf/v1.0/limit",
RawBody: params.RateRule,
PathParams: map[string]string{
"account_number": params.AccountNumber,
},
ParsedResponse: parsedResponse,
})
if err != nil {
return "", fmt.Errorf("error creating rate rule: %w", err)
}
return parsedResponse.ID, nil
}
// GetRateRule retrieves a rate rule for the provided account number and
// Rate Rule ID
func (c Client) GetRateRule(
params GetRateRuleParams,
) (*RateRuleGetOK, error) {
parsedResponse := &RateRuleGetOK{}
_, err := c.client.SubmitRequest(ecclient.SubmitRequestParams{
Method: ecclient.Get,
Path: "/v2/mcc/customers/{account_number}/waf/v1.0/limit/{rule_id}",
PathParams: map[string]string{
"account_number": params.AccountNumber,
"rule_id": params.RateRuleID,
},
ParsedResponse: parsedResponse,
})
if err != nil {
return nil, fmt.Errorf("error getting rate rule: %w", err)
}
return parsedResponse, nil
}
// UpdateRateRule updates a rate rule for the provided account number using the
// provided Rate Rule ID and Rate Rule properties.
func (c Client) UpdateRateRule(
params UpdateRateRuleParams,
) error {
_, err := c.client.SubmitRequest(ecclient.SubmitRequestParams{
Method: ecclient.Put,
Path: "/v2/mcc/customers/{account_number}/waf/v1.0/limit/{rule_id}",
RawBody: params.RateRule,
PathParams: map[string]string{
"account_number": params.AccountNumber,
"rule_id": params.RateRuleID,
},
})
if err != nil {
return fmt.Errorf("error updating rate rule: %w", err)
}
return nil
}
// DeleteRateRuleByID deletes a rate rule for the provided account numnber and
// Rate Rule ID
func (c Client) DeleteRateRule(
params DeleteRateRuleParams,
) error {
_, err := c.client.SubmitRequest(ecclient.SubmitRequestParams{
Method: ecclient.Delete,
Path: "/v2/mcc/customers/{account_number}/waf/v1.0/limit/{rule_id}",
PathParams: map[string]string{
"account_number": params.AccountNumber,
"rule_id": params.RateRuleID,
},
})
if err != nil {
return fmt.Errorf("error deleting rate rule: %w", err)
}
return nil
}
// GetAllRateRules retrives all of the Rate Rules for the provided account
// number.
func (c Client) GetAllRateRules(
params GetAllRateRulesParams,
) (*[]RateRuleGetAllOK, error) {
parsedResponse := &[]RateRuleGetAllOK{}
_, err := c.client.SubmitRequest(ecclient.SubmitRequestParams{
Method: ecclient.Get,
Path: "/v2/mcc/customers/{account_number}/waf/v1.0/limit",
PathParams: map[string]string{
"account_number": params.AccountNumber,
},
ParsedResponse: parsedResponse,
})
if err != nil {
return nil, fmt.Errorf("error getting all rate rules: %w", err)
}
return parsedResponse, nil
}