-
Notifications
You must be signed in to change notification settings - Fork 0
/
listrecords.go
121 lines (104 loc) · 2.73 KB
/
listrecords.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
package cfdns
import (
"context"
"fmt"
"net/http"
"net/url"
"strconv"
"time"
"github.com/simplesurance/cfdns/log"
)
// ListRecords lists DNS records on a zone.
//
// API Reference: https://developers.cloudflare.com/api/operations/dns-records-for-a-zone-list-dns-records
func (c *Client) ListRecords(
req *ListRecordsRequest,
) *Iterator[ListRecordsResponseItem] {
page := 0
total := 0
read := 0
return &Iterator[ListRecordsResponseItem]{
fetchNext: func(
ctx context.Context,
) ([]*ListRecordsResponseItem, bool, error) {
page++
queryParams := url.Values{
"direction": {"asc"},
"per_page": {strconv.Itoa(itemsPerPage)},
"page": {strconv.Itoa(page)},
"order": {"type"},
}
if req.Name != "" {
queryParams.Set("name", req.Name)
}
if req.Type != "" {
queryParams.Set("type", req.Type)
}
resp, err := sendRequestRetry[*listRecordsAPIResponse](
ctx,
c,
c.logger.SubLogger(log.WithPrefix("ListRecords"), log.WithInt("page", page)),
&request{
method: http.MethodGet,
path: fmt.Sprintf("zones/%s/dns_records", url.PathEscape(req.ZoneID)),
queryParams: queryParams,
body: nil,
})
if err != nil {
return nil, false, err
}
items := make([]*ListRecordsResponseItem, len(resp.body.Result))
for i, v := range resp.body.Result {
items[i] = &ListRecordsResponseItem{
ID: v.ID,
Name: v.Name,
Type: v.Type,
Content: v.Content,
Proxied: v.Proxied,
Comment: v.Comment,
}
if v.TTL > 1 {
items[i].TTL = time.Second * time.Duration(v.TTL)
}
}
total = resp.body.ResultInfo.TotalCount
read += len(resp.body.Result)
isLast := read >= total
return items, isLast, nil
},
}
}
type ListRecordsRequest struct {
ZoneID string
Name string // Name is used to filter by name.
Type string // Type is used to filter by type.
}
type ListRecordsResponseItem struct {
ID string
Content string
Name string
Type string
Proxied bool
Comment string
TTL time.Duration
}
type listRecordsAPIResponse struct {
cfResponseCommon
Result []listRecordsAPIResponseItem `json:"result"`
}
type listRecordsAPIResponseItem struct {
ID string `json:"id"`
Name string `json:"name"`
Content string `json:"content"`
Proxied bool `json:"proxied"`
Proxiable bool `json:"proxiable"`
Type string `json:"type"`
Comment string `json:"comment"`
CreatedOn time.Time `json:"created_on"`
ModifiedOn time.Time `json:"modified_on"`
Locked bool `json:"locked"`
Tags []string `json:"tags"`
TTL int `json:"ttl"`
ZoneID string `json:"zone_id"`
ZoneName string `json:"zone_name"`
}