forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail_routing_rules.go
273 lines (224 loc) · 8.63 KB
/
email_routing_rules.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package cloudflare
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
)
var ErrMissingRuleID = errors.New("required rule id missing")
type EmailRoutingRuleMatcher struct {
Type string `json:"type,omitempty"`
Field string `json:"field,omitempty"`
Value string `json:"value,omitempty"`
}
type EmailRoutingRuleAction struct {
Type string `json:"type,omitempty"`
Value []string `json:"value,omitempty"`
}
type EmailRoutingRule struct {
Tag string `json:"tag,omitempty"`
Name string `json:"name,omitempty"`
Priority int `json:"priority,omitempty"`
Enabled *bool `json:"enabled,omitempty"`
Matchers []EmailRoutingRuleMatcher `json:"matchers,omitempty"`
Actions []EmailRoutingRuleAction `json:"actions,omitempty"`
}
type ListEmailRoutingRulesParameters struct {
Enabled *bool `url:"enabled,omitempty"`
ResultInfo
}
type ListEmailRoutingRuleResponse struct {
Result []EmailRoutingRule `json:"result"`
ResultInfo `json:"result_info,omitempty"`
Response
}
type CreateEmailRoutingRuleParameters struct {
Matchers []EmailRoutingRuleMatcher `json:"matchers,omitempty"`
Actions []EmailRoutingRuleAction `json:"actions,omitempty"`
Name string `json:"name,omitempty"`
Enabled *bool `json:"enabled,omitempty"`
Priority int `json:"priority,omitempty"`
}
type CreateEmailRoutingRuleResponse struct {
Result EmailRoutingRule `json:"result"`
Response
}
type GetEmailRoutingRuleResponse struct {
Result EmailRoutingRule `json:"result"`
Response
}
type UpdateEmailRoutingRuleParameters struct {
Matchers []EmailRoutingRuleMatcher `json:"matchers,omitempty"`
Actions []EmailRoutingRuleAction `json:"actions,omitempty"`
Name string `json:"name,omitempty"`
Enabled *bool `json:"enabled,omitempty"`
Priority int `json:"priority,omitempty"`
RuleID string
}
type EmailRoutingCatchAllRule struct {
Tag string `json:"tag,omitempty"`
Name string `json:"name,omitempty"`
Enabled *bool `json:"enabled,omitempty"`
Matchers []EmailRoutingRuleMatcher `json:"matchers,omitempty"`
Actions []EmailRoutingRuleAction `json:"actions,omitempty"`
}
type EmailRoutingCatchAllRuleResponse struct {
Result EmailRoutingCatchAllRule `json:"result"`
Response
}
// ListEmailRoutingRules Lists existing routing rules.
//
// API reference: https://api.cloudflare.com/#email-routing-routing-rules-list-routing-rules
func (api *API) ListEmailRoutingRules(ctx context.Context, rc *ResourceContainer, params ListEmailRoutingRulesParameters) ([]EmailRoutingRule, *ResultInfo, error) {
if rc.Identifier == "" {
return []EmailRoutingRule{}, &ResultInfo{}, ErrMissingZoneID
}
autoPaginate := true
if params.PerPage >= 1 || params.Page >= 1 {
autoPaginate = false
}
if params.PerPage < 1 {
params.PerPage = 50
}
if params.Page < 1 {
params.Page = 1
}
var rules []EmailRoutingRule
var rResponse ListEmailRoutingRuleResponse
for {
rResponse = ListEmailRoutingRuleResponse{}
uri := buildURI(fmt.Sprintf("/zones/%s/email/routing/rules", rc.Identifier), params)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []EmailRoutingRule{}, &ResultInfo{}, err
}
err = json.Unmarshal(res, &rResponse)
if err != nil {
return []EmailRoutingRule{}, &ResultInfo{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
rules = append(rules, rResponse.Result...)
params.ResultInfo = rResponse.ResultInfo.Next()
if params.ResultInfo.Done() || !autoPaginate {
break
}
}
return rules, &rResponse.ResultInfo, nil
}
// CreateEmailRoutingRule Rules consist of a set of criteria for matching emails (such as an email being sent to a specific custom email address) plus a set of actions to take on the email (like forwarding it to a specific destination address).
//
// API reference: https://api.cloudflare.com/#email-routing-routing-rules-create-routing-rule
func (api *API) CreateEmailRoutingRule(ctx context.Context, rc *ResourceContainer, params CreateEmailRoutingRuleParameters) (EmailRoutingRule, error) {
if rc.Identifier == "" {
return EmailRoutingRule{}, ErrMissingZoneID
}
uri := fmt.Sprintf("/zones/%s/email/routing/rules", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, params)
if err != nil {
return EmailRoutingRule{}, err
}
var r CreateEmailRoutingRuleResponse
err = json.Unmarshal(res, &r)
if err != nil {
return EmailRoutingRule{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}
// GetEmailRoutingRule Get information for a specific routing rule already created.
//
// API reference: https://api.cloudflare.com/#email-routing-routing-rules-get-routing-rule
func (api *API) GetEmailRoutingRule(ctx context.Context, rc *ResourceContainer, ruleID string) (EmailRoutingRule, error) {
if rc.Identifier == "" {
return EmailRoutingRule{}, ErrMissingZoneID
}
uri := fmt.Sprintf("/zones/%s/email/routing/rules/%s", rc.Identifier, ruleID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return EmailRoutingRule{}, err
}
var r GetEmailRoutingRuleResponse
err = json.Unmarshal(res, &r)
if err != nil {
return EmailRoutingRule{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}
// UpdateEmailRoutingRule Update actions, matches, or enable/disable specific routing rules
//
// API reference: https://api.cloudflare.com/#email-routing-routing-rules-update-routing-rule
func (api *API) UpdateEmailRoutingRule(ctx context.Context, rc *ResourceContainer, params UpdateEmailRoutingRuleParameters) (EmailRoutingRule, error) {
if rc.Identifier == "" {
return EmailRoutingRule{}, ErrMissingZoneID
}
if params.RuleID == "" {
return EmailRoutingRule{}, ErrMissingRuleID
}
uri := fmt.Sprintf("/zones/%s/email/routing/rules/%s", rc.Identifier, params.RuleID)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, params)
if err != nil {
return EmailRoutingRule{}, err
}
var r GetEmailRoutingRuleResponse
err = json.Unmarshal(res, &r)
if err != nil {
return EmailRoutingRule{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}
// DeleteEmailRoutingRule Delete a specific routing rule.
//
// API reference: https://api.cloudflare.com/#email-routing-routing-rules-delete-routing-rule
func (api *API) DeleteEmailRoutingRule(ctx context.Context, rc *ResourceContainer, ruleID string) (EmailRoutingRule, error) {
if rc.Identifier == "" {
return EmailRoutingRule{}, ErrMissingZoneID
}
uri := fmt.Sprintf("/zones/%s/email/routing/rules/%s", rc.Identifier, ruleID)
res, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return EmailRoutingRule{}, err
}
var r GetEmailRoutingRuleResponse
err = json.Unmarshal(res, &r)
if err != nil {
return EmailRoutingRule{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}
// GetEmailRoutingCatchAllRule Get information on the default catch-all routing rule.
//
// API reference: https://api.cloudflare.com/#email-routing-routing-rules-get-catch-all-rule
func (api *API) GetEmailRoutingCatchAllRule(ctx context.Context, rc *ResourceContainer) (EmailRoutingCatchAllRule, error) {
if rc.Identifier == "" {
return EmailRoutingCatchAllRule{}, ErrMissingZoneID
}
uri := fmt.Sprintf("/zones/%s/email/routing/rules/catch_all", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return EmailRoutingCatchAllRule{}, err
}
var r EmailRoutingCatchAllRuleResponse
err = json.Unmarshal(res, &r)
if err != nil {
return EmailRoutingCatchAllRule{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}
// UpdateEmailRoutingCatchAllRule Enable or disable catch-all routing rule, or change action to forward to specific destination address.
//
// API reference: https://api.cloudflare.com/#email-routing-routing-rules-update-catch-all-rule
func (api *API) UpdateEmailRoutingCatchAllRule(ctx context.Context, rc *ResourceContainer, params EmailRoutingCatchAllRule) (EmailRoutingCatchAllRule, error) {
if rc.Identifier == "" {
return EmailRoutingCatchAllRule{}, ErrMissingZoneID
}
uri := fmt.Sprintf("/zones/%s/email/routing/rules/catch_all", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, params)
if err != nil {
return EmailRoutingCatchAllRule{}, err
}
var r EmailRoutingCatchAllRuleResponse
err = json.Unmarshal(res, &r)
if err != nil {
return EmailRoutingCatchAllRule{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}