forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecondary_dns_zone.go
172 lines (142 loc) · 5.18 KB
/
secondary_dns_zone.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
package cloudflare
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/pkg/errors"
)
const (
errSecondaryDNSInvalidAutoRefreshValue = "secondary DNS auto refresh value is invalid"
errSecondaryDNSInvalidZoneName = "secondary DNS zone name is invalid"
errSecondaryDNSInvalidPrimaries = "secondary DNS primaries value is invalid"
)
// SecondaryDNSZone contains the high level structure of a secondary DNS zone.
type SecondaryDNSZone struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Primaries []string `json:"primaries,omitempty"`
AutoRefreshSeconds int `json:"auto_refresh_seconds,omitempty"`
SoaSerial int `json:"soa_serial,omitempty"`
CreatedTime time.Time `json:"created_time,omitempty"`
CheckedTime time.Time `json:"checked_time,omitempty"`
ModifiedTime time.Time `json:"modified_time,omitempty"`
}
// SecondaryDNSZoneDetailResponse is the API response for a single secondary
// DNS zone.
type SecondaryDNSZoneDetailResponse struct {
Response
Result SecondaryDNSZone `json:"result"`
}
// SecondaryDNSZoneAXFRResponse is the API response for a single secondary
// DNS AXFR response.
type SecondaryDNSZoneAXFRResponse struct {
Response
Result string `json:"result"`
}
// GetSecondaryDNSZone returns the secondary DNS zone configuration for a
// single zone.
//
// API reference: https://api.cloudflare.com/#secondary-dns-secondary-zone-configuration-details
func (api *API) GetSecondaryDNSZone(ctx context.Context, zoneID string) (SecondaryDNSZone, error) {
uri := fmt.Sprintf("/zones/%s/secondary_dns", zoneID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return SecondaryDNSZone{}, err
}
var r SecondaryDNSZoneDetailResponse
err = json.Unmarshal(res, &r)
if err != nil {
return SecondaryDNSZone{}, errors.Wrap(err, errUnmarshalError)
}
return r.Result, nil
}
// CreateSecondaryDNSZone creates a secondary DNS zone.
//
// API reference: https://api.cloudflare.com/#secondary-dns-create-secondary-zone-configuration
func (api *API) CreateSecondaryDNSZone(ctx context.Context, zoneID string, zone SecondaryDNSZone) (SecondaryDNSZone, error) {
if err := validateRequiredSecondaryDNSZoneValues(zone); err != nil {
return SecondaryDNSZone{}, err
}
uri := fmt.Sprintf("/zones/%s/secondary_dns", zoneID)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri,
SecondaryDNSZone{
Name: zone.Name,
AutoRefreshSeconds: zone.AutoRefreshSeconds,
Primaries: zone.Primaries,
},
)
if err != nil {
return SecondaryDNSZone{}, err
}
result := SecondaryDNSZoneDetailResponse{}
if err := json.Unmarshal(res, &result); err != nil {
return SecondaryDNSZone{}, errors.Wrap(err, errUnmarshalError)
}
return result.Result, nil
}
// UpdateSecondaryDNSZone updates an existing secondary DNS zone.
//
// API reference: https://api.cloudflare.com/#secondary-dns-update-secondary-zone-configuration
func (api *API) UpdateSecondaryDNSZone(ctx context.Context, zoneID string, zone SecondaryDNSZone) (SecondaryDNSZone, error) {
if err := validateRequiredSecondaryDNSZoneValues(zone); err != nil {
return SecondaryDNSZone{}, err
}
uri := fmt.Sprintf("/zones/%s/secondary_dns", zoneID)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri,
SecondaryDNSZone{
Name: zone.Name,
AutoRefreshSeconds: zone.AutoRefreshSeconds,
Primaries: zone.Primaries,
},
)
if err != nil {
return SecondaryDNSZone{}, err
}
result := SecondaryDNSZoneDetailResponse{}
if err := json.Unmarshal(res, &result); err != nil {
return SecondaryDNSZone{}, errors.Wrap(err, errUnmarshalError)
}
return result.Result, nil
}
// DeleteSecondaryDNSZone deletes a secondary DNS zone.
//
// API reference: https://api.cloudflare.com/#secondary-dns-delete-secondary-zone-configuration
func (api *API) DeleteSecondaryDNSZone(ctx context.Context, zoneID string) error {
uri := fmt.Sprintf("/zones/%s/secondary_dns", zoneID)
_, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
return nil
}
// ForceSecondaryDNSZoneAXFR requests an immediate AXFR request.
//
// API reference: https://api.cloudflare.com/#secondary-dns-update-secondary-zone-configuration
func (api *API) ForceSecondaryDNSZoneAXFR(ctx context.Context, zoneID string) error {
uri := fmt.Sprintf("/zones/%s/secondary_dns/force_axfr", zoneID)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, nil)
if err != nil {
return err
}
result := SecondaryDNSZoneAXFRResponse{}
if err := json.Unmarshal(res, &result); err != nil {
return errors.Wrap(err, errUnmarshalError)
}
return nil
}
// validateRequiredSecondaryDNSZoneValues ensures that the payload matches the
// API requirements for required fields.
func validateRequiredSecondaryDNSZoneValues(zone SecondaryDNSZone) error {
if zone.Name == "" {
return errors.New(errSecondaryDNSInvalidZoneName)
}
if zone.AutoRefreshSeconds == 0 || zone.AutoRefreshSeconds < 0 {
return errors.New(errSecondaryDNSInvalidAutoRefreshValue)
}
if len(zone.Primaries) == 0 {
return errors.New(errSecondaryDNSInvalidPrimaries)
}
return nil
}