forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecondary_dns_tsig.go
132 lines (108 loc) · 3.9 KB
/
secondary_dns_tsig.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
package cloudflare
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/pkg/errors"
)
const (
errSecondaryDNSTSIGMissingID = "secondary DNS TSIG ID is required"
)
// SecondaryDNSTSIG contains the structure for a secondary DNS TSIG.
type SecondaryDNSTSIG struct {
ID string `json:"id,omitempty"`
Name string `json:"name"`
Secret string `json:"secret"`
Algo string `json:"algo"`
}
// SecondaryDNSTSIGDetailResponse is the API response for a single secondary
// DNS TSIG.
type SecondaryDNSTSIGDetailResponse struct {
Response
Result SecondaryDNSTSIG `json:"result"`
}
// SecondaryDNSTSIGListResponse is the API response for all secondary DNS TSIGs.
type SecondaryDNSTSIGListResponse struct {
Response
Result []SecondaryDNSTSIG `json:"result"`
}
// GetSecondaryDNSTSIG gets a single account level TSIG for a secondary DNS
// configuration.
//
// API reference: https://api.cloudflare.com/#secondary-dns-tsig--tsig-details
func (api *API) GetSecondaryDNSTSIG(ctx context.Context, accountID, tsigID string) (SecondaryDNSTSIG, error) {
uri := fmt.Sprintf("/accounts/%s/secondary_dns/tsigs/%s", accountID, tsigID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return SecondaryDNSTSIG{}, err
}
var r SecondaryDNSTSIGDetailResponse
err = json.Unmarshal(res, &r)
if err != nil {
return SecondaryDNSTSIG{}, errors.Wrap(err, errUnmarshalError)
}
return r.Result, nil
}
// ListSecondaryDNSTSIGs gets all account level TSIG for a secondary DNS
// configuration.
//
// API reference: https://api.cloudflare.com/#secondary-dns-tsig--list-tsigs
func (api *API) ListSecondaryDNSTSIGs(ctx context.Context, accountID string) ([]SecondaryDNSTSIG, error) {
uri := fmt.Sprintf("/accounts/%s/secondary_dns/tsigs", accountID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []SecondaryDNSTSIG{}, err
}
var r SecondaryDNSTSIGListResponse
err = json.Unmarshal(res, &r)
if err != nil {
return []SecondaryDNSTSIG{}, errors.Wrap(err, errUnmarshalError)
}
return r.Result, nil
}
// CreateSecondaryDNSTSIG creates a secondary DNS TSIG at the account level.
//
// API reference: https://api.cloudflare.com/#secondary-dns-tsig--create-tsig
func (api *API) CreateSecondaryDNSTSIG(ctx context.Context, accountID string, tsig SecondaryDNSTSIG) (SecondaryDNSTSIG, error) {
uri := fmt.Sprintf("/accounts/%s/secondary_dns/tsigs", accountID)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, tsig)
if err != nil {
return SecondaryDNSTSIG{}, err
}
result := SecondaryDNSTSIGDetailResponse{}
if err := json.Unmarshal(res, &result); err != nil {
return SecondaryDNSTSIG{}, errors.Wrap(err, errUnmarshalError)
}
return result.Result, nil
}
// UpdateSecondaryDNSTSIG updates an existing secondary DNS TSIG at
// the account level.
//
// API reference: https://api.cloudflare.com/#secondary-dns-tsig--update-tsig
func (api *API) UpdateSecondaryDNSTSIG(ctx context.Context, accountID string, tsig SecondaryDNSTSIG) (SecondaryDNSTSIG, error) {
if tsig.ID == "" {
return SecondaryDNSTSIG{}, errors.New(errSecondaryDNSTSIGMissingID)
}
uri := fmt.Sprintf("/accounts/%s/secondary_dns/tsigs/%s", accountID, tsig.ID)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, tsig)
if err != nil {
return SecondaryDNSTSIG{}, err
}
result := SecondaryDNSTSIGDetailResponse{}
if err := json.Unmarshal(res, &result); err != nil {
return SecondaryDNSTSIG{}, errors.Wrap(err, errUnmarshalError)
}
return result.Result, nil
}
// DeleteSecondaryDNSTSIG deletes a secondary DNS TSIG.
//
// API reference: https://api.cloudflare.com/#secondary-dns-tsig--delete-tsig
func (api *API) DeleteSecondaryDNSTSIG(ctx context.Context, accountID, tsigID string) error {
uri := fmt.Sprintf("/accounts/%s/secondary_dns/tsigs/%s", accountID, tsigID)
_, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
return nil
}