Skip to content

Commit

Permalink
Feature: Add function to zones API to add multiple ResourceRecordSets…
Browse files Browse the repository at this point in the history
… at once (#23)

* tests: use docker compose v2, update pdns-auth version

* apis/zones: add function to add multiple ResourceRecordSets

* apis/zones: deprecate AddRecordSetToZone
  • Loading branch information
Veratil authored Aug 29, 2024
1 parent fa48081 commit d57738d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 19 deletions.
6 changes: 6 additions & 0 deletions apis/zones/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ type Client interface {

// AddRecordSetToZone will add a new set of records to a zone. Existing record sets for
// the exact name/type combination will be replaced.
//
// Deprecated: Superceded by AddRecordSetsToZone
AddRecordSetToZone(ctx context.Context, serverID string, zoneID string, set ResourceRecordSet) error

// AddRecordSetsToZone will add new sets of records to a zone. Existing record sets for
// the exact name/type combination will be replaced.
AddRecordSetsToZone(ctx context.Context, serverID string, zoneID string, sets []ResourceRecordSet) error

// RemoveRecordSetFromZone removes a record set from a zone. The record set is matched
// by name and type.
RemoveRecordSetFromZone(ctx context.Context, serverID string, zoneID string, name string, recordType string) error
Expand Down
15 changes: 10 additions & 5 deletions apis/zones/zones_addrecordset.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@ package zones
import (
"context"
"fmt"
"github.com/mittwald/go-powerdns/pdnshttp"
"net/url"

"github.com/mittwald/go-powerdns/pdnshttp"
)

func (c *client) AddRecordSetToZone(ctx context.Context, serverID string, zoneID string, set ResourceRecordSet) error {
return c.AddRecordSetsToZone(ctx, serverID, zoneID, []ResourceRecordSet{set})
}

func (c *client) AddRecordSetsToZone(ctx context.Context, serverID string, zoneID string, sets []ResourceRecordSet) error {
path := fmt.Sprintf("/servers/%s/zones/%s", url.PathEscape(serverID), url.PathEscape(zoneID))

set.ChangeType = ChangeTypeReplace
for idx := range sets {
sets[idx].ChangeType = ChangeTypeReplace
}
patch := Zone{
ResourceRecordSets: []ResourceRecordSet{
set,
},
ResourceRecordSets: sets,
}

return c.httpClient.Patch(ctx, path, nil, pdnshttp.WithJSONRequestBody(&patch))
Expand Down
64 changes: 53 additions & 11 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import (
"context"
"flag"
"fmt"
"github.com/mittwald/go-powerdns/apis/search"
"github.com/mittwald/go-powerdns/apis/zones"
"github.com/mittwald/go-powerdns/pdnshttp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io"
"os"
"os/exec"
"testing"
"time"

"github.com/mittwald/go-powerdns/apis/search"
"github.com/mittwald/go-powerdns/apis/zones"
"github.com/mittwald/go-powerdns/pdnshttp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestMain(m *testing.M) {
Expand All @@ -24,12 +25,12 @@ func TestMain(m *testing.M) {
os.Exit(0)
}

runOrPanic("docker-compose", "rm", "-sfv")
runOrPanic("docker-compose", "down", "-v")
runOrPanic("docker-compose", "up", "-d")
runOrPanic("docker", "compose", "rm", "-sfv")
runOrPanic("docker", "compose", "down", "-v")
runOrPanic("docker", "compose", "up", "-d")

defer func() {
runOrPanic("docker-compose", "down", "-v")
runOrPanic("docker", "compose", "down", "-v")
}()

c, err := New(
Expand All @@ -53,7 +54,7 @@ func TestMain(m *testing.M) {
fmt.Println("Leaving containers running for further inspection")
fmt.Println("")
} else {
runOrPanic("docker-compose", "down", "-v")
runOrPanic("docker", "compose", "down", "-v")
}

os.Exit(e)
Expand Down Expand Up @@ -245,6 +246,47 @@ func TestAddRecordToZone(t *testing.T) {
require.NotNil(t, rs)
}

func TestAddRecordSetsToZone(t *testing.T) {
c := buildClient(t)

zone := zones.Zone{
Name: "example6.de.",
Type: zones.ZoneTypeZone,
Kind: zones.ZoneKindNative,
Nameservers: []string{
"ns1.example.com.",
"ns2.example.com.",
},
ResourceRecordSets: []zones.ResourceRecordSet{
{Name: "foo.example6.de.", Type: "A", TTL: 60, Records: []zones.Record{{Content: "127.0.0.1"}}},
},
}

ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
created, err := c.Zones().CreateZone(ctx, "localhost", zone)

require.Nil(t, err, "CreateZone returned error")

err = c.Zones().AddRecordSetsToZone(ctx, "localhost", created.ID,
[]zones.ResourceRecordSet{
{Name: "bar.example6.de.", Type: "A", TTL: 60, Records: []zones.Record{{Content: "127.0.0.2"}}},
{Name: "baz.example6.de.", Type: "A", TTL: 60, Records: []zones.Record{{Content: "127.0.0.3"}}},
},
)

require.Nil(t, err, "AddRecordSetsToZone returned error")

updated, err := c.Zones().GetZone(ctx, "localhost", created.ID)

require.Nil(t, err)

rs := updated.GetRecordSet("bar.example6.de.", "A")
require.NotNil(t, rs)
rs = updated.GetRecordSet("baz.example6.de.", "A")
require.NotNil(t, rs)
}

func TestSelectZoneWithoutRRSets(t *testing.T) {
c := buildClient(t)

Expand Down Expand Up @@ -410,7 +452,7 @@ func TestExportZone(t *testing.T) {

export, sErr := c.Zones().ExportZone(ctx, "localhost", created.ID)

date := time.Now().Format("20060102") + "01"
date := time.Now().UTC().Format("20060102") + "01"

require.Nil(t, sErr)
require.Equal(t, "example-export.de.\t60\tIN\tA\t127.0.0.1\nexample-export.de.\t3600\tIN\tNS\tns1.example.com.\nexample-export.de.\t3600\tIN\tNS\tns2.example.com.\nexample-export.de.\t3600\tIN\tSOA\ta.misconfigured.dns.server.invalid. hostmaster.example-export.de. "+date+" 10800 3600 604800 3600\n", string(export))
Expand Down
5 changes: 2 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
version: "3"
services:
powerdns:
image: powerdns/pdns-auth-46:4.6.3
image: powerdns/pdns-auth-49:4.9.1
environment:
PDNS_AUTH_API_KEY: secret
ports:
- 8081:8081
- 8053:53/udp
- 8053:53/tcp
- 8053:53/tcp

0 comments on commit d57738d

Please sign in to comment.