From b76021dd3a19b2932f11ecbe6b3fea9328f1178a Mon Sep 17 00:00:00 2001 From: Niall <18366490+Niallfitzy1@users.noreply.github.com> Date: Wed, 16 Oct 2024 22:23:30 +0100 Subject: [PATCH 1/2] chore: fix creates, add integration test for root --- client.go | 8 ++++++- integration_test.go | 53 +++++++++++++++++++++++++++++++++++++++++++++ provider.go | 6 ++++- 3 files changed, 65 insertions(+), 2 deletions(-) diff --git a/client.go b/client.go index e4d4557..8db600d 100644 --- a/client.go +++ b/client.go @@ -55,7 +55,13 @@ func (p *Provider) getMatchingRecord(r libdns.Record, zone string) ([]libdns.Rec if err != nil { return recs, err } - endpoint := fmt.Sprintf("/dns/retrieveByNameType/%s/%s/%s", trimmedZone, r.Type, r.Name) + + trimmedName := r.Name + if trimmedName == "@" { + trimmedName = "" + } + + endpoint := fmt.Sprintf("/dns/retrieveByNameType/%s/%s/%s", trimmedZone, r.Type, trimmedName) response, err := MakeApiRequest(endpoint, bytes.NewReader(credentialJson), pkbnRecordsResponse{}) if err != nil { diff --git a/integration_test.go b/integration_test.go index db53093..728c4ba 100644 --- a/integration_test.go +++ b/integration_test.go @@ -44,6 +44,39 @@ func createOrGetTestRecord(t *testing.T, provider Provider, zone string) libdns. if err != nil { t.Error(err) + t.Fail() + } + + if len(appendedRecords) != 1 { + t.Errorf("Incorrect amount of records %d created", len(appendedRecords)) + } + + testRecord = appendedRecords[0] + } + + return testRecord +} + +func createOrGetRootRecord(t *testing.T, provider Provider, zone string) libdns.Record { + if testRecord.ID == "" { + testValue := "test-value" + ttl := time.Duration(600 * time.Second) + recordType := "TXT" + testFullName := "@" + + //Create record + appendedRecords, err := provider.AppendRecords(context.TODO(), zone, []libdns.Record{ + { + Type: recordType, + Name: testFullName, + TTL: ttl, + Value: testValue, + }, + }) + + if err != nil { + t.Error(err) + t.Fail() } if len(appendedRecords) != 1 { @@ -120,6 +153,26 @@ func TestProvider_AppendRecords(t *testing.T) { t.Logf("Created record: \n%v\n", createdRecord.ID) } +func TestProvider_AppendRootRecord(t *testing.T) { + provider, zone := getProvider(t) + + //Get records + initialRecords := getInitialRecords(t, provider, zone) + + createdRecord := createOrGetRootRecord(t, provider, zone) + //Get records + postCreatedRecords, err := provider.GetRecords(context.TODO(), zone) + if err != nil { + t.Error(err) + } + + if len(postCreatedRecords) != len(initialRecords)+1 { + t.Errorf("Additional record not created") + } + + t.Logf("Created record: \n%v\n", createdRecord.ID) +} + func TestProvider_UpdateRecordsById(t *testing.T) { provider, zone := getProvider(t) diff --git a/provider.go b/provider.go index 65feb15..8a850c6 100644 --- a/provider.go +++ b/provider.go @@ -57,7 +57,11 @@ func (p *Provider) AppendRecords(_ context.Context, zone string, records []libdn record.TTL = 600 * time.Second } ttlInSeconds := int(record.TTL / time.Second) - trimmedName := libdns.RelativeName(record.Name, zone) + relativeName := libdns.RelativeName(record.Name, zone) + trimmedName := relativeName + if relativeName == "@" { + trimmedName = "" + } reqBody := pkbnRecordPayload{&credentials, record.Value, trimmedName, strconv.Itoa(ttlInSeconds), record.Type} reqJson, err := json.Marshal(reqBody) From 2bef06f647acca9a80d6f2f52d356bd79baa4492 Mon Sep 17 00:00:00 2001 From: Niall <18366490+Niallfitzy1@users.noreply.github.com> Date: Wed, 16 Oct 2024 22:57:32 +0100 Subject: [PATCH 2/2] chore: fix updates too --- client.go | 11 +++++-- integration_test.go | 80 +++++++++++++++++++++++++++++++-------------- 2 files changed, 63 insertions(+), 28 deletions(-) diff --git a/client.go b/client.go index 8db600d..8b4e08b 100644 --- a/client.go +++ b/client.go @@ -56,8 +56,9 @@ func (p *Provider) getMatchingRecord(r libdns.Record, zone string) ([]libdns.Rec return recs, err } - trimmedName := r.Name - if trimmedName == "@" { + relativeName := libdns.RelativeName(r.Name, zone) + trimmedName := relativeName + if relativeName == "@" { trimmedName = "" } @@ -87,7 +88,11 @@ func (p *Provider) updateRecords(_ context.Context, zone string, records []libdn record.TTL = 600 * time.Second } ttlInSeconds := int(record.TTL / time.Second) - trimmedName := libdns.RelativeName(record.Name, zone) + relativeName := libdns.RelativeName(record.Name, zone) + trimmedName := relativeName + if relativeName == "@" { + trimmedName = "" + } reqBody := pkbnRecordPayload{&credentials, record.Value, trimmedName, strconv.Itoa(ttlInSeconds), record.Type} reqJson, err := json.Marshal(reqBody) diff --git a/integration_test.go b/integration_test.go index 728c4ba..006ce45 100644 --- a/integration_test.go +++ b/integration_test.go @@ -58,35 +58,31 @@ func createOrGetTestRecord(t *testing.T, provider Provider, zone string) libdns. } func createOrGetRootRecord(t *testing.T, provider Provider, zone string) libdns.Record { - if testRecord.ID == "" { - testValue := "test-value" - ttl := time.Duration(600 * time.Second) - recordType := "TXT" - testFullName := "@" - - //Create record - appendedRecords, err := provider.AppendRecords(context.TODO(), zone, []libdns.Record{ - { - Type: recordType, - Name: testFullName, - TTL: ttl, - Value: testValue, - }, - }) + testValue := "test-value" + ttl := time.Duration(600 * time.Second) + recordType := "CNAME" + testFullName := "@" - if err != nil { - t.Error(err) - t.Fail() - } + //Create record + appendedRecords, err := provider.AppendRecords(context.TODO(), zone, []libdns.Record{ + { + Type: recordType, + Name: testFullName, + TTL: ttl, + Value: testValue, + }, + }) - if len(appendedRecords) != 1 { - t.Errorf("Incorrect amount of records %d created", len(appendedRecords)) - } + if err != nil { + t.Error(err) + t.Fail() + } - testRecord = appendedRecords[0] + if len(appendedRecords) != 1 { + t.Errorf("Incorrect amount of records %d created", len(appendedRecords)) } - return testRecord + return appendedRecords[0] } func getProvider(t *testing.T) (Provider, string) { @@ -118,6 +114,7 @@ func TestProvider_CheckCredentials(t *testing.T) { if err != nil { t.Error(err) + t.Fatal() } } @@ -153,7 +150,7 @@ func TestProvider_AppendRecords(t *testing.T) { t.Logf("Created record: \n%v\n", createdRecord.ID) } -func TestProvider_AppendRootRecord(t *testing.T) { +func TestProvider_ModifyRootRecord(t *testing.T) { provider, zone := getProvider(t) //Get records @@ -171,6 +168,39 @@ func TestProvider_AppendRootRecord(t *testing.T) { } t.Logf("Created record: \n%v\n", createdRecord.ID) + + updatedTestValue := "updated-test-value" + // Update record + updatedRecords, err := provider.SetRecords(context.TODO(), zone, []libdns.Record{ + { + ID: createdRecord.ID, + Type: "CNAME", + Name: "@", + TTL: time.Duration(600 * time.Second), + Value: updatedTestValue, + }, + }) + + if err != nil { + t.Error(err) + } + + if len(updatedRecords) != 1 { + t.Logf("Incorrect amount of records changed") + } + + t.Logf("Updated root record: \n%v\n", updatedRecords[0]) + + deleteRecords, err := provider.DeleteRecords(context.TODO(), zone, []libdns.Record{createdRecord}) + if err != nil { + t.Error(err) + } + + if len(deleteRecords) != 1 { + t.Errorf("Deleted incorrect amount of records %d", len(deleteRecords)) + } + + t.Logf("Deleted record: \n%v\n", deleteRecords[0]) } func TestProvider_UpdateRecordsById(t *testing.T) {