-
Notifications
You must be signed in to change notification settings - Fork 0
/
createrecord.go
91 lines (81 loc) · 2.01 KB
/
createrecord.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
package cfdns
import (
"context"
"fmt"
"net/http"
"net/url"
"time"
"github.com/simplesurance/cfdns/log"
)
// CreateRecord creates a DNS record on CloudFlare. A TTL of 1 second or less
// will use CloudFlare's "automatic" TTL.
//
// API Reference: https://developers.cloudflare.com/api/operations/dns-records-for-a-zone-create-dns-record
func (c *Client) CreateRecord(
ctx context.Context,
req *CreateRecordRequest,
) (*CreateRecordResponse, error) {
ttl := 1 // on CF the value "1" means "automatic"
if req.TTL > time.Second {
ttl = int(req.TTL.Seconds())
}
resp, err := sendRequestRetry[*createRecordAPIResponse](
ctx,
c,
c.logger.SubLogger(log.WithPrefix("CreateDNSRecord")),
&request{
method: http.MethodPost,
path: fmt.Sprintf("zones/%s/dns_records", url.PathEscape(req.ZoneID)),
queryParams: url.Values{},
body: &createRecordAPIRequest{
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 created with ID=%s",
req.Name, req.Type, req.Content, resp.body.Result.ID))
})
return &CreateRecordResponse{
ID: resp.body.Result.ID,
Name: resp.body.Result.Name,
}, err
}
type CreateRecordRequest struct {
ZoneID string
Name string
Type string
Content string
Proxied bool
Tags []string
Comment string
TTL time.Duration
}
type CreateRecordResponse struct {
ID string
Name string
}
type createRecordAPIRequest 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 createRecordAPIResponse struct {
cfResponseCommon
Result struct {
ID string `json:"id"`
Name string `json:"name"`
} `json:"result"`
}