Skip to content

Commit

Permalink
Feature: Add function to zones API to remove multiple ResourceRecordS…
Browse files Browse the repository at this point in the history
…ets at once (#24)

* apis/zones: add function to remove multiple ResourceRecordSets

* apis/zones: deprecate RemoveRecordSetFromZone
  • Loading branch information
Veratil authored Sep 2, 2024
1 parent d57738d commit fe7832a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
6 changes: 6 additions & 0 deletions apis/zones/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@ type Client interface {

// RemoveRecordSetFromZone removes a record set from a zone. The record set is matched
// by name and type.
//
// Deprecated: Superceded by RemoveRecordSetsFromZone
RemoveRecordSetFromZone(ctx context.Context, serverID string, zoneID string, name string, recordType string) error

// RemoveRecordSetsFromZone removes record sets from a zone. The record sets are matched
// by name and type.
RemoveRecordSetsFromZone(ctx context.Context, serverID string, zoneID string, sets []ResourceRecordSet) error

// RetrieveFromMaster retrieves a slave zone from its master
RetrieveFromMaster(ctx context.Context, serverID string, zoneID string) error

Expand Down
16 changes: 12 additions & 4 deletions apis/zones/zones_removerecordset.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,29 @@ package zones
import (
"context"
"fmt"
"github.com/mittwald/go-powerdns/pdnshttp"
"net/url"

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

func (c *client) RemoveRecordSetFromZone(ctx context.Context, serverID string, zoneID string, name string, recordType string) error {
path := fmt.Sprintf("/servers/%s/zones/%s", url.PathEscape(serverID), url.PathEscape(zoneID))

set := ResourceRecordSet{
Name: name,
Type: recordType,
ChangeType: ChangeTypeDelete,
}

return c.RemoveRecordSetsFromZone(ctx, serverID, zoneID, []ResourceRecordSet{set})
}

func (c *client) RemoveRecordSetsFromZone(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 = ChangeTypeDelete
}
patch := Zone{
ResourceRecordSets: []ResourceRecordSet{set},
ResourceRecordSets: sets,
}

return c.httpClient.Patch(ctx, path, nil, pdnshttp.WithJSONRequestBody(&patch))
Expand Down
50 changes: 50 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,56 @@ func TestRemoveRecordFromZone(t *testing.T) {
require.Nil(t, rs)
}

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

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

ctx, cancel := context.WithTimeout(context.Background(), 20*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.example7.de.", Type: "A", TTL: 60, Records: []zones.Record{{Content: "127.0.0.2"}}},
{Name: "baz.example7.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)
rs1 := updated.GetRecordSet("bar.example7.de.", "A")
require.NotNil(t, rs1)
rs2 := updated.GetRecordSet("baz.example7.de.", "A")
require.NotNil(t, rs2)

err = c.Zones().RemoveRecordSetsFromZone(ctx, "localhost", created.ID, []zones.ResourceRecordSet{*rs1, *rs2})
require.Nil(t, err, "RemoveRecordSetsFromZone returned error")

updated, err = c.Zones().GetZone(ctx, "localhost", created.ID)
require.Nil(t, err)
rs := updated.GetRecordSet("bar.example7.de.", "A")
require.Nil(t, rs)
rs = updated.GetRecordSet("baz.example7.de.", "A")
require.Nil(t, rs)
}

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

Expand Down

0 comments on commit fe7832a

Please sign in to comment.