From c0d6c00670bd1d37ddc65ff0073d4283e83284ca Mon Sep 17 00:00:00 2001 From: Jacob White Date: Wed, 21 Dec 2022 22:50:28 -0500 Subject: [PATCH 1/8] Add tags and comments to DNS records --- cmd/flarectl/dns.go | 2 +- cmd/flarectl/zone.go | 2 +- dns.go | 100 +++++++++++++++++++++++++++++----- dns_example_test.go | 8 +-- dns_test.go | 126 +++++++++++++++++++++++++++++++++++++++++-- example_test.go | 2 +- 6 files changed, 218 insertions(+), 22 deletions(-) diff --git a/cmd/flarectl/dns.go b/cmd/flarectl/dns.go index db9653e0376..69ec20e32fe 100644 --- a/cmd/flarectl/dns.go +++ b/cmd/flarectl/dns.go @@ -88,7 +88,7 @@ func dnsCreateOrUpdate(c *cli.Context) error { rr := cloudflare.DNSRecord{ Name: name + "." + zone, } - records, err := api.DNSRecords(context.Background(), zoneID, rr) + records, _, err := api.DNSRecords(context.Background(), zoneID, rr, cloudflare.DNSListParameters{}) if err != nil { fmt.Fprintln(os.Stderr, "Error fetching DNS records: ", err) return err diff --git a/cmd/flarectl/zone.go b/cmd/flarectl/zone.go index 5cd2c81a4c8..e4c8e291327 100644 --- a/cmd/flarectl/zone.go +++ b/cmd/flarectl/zone.go @@ -300,7 +300,7 @@ func zoneRecords(c *cli.Context) error { rr.Content = c.String("content") } var err error - records, err = api.DNSRecords(context.Background(), zoneID, rr) + records, _, err = api.DNSRecords(context.Background(), zoneID, rr, cloudflare.DNSListParameters{}) if err != nil { fmt.Println(err) return err diff --git a/dns.go b/dns.go index affb167efb6..1bde15391fe 100644 --- a/dns.go +++ b/dns.go @@ -29,6 +29,8 @@ type DNSRecord struct { Proxied *bool `json:"proxied,omitempty"` Proxiable bool `json:"proxiable,omitempty"` Locked bool `json:"locked,omitempty"` + Comment string `json:"comment,omitempty"` + Tags []string `json:"tags,omitempty"` } // DNSRecordResponse represents the response from the DNS endpoint. @@ -38,6 +40,39 @@ type DNSRecordResponse struct { ResultInfo `json:"result_info"` } +type TagQueryParameter string + +const ( + TagQueryPresent TagQueryParameter = "present" + TagQueryAbsent TagQueryParameter = "absent" + TagQueryExact TagQueryParameter = "exact" + TagQueryContains TagQueryParameter = "contains" + TagQueryStartsWith TagQueryParameter = "startswith" + TagQueryEndsWith TagQueryParameter = "endswith" +) + +type TagSearch struct { + Tag string + Query TagQueryParameter +} + +type ListDirection string + +const ( + ListDirectionAsc ListDirection = "asc" + ListDirectionDesc ListDirection = "desc" +) + +type DNSListParameters struct { + TagSearch []TagSearch `url:"-"` + Order string `url:"order,omitempty"` + TagMatch string `url:"tag-match,omitempty"` + Direction ListDirection `url:"direction,omitempty"` + TagQuery string `url:"-"` + Match string `url:"match,omitempty"` + ResultInfo +} + // DNSListResponse represents the response from the list DNS records endpoint. type DNSListResponse struct { Result []DNSRecord `json:"result"` @@ -90,7 +125,7 @@ func (api *API) CreateDNSRecord(ctx context.Context, zoneID string, rr DNSRecord // This takes a DNSRecord to allow filtering of the results returned. // // API reference: https://api.cloudflare.com/#dns-records-for-a-zone-list-dns-records -func (api *API) DNSRecords(ctx context.Context, zoneID string, rr DNSRecord) ([]DNSRecord, error) { +func (api *API) DNSRecords(ctx context.Context, zoneID string, rr DNSRecord, params DNSListParameters) ([]DNSRecord, *ResultInfo, error) { // Construct a query string v := url.Values{} // Using default per_page value as specified by the API @@ -104,30 +139,71 @@ func (api *API) DNSRecords(ctx context.Context, zoneID string, rr DNSRecord) ([] v.Set("content", rr.Content) } + if rr.Proxied != nil { + v.Set("proxied", strconv.FormatBool(*rr.Proxied)) + } + + if len(params.TagSearch) > 0 { + for _, tagParam := range params.TagSearch { + tagText := fmt.Sprintf("tag.%s", tagParam.Tag) + if tagParam.Query != "" { + tagText += fmt.Sprintf(".%s", tagParam.Query) + } + v.Add("tag", tagText) + } + } + + if params.Order != "" { + v.Set("order", params.Order) + } + + if params.TagMatch != "" { + v.Set("tag-match", params.TagMatch) + } + + if params.Direction != "" { + v.Set("direction", string(params.Direction)) + } + + if params.Match != "" { + v.Set("match", params.Match) + } + + autoPaginate := true + if params.PerPage >= 1 || params.Page >= 1 { + autoPaginate = false + } + + if params.PerPage < 1 { + params.PerPage = 50 + } + + if params.Page < 1 { + params.Page = 1 + } + var records []DNSRecord - page := 1 + var listResponse DNSListResponse // Loop over makeRequest until what we've fetched all records for { - v.Set("page", strconv.Itoa(page)) + v.Set("page", strconv.Itoa(params.Page)) uri := fmt.Sprintf("/zones/%s/dns_records?%s", zoneID, v.Encode()) res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) if err != nil { - return []DNSRecord{}, err + return []DNSRecord{}, &ResultInfo{}, err } - var r DNSListResponse - err = json.Unmarshal(res, &r) + err = json.Unmarshal(res, &listResponse) if err != nil { - return []DNSRecord{}, fmt.Errorf("%s: %w", errUnmarshalError, err) + return []DNSRecord{}, &ResultInfo{}, fmt.Errorf("%s: %w", errUnmarshalError, err) } - records = append(records, r.Result...) - if r.ResultInfo.Page >= r.ResultInfo.TotalPages { + records = append(records, listResponse.Result...) + params.ResultInfo = listResponse.ResultInfo.Next() + if params.ResultInfo.Done() || !autoPaginate { break } - // Loop around and fetch the next page - page++ } - return records, nil + return records, &listResponse.ResultInfo, nil } // DNSRecord returns a single DNS record for the given zone & record diff --git a/dns_example_test.go b/dns_example_test.go index 8f0a5f33c74..2af07b87bb6 100644 --- a/dns_example_test.go +++ b/dns_example_test.go @@ -20,7 +20,7 @@ func ExampleAPI_DNSRecords_all() { } // Fetch all records for a zone - recs, err := api.DNSRecords(context.Background(), zoneID, cloudflare.DNSRecord{}) + recs, _, err := api.DNSRecords(context.Background(), zoneID, cloudflare.DNSRecord{}, cloudflare.DNSListParameters{}) if err != nil { log.Fatal(err) } @@ -43,7 +43,7 @@ func ExampleAPI_DNSRecords_filterByContent() { // Fetch only records whose content is 198.51.100.1 localhost := cloudflare.DNSRecord{Content: "198.51.100.1"} - recs, err := api.DNSRecords(context.Background(), zoneID, localhost) + recs, _, err := api.DNSRecords(context.Background(), zoneID, localhost, cloudflare.DNSListParameters{}) if err != nil { log.Fatal(err) } @@ -67,7 +67,7 @@ func ExampleAPI_DNSRecords_filterByName() { // Fetch records of any type with name "foo.example.com" // The name must be fully-qualified foo := cloudflare.DNSRecord{Name: "foo.example.com"} - recs, err := api.DNSRecords(context.Background(), zoneID, foo) + recs, _, err := api.DNSRecords(context.Background(), zoneID, foo, cloudflare.DNSListParameters{}) if err != nil { log.Fatal(err) } @@ -90,7 +90,7 @@ func ExampleAPI_DNSRecords_filterByType() { // Fetch only AAAA type records aaaa := cloudflare.DNSRecord{Type: "AAAA"} - recs, err := api.DNSRecords(context.Background(), zoneID, aaaa) + recs, _, err := api.DNSRecords(context.Background(), zoneID, aaaa, cloudflare.DNSListParameters{}) if err != nil { log.Fatal(err) } diff --git a/dns_test.go b/dns_test.go index b2cc16fdb05..093977f35ee 100644 --- a/dns_test.go +++ b/dns_test.go @@ -200,8 +200,10 @@ func TestDNSRecords(t *testing.T) { } ], "result_info": { + "count": 1, "page": 1, - "total_pages": 1 + "per_page": 20, + "total_count": 2000 } }`) } @@ -231,12 +233,126 @@ func TestDNSRecords(t *testing.T) { }, }} - actual, err := client.DNSRecords(context.Background(), testZoneID, unicodeInput) + actual, _, err := client.DNSRecords(context.Background(), testZoneID, unicodeInput, DNSListParameters{}) require.NoError(t, err) assert.Equal(t, want, actual) } +func TestDNSRecordsSearch(t *testing.T) { + setup() + defer teardown() + + unicodeInput := DNSRecord{ + Name: "example.com", + Type: "A", + Content: "198.51.100.4", + } + asciiInput := DNSRecord{ + Name: "example.com", + Type: "A", + Content: "198.51.100.4", + } + + handler := func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method) + assert.Equal(t, asciiInput.Name, r.URL.Query().Get("name")) + assert.Equal(t, asciiInput.Type, r.URL.Query().Get("type")) + assert.Equal(t, asciiInput.Content, r.URL.Query().Get("content")) + assert.Equal(t, "all", r.URL.Query().Get("match")) + assert.Equal(t, "any", r.URL.Query().Get("tag-match")) + assert.Equal(t, "1", r.URL.Query().Get("page")) + assert.Equal(t, "type", r.URL.Query().Get("order")) + assert.Equal(t, "asc", r.URL.Query().Get("direction")) + fmt.Println(r.URL.Query()["tag"]) + tags := r.URL.Query()["tag"] + assert.Equal(t, 2, len(tags)) + assert.Equal(t, "tag.tag1", tags[0]) + assert.Equal(t, "tag.tag2.contains", tags[1]) + + w.Header().Set("content-type", "application/json") + fmt.Fprint(w, `{ + "success": true, + "errors": [], + "messages": [], + "result": [ + { + "id": "372e67954025e0ba6aaa6d586b9e0b59", + "type": "A", + "name": "example.com", + "content": "198.51.100.4", + "proxiable": true, + "proxied": false, + "ttl": 120, + "locked": false, + "zone_id": "d56084adb405e0b7e32c52321bf07be6", + "zone_name": "example.com", + "created_on": "2014-01-01T05:20:00Z", + "modified_on": "2014-01-01T05:20:00Z", + "data": {}, + "meta": { + "auto_added": true, + "source": "primary" + }, + "tags": ["tag1", "tag2extended"] + } + ], + "result_info": { + "count": 1, + "page": 1, + "per_page": 20, + "total_count": 2000 + } + }`) + } + + mux.HandleFunc("/zones/"+testZoneID+"/dns_records", handler) + + proxied := false + createdOn, _ := time.Parse(time.RFC3339, "2014-01-01T05:20:00Z") + modifiedOn, _ := time.Parse(time.RFC3339, "2014-01-01T05:20:00Z") + want := []DNSRecord{{ + ID: "372e67954025e0ba6aaa6d586b9e0b59", + Type: "A", + Name: asciiInput.Name, + Content: asciiInput.Content, + Proxiable: true, + Proxied: &proxied, + TTL: 120, + Locked: false, + ZoneID: testZoneID, + ZoneName: "example.com", + CreatedOn: createdOn, + ModifiedOn: modifiedOn, + Data: map[string]interface{}{}, + Meta: map[string]interface{}{ + "auto_added": true, + "source": "primary", + }, + Tags: []string{"tag1", "tag2extended"}, + }} + + actual, resultInfo, err := client.DNSRecords(context.Background(), testZoneID, unicodeInput, DNSListParameters{ + Match: "all", + Order: "type", + Direction: ListDirectionAsc, + TagMatch: "any", + TagSearch: []TagSearch{ + { + Tag: "tag1", + }, + { + Tag: "tag2", + Query: TagQueryContains, + }, + }, + }) + require.NoError(t, err) + assert.Equal(t, 2000, resultInfo.Total) + + assert.Equal(t, want, actual) +} + func TestDNSRecord(t *testing.T) { setup() defer teardown() @@ -266,7 +382,9 @@ func TestDNSRecord(t *testing.T) { "meta": { "auto_added": true, "source": "primary" - } + }, + "comment": "This is a comment", + "tags": ["tag1", "tag2"] } }`) } @@ -295,6 +413,8 @@ func TestDNSRecord(t *testing.T) { "auto_added": true, "source": "primary", }, + Comment: "This is a comment", + Tags: []string{"tag1", "tag2"}, } actual, err := client.DNSRecord(context.Background(), testZoneID, dnsRecordID) diff --git a/example_test.go b/example_test.go index 3320d363f23..ff8c9867629 100644 --- a/example_test.go +++ b/example_test.go @@ -28,7 +28,7 @@ func Example() { } // Fetch all DNS records for example.org - records, err := api.DNSRecords(context.Background(), zoneID, cloudflare.DNSRecord{}) + records, _, err := api.DNSRecords(context.Background(), zoneID, cloudflare.DNSRecord{}, cloudflare.DNSListParameters{}) if err != nil { fmt.Println(err) return From e6ec95d36f2803d16e95e7dc31ce92d974bd4c20 Mon Sep 17 00:00:00 2001 From: Jacob White Date: Wed, 21 Dec 2022 22:52:02 -0500 Subject: [PATCH 2/8] Add changelog --- .changelog/1151.txt | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changelog/1151.txt diff --git a/.changelog/1151.txt b/.changelog/1151.txt new file mode 100644 index 00000000000..9a333df6e78 --- /dev/null +++ b/.changelog/1151.txt @@ -0,0 +1,6 @@ +```release-note:breaking-change +dns: changes how DNS records are listed +``` +```release-note:enhancement +dns: add support for tags and comments +``` \ No newline at end of file From 465a7f8de145d295e4d0c3c95dda4d47a68fa4f6 Mon Sep 17 00:00:00 2001 From: Jacob White Date: Wed, 21 Dec 2022 23:26:12 -0500 Subject: [PATCH 3/8] Replace method signatures --- .changelog/1151.txt | 4 ++++ cmd/flarectl/dns.go | 12 +++++----- cmd/flarectl/zone.go | 6 ++--- dns.go | 41 +++++++++++++++++++++---------- dns_example_test.go | 10 ++++---- dns_test.go | 57 ++++++++++++++++++++++++++++---------------- example_test.go | 2 +- 7 files changed, 83 insertions(+), 49 deletions(-) diff --git a/.changelog/1151.txt b/.changelog/1151.txt index 9a333df6e78..502952ff37e 100644 --- a/.changelog/1151.txt +++ b/.changelog/1151.txt @@ -1,6 +1,10 @@ ```release-note:breaking-change dns: changes how DNS records are listed ``` + ```release-note:enhancement dns: add support for tags and comments +``` +```release-note:breaking-change +dns: method signatures have been updated to align with the upcoming client conventions ``` \ No newline at end of file diff --git a/cmd/flarectl/dns.go b/cmd/flarectl/dns.go index 69ec20e32fe..2d0712a53c7 100644 --- a/cmd/flarectl/dns.go +++ b/cmd/flarectl/dns.go @@ -50,7 +50,7 @@ func dnsCreate(c *cli.Context) error { Proxied: &proxy, Priority: &priority, } - resp, err := api.CreateDNSRecord(context.Background(), zoneID, record) + resp, err := api.CreateDNSRecord(context.Background(), cloudflare.ZoneIdentifier(zoneID), record) if err != nil { fmt.Fprintln(os.Stderr, "Error creating DNS record: ", err) return err @@ -88,7 +88,7 @@ func dnsCreateOrUpdate(c *cli.Context) error { rr := cloudflare.DNSRecord{ Name: name + "." + zone, } - records, _, err := api.DNSRecords(context.Background(), zoneID, rr, cloudflare.DNSListParameters{}) + records, _, err := api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), rr, cloudflare.DNSListParameters{}) if err != nil { fmt.Fprintln(os.Stderr, "Error fetching DNS records: ", err) return err @@ -108,7 +108,7 @@ func dnsCreateOrUpdate(c *cli.Context) error { rr.Proxied = &proxy rr.Priority = &priority - err := api.UpdateDNSRecord(context.Background(), zoneID, r.ID, rr) + err := api.UpdateDNSRecord(context.Background(), cloudflare.ZoneIdentifier(zoneID), r.ID, rr) if err != nil { fmt.Println("Error updating DNS record:", err) return err @@ -126,7 +126,7 @@ func dnsCreateOrUpdate(c *cli.Context) error { rr.Proxied = &proxy rr.Priority = &priority // TODO: Print the response. - resp, err = api.CreateDNSRecord(context.Background(), zoneID, rr) + resp, err = api.CreateDNSRecord(context.Background(), cloudflare.ZoneIdentifier(zoneID), rr) if err != nil { fmt.Println("Error creating DNS record:", err) return err @@ -171,7 +171,7 @@ func dnsUpdate(c *cli.Context) error { Proxied: &proxy, Priority: &priority, } - err = api.UpdateDNSRecord(context.Background(), zoneID, recordID, record) + err = api.UpdateDNSRecord(context.Background(), cloudflare.ZoneIdentifier(zoneID), recordID, record) if err != nil { fmt.Fprintln(os.Stderr, "Error updating DNS record: ", err) return err @@ -194,7 +194,7 @@ func dnsDelete(c *cli.Context) error { return err } - err = api.DeleteDNSRecord(context.Background(), zoneID, recordID) + err = api.DeleteDNSRecord(context.Background(), cloudflare.ZoneIdentifier(zoneID), recordID) if err != nil { fmt.Fprintln(os.Stderr, "Error deleting DNS record: ", err) return err diff --git a/cmd/flarectl/zone.go b/cmd/flarectl/zone.go index e4c8e291327..b8e21c4eb3e 100644 --- a/cmd/flarectl/zone.go +++ b/cmd/flarectl/zone.go @@ -279,11 +279,11 @@ func zoneRecords(c *cli.Context) error { return err } - // Create a an empty record for searching for records + // Create an empty record for searching for records rr := cloudflare.DNSRecord{} var records []cloudflare.DNSRecord if c.String("id") != "" { - rec, err := api.DNSRecord(context.Background(), zoneID, c.String("id")) + rec, err := api.GetDNSRecord(context.Background(), cloudflare.ZoneIdentifier(zoneID), c.String("id")) if err != nil { fmt.Println(err) return err @@ -300,7 +300,7 @@ func zoneRecords(c *cli.Context) error { rr.Content = c.String("content") } var err error - records, _, err = api.DNSRecords(context.Background(), zoneID, rr, cloudflare.DNSListParameters{}) + records, _, err = api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), rr, cloudflare.DNSListParameters{}) if err != nil { fmt.Println(err) return err diff --git a/dns.go b/dns.go index 1bde15391fe..357ec6208b0 100644 --- a/dns.go +++ b/dns.go @@ -102,10 +102,13 @@ func toUTS46ASCII(name string) string { // CreateDNSRecord creates a DNS record for the zone identifier. // // API reference: https://api.cloudflare.com/#dns-records-for-a-zone-create-dns-record -func (api *API) CreateDNSRecord(ctx context.Context, zoneID string, rr DNSRecord) (*DNSRecordResponse, error) { +func (api *API) CreateDNSRecord(ctx context.Context, rc *ResourceContainer, rr DNSRecord) (*DNSRecordResponse, error) { + if rc.Identifier == "" { + return nil, ErrMissingZoneID + } rr.Name = toUTS46ASCII(rr.Name) - uri := fmt.Sprintf("/zones/%s/dns_records", zoneID) + uri := fmt.Sprintf("/zones/%s/dns_records", rc.Identifier) res, err := api.makeRequestContext(ctx, http.MethodPost, uri, rr) if err != nil { return nil, err @@ -120,12 +123,15 @@ func (api *API) CreateDNSRecord(ctx context.Context, zoneID string, rr DNSRecord return recordResp, nil } -// DNSRecords returns a slice of DNS records for the given zone identifier. +// ListDNSRecords returns a slice of DNS records for the given zone identifier. // // This takes a DNSRecord to allow filtering of the results returned. // // API reference: https://api.cloudflare.com/#dns-records-for-a-zone-list-dns-records -func (api *API) DNSRecords(ctx context.Context, zoneID string, rr DNSRecord, params DNSListParameters) ([]DNSRecord, *ResultInfo, error) { +func (api *API) ListDNSRecords(ctx context.Context, rc *ResourceContainer, rr DNSRecord, params DNSListParameters) ([]DNSRecord, *ResultInfo, error) { + if rc.Identifier == "" { + return nil, nil, ErrMissingZoneID + } // Construct a query string v := url.Values{} // Using default per_page value as specified by the API @@ -188,7 +194,7 @@ func (api *API) DNSRecords(ctx context.Context, zoneID string, rr DNSRecord, par // Loop over makeRequest until what we've fetched all records for { v.Set("page", strconv.Itoa(params.Page)) - uri := fmt.Sprintf("/zones/%s/dns_records?%s", zoneID, v.Encode()) + uri := fmt.Sprintf("/zones/%s/dns_records?%s", rc.Identifier, v.Encode()) res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) if err != nil { return []DNSRecord{}, &ResultInfo{}, err @@ -206,12 +212,15 @@ func (api *API) DNSRecords(ctx context.Context, zoneID string, rr DNSRecord, par return records, &listResponse.ResultInfo, nil } -// DNSRecord returns a single DNS record for the given zone & record +// GetDNSRecord returns a single DNS record for the given zone & record // identifiers. // // API reference: https://api.cloudflare.com/#dns-records-for-a-zone-dns-record-details -func (api *API) DNSRecord(ctx context.Context, zoneID, recordID string) (DNSRecord, error) { - uri := fmt.Sprintf("/zones/%s/dns_records/%s", zoneID, recordID) +func (api *API) GetDNSRecord(ctx context.Context, rc *ResourceContainer, recordID string) (DNSRecord, error) { + if rc.Identifier == "" { + return DNSRecord{}, ErrMissingZoneID + } + uri := fmt.Sprintf("/zones/%s/dns_records/%s", rc.Identifier, recordID) res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) if err != nil { return DNSRecord{}, err @@ -228,13 +237,16 @@ func (api *API) DNSRecord(ctx context.Context, zoneID, recordID string) (DNSReco // identifiers. // // API reference: https://api.cloudflare.com/#dns-records-for-a-zone-update-dns-record -func (api *API) UpdateDNSRecord(ctx context.Context, zoneID, recordID string, rr DNSRecord) error { +func (api *API) UpdateDNSRecord(ctx context.Context, rc *ResourceContainer, recordID string, rr DNSRecord) error { + if rc.Identifier == "" { + return ErrMissingZoneID + } rr.Name = toUTS46ASCII(rr.Name) // Populate the record name from the existing one if the update didn't // specify it. if rr.Name == "" || rr.Type == "" { - rec, err := api.DNSRecord(ctx, zoneID, recordID) + rec, err := api.GetDNSRecord(ctx, rc, recordID) if err != nil { return err } @@ -246,7 +258,7 @@ func (api *API) UpdateDNSRecord(ctx context.Context, zoneID, recordID string, rr rr.Type = rec.Type } } - uri := fmt.Sprintf("/zones/%s/dns_records/%s", zoneID, recordID) + uri := fmt.Sprintf("/zones/%s/dns_records/%s", rc.Identifier, recordID) res, err := api.makeRequestContext(ctx, http.MethodPatch, uri, rr) if err != nil { return err @@ -263,8 +275,11 @@ func (api *API) UpdateDNSRecord(ctx context.Context, zoneID, recordID string, rr // identifiers. // // API reference: https://api.cloudflare.com/#dns-records-for-a-zone-delete-dns-record -func (api *API) DeleteDNSRecord(ctx context.Context, zoneID, recordID string) error { - uri := fmt.Sprintf("/zones/%s/dns_records/%s", zoneID, recordID) +func (api *API) DeleteDNSRecord(ctx context.Context, rc *ResourceContainer, recordID string) error { + if rc.Identifier == "" { + return ErrMissingZoneID + } + uri := fmt.Sprintf("/zones/%s/dns_records/%s", rc.Identifier, recordID) res, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil) if err != nil { return err diff --git a/dns_example_test.go b/dns_example_test.go index 2af07b87bb6..7c799f81aa9 100644 --- a/dns_example_test.go +++ b/dns_example_test.go @@ -5,7 +5,7 @@ import ( "fmt" "log" - cloudflare "github.com/cloudflare/cloudflare-go" + "github.com/cloudflare/cloudflare-go" ) func ExampleAPI_DNSRecords_all() { @@ -20,7 +20,7 @@ func ExampleAPI_DNSRecords_all() { } // Fetch all records for a zone - recs, _, err := api.DNSRecords(context.Background(), zoneID, cloudflare.DNSRecord{}, cloudflare.DNSListParameters{}) + recs, _, err := api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), cloudflare.DNSRecord{}, cloudflare.DNSListParameters{}) if err != nil { log.Fatal(err) } @@ -43,7 +43,7 @@ func ExampleAPI_DNSRecords_filterByContent() { // Fetch only records whose content is 198.51.100.1 localhost := cloudflare.DNSRecord{Content: "198.51.100.1"} - recs, _, err := api.DNSRecords(context.Background(), zoneID, localhost, cloudflare.DNSListParameters{}) + recs, _, err := api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), localhost, cloudflare.DNSListParameters{}) if err != nil { log.Fatal(err) } @@ -67,7 +67,7 @@ func ExampleAPI_DNSRecords_filterByName() { // Fetch records of any type with name "foo.example.com" // The name must be fully-qualified foo := cloudflare.DNSRecord{Name: "foo.example.com"} - recs, _, err := api.DNSRecords(context.Background(), zoneID, foo, cloudflare.DNSListParameters{}) + recs, _, err := api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), foo, cloudflare.DNSListParameters{}) if err != nil { log.Fatal(err) } @@ -90,7 +90,7 @@ func ExampleAPI_DNSRecords_filterByType() { // Fetch only AAAA type records aaaa := cloudflare.DNSRecord{Type: "AAAA"} - recs, _, err := api.DNSRecords(context.Background(), zoneID, aaaa, cloudflare.DNSListParameters{}) + recs, _, err := api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), aaaa, cloudflare.DNSListParameters{}) if err != nil { log.Fatal(err) } diff --git a/dns_test.go b/dns_test.go index 093977f35ee..2ed51ee8460 100644 --- a/dns_test.go +++ b/dns_test.go @@ -146,7 +146,9 @@ func TestCreateDNSRecord(t *testing.T) { Response: Response{Success: true, Errors: []ResponseInfo{}, Messages: []ResponseInfo{}}, } - actual, err := client.CreateDNSRecord(context.Background(), testZoneID, unicodeInput) + _, err := client.CreateDNSRecord(context.Background(), ZoneIdentifier(""), unicodeInput) + assert.Equal(t, ErrMissingZoneID, err) + actual, err := client.CreateDNSRecord(context.Background(), ZoneIdentifier(testZoneID), unicodeInput) require.NoError(t, err) assert.Equal(t, want, actual) @@ -233,7 +235,10 @@ func TestDNSRecords(t *testing.T) { }, }} - actual, _, err := client.DNSRecords(context.Background(), testZoneID, unicodeInput, DNSListParameters{}) + _, _, err := client.ListDNSRecords(context.Background(), ZoneIdentifier(""), unicodeInput, DNSListParameters{}) + assert.Equal(t, ErrMissingZoneID, err) + + actual, _, err := client.ListDNSRecords(context.Background(), ZoneIdentifier(testZoneID), unicodeInput, DNSListParameters{}) require.NoError(t, err) assert.Equal(t, want, actual) @@ -243,12 +248,7 @@ func TestDNSRecordsSearch(t *testing.T) { setup() defer teardown() - unicodeInput := DNSRecord{ - Name: "example.com", - Type: "A", - Content: "198.51.100.4", - } - asciiInput := DNSRecord{ + recordInput := DNSRecord{ Name: "example.com", Type: "A", Content: "198.51.100.4", @@ -256,9 +256,9 @@ func TestDNSRecordsSearch(t *testing.T) { handler := func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method) - assert.Equal(t, asciiInput.Name, r.URL.Query().Get("name")) - assert.Equal(t, asciiInput.Type, r.URL.Query().Get("type")) - assert.Equal(t, asciiInput.Content, r.URL.Query().Get("content")) + assert.Equal(t, recordInput.Name, r.URL.Query().Get("name")) + assert.Equal(t, recordInput.Type, r.URL.Query().Get("type")) + assert.Equal(t, recordInput.Content, r.URL.Query().Get("content")) assert.Equal(t, "all", r.URL.Query().Get("match")) assert.Equal(t, "any", r.URL.Query().Get("tag-match")) assert.Equal(t, "1", r.URL.Query().Get("page")) @@ -282,7 +282,7 @@ func TestDNSRecordsSearch(t *testing.T) { "name": "example.com", "content": "198.51.100.4", "proxiable": true, - "proxied": false, + "proxied": true, "ttl": 120, "locked": false, "zone_id": "d56084adb405e0b7e32c52321bf07be6", @@ -308,14 +308,14 @@ func TestDNSRecordsSearch(t *testing.T) { mux.HandleFunc("/zones/"+testZoneID+"/dns_records", handler) - proxied := false + proxied := true createdOn, _ := time.Parse(time.RFC3339, "2014-01-01T05:20:00Z") modifiedOn, _ := time.Parse(time.RFC3339, "2014-01-01T05:20:00Z") want := []DNSRecord{{ ID: "372e67954025e0ba6aaa6d586b9e0b59", Type: "A", - Name: asciiInput.Name, - Content: asciiInput.Content, + Name: recordInput.Name, + Content: recordInput.Content, Proxiable: true, Proxied: &proxied, TTL: 120, @@ -332,7 +332,10 @@ func TestDNSRecordsSearch(t *testing.T) { Tags: []string{"tag1", "tag2extended"}, }} - actual, resultInfo, err := client.DNSRecords(context.Background(), testZoneID, unicodeInput, DNSListParameters{ + actual, resultInfo, err := client.ListDNSRecords(context.Background(), ZoneIdentifier(testZoneID), recordInput, DNSListParameters{ + ResultInfo: ResultInfo{ + Page: 1, + }, Match: "all", Order: "type", Direction: ListDirectionAsc, @@ -417,7 +420,10 @@ func TestDNSRecord(t *testing.T) { Tags: []string{"tag1", "tag2"}, } - actual, err := client.DNSRecord(context.Background(), testZoneID, dnsRecordID) + _, err := client.GetDNSRecord(context.Background(), ZoneIdentifier(""), dnsRecordID) + assert.Equal(t, ErrMissingZoneID, err) + + actual, err := client.GetDNSRecord(context.Background(), ZoneIdentifier(testZoneID), dnsRecordID) require.NoError(t, err) assert.Equal(t, want, actual) @@ -475,7 +481,10 @@ func TestUpdateDNSRecord(t *testing.T) { mux.HandleFunc("/zones/"+testZoneID+"/dns_records/"+dnsRecordID, handler) - err := client.UpdateDNSRecord(context.Background(), testZoneID, dnsRecordID, input) + err := client.UpdateDNSRecord(context.Background(), ZoneIdentifier(""), dnsRecordID, input) + assert.Equal(t, ErrMissingZoneID, err) + + err = client.UpdateDNSRecord(context.Background(), ZoneIdentifier(testZoneID), dnsRecordID, input) require.NoError(t, err) } @@ -584,7 +593,10 @@ func TestUpdateDNSRecordWithoutName(t *testing.T) { mux.HandleFunc("/zones/"+testZoneID+"/dns_records/"+dnsRecordID, handler) - err := client.UpdateDNSRecord(context.Background(), testZoneID, dnsRecordID, unicodeInput) + err := client.UpdateDNSRecord(context.Background(), ZoneIdentifier(""), dnsRecordID, unicodeInput) + assert.Equal(t, ErrMissingZoneID, err) + + err = client.UpdateDNSRecord(context.Background(), ZoneIdentifier(testZoneID), dnsRecordID, unicodeInput) require.NoError(t, err) } @@ -692,7 +704,7 @@ func TestUpdateDNSRecordWithoutType(t *testing.T) { mux.HandleFunc("/zones/"+testZoneID+"/dns_records/"+dnsRecordID, handler) - err := client.UpdateDNSRecord(context.Background(), testZoneID, dnsRecordID, unicodeInput) + err := client.UpdateDNSRecord(context.Background(), ZoneIdentifier(testZoneID), dnsRecordID, unicodeInput) require.NoError(t, err) } @@ -718,6 +730,9 @@ func TestDeleteDNSRecord(t *testing.T) { mux.HandleFunc("/zones/"+testZoneID+"/dns_records/"+dnsRecordID, handler) - err := client.DeleteDNSRecord(context.Background(), testZoneID, dnsRecordID) + err := client.DeleteDNSRecord(context.Background(), ZoneIdentifier(""), dnsRecordID) + assert.Equal(t, ErrMissingZoneID, err) + + err = client.DeleteDNSRecord(context.Background(), ZoneIdentifier(testZoneID), dnsRecordID) require.NoError(t, err) } diff --git a/example_test.go b/example_test.go index ff8c9867629..dd38c54897f 100644 --- a/example_test.go +++ b/example_test.go @@ -28,7 +28,7 @@ func Example() { } // Fetch all DNS records for example.org - records, _, err := api.DNSRecords(context.Background(), zoneID, cloudflare.DNSRecord{}, cloudflare.DNSListParameters{}) + records, _, err := api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), cloudflare.DNSRecord{}, cloudflare.DNSListParameters{}) if err != nil { fmt.Println(err) return From 1d918688edd2093bb8c4a002b48557c3712d5056 Mon Sep 17 00:00:00 2001 From: Jacob White Date: Wed, 21 Dec 2022 23:28:49 -0500 Subject: [PATCH 4/8] Fix example refernce --- dns_example_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dns_example_test.go b/dns_example_test.go index 7c799f81aa9..9c9e32cc447 100644 --- a/dns_example_test.go +++ b/dns_example_test.go @@ -8,7 +8,7 @@ import ( "github.com/cloudflare/cloudflare-go" ) -func ExampleAPI_DNSRecords_all() { +func ExampleAPI_ListDNSRecords_all() { api, err := cloudflare.New("deadbeef", "test@example.org") if err != nil { log.Fatal(err) @@ -30,7 +30,7 @@ func ExampleAPI_DNSRecords_all() { } } -func ExampleAPI_DNSRecords_filterByContent() { +func ExampleAPI_ListDNSRecords_filterByContent() { api, err := cloudflare.New("deadbeef", "test@example.org") if err != nil { log.Fatal(err) @@ -53,7 +53,7 @@ func ExampleAPI_DNSRecords_filterByContent() { } } -func ExampleAPI_DNSRecords_filterByName() { +func ExampleAPI_ListDNSRecords_filterByName() { api, err := cloudflare.New("deadbeef", "test@example.org") if err != nil { log.Fatal(err) @@ -77,7 +77,7 @@ func ExampleAPI_DNSRecords_filterByName() { } } -func ExampleAPI_DNSRecords_filterByType() { +func ExampleAPI_ListDNSRecords_filterByType() { api, err := cloudflare.New("deadbeef", "test@example.org") if err != nil { log.Fatal(err) From 4f0c4c368862c109242903b4d93331ea1d688632 Mon Sep 17 00:00:00 2001 From: Jacob White Date: Thu, 22 Dec 2022 16:52:53 -0500 Subject: [PATCH 5/8] Address PR comments --- .changelog/1151.txt | 1 + dns.go | 106 +++++++++++++++----------------------------- dns_test.go | 16 ------- utils.go | 2 +- 4 files changed, 37 insertions(+), 88 deletions(-) diff --git a/.changelog/1151.txt b/.changelog/1151.txt index 502952ff37e..9b98839e1c9 100644 --- a/.changelog/1151.txt +++ b/.changelog/1151.txt @@ -5,6 +5,7 @@ dns: changes how DNS records are listed ```release-note:enhancement dns: add support for tags and comments ``` + ```release-note:breaking-change dns: method signatures have been updated to align with the upcoming client conventions ``` \ No newline at end of file diff --git a/dns.go b/dns.go index 357ec6208b0..ce6c12f9899 100644 --- a/dns.go +++ b/dns.go @@ -5,8 +5,6 @@ import ( "encoding/json" "fmt" "net/http" - "net/url" - "strconv" "time" "golang.org/x/net/idna" @@ -14,11 +12,11 @@ import ( // DNSRecord represents a DNS record in a zone. type DNSRecord struct { - CreatedOn time.Time `json:"created_on,omitempty"` - ModifiedOn time.Time `json:"modified_on,omitempty"` - Type string `json:"type,omitempty"` - Name string `json:"name,omitempty"` - Content string `json:"content,omitempty"` + CreatedOn time.Time `json:"created_on,omitempty" url:"created_on,omitempty"` + ModifiedOn time.Time `json:"modified_on,omitempty" url:"modified_on,omitempty"` + Type string `json:"type,omitempty" url:"type,omitempty"` + Name string `json:"name,omitempty" url:"name,omitempty"` + Content string `json:"content,omitempty" url:"content,omitempty"` Meta interface{} `json:"meta,omitempty"` Data interface{} `json:"data,omitempty"` // data returned by: SRV, LOC ID string `json:"id,omitempty"` @@ -26,10 +24,10 @@ type DNSRecord struct { ZoneName string `json:"zone_name,omitempty"` Priority *uint16 `json:"priority,omitempty"` TTL int `json:"ttl,omitempty"` - Proxied *bool `json:"proxied,omitempty"` + Proxied *bool `json:"proxied,omitempty" url:"proxied,omitempty"` Proxiable bool `json:"proxiable,omitempty"` Locked bool `json:"locked,omitempty"` - Comment string `json:"comment,omitempty"` + Comment string `json:"comment,omitempty" url:"comment,omitempty"` Tags []string `json:"tags,omitempty"` } @@ -40,21 +38,21 @@ type DNSRecordResponse struct { ResultInfo `json:"result_info"` } -type TagQueryParameter string - -const ( - TagQueryPresent TagQueryParameter = "present" - TagQueryAbsent TagQueryParameter = "absent" - TagQueryExact TagQueryParameter = "exact" - TagQueryContains TagQueryParameter = "contains" - TagQueryStartsWith TagQueryParameter = "startswith" - TagQueryEndsWith TagQueryParameter = "endswith" -) - -type TagSearch struct { - Tag string - Query TagQueryParameter -} +//type TagQueryParameter string +// +//const ( +// TagQueryPresent TagQueryParameter = "present" +// TagQueryAbsent TagQueryParameter = "absent" +// TagQueryExact TagQueryParameter = "exact" +// TagQueryContains TagQueryParameter = "contains" +// TagQueryStartsWith TagQueryParameter = "startswith" +// TagQueryEndsWith TagQueryParameter = "endswith" +//) +// +//type TagSearch struct { +// Tag string +// Query TagQueryParameter +//} type ListDirection string @@ -64,15 +62,20 @@ const ( ) type DNSListParameters struct { - TagSearch []TagSearch `url:"-"` - Order string `url:"order,omitempty"` - TagMatch string `url:"tag-match,omitempty"` + //TagSearch []TagSearch `url:"-"` + Order string `url:"order,omitempty"` + //TagMatch string `url:"tag-match,omitempty"` Direction ListDirection `url:"direction,omitempty"` - TagQuery string `url:"-"` - Match string `url:"match,omitempty"` + //TagQuery string `url:"-"` + Match string `url:"match,omitempty"` ResultInfo } +type ListDNSRecordsCombined struct { + DNSRecord + DNSListParameters +} + // DNSListResponse represents the response from the list DNS records endpoint. type DNSListResponse struct { Result []DNSRecord `json:"result"` @@ -132,47 +135,9 @@ func (api *API) ListDNSRecords(ctx context.Context, rc *ResourceContainer, rr DN if rc.Identifier == "" { return nil, nil, ErrMissingZoneID } - // Construct a query string - v := url.Values{} - // Using default per_page value as specified by the API - if rr.Name != "" { - v.Set("name", toUTS46ASCII(rr.Name)) - } - if rr.Type != "" { - v.Set("type", rr.Type) - } - if rr.Content != "" { - v.Set("content", rr.Content) - } - if rr.Proxied != nil { - v.Set("proxied", strconv.FormatBool(*rr.Proxied)) - } - - if len(params.TagSearch) > 0 { - for _, tagParam := range params.TagSearch { - tagText := fmt.Sprintf("tag.%s", tagParam.Tag) - if tagParam.Query != "" { - tagText += fmt.Sprintf(".%s", tagParam.Query) - } - v.Add("tag", tagText) - } - } - - if params.Order != "" { - v.Set("order", params.Order) - } - - if params.TagMatch != "" { - v.Set("tag-match", params.TagMatch) - } - - if params.Direction != "" { - v.Set("direction", string(params.Direction)) - } - - if params.Match != "" { - v.Set("match", params.Match) + if rr.Name != "" { + rr.Name = toUTS46ASCII(rr.Name) } autoPaginate := true @@ -193,8 +158,7 @@ func (api *API) ListDNSRecords(ctx context.Context, rc *ResourceContainer, rr DN // Loop over makeRequest until what we've fetched all records for { - v.Set("page", strconv.Itoa(params.Page)) - uri := fmt.Sprintf("/zones/%s/dns_records?%s", rc.Identifier, v.Encode()) + uri := buildURI(fmt.Sprintf("/zones/%s/dns_records", rc.Identifier), ListDNSRecordsCombined{rr, params}) res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) if err != nil { return []DNSRecord{}, &ResultInfo{}, err diff --git a/dns_test.go b/dns_test.go index 2ed51ee8460..fc61d3e28b3 100644 --- a/dns_test.go +++ b/dns_test.go @@ -260,15 +260,9 @@ func TestDNSRecordsSearch(t *testing.T) { assert.Equal(t, recordInput.Type, r.URL.Query().Get("type")) assert.Equal(t, recordInput.Content, r.URL.Query().Get("content")) assert.Equal(t, "all", r.URL.Query().Get("match")) - assert.Equal(t, "any", r.URL.Query().Get("tag-match")) assert.Equal(t, "1", r.URL.Query().Get("page")) assert.Equal(t, "type", r.URL.Query().Get("order")) assert.Equal(t, "asc", r.URL.Query().Get("direction")) - fmt.Println(r.URL.Query()["tag"]) - tags := r.URL.Query()["tag"] - assert.Equal(t, 2, len(tags)) - assert.Equal(t, "tag.tag1", tags[0]) - assert.Equal(t, "tag.tag2.contains", tags[1]) w.Header().Set("content-type", "application/json") fmt.Fprint(w, `{ @@ -339,16 +333,6 @@ func TestDNSRecordsSearch(t *testing.T) { Match: "all", Order: "type", Direction: ListDirectionAsc, - TagMatch: "any", - TagSearch: []TagSearch{ - { - Tag: "tag1", - }, - { - Tag: "tag2", - Query: TagQueryContains, - }, - }, }) require.NoError(t, err) assert.Equal(t, 2000, resultInfo.Total) diff --git a/utils.go b/utils.go index 38d4315d763..bc2dc563384 100644 --- a/utils.go +++ b/utils.go @@ -16,7 +16,7 @@ func buildURI(path string, options interface{}) string { } // loadFixture takes a series of path components and returns the JSON fixture at -// that locationassociated. +// that location associated. func loadFixture(parts ...string) string { paths := []string{"testdata", "fixtures"} paths = append(paths, parts...) From 1b620d1fc2585ee01091f89db4b19a2a4b78365f Mon Sep 17 00:00:00 2001 From: Jacob White Date: Thu, 22 Dec 2022 17:13:35 -0500 Subject: [PATCH 6/8] Don't export combined DNS list parameters --- dns.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dns.go b/dns.go index ce6c12f9899..eb188c3e617 100644 --- a/dns.go +++ b/dns.go @@ -71,7 +71,7 @@ type DNSListParameters struct { ResultInfo } -type ListDNSRecordsCombined struct { +type listDNSRecordsCombined struct { DNSRecord DNSListParameters } @@ -158,7 +158,7 @@ func (api *API) ListDNSRecords(ctx context.Context, rc *ResourceContainer, rr DN // Loop over makeRequest until what we've fetched all records for { - uri := buildURI(fmt.Sprintf("/zones/%s/dns_records", rc.Identifier), ListDNSRecordsCombined{rr, params}) + uri := buildURI(fmt.Sprintf("/zones/%s/dns_records", rc.Identifier), listDNSRecordsCombined{rr, params}) res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) if err != nil { return []DNSRecord{}, &ResultInfo{}, err From 3f86deb0bbaf80ee6db4867d47850a20bad302bd Mon Sep 17 00:00:00 2001 From: Jacob White Date: Thu, 22 Dec 2022 17:14:52 -0500 Subject: [PATCH 7/8] DNSListParameters -> ListDNSParameters --- cmd/flarectl/dns.go | 2 +- cmd/flarectl/zone.go | 2 +- dns.go | 6 +++--- dns_example_test.go | 8 ++++---- dns_test.go | 6 +++--- example_test.go | 2 +- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/cmd/flarectl/dns.go b/cmd/flarectl/dns.go index 2d0712a53c7..b556bc8bd50 100644 --- a/cmd/flarectl/dns.go +++ b/cmd/flarectl/dns.go @@ -88,7 +88,7 @@ func dnsCreateOrUpdate(c *cli.Context) error { rr := cloudflare.DNSRecord{ Name: name + "." + zone, } - records, _, err := api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), rr, cloudflare.DNSListParameters{}) + records, _, err := api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), rr, cloudflare.ListDNSParameters{}) if err != nil { fmt.Fprintln(os.Stderr, "Error fetching DNS records: ", err) return err diff --git a/cmd/flarectl/zone.go b/cmd/flarectl/zone.go index b8e21c4eb3e..662cdb2aefd 100644 --- a/cmd/flarectl/zone.go +++ b/cmd/flarectl/zone.go @@ -300,7 +300,7 @@ func zoneRecords(c *cli.Context) error { rr.Content = c.String("content") } var err error - records, _, err = api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), rr, cloudflare.DNSListParameters{}) + records, _, err = api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), rr, cloudflare.ListDNSParameters{}) if err != nil { fmt.Println(err) return err diff --git a/dns.go b/dns.go index eb188c3e617..bb8776fcf51 100644 --- a/dns.go +++ b/dns.go @@ -61,7 +61,7 @@ const ( ListDirectionDesc ListDirection = "desc" ) -type DNSListParameters struct { +type ListDNSParameters struct { //TagSearch []TagSearch `url:"-"` Order string `url:"order,omitempty"` //TagMatch string `url:"tag-match,omitempty"` @@ -73,7 +73,7 @@ type DNSListParameters struct { type listDNSRecordsCombined struct { DNSRecord - DNSListParameters + ListDNSParameters } // DNSListResponse represents the response from the list DNS records endpoint. @@ -131,7 +131,7 @@ func (api *API) CreateDNSRecord(ctx context.Context, rc *ResourceContainer, rr D // This takes a DNSRecord to allow filtering of the results returned. // // API reference: https://api.cloudflare.com/#dns-records-for-a-zone-list-dns-records -func (api *API) ListDNSRecords(ctx context.Context, rc *ResourceContainer, rr DNSRecord, params DNSListParameters) ([]DNSRecord, *ResultInfo, error) { +func (api *API) ListDNSRecords(ctx context.Context, rc *ResourceContainer, rr DNSRecord, params ListDNSParameters) ([]DNSRecord, *ResultInfo, error) { if rc.Identifier == "" { return nil, nil, ErrMissingZoneID } diff --git a/dns_example_test.go b/dns_example_test.go index 9c9e32cc447..b84d558050c 100644 --- a/dns_example_test.go +++ b/dns_example_test.go @@ -20,7 +20,7 @@ func ExampleAPI_ListDNSRecords_all() { } // Fetch all records for a zone - recs, _, err := api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), cloudflare.DNSRecord{}, cloudflare.DNSListParameters{}) + recs, _, err := api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), cloudflare.DNSRecord{}, cloudflare.ListDNSParameters{}) if err != nil { log.Fatal(err) } @@ -43,7 +43,7 @@ func ExampleAPI_ListDNSRecords_filterByContent() { // Fetch only records whose content is 198.51.100.1 localhost := cloudflare.DNSRecord{Content: "198.51.100.1"} - recs, _, err := api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), localhost, cloudflare.DNSListParameters{}) + recs, _, err := api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), localhost, cloudflare.ListDNSParameters{}) if err != nil { log.Fatal(err) } @@ -67,7 +67,7 @@ func ExampleAPI_ListDNSRecords_filterByName() { // Fetch records of any type with name "foo.example.com" // The name must be fully-qualified foo := cloudflare.DNSRecord{Name: "foo.example.com"} - recs, _, err := api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), foo, cloudflare.DNSListParameters{}) + recs, _, err := api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), foo, cloudflare.ListDNSParameters{}) if err != nil { log.Fatal(err) } @@ -90,7 +90,7 @@ func ExampleAPI_ListDNSRecords_filterByType() { // Fetch only AAAA type records aaaa := cloudflare.DNSRecord{Type: "AAAA"} - recs, _, err := api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), aaaa, cloudflare.DNSListParameters{}) + recs, _, err := api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), aaaa, cloudflare.ListDNSParameters{}) if err != nil { log.Fatal(err) } diff --git a/dns_test.go b/dns_test.go index fc61d3e28b3..e317366b34d 100644 --- a/dns_test.go +++ b/dns_test.go @@ -235,10 +235,10 @@ func TestDNSRecords(t *testing.T) { }, }} - _, _, err := client.ListDNSRecords(context.Background(), ZoneIdentifier(""), unicodeInput, DNSListParameters{}) + _, _, err := client.ListDNSRecords(context.Background(), ZoneIdentifier(""), unicodeInput, ListDNSParameters{}) assert.Equal(t, ErrMissingZoneID, err) - actual, _, err := client.ListDNSRecords(context.Background(), ZoneIdentifier(testZoneID), unicodeInput, DNSListParameters{}) + actual, _, err := client.ListDNSRecords(context.Background(), ZoneIdentifier(testZoneID), unicodeInput, ListDNSParameters{}) require.NoError(t, err) assert.Equal(t, want, actual) @@ -326,7 +326,7 @@ func TestDNSRecordsSearch(t *testing.T) { Tags: []string{"tag1", "tag2extended"}, }} - actual, resultInfo, err := client.ListDNSRecords(context.Background(), ZoneIdentifier(testZoneID), recordInput, DNSListParameters{ + actual, resultInfo, err := client.ListDNSRecords(context.Background(), ZoneIdentifier(testZoneID), recordInput, ListDNSParameters{ ResultInfo: ResultInfo{ Page: 1, }, diff --git a/example_test.go b/example_test.go index dd38c54897f..43e41ea608d 100644 --- a/example_test.go +++ b/example_test.go @@ -28,7 +28,7 @@ func Example() { } // Fetch all DNS records for example.org - records, _, err := api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), cloudflare.DNSRecord{}, cloudflare.DNSListParameters{}) + records, _, err := api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), cloudflare.DNSRecord{}, cloudflare.ListDNSParameters{}) if err != nil { fmt.Println(err) return From 0efc21194956b6908849cd2d70563afb512c2f4e Mon Sep 17 00:00:00 2001 From: Jacob Bednarz Date: Wed, 28 Dec 2022 11:31:46 +1100 Subject: [PATCH 8/8] update method signatures correctly --- .changelog/1151.txt | 14 +++-- cmd/flarectl/dns.go | 27 +++++---- cmd/flarectl/zone.go | 4 +- dns.go | 130 +++++++++++++++++++++++++------------------ dns_example_test.go | 15 ++--- dns_test.go | 85 +++++++++++++++------------- example_test.go | 2 +- 7 files changed, 157 insertions(+), 120 deletions(-) diff --git a/.changelog/1151.txt b/.changelog/1151.txt index 9b98839e1c9..5482d72f8ed 100644 --- a/.changelog/1151.txt +++ b/.changelog/1151.txt @@ -1,11 +1,15 @@ -```release-note:breaking-change -dns: changes how DNS records are listed -``` - ```release-note:enhancement dns: add support for tags and comments ``` ```release-note:breaking-change dns: method signatures have been updated to align with the upcoming client conventions -``` \ No newline at end of file +``` + +```release-note:breaking-change +dns: `DNSRecords` has been renamed to `ListDNSRecords` +``` + +```release-note:breaking-change +dns: `DNSRecord` has been renamed to `GetDNSRecord` +``` diff --git a/cmd/flarectl/dns.go b/cmd/flarectl/dns.go index b556bc8bd50..aa7617e022d 100644 --- a/cmd/flarectl/dns.go +++ b/cmd/flarectl/dns.go @@ -42,7 +42,7 @@ func dnsCreate(c *cli.Context) error { return err } - record := cloudflare.DNSRecord{ + record := cloudflare.CreateDNSRecordParams{ Name: name, Type: strings.ToUpper(rtype), Content: content, @@ -84,11 +84,7 @@ func dnsCreateOrUpdate(c *cli.Context) error { return err } - // Look for an existing record - rr := cloudflare.DNSRecord{ - Name: name + "." + zone, - } - records, _, err := api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), rr, cloudflare.ListDNSParameters{}) + records, _, err := api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), cloudflare.ListDNSRecordsParams{Name: name + "." + zone}) if err != nil { fmt.Fprintln(os.Stderr, "Error fetching DNS records: ", err) return err @@ -101,6 +97,7 @@ func dnsCreateOrUpdate(c *cli.Context) error { // has multiple RRs we'll just update the first one. for _, r := range records { if r.Type == rtype { + rr := cloudflare.UpdateDNSRecordParams{} rr.ID = r.ID rr.Type = r.Type rr.Content = content @@ -108,18 +105,28 @@ func dnsCreateOrUpdate(c *cli.Context) error { rr.Proxied = &proxy rr.Priority = &priority - err := api.UpdateDNSRecord(context.Background(), cloudflare.ZoneIdentifier(zoneID), r.ID, rr) + err := api.UpdateDNSRecord(context.Background(), cloudflare.ZoneIdentifier(zoneID), rr) if err != nil { fmt.Println("Error updating DNS record:", err) return err } resp = &cloudflare.DNSRecordResponse{ - Result: rr, + Result: cloudflare.DNSRecord{ + ID: rr.ID, + Name: rr.Name, + Type: rr.Type, + Content: rr.Content, + TTL: rr.TTL, + Proxiable: rr.Proxiable, + Proxied: &proxy, + Locked: rr.Locked, + }, } } } } else { // Record doesn't exist - create it + rr := cloudflare.CreateDNSRecordParams{} rr.Type = rtype rr.Content = content rr.TTL = ttl @@ -162,7 +169,7 @@ func dnsUpdate(c *cli.Context) error { return err } - record := cloudflare.DNSRecord{ + record := cloudflare.UpdateDNSRecordParams{ ID: recordID, Name: name, Type: strings.ToUpper(rtype), @@ -171,7 +178,7 @@ func dnsUpdate(c *cli.Context) error { Proxied: &proxy, Priority: &priority, } - err = api.UpdateDNSRecord(context.Background(), cloudflare.ZoneIdentifier(zoneID), recordID, record) + err = api.UpdateDNSRecord(context.Background(), cloudflare.ZoneIdentifier(zoneID), record) if err != nil { fmt.Fprintln(os.Stderr, "Error updating DNS record: ", err) return err diff --git a/cmd/flarectl/zone.go b/cmd/flarectl/zone.go index 662cdb2aefd..19915185ce5 100644 --- a/cmd/flarectl/zone.go +++ b/cmd/flarectl/zone.go @@ -280,7 +280,7 @@ func zoneRecords(c *cli.Context) error { } // Create an empty record for searching for records - rr := cloudflare.DNSRecord{} + rr := cloudflare.ListDNSRecordsParams{} var records []cloudflare.DNSRecord if c.String("id") != "" { rec, err := api.GetDNSRecord(context.Background(), cloudflare.ZoneIdentifier(zoneID), c.String("id")) @@ -300,7 +300,7 @@ func zoneRecords(c *cli.Context) error { rr.Content = c.String("content") } var err error - records, _, err = api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), rr, cloudflare.ListDNSParameters{}) + records, _, err = api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), rr) if err != nil { fmt.Println(err) return err diff --git a/dns.go b/dns.go index bb8776fcf51..9e1d767f819 100644 --- a/dns.go +++ b/dns.go @@ -12,11 +12,11 @@ import ( // DNSRecord represents a DNS record in a zone. type DNSRecord struct { - CreatedOn time.Time `json:"created_on,omitempty" url:"created_on,omitempty"` - ModifiedOn time.Time `json:"modified_on,omitempty" url:"modified_on,omitempty"` - Type string `json:"type,omitempty" url:"type,omitempty"` - Name string `json:"name,omitempty" url:"name,omitempty"` - Content string `json:"content,omitempty" url:"content,omitempty"` + CreatedOn time.Time `json:"created_on,omitempty"` + ModifiedOn time.Time `json:"modified_on,omitempty"` + Type string `json:"type,omitempty"` + Name string `json:"name,omitempty"` + Content string `json:"content,omitempty"` Meta interface{} `json:"meta,omitempty"` Data interface{} `json:"data,omitempty"` // data returned by: SRV, LOC ID string `json:"id,omitempty"` @@ -24,10 +24,10 @@ type DNSRecord struct { ZoneName string `json:"zone_name,omitempty"` Priority *uint16 `json:"priority,omitempty"` TTL int `json:"ttl,omitempty"` - Proxied *bool `json:"proxied,omitempty" url:"proxied,omitempty"` + Proxied *bool `json:"proxied,omitempty"` Proxiable bool `json:"proxiable,omitempty"` Locked bool `json:"locked,omitempty"` - Comment string `json:"comment,omitempty" url:"comment,omitempty"` + Comment string `json:"comment,omitempty"` Tags []string `json:"tags,omitempty"` } @@ -38,22 +38,6 @@ type DNSRecordResponse struct { ResultInfo `json:"result_info"` } -//type TagQueryParameter string -// -//const ( -// TagQueryPresent TagQueryParameter = "present" -// TagQueryAbsent TagQueryParameter = "absent" -// TagQueryExact TagQueryParameter = "exact" -// TagQueryContains TagQueryParameter = "contains" -// TagQueryStartsWith TagQueryParameter = "startswith" -// TagQueryEndsWith TagQueryParameter = "endswith" -//) -// -//type TagSearch struct { -// Tag string -// Query TagQueryParameter -//} - type ListDirection string const ( @@ -61,19 +45,40 @@ const ( ListDirectionDesc ListDirection = "desc" ) -type ListDNSParameters struct { - //TagSearch []TagSearch `url:"-"` - Order string `url:"order,omitempty"` - //TagMatch string `url:"tag-match,omitempty"` - Direction ListDirection `url:"direction,omitempty"` - //TagQuery string `url:"-"` - Match string `url:"match,omitempty"` +type ListDNSRecordsParams struct { + CreatedOn time.Time `json:"created_on,omitempty" url:"created_on,omitempty"` + ModifiedOn time.Time `json:"modified_on,omitempty" url:"modified_on,omitempty"` + Type string `json:"type,omitempty" url:"type,omitempty"` + Name string `json:"name,omitempty" url:"name,omitempty"` + Content string `json:"content,omitempty" url:"content,omitempty"` + Proxied *bool `json:"proxied,omitempty" url:"proxied,omitempty"` + Comment string `json:"comment,omitempty" url:"comment,omitempty"` + Tags []string `json:"tags,omitempty"` + Order string `url:"order,omitempty"` + Direction ListDirection `url:"direction,omitempty"` + Match string `url:"match,omitempty"` + ResultInfo } -type listDNSRecordsCombined struct { - DNSRecord - ListDNSParameters +type UpdateDNSRecordParams struct { + CreatedOn time.Time `json:"created_on,omitempty" url:"created_on,omitempty"` + ModifiedOn time.Time `json:"modified_on,omitempty" url:"modified_on,omitempty"` + Type string `json:"type,omitempty" url:"type,omitempty"` + Name string `json:"name,omitempty" url:"name,omitempty"` + Content string `json:"content,omitempty" url:"content,omitempty"` + Meta interface{} `json:"meta,omitempty"` + Data interface{} `json:"data,omitempty"` // data returned by: SRV, LOC + ID string `json:"id,omitempty"` + ZoneID string `json:"zone_id,omitempty"` + ZoneName string `json:"zone_name,omitempty"` + Priority *uint16 `json:"priority,omitempty"` + TTL int `json:"ttl,omitempty"` + Proxied *bool `json:"proxied,omitempty" url:"proxied,omitempty"` + Proxiable bool `json:"proxiable,omitempty"` + Locked bool `json:"locked,omitempty"` + Comment string `json:"comment,omitempty" url:"comment,omitempty"` + Tags []string `json:"tags,omitempty"` } // DNSListResponse represents the response from the list DNS records endpoint. @@ -102,17 +107,37 @@ func toUTS46ASCII(name string) string { return name } +type CreateDNSRecordParams struct { + CreatedOn time.Time `json:"created_on,omitempty" url:"created_on,omitempty"` + ModifiedOn time.Time `json:"modified_on,omitempty" url:"modified_on,omitempty"` + Type string `json:"type,omitempty" url:"type,omitempty"` + Name string `json:"name,omitempty" url:"name,omitempty"` + Content string `json:"content,omitempty" url:"content,omitempty"` + Meta interface{} `json:"meta,omitempty"` + Data interface{} `json:"data,omitempty"` // data returned by: SRV, LOC + ID string `json:"id,omitempty"` + ZoneID string `json:"zone_id,omitempty"` + ZoneName string `json:"zone_name,omitempty"` + Priority *uint16 `json:"priority,omitempty"` + TTL int `json:"ttl,omitempty"` + Proxied *bool `json:"proxied,omitempty" url:"proxied,omitempty"` + Proxiable bool `json:"proxiable,omitempty"` + Locked bool `json:"locked,omitempty"` + Comment string `json:"comment,omitempty" url:"comment,omitempty"` + Tags []string `json:"tags,omitempty"` +} + // CreateDNSRecord creates a DNS record for the zone identifier. // // API reference: https://api.cloudflare.com/#dns-records-for-a-zone-create-dns-record -func (api *API) CreateDNSRecord(ctx context.Context, rc *ResourceContainer, rr DNSRecord) (*DNSRecordResponse, error) { +func (api *API) CreateDNSRecord(ctx context.Context, rc *ResourceContainer, params CreateDNSRecordParams) (*DNSRecordResponse, error) { if rc.Identifier == "" { return nil, ErrMissingZoneID } - rr.Name = toUTS46ASCII(rr.Name) + params.Name = toUTS46ASCII(params.Name) uri := fmt.Sprintf("/zones/%s/dns_records", rc.Identifier) - res, err := api.makeRequestContext(ctx, http.MethodPost, uri, rr) + res, err := api.makeRequestContext(ctx, http.MethodPost, uri, params) if err != nil { return nil, err } @@ -128,16 +153,14 @@ func (api *API) CreateDNSRecord(ctx context.Context, rc *ResourceContainer, rr D // ListDNSRecords returns a slice of DNS records for the given zone identifier. // -// This takes a DNSRecord to allow filtering of the results returned. -// // API reference: https://api.cloudflare.com/#dns-records-for-a-zone-list-dns-records -func (api *API) ListDNSRecords(ctx context.Context, rc *ResourceContainer, rr DNSRecord, params ListDNSParameters) ([]DNSRecord, *ResultInfo, error) { +func (api *API) ListDNSRecords(ctx context.Context, rc *ResourceContainer, params ListDNSRecordsParams) ([]DNSRecord, *ResultInfo, error) { if rc.Identifier == "" { return nil, nil, ErrMissingZoneID } - if rr.Name != "" { - rr.Name = toUTS46ASCII(rr.Name) + if params.Name != "" { + params.Name = toUTS46ASCII(params.Name) } autoPaginate := true @@ -156,9 +179,8 @@ func (api *API) ListDNSRecords(ctx context.Context, rc *ResourceContainer, rr DN var records []DNSRecord var listResponse DNSListResponse - // Loop over makeRequest until what we've fetched all records for { - uri := buildURI(fmt.Sprintf("/zones/%s/dns_records", rc.Identifier), listDNSRecordsCombined{rr, params}) + uri := buildURI(fmt.Sprintf("/zones/%s/dns_records", rc.Identifier), params) res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) if err != nil { return []DNSRecord{}, &ResultInfo{}, err @@ -201,29 +223,31 @@ func (api *API) GetDNSRecord(ctx context.Context, rc *ResourceContainer, recordI // identifiers. // // API reference: https://api.cloudflare.com/#dns-records-for-a-zone-update-dns-record -func (api *API) UpdateDNSRecord(ctx context.Context, rc *ResourceContainer, recordID string, rr DNSRecord) error { +func (api *API) UpdateDNSRecord(ctx context.Context, rc *ResourceContainer, params UpdateDNSRecordParams) error { if rc.Identifier == "" { return ErrMissingZoneID } - rr.Name = toUTS46ASCII(rr.Name) + + params.Name = toUTS46ASCII(params.Name) // Populate the record name from the existing one if the update didn't // specify it. - if rr.Name == "" || rr.Type == "" { - rec, err := api.GetDNSRecord(ctx, rc, recordID) + if params.Name == "" || params.Type == "" { + rec, err := api.GetDNSRecord(ctx, rc, params.ID) if err != nil { return err } - if rr.Name == "" { - rr.Name = rec.Name + if params.Name == "" { + params.Name = rec.Name } - if rr.Type == "" { - rr.Type = rec.Type + if params.Type == "" { + params.Type = rec.Type } } - uri := fmt.Sprintf("/zones/%s/dns_records/%s", rc.Identifier, recordID) - res, err := api.makeRequestContext(ctx, http.MethodPatch, uri, rr) + + uri := fmt.Sprintf("/zones/%s/dns_records/%s", rc.Identifier, params.ID) + res, err := api.makeRequestContext(ctx, http.MethodPatch, uri, params) if err != nil { return err } diff --git a/dns_example_test.go b/dns_example_test.go index b84d558050c..e864a16fea0 100644 --- a/dns_example_test.go +++ b/dns_example_test.go @@ -20,7 +20,7 @@ func ExampleAPI_ListDNSRecords_all() { } // Fetch all records for a zone - recs, _, err := api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), cloudflare.DNSRecord{}, cloudflare.ListDNSParameters{}) + recs, _, err := api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), cloudflare.ListDNSRecordsParams{}) if err != nil { log.Fatal(err) } @@ -41,9 +41,7 @@ func ExampleAPI_ListDNSRecords_filterByContent() { log.Fatal(err) } - // Fetch only records whose content is 198.51.100.1 - localhost := cloudflare.DNSRecord{Content: "198.51.100.1"} - recs, _, err := api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), localhost, cloudflare.ListDNSParameters{}) + recs, _, err := api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), cloudflare.ListDNSRecordsParams{Content: "198.51.100.1"}) if err != nil { log.Fatal(err) } @@ -64,10 +62,7 @@ func ExampleAPI_ListDNSRecords_filterByName() { log.Fatal(err) } - // Fetch records of any type with name "foo.example.com" - // The name must be fully-qualified - foo := cloudflare.DNSRecord{Name: "foo.example.com"} - recs, _, err := api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), foo, cloudflare.ListDNSParameters{}) + recs, _, err := api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), cloudflare.ListDNSRecordsParams{Name: "foo.example.com"}) if err != nil { log.Fatal(err) } @@ -88,9 +83,7 @@ func ExampleAPI_ListDNSRecords_filterByType() { log.Fatal(err) } - // Fetch only AAAA type records - aaaa := cloudflare.DNSRecord{Type: "AAAA"} - recs, _, err := api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), aaaa, cloudflare.ListDNSParameters{}) + recs, _, err := api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), cloudflare.ListDNSRecordsParams{Type: "AAAA"}) if err != nil { log.Fatal(err) } diff --git a/dns_test.go b/dns_test.go index e317366b34d..08a55e4eb85 100644 --- a/dns_test.go +++ b/dns_test.go @@ -68,14 +68,6 @@ func TestCreateDNSRecord(t *testing.T) { priority := uint16(10) proxied := false - unicodeInput := DNSRecord{ - Type: "A", - Name: "😺.example.com", - Content: "198.51.100.4", - TTL: 120, - Priority: &priority, - Proxied: &proxied, - } asciiInput := DNSRecord{ Type: "A", Name: "xn--138h.example.com", @@ -146,9 +138,16 @@ func TestCreateDNSRecord(t *testing.T) { Response: Response{Success: true, Errors: []ResponseInfo{}, Messages: []ResponseInfo{}}, } - _, err := client.CreateDNSRecord(context.Background(), ZoneIdentifier(""), unicodeInput) + _, err := client.CreateDNSRecord(context.Background(), ZoneIdentifier(""), CreateDNSRecordParams{}) assert.Equal(t, ErrMissingZoneID, err) - actual, err := client.CreateDNSRecord(context.Background(), ZoneIdentifier(testZoneID), unicodeInput) + + actual, err := client.CreateDNSRecord(context.Background(), ZoneIdentifier(testZoneID), CreateDNSRecordParams{ + Type: "A", + Name: "😺.example.com", + Content: "198.51.100.4", + TTL: 120, + Priority: &priority, + Proxied: &proxied}) require.NoError(t, err) assert.Equal(t, want, actual) @@ -158,11 +157,6 @@ func TestDNSRecords(t *testing.T) { setup() defer teardown() - unicodeInput := DNSRecord{ - Name: "😺.example.com", - Type: "A", - Content: "198.51.100.4", - } asciiInput := DNSRecord{ Name: "xn--138h.example.com", Type: "A", @@ -235,10 +229,14 @@ func TestDNSRecords(t *testing.T) { }, }} - _, _, err := client.ListDNSRecords(context.Background(), ZoneIdentifier(""), unicodeInput, ListDNSParameters{}) + _, _, err := client.ListDNSRecords(context.Background(), ZoneIdentifier(""), ListDNSRecordsParams{}) assert.Equal(t, ErrMissingZoneID, err) - actual, _, err := client.ListDNSRecords(context.Background(), ZoneIdentifier(testZoneID), unicodeInput, ListDNSParameters{}) + actual, _, err := client.ListDNSRecords(context.Background(), ZoneIdentifier(testZoneID), ListDNSRecordsParams{ + Name: "😺.example.com", + Type: "A", + Content: "198.51.100.4", + }) require.NoError(t, err) assert.Equal(t, want, actual) @@ -326,13 +324,16 @@ func TestDNSRecordsSearch(t *testing.T) { Tags: []string{"tag1", "tag2extended"}, }} - actual, resultInfo, err := client.ListDNSRecords(context.Background(), ZoneIdentifier(testZoneID), recordInput, ListDNSParameters{ + actual, resultInfo, err := client.ListDNSRecords(context.Background(), ZoneIdentifier(testZoneID), ListDNSRecordsParams{ ResultInfo: ResultInfo{ Page: 1, }, Match: "all", Order: "type", Direction: ListDirectionAsc, + Name: "example.com", + Type: "A", + Content: "198.51.100.4", }) require.NoError(t, err) assert.Equal(t, 2000, resultInfo.Total) @@ -419,6 +420,7 @@ func TestUpdateDNSRecord(t *testing.T) { proxied := false input := DNSRecord{ + ID: "372e67954025e0ba6aaa6d586b9e0b59", Type: "A", Name: "example.com", Content: "198.51.100.4", @@ -465,10 +467,17 @@ func TestUpdateDNSRecord(t *testing.T) { mux.HandleFunc("/zones/"+testZoneID+"/dns_records/"+dnsRecordID, handler) - err := client.UpdateDNSRecord(context.Background(), ZoneIdentifier(""), dnsRecordID, input) + err := client.UpdateDNSRecord(context.Background(), ZoneIdentifier(""), UpdateDNSRecordParams{}) assert.Equal(t, ErrMissingZoneID, err) - err = client.UpdateDNSRecord(context.Background(), ZoneIdentifier(testZoneID), dnsRecordID, input) + err = client.UpdateDNSRecord(context.Background(), ZoneIdentifier(testZoneID), UpdateDNSRecordParams{ + ID: dnsRecordID, + Type: "A", + Name: "example.com", + Content: "198.51.100.4", + TTL: 120, + Proxied: &proxied, + }) require.NoError(t, err) } @@ -479,6 +488,7 @@ func TestUpdateDNSRecordWithoutName(t *testing.T) { proxied := false asciiInput := DNSRecord{ + ID: "372e67954025e0ba6aaa6d586b9e0b59", Name: "xn--138h.example.com", Type: "A", Content: "198.51.100.4", @@ -486,14 +496,6 @@ func TestUpdateDNSRecordWithoutName(t *testing.T) { Proxied: &proxied, } - unicodeInput := DNSRecord{ - Name: "😺.example.com", - Type: "A", - Content: "198.51.100.4", - TTL: 120, - Proxied: &proxied, - } - handleUpdateDNSRecord := func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, http.MethodPatch, r.Method, "Expected method 'PATCH', got %s", r.Method) @@ -577,10 +579,17 @@ func TestUpdateDNSRecordWithoutName(t *testing.T) { mux.HandleFunc("/zones/"+testZoneID+"/dns_records/"+dnsRecordID, handler) - err := client.UpdateDNSRecord(context.Background(), ZoneIdentifier(""), dnsRecordID, unicodeInput) + err := client.UpdateDNSRecord(context.Background(), ZoneIdentifier(""), UpdateDNSRecordParams{}) assert.Equal(t, ErrMissingZoneID, err) - err = client.UpdateDNSRecord(context.Background(), ZoneIdentifier(testZoneID), dnsRecordID, unicodeInput) + err = client.UpdateDNSRecord(context.Background(), ZoneIdentifier(testZoneID), UpdateDNSRecordParams{ + ID: dnsRecordID, + Type: "A", + Name: "xn--138h.example.com", + Content: "198.51.100.4", + TTL: 120, + Proxied: &proxied, + }) require.NoError(t, err) } @@ -590,19 +599,13 @@ func TestUpdateDNSRecordWithoutType(t *testing.T) { proxied := false - unicodeInput := DNSRecord{ - Name: "😺.example.com", - Content: "198.51.100.4", - TTL: 120, - Proxied: &proxied, - } - completedASCIIInput := DNSRecord{ Name: "xn--138h.example.com", Type: "A", Content: "198.51.100.4", TTL: 120, Proxied: &proxied, + ID: "372e67954025e0ba6aaa6d586b9e0b59", } handleUpdateDNSRecord := func(w http.ResponseWriter, r *http.Request) { @@ -688,7 +691,13 @@ func TestUpdateDNSRecordWithoutType(t *testing.T) { mux.HandleFunc("/zones/"+testZoneID+"/dns_records/"+dnsRecordID, handler) - err := client.UpdateDNSRecord(context.Background(), ZoneIdentifier(testZoneID), dnsRecordID, unicodeInput) + err := client.UpdateDNSRecord(context.Background(), ZoneIdentifier(testZoneID), UpdateDNSRecordParams{ + ID: dnsRecordID, + Name: "😺.example.com", + Content: "198.51.100.4", + TTL: 120, + Proxied: &proxied, + }) require.NoError(t, err) } diff --git a/example_test.go b/example_test.go index 43e41ea608d..5c6cc83e4f6 100644 --- a/example_test.go +++ b/example_test.go @@ -28,7 +28,7 @@ func Example() { } // Fetch all DNS records for example.org - records, _, err := api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), cloudflare.DNSRecord{}, cloudflare.ListDNSParameters{}) + records, _, err := api.ListDNSRecords(context.Background(), cloudflare.ZoneIdentifier(zoneID), cloudflare.ListDNSRecordsParams{}) if err != nil { fmt.Println(err) return