Skip to content

Commit

Permalink
apis/zones: add function to add multiple ResourceRecordSets
Browse files Browse the repository at this point in the history
  • Loading branch information
Veratil committed Aug 26, 2024
1 parent ef28a4a commit ae05536
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
4 changes: 4 additions & 0 deletions apis/zones/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ type Client interface {
// the exact name/type combination will be replaced.
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
16 changes: 15 additions & 1 deletion apis/zones/zones_addrecordset.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ 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 {
Expand All @@ -19,3 +20,16 @@ func (c *client) AddRecordSetToZone(ctx context.Context, serverID string, zoneID

return c.httpClient.Patch(ctx, path, nil, pdnshttp.WithJSONRequestBody(&patch))
}

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))

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

return c.httpClient.Patch(ctx, path, nil, pdnshttp.WithJSONRequestBody(&patch))
}
41 changes: 41 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,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

0 comments on commit ae05536

Please sign in to comment.