-
Notifications
You must be signed in to change notification settings - Fork 2
/
zone.go
179 lines (145 loc) · 4.14 KB
/
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
173
174
175
176
177
178
179
package hcdns
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"time"
)
type Zone struct {
ID string `json:"id"`
Name string `json:"name"`
TTL int `json:"ttl"`
Registrar string `json:"registrar"`
LegacyDNSHost string `json:"legacy_dns_host"`
LegacyNS []string `json:"legacy_ns"`
NS []string `json:"ns"`
Created Time `json:"created"`
Verified Time `json:"verified"`
Modified Time `json:"modified"`
Project string `json:"project"`
Owner string `json:"owner"`
Permission string `json:"permission"`
ZoneType struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Prices any `json:"prices"`
} `json:"zone_type"`
Status string `json:"status"`
Paused bool `json:"paused"`
IsSecondaryDNS bool `json:"is_secondary_dns"`
TxtVerification struct {
Name string `json:"name"`
Token string `json:"token"`
} `json:"txt_verification"`
RecordsCount int `json:"records_count"`
c *Client `json:"-"`
}
func (z *Zone) UpdateDefaultTTL(ctx context.Context, ttl time.Duration) error {
payload := zoneReq{
Name: z.Name,
TTL: uint64(ttl.Seconds()),
}
json, err := json.Marshal(payload)
if err != nil {
return fmt.Errorf("encode: %w", err)
}
root, err := z.c.do(ctx, http.MethodPut, "zones/"+z.ID, bytes.NewBuffer(json), nil)
if err != nil {
return fmt.Errorf("request: %w", err)
}
z.TTL = root.Zone.TTL
return nil
}
func (z *Zone) Delete(ctx context.Context) error {
_, err := z.c.do(ctx, http.MethodDelete, "zones/"+z.ID, http.NoBody, nil)
if err != nil {
return fmt.Errorf("request: %w", err)
}
return nil
}
func (z *Zone) Records(ctx context.Context) ([]Record, error) {
var (
records []Record
query = make(url.Values, 2) //nolint:mnd
page uint = 1
)
query.Set("zone_id", z.ID)
for {
query.Set("page", strconv.FormatUint(uint64(page), 10))
root, err := z.c.do(ctx, http.MethodGet, "records", http.NoBody, query)
if err != nil {
return nil, fmt.Errorf("request: %w", err)
}
records = append(records, root.Records...)
if root.Meta.Pagination.NextPage == page {
break
}
page = root.Meta.Pagination.NextPage
}
for i := range records {
r := &records[i]
r.c = z.c
r.zoneID = z.ID
}
return records, nil
}
func (z *Zone) Record(ctx context.Context, id string) (*Record, error) {
root, err := z.c.do(ctx, http.MethodGet, "records/"+id, http.NoBody, nil)
if err != nil {
return nil, fmt.Errorf("request: %w", err)
}
root.Record.c = z.c
root.Record.zoneID = z.ID
return &root.Record, nil
}
func (z *Zone) CreateRecord(ctx context.Context, _type RecordType, name, value string) (*Record, error) {
return z.CreateRecordWithTTL(ctx, _type, name, value, 0)
}
func (z *Zone) CreateRecordWithTTL(ctx context.Context, _type RecordType, name, value string,
ttl time.Duration,
) (*Record, error) {
payload := recordReq{
Type: _type,
Name: name,
Value: value,
TTL: uint64(ttl.Seconds()),
ZoneID: z.ID,
}
json, err := json.Marshal(payload)
if err != nil {
return nil, fmt.Errorf("encode: %w", err)
}
root, err := z.c.do(ctx, http.MethodPost, "records", bytes.NewBuffer(json), nil)
if err != nil {
return nil, fmt.Errorf("request: %w", err)
}
root.Record.c = z.c
root.Record.zoneID = z.ID
return &root.Record, nil
}
func (z *Zone) createNSRecords(ctx context.Context) error {
for _, ns := range []string{"helium.ns.hetzner.de.", "hydrogen.ns.hetzner.com.", "oxygen.ns.hetzner.com."} {
if _, err := z.CreateRecord(ctx, NS, "@", ns); err != nil {
return fmt.Errorf("create NS record %s: %w", ns, err)
}
}
return nil
}
func (z *Zone) PrimaryServers(ctx context.Context) ([]PrimaryServer, error) {
query := make(url.Values, 1)
query.Set("zone_id", z.ID)
root, err := z.c.do(ctx, http.MethodGet, "primary_servers", http.NoBody, query)
if err != nil {
return nil, fmt.Errorf("request: %w", err)
}
return root.PrimaryServers, nil
}
type zoneReq struct {
Name string `json:"name"`
TTL uint64 `json:"ttl,omitempty"`
}