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

chore: support mutating & querying root (@) records #12

Merged
merged 2 commits into from
Oct 16, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ 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)

relativeName := libdns.RelativeName(r.Name, zone)
trimmedName := relativeName
if relativeName == "@" {
trimmedName = ""
}

endpoint := fmt.Sprintf("/dns/retrieveByNameType/%s/%s/%s", trimmedZone, r.Type, trimmedName)
response, err := MakeApiRequest(endpoint, bytes.NewReader(credentialJson), pkbnRecordsResponse{})

if err != nil {
Expand All @@ -81,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)
Expand Down
83 changes: 83 additions & 0 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func createOrGetTestRecord(t *testing.T, provider Provider, zone string) libdns.

if err != nil {
t.Error(err)
t.Fail()
}

if len(appendedRecords) != 1 {
Expand All @@ -56,6 +57,34 @@ func createOrGetTestRecord(t *testing.T, provider Provider, zone string) libdns.
return testRecord
}

func createOrGetRootRecord(t *testing.T, provider Provider, zone string) libdns.Record {
testValue := "test-value"
ttl := time.Duration(600 * time.Second)
recordType := "CNAME"
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 {
t.Errorf("Incorrect amount of records %d created", len(appendedRecords))
}

return appendedRecords[0]
}

func getProvider(t *testing.T) (Provider, string) {
envErr := godotenv.Load()
if envErr != nil {
Expand Down Expand Up @@ -85,6 +114,7 @@ func TestProvider_CheckCredentials(t *testing.T) {

if err != nil {
t.Error(err)
t.Fatal()
}
}

Expand Down Expand Up @@ -120,6 +150,59 @@ func TestProvider_AppendRecords(t *testing.T) {
t.Logf("Created record: \n%v\n", createdRecord.ID)
}

func TestProvider_ModifyRootRecord(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)

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) {
provider, zone := getProvider(t)

Expand Down
6 changes: 5 additions & 1 deletion provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down