/
domains.go
131 lines (109 loc) · 3.81 KB
/
domains.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
package dnsimple
import (
"context"
"fmt"
)
// DomainsService handles communication with the domain related
// methods of the DNSimple API.
//
// See https://developer.dnsimple.com/v2/domains/
type DomainsService struct {
client *Client
}
// Domain represents a domain in DNSimple.
type Domain struct {
ID int64 `json:"id,omitempty"`
AccountID int64 `json:"account_id,omitempty"`
RegistrantID int64 `json:"registrant_id,omitempty"`
Name string `json:"name,omitempty"`
UnicodeName string `json:"unicode_name,omitempty"`
Token string `json:"token,omitempty"`
State string `json:"state,omitempty"`
AutoRenew bool `json:"auto_renew,omitempty"`
PrivateWhois bool `json:"private_whois,omitempty"`
ExpiresAt string `json:"expires_at,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
}
func domainPath(accountID string, domainIdentifier string) (path string) {
path = fmt.Sprintf("/%v/domains", accountID)
if domainIdentifier != "" {
path += fmt.Sprintf("/%v", domainIdentifier)
}
return
}
// DomainResponse represents a response from an API method that returns a Domain struct.
type DomainResponse struct {
Response
Data *Domain `json:"data"`
}
// DomainsResponse represents a response from an API method that returns a collection of Domain struct.
type DomainsResponse struct {
Response
Data []Domain `json:"data"`
}
// DomainListOptions specifies the optional parameters you can provide
// to customize the DomainsService.ListDomains method.
type DomainListOptions struct {
// Select domains where the name contains given string.
NameLike *string `url:"name_like,omitempty"`
// Select domains where the registrant matches given ID.
RegistrantID *int `url:"registrant_id,omitempty"`
ListOptions
}
// ListDomains lists the domains for an account.
//
// See https://developer.dnsimple.com/v2/domains/#list
func (s *DomainsService) ListDomains(ctx context.Context, accountID string, options *DomainListOptions) (*DomainsResponse, error) {
path := versioned(domainPath(accountID, ""))
domainsResponse := &DomainsResponse{}
path, err := addURLQueryOptions(path, options)
if err != nil {
return nil, err
}
resp, err := s.client.get(ctx, path, domainsResponse)
if err != nil {
return nil, err
}
domainsResponse.HTTPResponse = resp
return domainsResponse, nil
}
// CreateDomain creates a new domain in the account.
//
// See https://developer.dnsimple.com/v2/domains/#create
func (s *DomainsService) CreateDomain(ctx context.Context, accountID string, domainAttributes Domain) (*DomainResponse, error) {
path := versioned(domainPath(accountID, ""))
domainResponse := &DomainResponse{}
resp, err := s.client.post(ctx, path, domainAttributes, domainResponse)
if err != nil {
return nil, err
}
domainResponse.HTTPResponse = resp
return domainResponse, nil
}
// GetDomain fetches a domain.
//
// See https://developer.dnsimple.com/v2/domains/#get
func (s *DomainsService) GetDomain(ctx context.Context, accountID string, domainIdentifier string) (*DomainResponse, error) {
path := versioned(domainPath(accountID, domainIdentifier))
domainResponse := &DomainResponse{}
resp, err := s.client.get(ctx, path, domainResponse)
if err != nil {
return nil, err
}
domainResponse.HTTPResponse = resp
return domainResponse, nil
}
// DeleteDomain PERMANENTLY deletes a domain from the account.
//
// See https://developer.dnsimple.com/v2/domains/#delete
func (s *DomainsService) DeleteDomain(ctx context.Context, accountID string, domainIdentifier string) (*DomainResponse, error) {
path := versioned(domainPath(accountID, domainIdentifier))
domainResponse := &DomainResponse{}
resp, err := s.client.delete(ctx, path, nil, nil)
if err != nil {
return nil, err
}
domainResponse.HTTPResponse = resp
return domainResponse, nil
}