-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
domains.go
187 lines (150 loc) · 4.69 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
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
176
177
178
179
180
181
182
183
184
185
186
187
package desec
import (
"context"
"fmt"
"net/http"
"net/url"
"time"
)
// Domain a domain representation.
type Domain struct {
Name string `json:"name,omitempty"`
MinimumTTL int `json:"minimum_ttl,omitempty"`
Keys []DomainKey `json:"keys,omitempty"`
Created *time.Time `json:"created,omitempty"`
Published *time.Time `json:"published,omitempty"`
}
// DomainKey a domain key representation.
type DomainKey struct {
DNSKey string `json:"dnskey,omitempty"`
DS []string `json:"ds,omitempty"`
Flags int `json:"flags,omitempty"`
KeyType string `json:"keytype,omitempty"`
}
// DomainsService handles communication with the domain related methods of the deSEC API.
//
// https://desec.readthedocs.io/en/latest/dns/domains.html
type DomainsService struct {
client *Client
}
// Create creating a domain.
// https://desec.readthedocs.io/en/latest/dns/domains.html#creating-a-domain
func (s *DomainsService) Create(ctx context.Context, domainName string) (*Domain, error) {
endpoint, err := s.client.createEndpoint("domains")
if err != nil {
return nil, fmt.Errorf("failed to create endpoint: %w", err)
}
req, err := s.client.newRequest(ctx, http.MethodPost, endpoint, Domain{Name: domainName})
if err != nil {
return nil, err
}
resp, err := s.client.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to call API: %w", err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusCreated {
return nil, handleError(resp)
}
var domain Domain
err = handleResponse(resp, &domain)
if err != nil {
return nil, err
}
return &domain, nil
}
// GetAll listing domains.
// https://desec.readthedocs.io/en/latest/dns/domains.html#listing-domains
func (s *DomainsService) GetAll(ctx context.Context) ([]Domain, error) {
return s.getAll(ctx, nil)
}
// GetResponsible returns the responsible domain for a given DNS query name.
// https://desec.readthedocs.io/en/latest/dns/domains.html#identifying-the-responsible-domain-for-a-dns-name
func (s *DomainsService) GetResponsible(ctx context.Context, domainName string) (*Domain, error) {
queryValues := url.Values{}
queryValues.Set("owns_qname", domainName)
domains, err := s.getAll(ctx, queryValues)
if err != nil {
return nil, err
}
if len(domains) == 0 {
return nil, &NotFoundError{Detail: "no responsible domain found"}
}
return &domains[0], nil
}
// getAll listing domains.
// https://desec.readthedocs.io/en/latest/dns/domains.html#listing-domains
func (s *DomainsService) getAll(ctx context.Context, query url.Values) ([]Domain, error) {
endpoint, err := s.client.createEndpoint("domains")
if err != nil {
return nil, fmt.Errorf("failed to create endpoint: %w", err)
}
req, err := s.client.newRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return nil, err
}
if len(query) > 0 {
req.URL.RawQuery = query.Encode()
}
resp, err := s.client.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to call API: %w", err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
return nil, handleError(resp)
}
var domains []Domain
err = handleResponse(resp, &domains)
if err != nil {
return nil, err
}
return domains, nil
}
// Get retrieving a specific domain.
// https://desec.readthedocs.io/en/latest/dns/domains.html#retrieving-a-specific-domain
func (s *DomainsService) Get(ctx context.Context, domainName string) (*Domain, error) {
endpoint, err := s.client.createEndpoint("domains", domainName)
if err != nil {
return nil, fmt.Errorf("failed to create endpoint: %w", err)
}
req, err := s.client.newRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return nil, err
}
resp, err := s.client.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to call API: %w", err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
return nil, handleError(resp)
}
var domains Domain
err = handleResponse(resp, &domains)
if err != nil {
return nil, err
}
return &domains, nil
}
// Delete deleting a domain.
// https://desec.readthedocs.io/en/latest/dns/domains.html#deleting-a-domain
func (s *DomainsService) Delete(ctx context.Context, domainName string) error {
endpoint, err := s.client.createEndpoint("domains", domainName)
if err != nil {
return fmt.Errorf("failed to create endpoint: %w", err)
}
req, err := s.client.newRequest(ctx, http.MethodDelete, endpoint, nil)
if err != nil {
return err
}
resp, err := s.client.httpClient.Do(req)
if err != nil {
return fmt.Errorf("failed to call API: %w", err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusNoContent {
return handleError(resp)
}
return nil
}