Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Nov 26, 2024
1 parent f91e8bc commit ed88989
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 25 deletions.
39 changes: 15 additions & 24 deletions records.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,13 @@ func (s *RecordsService) Delete(ctx context.Context, domainName, subName, record
Bulk operation
*/

type UpdateMode string

Check failure on line 275 in records.go

View workflow job for this annotation

GitHub Actions / Main Process

exported: exported type UpdateMode should have comment or be unexported (revive)

const (
FullResourceUpdateMode = http.MethodPut

Check failure on line 278 in records.go

View workflow job for this annotation

GitHub Actions / Main Process

exported: exported const FullResourceUpdateMode should have comment (or a comment on this block) or be unexported (revive)
OnlyFields UpdateMode = http.MethodPatch
)

// BulkCreate creates new RRSets in bulk.
// https://desec.readthedocs.io/en/latest/dns/rrsets.html#bulk-creation-of-rrsets
func (s *RecordsService) BulkCreate(ctx context.Context, domainName string, rrSets []RRSet) ([]RRSet, error) {
Expand Down Expand Up @@ -305,15 +312,15 @@ func (s *RecordsService) BulkCreate(ctx context.Context, domainName string, rrSe
return newRRSets, nil
}

// BulkUpdate updates RRSets in bulk (PUT).
// BulkUpdate updates RRSets in bulk.
// https://desec.readthedocs.io/en/latest/dns/rrsets.html#bulk-modification-of-rrsets
func (s *RecordsService) BulkUpdate(ctx context.Context, domainName string, rrSets []RRSet) ([]RRSet, error) {
func (s *RecordsService) BulkUpdate(ctx context.Context, mode UpdateMode, domainName string, rrSets []RRSet) ([]RRSet, error) {
endpoint, err := s.client.createEndpoint("domains", domainName, "rrsets")
if err != nil {
return nil, fmt.Errorf("failed to create endpoint: %w", err)
}

req, err := s.client.newRequest(ctx, http.MethodPut, endpoint, rrSets)
req, err := s.client.newRequest(ctx, string(mode), endpoint, rrSets)
if err != nil {
return nil, err
}
Expand All @@ -329,44 +336,28 @@ func (s *RecordsService) BulkUpdate(ctx context.Context, domainName string, rrSe
return nil, handleError(resp)
}

var updatedRRSets []RRSet
err = handleResponse(resp, &updatedRRSets)
var results []RRSet
err = handleResponse(resp, &results)
if err != nil {
return nil, err
}

return updatedRRSets, nil
return results, nil
}

// BulkDelete deletes RRSets in bulk (PUT).
// BulkDelete deletes RRSets in bulk (uses FullResourceUpdateMode).
// https://desec.readthedocs.io/en/latest/dns/rrsets.html#bulk-deletion-of-rrsets
func (s *RecordsService) BulkDelete(ctx context.Context, domainName string, rrSets []RRSet) error {
endpoint, err := s.client.createEndpoint("domains", domainName, "rrsets")
if err != nil {
return fmt.Errorf("failed to create endpoint: %w", err)
}

deleteRRSets := make([]RRSet, len(rrSets))
for i, rrSet := range rrSets {
rrSet.Records = []string{}
deleteRRSets[i] = rrSet
}

req, err := s.client.newRequest(ctx, http.MethodPut, endpoint, deleteRRSets)
_, err := s.BulkUpdate(ctx, FullResourceUpdateMode, domainName, deleteRRSets)
if err != nil {
return err
}

resp, err := s.client.httpClient.Do(req)
if err != nil {
return fmt.Errorf("failed to call API: %w", err)
}

defer func() { _ = resp.Body.Close() }()

if resp.StatusCode != http.StatusOK {
return handleError(resp)
}

return nil
}
2 changes: 1 addition & 1 deletion records_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ func TestRecordsService_BulkUpdate(t *testing.T) {
TTL: 300,
}}

updatedRecord, err := client.Records.BulkUpdate(context.Background(), "example.dedyn.io", rrSets)
updatedRecord, err := client.Records.BulkUpdate(context.Background(), FullResourceUpdateMode, "example.dedyn.io", rrSets)
require.NoError(t, err)

expected := []RRSet{{
Expand Down

0 comments on commit ed88989

Please sign in to comment.