diff --git a/apis/zones/interface.go b/apis/zones/interface.go index 1ea785b..83bdef8 100644 --- a/apis/zones/interface.go +++ b/apis/zones/interface.go @@ -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 diff --git a/apis/zones/zones_addrecordset.go b/apis/zones/zones_addrecordset.go index 0982f0c..344f9ac 100644 --- a/apis/zones/zones_addrecordset.go +++ b/apis/zones/zones_addrecordset.go @@ -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 { @@ -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)) +} diff --git a/client_test.go b/client_test.go index 1c42884..87a27b2 100644 --- a/client_test.go +++ b/client_test.go @@ -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)