-
Notifications
You must be signed in to change notification settings - Fork 0
/
updaterecord.go
92 lines (82 loc) · 2.12 KB
/
updaterecord.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
package cfdns
import (
"context"
"fmt"
"net/http"
"net/url"
"time"
"github.com/simplesurance/cfdns/log"
)
// UpdateRecord updates a DNS record on CloudFlare. A TTL of 1 second or less
// will use the "automatic" TTL from CloudFlare.
//
// API Reference: https://developers.cloudflare.com/api/operations/dns-records-for-a-zone-update-dns-record
func (c *Client) UpdateRecord(
ctx context.Context,
req *UpdateRecordRequest,
) (*UpdateRecordResponse, error) {
ttl := 1 // on CF the value "1" means "automatic"
if req.TTL > time.Second {
ttl = int(req.TTL.Seconds())
}
// PUT https://api.cloudflare.com/client/v4/zones/{zone_identifier}/dns_records/{identifier}
resp, err := sendRequestRetry[*updateRecordAPIResponse](
ctx,
c,
c.logger.SubLogger(log.WithPrefix("UpdateDNSRecord")),
&request{
method: http.MethodPut,
path: fmt.Sprintf("zones/%s/dns_records/%s",
url.PathEscape(req.ZoneID),
url.PathEscape(req.RecordID)),
queryParams: url.Values{},
body: &updateRecordAPIRequest{
Name: req.Name,
Type: req.Type,
Content: req.Content,
Proxied: req.Proxied,
Tags: req.Tags,
Comment: req.Comment,
TTL: ttl,
},
})
if err != nil {
return nil, err
}
c.logger.D(func(log log.DebugFn) {
log(fmt.Sprintf("Record %s (%s %s %s) updated",
req.RecordID, req.Name, req.Type, req.Content))
})
return &UpdateRecordResponse{
ModifiedOn: resp.body.Result.ModifiedOn,
}, err
}
type UpdateRecordRequest struct {
ZoneID string
RecordID string
Name string
Type string
Content string
Proxied bool
Tags []string
Comment string
TTL time.Duration
}
type UpdateRecordResponse struct {
ModifiedOn time.Time
}
type updateRecordAPIRequest struct {
Name string `json:"name"`
Type string `json:"type"`
Content string `json:"content"`
TTL int `json:"ttl,omitempty"`
Proxied bool `json:"proxied,omitempty"`
Tags []string `json:"tags,omitempty"`
Comment string `json:"comment,omitempty"`
}
type updateRecordAPIResponse struct {
cfResponseCommon
Result struct {
ModifiedOn time.Time `json:"modified_on"`
} `json:"result"`
}