Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Create/UpdateDNSRecord method signatures #1243

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/1243.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:breaking-change
dns: Changed Create/UpdateDNSRecord method signatures to return (DNSRecord, error)
```
24 changes: 7 additions & 17 deletions cmd/flarectl/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ func dnsCreate(c *cli.Context) error {
Proxied: &proxy,
Priority: &priority,
}
resp, err := api.CreateDNSRecord(context.Background(), cloudflare.ZoneIdentifier(zoneID), record)
result, err := api.CreateDNSRecord(context.Background(), cloudflare.ZoneIdentifier(zoneID), record)
if err != nil {
fmt.Fprintln(os.Stderr, "Error creating DNS record: ", err)
return err
}

output := [][]string{
formatDNSRecord(resp.Result),
formatDNSRecord(result),
}

writeTable(c, output, "ID", "Name", "Type", "Content", "TTL", "Proxiable", "Proxy", "Locked")
Expand Down Expand Up @@ -90,7 +90,7 @@ func dnsCreateOrUpdate(c *cli.Context) error {
return err
}

var resp *cloudflare.DNSRecordResponse
var result cloudflare.DNSRecord
if len(records) > 0 {
// Record exists - find the ID and update it.
// This is imprecise without knowing the original content; if a label
Expand All @@ -105,21 +105,11 @@ func dnsCreateOrUpdate(c *cli.Context) error {
rr.Proxied = &proxy
rr.Priority = &priority

err := api.UpdateDNSRecord(context.Background(), cloudflare.ZoneIdentifier(zoneID), rr)
result, 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: cloudflare.DNSRecord{
ID: rr.ID,
Name: rr.Name,
Type: rr.Type,
Content: rr.Content,
TTL: rr.TTL,
Proxied: &proxy,
},
}
}
}
} else {
Expand All @@ -131,15 +121,15 @@ func dnsCreateOrUpdate(c *cli.Context) error {
rr.Proxied = &proxy
rr.Priority = &priority
// TODO: Print the response.
resp, err = api.CreateDNSRecord(context.Background(), cloudflare.ZoneIdentifier(zoneID), rr)
result, err = api.CreateDNSRecord(context.Background(), cloudflare.ZoneIdentifier(zoneID), rr)
if err != nil {
fmt.Println("Error creating DNS record:", err)
return err
}
}

output := [][]string{
formatDNSRecord(resp.Result),
formatDNSRecord(result),
}

writeTable(c, output, "ID", "Name", "Type", "Content", "TTL", "Proxiable", "Proxy", "Locked")
Expand Down Expand Up @@ -176,7 +166,7 @@ func dnsUpdate(c *cli.Context) error {
Proxied: &proxy,
Priority: &priority,
}
err = api.UpdateDNSRecord(context.Background(), cloudflare.ZoneIdentifier(zoneID), record)
_, err = api.UpdateDNSRecord(context.Background(), cloudflare.ZoneIdentifier(zoneID), record)
if err != nil {
fmt.Fprintln(os.Stderr, "Error updating DNS record: ", err)
return err
Expand Down
28 changes: 15 additions & 13 deletions dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,25 +127,25 @@ type CreateDNSRecordParams struct {
// 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, params CreateDNSRecordParams) (*DNSRecordResponse, error) {
func (api *API) CreateDNSRecord(ctx context.Context, rc *ResourceContainer, params CreateDNSRecordParams) (DNSRecord, error) {
if rc.Identifier == "" {
return nil, ErrMissingZoneID
return DNSRecord{}, ErrMissingZoneID
}
params.Name = toUTS46ASCII(params.Name)

uri := fmt.Sprintf("/zones/%s/dns_records", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, params)
if err != nil {
return nil, err
return DNSRecord{}, err
}

var recordResp *DNSRecordResponse
err = json.Unmarshal(res, &recordResp)
if err != nil {
return nil, fmt.Errorf("%s: %w", errUnmarshalError, err)
return DNSRecord{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}

return recordResp, nil
return recordResp.Result, nil
}

// ListDNSRecords returns a slice of DNS records for the given zone identifier.
Expand Down Expand Up @@ -227,28 +227,30 @@ 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, params UpdateDNSRecordParams) error {
func (api *API) UpdateDNSRecord(ctx context.Context, rc *ResourceContainer, params UpdateDNSRecordParams) (DNSRecord, error) {
if rc.Identifier == "" {
return ErrMissingZoneID
return DNSRecord{}, ErrMissingZoneID
}

if params.ID == "" {
return ErrMissingDNSRecordID
return DNSRecord{}, ErrMissingDNSRecordID
}

params.Name = toUTS46ASCII(params.Name)

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
return DNSRecord{}, err
}
var r DNSRecordResponse
err = json.Unmarshal(res, &r)

var recordResp *DNSRecordResponse
err = json.Unmarshal(res, &recordResp)
if err != nil {
return fmt.Errorf("%s: %w", errUnmarshalError, err)
return DNSRecord{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return nil

return recordResp.Result, nil
}

// DeleteDNSRecord deletes a single DNS record for the given zone & record
Expand Down
43 changes: 20 additions & 23 deletions dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,26 +116,23 @@ func TestCreateDNSRecord(t *testing.T) {

createdOn, _ := time.Parse(time.RFC3339, "2014-01-01T05:20:00Z")
modifiedOn, _ := time.Parse(time.RFC3339, "2014-01-01T05:20:00Z")
want := &DNSRecordResponse{
Result: DNSRecord{
ID: "372e67954025e0ba6aaa6d586b9e0b59",
Type: asciiInput.Type,
Name: asciiInput.Name,
Content: asciiInput.Content,
Proxiable: true,
Proxied: asciiInput.Proxied,
TTL: asciiInput.TTL,
ZoneID: testZoneID,
ZoneName: "example.com",
CreatedOn: createdOn,
ModifiedOn: modifiedOn,
Data: map[string]interface{}{},
Meta: map[string]interface{}{
"auto_added": true,
"source": "primary",
},
want := DNSRecord{
ID: "372e67954025e0ba6aaa6d586b9e0b59",
Type: asciiInput.Type,
Name: asciiInput.Name,
Content: asciiInput.Content,
Proxiable: true,
Proxied: asciiInput.Proxied,
TTL: asciiInput.TTL,
ZoneID: testZoneID,
ZoneName: "example.com",
CreatedOn: createdOn,
ModifiedOn: modifiedOn,
Data: map[string]interface{}{},
Meta: map[string]interface{}{
"auto_added": true,
"source": "primary",
},
Response: Response{Success: true, Errors: []ResponseInfo{}, Messages: []ResponseInfo{}},
}

_, err := client.CreateDNSRecord(context.Background(), ZoneIdentifier(""), CreateDNSRecordParams{})
Expand Down Expand Up @@ -549,13 +546,13 @@ func TestUpdateDNSRecord(t *testing.T) {

mux.HandleFunc("/zones/"+testZoneID+"/dns_records/"+dnsRecordID, handler)

err := client.UpdateDNSRecord(context.Background(), ZoneIdentifier(""), UpdateDNSRecordParams{ID: dnsRecordID})
_, err := client.UpdateDNSRecord(context.Background(), ZoneIdentifier(""), UpdateDNSRecordParams{ID: dnsRecordID})
assert.ErrorIs(t, err, ErrMissingZoneID)

err = client.UpdateDNSRecord(context.Background(), ZoneIdentifier(testZoneID), UpdateDNSRecordParams{})
_, err = client.UpdateDNSRecord(context.Background(), ZoneIdentifier(testZoneID), UpdateDNSRecordParams{})
assert.ErrorIs(t, err, ErrMissingDNSRecordID)

err = client.UpdateDNSRecord(context.Background(), ZoneIdentifier(testZoneID), UpdateDNSRecordParams{
_, err = client.UpdateDNSRecord(context.Background(), ZoneIdentifier(testZoneID), UpdateDNSRecordParams{
ID: dnsRecordID,
Type: "A",
Name: "😺.example.com",
Expand Down Expand Up @@ -617,7 +614,7 @@ func TestUpdateDNSRecord_ClearComment(t *testing.T) {

mux.HandleFunc("/zones/"+testZoneID+"/dns_records/"+dnsRecordID, handler)

err := client.UpdateDNSRecord(context.Background(), ZoneIdentifier(testZoneID), UpdateDNSRecordParams{
_, err := client.UpdateDNSRecord(context.Background(), ZoneIdentifier(testZoneID), UpdateDNSRecordParams{
ID: dnsRecordID,
Comment: "",
})
Expand Down