Skip to content

Commit

Permalink
Handle case where ZoneRecord name can be non present
Browse files Browse the repository at this point in the history
Closes GH-33
  • Loading branch information
weppos committed May 2, 2020
1 parent 4452245 commit b357415
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 15 deletions.
8 changes: 8 additions & 0 deletions dnsimple/dnsimple.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,11 @@ func addURLQueryOptions(path string, options interface{}) (string, error) {

return u.String(), nil
}

// Int64P is a helper routine that allocates a new int64 value
// to store v and returns a pointer to it.
func Int64P(v int64) *int64 { return &v }

// StringP is a helper routine that allocates a new string value
// to store v and returns a pointer to it.
func StringP(v string) *string { return &v }
3 changes: 2 additions & 1 deletion dnsimple/live_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ func TestLive_Zones(t *testing.T) {
}

zoneName := domainResponse.Data.Name
recordResponse, err := dnsimpleClient.Zones.CreateRecord(context.Background(), accountID, zoneName, ZoneRecord{Name: fmt.Sprintf("%v", time.Now().Unix()), Type: "TXT", Content: "Test"})
recordName := fmt.Sprintf("%v", time.Now().Unix())
recordResponse, err := dnsimpleClient.Zones.CreateRecord(context.Background(), accountID, zoneName, ZoneRecordAttributes{Name: &recordName, Type: "TXT", Content: "Test"})
if err != nil {
t.Fatalf("Live Zones/CreateRecord() returned error: %v", err)
}
Expand Down
21 changes: 18 additions & 3 deletions dnsimple/zones_records.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
)

// ZoneRecord represents a DNS record in DNSimple.
// ZoneRecord represents a zone record in DNSimple.
type ZoneRecord struct {
ID int64 `json:"id,omitempty"`
ZoneID string `json:"zone_id,omitempty"`
Expand All @@ -21,6 +21,21 @@ type ZoneRecord struct {
UpdatedAt string `json:"updated_at,omitempty"`
}

// ZoneRecordAttributes represents the attributes you can send to create/update a zone record.
//
// Compared to most other calls in this library, you should not use ZoneRecord as payload for record calls.
// This is because it can lead to side effects due to the inability of go to distinguish between a non-present string
// and an empty string. Name can be both, therefore a specific struct is required.
type ZoneRecordAttributes struct {
ZoneID string `json:"zone_id,omitempty"`
Type string `json:"type,omitempty"`
Name *string `json:"name"`
Content string `json:"content,omitempty"`
TTL int `json:"ttl,omitempty"`
Priority int `json:"priority,omitempty"`
Regions []string `json:"regions,omitempty"`
}

func zoneRecordPath(accountID string, zoneName string, recordID int64) (path string) {
path = fmt.Sprintf("/%v/zones/%v/records", accountID, zoneName)
if recordID != 0 {
Expand Down Expand Up @@ -81,7 +96,7 @@ func (s *ZonesService) ListRecords(ctx context.Context, accountID string, zoneNa
// CreateRecord creates a zone record.
//
// See https://developer.dnsimple.com/v2/zones/records/#createZoneRecord
func (s *ZonesService) CreateRecord(ctx context.Context, accountID string, zoneName string, recordAttributes ZoneRecord) (*ZoneRecordResponse, error) {
func (s *ZonesService) CreateRecord(ctx context.Context, accountID string, zoneName string, recordAttributes ZoneRecordAttributes) (*ZoneRecordResponse, error) {
path := versioned(zoneRecordPath(accountID, zoneName, 0))
recordResponse := &ZoneRecordResponse{}

Expand Down Expand Up @@ -113,7 +128,7 @@ func (s *ZonesService) GetRecord(ctx context.Context, accountID string, zoneName
// UpdateRecord updates a zone record.
//
// See https://developer.dnsimple.com/v2/zones/records/#updateZoneRecord
func (s *ZonesService) UpdateRecord(ctx context.Context, accountID string, zoneName string, recordID int64, recordAttributes ZoneRecord) (*ZoneRecordResponse, error) {
func (s *ZonesService) UpdateRecord(ctx context.Context, accountID string, zoneName string, recordID int64, recordAttributes ZoneRecordAttributes) (*ZoneRecordResponse, error) {
path := versioned(zoneRecordPath(accountID, zoneName, recordID))
recordResponse := &ZoneRecordResponse{}
resp, err := s.client.patch(ctx, path, recordAttributes, recordResponse)
Expand Down
28 changes: 17 additions & 11 deletions dnsimple/zones_records_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestZonesService_CreateRecord(t *testing.T) {
})

accountID := "1010"
recordValues := ZoneRecord{Name: "foo", Content: "mxa.example.com", Type: "MX"}
recordValues := ZoneRecordAttributes{Name: StringP("foo"), Content: "mxa.example.com", Type: "MX"}

recordResponse, err := client.Zones.CreateRecord(context.Background(), accountID, "example.com", recordValues)
if err != nil {
Expand Down Expand Up @@ -141,7 +141,7 @@ func TestZonesService_CreateRecord_BlankName(t *testing.T) {
io.Copy(w, httpResponse.Body)
})

recordValues := ZoneRecord{Name: "", Content: "127.0.0.1", Type: "A"}
recordValues := ZoneRecordAttributes{Name: StringP(""), Content: "127.0.0.1", Type: "A"}

recordResponse, err := client.Zones.CreateRecord(context.Background(), "1010", "example.com", recordValues)
if err != nil {
Expand All @@ -161,7 +161,7 @@ func TestZonesService_CreateRecord_Regions(t *testing.T) {
setupMockServer()
defer teardownMockServer()

var recordValues ZoneRecord
var recordValues ZoneRecordAttributes

mux.HandleFunc("/v2/1/zones/example.com/records", func(w http.ResponseWriter, r *http.Request) {
httpResponse := httpResponseFixture(t, "/api/createZoneRecord/created.http")
Expand All @@ -173,7 +173,8 @@ func TestZonesService_CreateRecord_Regions(t *testing.T) {
io.Copy(w, httpResponse.Body)
})

recordValues = ZoneRecord{Name: "foo", Regions: []string{}}
recordValues = ZoneRecordAttributes{Name: StringP("foo"), Regions: []string{}}

if _, err := client.Zones.CreateRecord(context.Background(), "1", "example.com", recordValues); err != nil {
t.Fatalf("Zones.CreateRecord() returned error: %v", err)
}
Expand All @@ -188,7 +189,8 @@ func TestZonesService_CreateRecord_Regions(t *testing.T) {
io.Copy(w, httpResponse.Body)
})

recordValues = ZoneRecord{Name: "foo", Regions: []string{"global"}}
recordValues = ZoneRecordAttributes{Name: StringP("foo"), Regions: []string{"global"}}

if _, err := client.Zones.CreateRecord(context.Background(), "2", "example.com", recordValues); err != nil {
t.Fatalf("Zones.CreateRecord() returned error: %v", err)
}
Expand All @@ -203,7 +205,8 @@ func TestZonesService_CreateRecord_Regions(t *testing.T) {
io.Copy(w, httpResponse.Body)
})

recordValues = ZoneRecord{Name: "foo", Regions: []string{"global"}}
recordValues = ZoneRecordAttributes{Name: StringP("foo"), Regions: []string{"global"}}

if _, err := client.Zones.CreateRecord(context.Background(), "2", "example.com", recordValues); err != nil {
t.Fatalf("Zones.CreateRecord() returned error: %v", err)
}
Expand Down Expand Up @@ -268,7 +271,7 @@ func TestZonesService_UpdateRecord(t *testing.T) {
})

accountID := "1010"
recordValues := ZoneRecord{Name: "foo", Content: "127.0.0.1"}
recordValues := ZoneRecordAttributes{Name: StringP("foo"), Content: "127.0.0.1"}

recordResponse, err := client.Zones.UpdateRecord(context.Background(), accountID, "example.com", 5, recordValues)
if err != nil {
Expand All @@ -288,7 +291,7 @@ func TestZonesService_UpdateRecord_Regions(t *testing.T) {
setupMockServer()
defer teardownMockServer()

var recordValues ZoneRecord
var recordValues ZoneRecordAttributes

mux.HandleFunc("/v2/1/zones/example.com/records/1", func(w http.ResponseWriter, r *http.Request) {
httpResponse := httpResponseFixture(t, "/api/updateZoneRecord/success.http")
Expand All @@ -300,7 +303,8 @@ func TestZonesService_UpdateRecord_Regions(t *testing.T) {
io.Copy(w, httpResponse.Body)
})

recordValues = ZoneRecord{Name: "foo", Regions: []string{}}
recordValues = ZoneRecordAttributes{Name: StringP("foo"), Regions: []string{}}

if _, err := client.Zones.UpdateRecord(context.Background(), "1", "example.com", 1, recordValues); err != nil {
t.Fatalf("Zones.UpdateRecord() returned error: %v", err)
}
Expand All @@ -315,7 +319,8 @@ func TestZonesService_UpdateRecord_Regions(t *testing.T) {
io.Copy(w, httpResponse.Body)
})

recordValues = ZoneRecord{Name: "foo", Regions: []string{"global"}}
recordValues = ZoneRecordAttributes{Name: StringP("foo"), Regions: []string{"global"}}

if _, err := client.Zones.UpdateRecord(context.Background(), "2", "example.com", 1, recordValues); err != nil {
t.Fatalf("Zones.UpdateRecord() returned error: %v", err)
}
Expand All @@ -330,7 +335,8 @@ func TestZonesService_UpdateRecord_Regions(t *testing.T) {
io.Copy(w, httpResponse.Body)
})

recordValues = ZoneRecord{Name: "foo", Regions: []string{"global"}}
recordValues = ZoneRecordAttributes{Name: StringP("foo"), Regions: []string{"global"}}

if _, err := client.Zones.UpdateRecord(context.Background(), "2", "example.com", 1, recordValues); err != nil {
t.Fatalf("Zones.UpdateRecord() returned error: %v", err)
}
Expand Down

0 comments on commit b357415

Please sign in to comment.