forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
email_routing_settings.go
117 lines (101 loc) · 3.68 KB
/
email_routing_settings.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
package cloudflare
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
)
type EmailRoutingSettings struct {
Tag string `json:"tag,omitempty"`
Name string `json:"name,omitempty"`
Enabled bool `json:"enabled,omitempty"`
Created *time.Time `json:"created,omitempty"`
Modified *time.Time `json:"modified,omitempty"`
SkipWizard *bool `json:"skip_wizard,omitempty"`
Status string `json:"status,omitempty"`
}
type EmailRoutingSettingsResponse struct {
Result EmailRoutingSettings `json:"result,omitempty"`
Response
}
type EmailRoutingDNSSettingsResponse struct {
Result []DNSRecord `json:"result,omitempty"`
Response
}
// GetEmailRoutingSettings Get information about the settings for your Email Routing zone.
//
// API reference: https://api.cloudflare.com/#email-routing-settings-get-email-routing-settings
func (api *API) GetEmailRoutingSettings(ctx context.Context, rc *ResourceContainer) (EmailRoutingSettings, error) {
if rc.Identifier == "" {
return EmailRoutingSettings{}, ErrMissingZoneID
}
uri := fmt.Sprintf("/zones/%s/email/routing", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return EmailRoutingSettings{}, err
}
var r EmailRoutingSettingsResponse
err = json.Unmarshal(res, &r)
if err != nil {
return EmailRoutingSettings{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}
// EnableEmailRouting Enable you Email Routing zone. Add and lock the necessary MX and SPF records.
//
// API reference: https://api.cloudflare.com/#email-routing-settings-enable-email-routing
func (api *API) EnableEmailRouting(ctx context.Context, rc *ResourceContainer) (EmailRoutingSettings, error) {
if rc.Identifier == "" {
return EmailRoutingSettings{}, ErrMissingZoneID
}
uri := fmt.Sprintf("/zones/%s/email/routing/enable", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, nil)
if err != nil {
return EmailRoutingSettings{}, err
}
var r EmailRoutingSettingsResponse
err = json.Unmarshal(res, &r)
if err != nil {
return EmailRoutingSettings{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}
// DisableEmailRouting Disable your Email Routing zone. Also removes additional MX records previously required for Email Routing to work.
//
// API reference: https://api.cloudflare.com/#email-routing-settings-disable-email-routing
func (api *API) DisableEmailRouting(ctx context.Context, rc *ResourceContainer) (EmailRoutingSettings, error) {
if rc.Identifier == "" {
return EmailRoutingSettings{}, ErrMissingZoneID
}
uri := fmt.Sprintf("/zones/%s/email/routing/disable", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, nil)
if err != nil {
return EmailRoutingSettings{}, err
}
var r EmailRoutingSettingsResponse
err = json.Unmarshal(res, &r)
if err != nil {
return EmailRoutingSettings{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}
// GetEmailRoutingDNSSettings Show the DNS records needed to configure your Email Routing zone.
//
// API reference: https://api.cloudflare.com/#email-routing-settings-email-routing---dns-settings
func (api *API) GetEmailRoutingDNSSettings(ctx context.Context, rc *ResourceContainer) ([]DNSRecord, error) {
if rc.Identifier == "" {
return []DNSRecord{}, ErrMissingZoneID
}
uri := fmt.Sprintf("/zones/%s/email/routing/dns", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []DNSRecord{}, err
}
var r EmailRoutingDNSSettingsResponse
err = json.Unmarshal(res, &r)
if err != nil {
return []DNSRecord{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}