forked from cloudflare/cloudflare-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registrar.go
175 lines (152 loc) · 6.02 KB
/
registrar.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
package cloudflare
import (
"encoding/json"
"fmt"
"time"
"github.com/pkg/errors"
)
// RegistrarDomain is the structure of the API response for a new
// Cloudflare Registrar domain.
type RegistrarDomain struct {
ID string `json:"id"`
Available bool `json:"available"`
SupportedTLD bool `json:"supported_tld"`
CanRegister bool `json:"can_register"`
TransferIn RegistrarTransferIn `json:"transfer_in"`
CurrentRegistrar string `json:"current_registrar"`
ExpiresAt time.Time `json:"expires_at"`
RegistryStatuses string `json:"registry_statuses"`
Locked bool `json:"locked"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
RegistrantContact RegistrantContact `json:"registrant_contact"`
}
// RegistrarTransferIn contains the structure for a domain transfer in
// request.
type RegistrarTransferIn struct {
UnlockDomain string `json:"unlock_domain"`
DisablePrivacy string `json:"disable_privacy"`
EnterAuthCode string `json:"enter_auth_code"`
ApproveTransfer string `json:"approve_transfer"`
AcceptFoa string `json:"accept_foa"`
CanCancelTransfer bool `json:"can_cancel_transfer"`
}
// RegistrantContact is the contact details for the domain registration.
type RegistrantContact struct {
ID string `json:"id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Organization string `json:"organization"`
Address string `json:"address"`
Address2 string `json:"address2"`
City string `json:"city"`
State string `json:"state"`
Zip string `json:"zip"`
Country string `json:"country"`
Phone string `json:"phone"`
Email string `json:"email"`
Fax string `json:"fax"`
}
// RegistrarDomainConfiguration is the structure for making updates to
// and existing domain.
type RegistrarDomainConfiguration struct {
NameServers []string `json:"name_servers"`
Privacy bool `json:"privacy"`
Locked bool `json:"locked"`
AutoRenew bool `json:"auto_renew"`
}
// RegistrarDomainDetailResponse is the structure of the detailed
// response from the API for a single domain.
type RegistrarDomainDetailResponse struct {
Response
Result RegistrarDomain `json:"result"`
}
// RegistrarDomainsDetailResponse is the structure of the detailed
// response from the API.
type RegistrarDomainsDetailResponse struct {
Response
Result []RegistrarDomain `json:"result"`
}
// RegistrarDomain returns a single domain based on the account ID and
// domain name.
//
// API reference: https://api.cloudflare.com/#registrar-domains-get-domain
func (api *API) RegistrarDomain(accountID, domainName string) (RegistrarDomain, error) {
uri := fmt.Sprintf("/accounts/%s/registrar/domains/%s", accountID, domainName)
res, err := api.makeRequest("GET", uri, nil)
if err != nil {
return RegistrarDomain{}, errors.Wrap(err, errMakeRequestError)
}
var r RegistrarDomainDetailResponse
err = json.Unmarshal(res, &r)
if err != nil {
return RegistrarDomain{}, errors.Wrap(err, errUnmarshalError)
}
return r.Result, nil
}
// RegistrarDomains returns all registrar domains based on the account
// ID.
//
// API reference: https://api.cloudflare.com/#registrar-domains-list-domains
func (api *API) RegistrarDomains(accountID string) ([]RegistrarDomain, error) {
uri := "/accounts/" + accountID + "/registrar/domains"
res, err := api.makeRequest("POST", uri, nil)
if err != nil {
return []RegistrarDomain{}, errors.Wrap(err, errMakeRequestError)
}
var r RegistrarDomainsDetailResponse
err = json.Unmarshal(res, &r)
if err != nil {
return []RegistrarDomain{}, errors.Wrap(err, errUnmarshalError)
}
return r.Result, nil
}
// TransferRegistrarDomain initiates the transfer from another registrar
// to Cloudflare Registrar.
//
// API reference: https://api.cloudflare.com/#registrar-domains-transfer-domain
func (api *API) TransferRegistrarDomain(accountID, domainName string) ([]RegistrarDomain, error) {
uri := fmt.Sprintf("/accounts/%s/registrar/domains/%s/transfer", accountID, domainName)
res, err := api.makeRequest("POST", uri, nil)
if err != nil {
return []RegistrarDomain{}, errors.Wrap(err, errMakeRequestError)
}
var r RegistrarDomainsDetailResponse
err = json.Unmarshal(res, &r)
if err != nil {
return []RegistrarDomain{}, errors.Wrap(err, errUnmarshalError)
}
return r.Result, nil
}
// CancelRegistrarDomainTransfer cancels a pending domain transfer.
//
// API reference: https://api.cloudflare.com/#registrar-domains-cancel-transfer
func (api *API) CancelRegistrarDomainTransfer(accountID, domainName string) ([]RegistrarDomain, error) {
uri := fmt.Sprintf("/accounts/%s/registrar/domains/%s/cancel_transfer", accountID, domainName)
res, err := api.makeRequest("POST", uri, nil)
if err != nil {
return []RegistrarDomain{}, errors.Wrap(err, errMakeRequestError)
}
var r RegistrarDomainsDetailResponse
err = json.Unmarshal(res, &r)
if err != nil {
return []RegistrarDomain{}, errors.Wrap(err, errUnmarshalError)
}
return r.Result, nil
}
// UpdateRegistrarDomain updates an existing Registrar Domain configuration.
//
// API reference: https://api.cloudflare.com/#registrar-domains-update-domain
func (api *API) UpdateRegistrarDomain(accountID, domainName string, domainConfiguration RegistrarDomainConfiguration) (RegistrarDomain, error) {
uri := fmt.Sprintf("/accounts/%s/registrar/domains/%s", accountID, domainName)
res, err := api.makeRequest("PUT", uri, domainConfiguration)
if err != nil {
return RegistrarDomain{}, errors.Wrap(err, errMakeRequestError)
}
var r RegistrarDomainDetailResponse
err = json.Unmarshal(res, &r)
if err != nil {
return RegistrarDomain{}, errors.Wrap(err, errUnmarshalError)
}
return r.Result, nil
}