-
Notifications
You must be signed in to change notification settings - Fork 2
/
record.go
88 lines (75 loc) · 1.9 KB
/
record.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
package hcdns
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"time"
)
type Record struct {
ID string `json:"id"`
Type RecordType `json:"type"`
Name string `json:"name"`
Value string `json:"value"`
TTL int `json:"ttl,omitempty"`
ZoneID string `json:"zone_id"`
Created Time `json:"created"`
Modified Time `json:"modified"`
c *Client `json:"-"`
zoneID string `json:"-"`
}
func (r *Record) UpdateValue(ctx context.Context, value string) error {
return r.UpdateValueAndTTL(ctx, value, 0)
}
func (r *Record) UpdateValueAndTTL(ctx context.Context, value string, ttl time.Duration) error {
payload := recordReq{
Type: r.Type,
Name: r.Name,
Value: value,
TTL: uint64(ttl.Seconds()),
ZoneID: r.zoneID,
}
json, err := json.Marshal(payload)
if err != nil {
return fmt.Errorf("encode: %w", err)
}
root, err := r.c.do(ctx, http.MethodPut, "records/"+r.ID, bytes.NewBuffer(json), nil)
if err != nil {
return fmt.Errorf("request: %w", err)
}
r.Value = root.Record.Value
r.TTL = root.Record.TTL
return nil
}
func (r *Record) Delete(ctx context.Context) error {
_, err := r.c.do(ctx, http.MethodDelete, "records/"+r.ID, http.NoBody, nil)
if err != nil {
return fmt.Errorf("request: %w", err)
}
return nil
}
type RecordType string
const (
A RecordType = "A"
AAAA RecordType = "AAAA"
NS RecordType = "NS"
MX RecordType = "MX"
CNAME RecordType = "CNAME"
RP RecordType = "RP"
TXT RecordType = "TXT"
SOA RecordType = "SOA"
HINFO RecordType = "HINFO"
SRV RecordType = "SRV"
DANE RecordType = "DANE"
TLSA RecordType = "TLSA"
DS RecordType = "DS"
CAA RecordType = "CAA"
)
type recordReq struct {
Type RecordType `json:"type"`
Name string `json:"name"`
Value string `json:"value"`
TTL uint64 `json:"ttl,omitempty"`
ZoneID string `json:"zone_id"`
}