Skip to content

Commit

Permalink
dont send TTL of zero (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
jandelgado authored Aug 5, 2021
1 parent 2b70a9d commit ffec74b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
20 changes: 15 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"

Expand Down Expand Up @@ -51,11 +50,22 @@ type record struct {
Name string `json:"name"`
Type string `json:"type"`
Content string `json:"content"`
TTL int `json:"ttl"`
TTL *int `json:"ttl,omitempty"`
Prio int `json:"prio"`
Disabled bool `json:"disabled,omitempty"` // TODO default=true
}

// IONOS does not accept TTL values < 60, and returns status 400. If the
// TTL is 0, we leave the field empty, by setting the struct value to nil.
func optTTL(ttl float64) *int {
var intTTL *int
if ttl > 0 {
tmp := int(ttl)
intTTL = &tmp
}
return intTTL
}

func doRequest(token string, request *http.Request) ([]byte, error) {
request.Header.Add("X-API-Key", token)
request.Header.Add("Content-Type", "application/json")
Expand Down Expand Up @@ -177,7 +187,7 @@ func createRecord(ctx context.Context, token string, zoneName string, r libdns.R
// IONOS: Name is fully qualified
Name: libdns.AbsoluteName(r.Name, zoneName),
Content: r.Value,
TTL: int(r.TTL.Seconds()),
TTL: optTTL(r.TTL.Seconds()),
}}

reqBuffer, err := json.Marshal(reqData)
Expand All @@ -199,7 +209,7 @@ func createRecord(ctx context.Context, token string, zoneName string, r libdns.R
if err != nil {
// Thats bad, the record was created, but we can not read it ?!
// in this case we just return an empty ID
log.Printf("ERROR could not find record: %+v", err)
// log.Printf("ERROR could not find record: %+v", err)
}

return libdns.Record{
Expand Down Expand Up @@ -238,7 +248,7 @@ func updateRecord(ctx context.Context, token string, zone string, r libdns.Recor
Type: r.Type,
Name: libdns.AbsoluteName(r.Name, zone),
Content: r.Value,
TTL: int(r.TTL.Seconds()),
TTL: optTTL(r.TTL.Seconds()),
}

reqBuffer, err := json.Marshal(reqData)
Expand Down
18 changes: 16 additions & 2 deletions provider_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// end-to-end test suite, using the original IONOS API service (i.e. no test
// doubles - be careful).
// set environment variables
// set environment variables
// LIBDNS_IONOS_TEST_TOKEN - API token
// LIBDNS_IONOS_TEST_ZONE - domain
// before running the test.
Expand Down Expand Up @@ -44,6 +44,11 @@ func setupTestRecords(t *testing.T, p *ionos.Provider) ([]libdns.Record, testRec
Name: "test3",
Value: "test3",
TTL: ttl,
}, {
Type: "TXT",
Name: "test4",
Value: "test4",
TTL: 0,
},
}

Expand Down Expand Up @@ -87,18 +92,21 @@ func Test_AppendRecords(t *testing.T) {

testCases := []struct {
records []libdns.Record
expected []libdns.Record}{
expected []libdns.Record
}{
{
// multiple records
records: []libdns.Record{
{Type: "TXT", Name: "test_1", Value: "test_1", TTL: ttl},
{Type: "TXT", Name: "test_2", Value: "test_2", TTL: ttl},
{Type: "TXT", Name: "test_3", Value: "test_3", TTL: ttl},
{Type: "TXT", Name: "test_4", Value: "test_3", TTL: 0},
},
expected: []libdns.Record{
{Type: "TXT", Name: "test_1", Value: "test_1", TTL: ttl},
{Type: "TXT", Name: "test_2", Value: "test_2", TTL: ttl},
{Type: "TXT", Name: "test_3", Value: "test_3", TTL: ttl},
{Type: "TXT", Name: "test_4", Value: "test_3", TTL: 0},
},
},
{
Expand Down Expand Up @@ -244,6 +252,12 @@ func Test_SetRecords(t *testing.T) {
Value: "new_test2",
TTL: ttl,
},
{
Type: "TXT",
Name: "new_test3",
Value: "new_test3",
TTL: 0,
},
}

allRecords := append(existingRecords, newTestRecords...)
Expand Down

0 comments on commit ffec74b

Please sign in to comment.