feat(apply): per-record selection + deletes-before-updates ordering
RecordDiff.Key() gives a stable normalized identifier ("TYPE name.") for
every diff kind, exposed as recordView.Key. ApplyRequest now takes
Updates/Prunes key lists instead of two booleans, so callers can apply a
subset of records. service.Apply builds the applied set with selected
prunes (Delete) added before selected updates (Add/Update) — an
invariant, not an option — since the provider rejects an Add/Update
whose name still conflicts with an existing record (e.g. a CNAME cannot
be created while an A on the same name still exists).
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BwxdSt4reTm7Dj1oxRvpP3
This commit is contained in:
@@ -50,8 +50,8 @@ type Recorder interface {
|
||||
}
|
||||
|
||||
type ApplyRequest struct {
|
||||
ApplyUpdates bool
|
||||
ApplyPrunes bool
|
||||
Updates []string // record keys (RecordDiff.Key) to add/update
|
||||
Prunes []string // record keys to delete
|
||||
}
|
||||
|
||||
type DomainService struct {
|
||||
@@ -128,18 +128,29 @@ func (s *DomainService) ZoneRecords(ctx context.Context, projectID, domainID uui
|
||||
return recs, nil
|
||||
}
|
||||
|
||||
// Apply applies updates always (when ApplyUpdates) and prunes only when ApplyPrunes.
|
||||
// Apply applies exactly the diffs whose keys are selected in req.Updates and
|
||||
// req.Prunes. Selected prunes are added to the applied set BEFORE selected
|
||||
// updates: deletes first is an invariant, not an option — the provider
|
||||
// rejects an Add/Update whose name still has a conflicting record (e.g. a
|
||||
// CNAME cannot be created while an A on the same name exists), so pruning the
|
||||
// old records before applying updates avoids that.
|
||||
func (s *DomainService) Apply(ctx context.Context, projectID, domainID uuid.UUID, req ApplyRequest) (diff.Changeset, error) {
|
||||
p, creds, ref, cs, err := s.resolve(ctx, projectID, domainID)
|
||||
if err != nil {
|
||||
return diff.Changeset{}, err
|
||||
}
|
||||
selPrunes := toSet(req.Prunes)
|
||||
selUpdates := toSet(req.Updates)
|
||||
var toApply []diff.RecordDiff
|
||||
if req.ApplyUpdates {
|
||||
toApply = append(toApply, cs.Updates()...)
|
||||
for _, d := range cs.Prunes() {
|
||||
if selPrunes[d.Key()] {
|
||||
toApply = append(toApply, d)
|
||||
}
|
||||
}
|
||||
if req.ApplyPrunes {
|
||||
toApply = append(toApply, cs.Prunes()...)
|
||||
for _, d := range cs.Updates() {
|
||||
if selUpdates[d.Key()] {
|
||||
toApply = append(toApply, d)
|
||||
}
|
||||
}
|
||||
applied := diff.Changeset{Diffs: toApply}
|
||||
if len(toApply) > 0 {
|
||||
@@ -149,3 +160,11 @@ func (s *DomainService) Apply(ctx context.Context, projectID, domainID uuid.UUID
|
||||
}
|
||||
return applied, nil
|
||||
}
|
||||
|
||||
func toSet(keys []string) map[string]bool {
|
||||
m := make(map[string]bool, len(keys))
|
||||
for _, k := range keys {
|
||||
m[k] = true
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
@@ -125,39 +125,69 @@ func TestZoneRecordsReadsProviderDirectly(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyRespectsPruneGuard(t *testing.T) {
|
||||
// зона содержит лишнюю запись b (нет в шаблоне) → Prune-кандидат
|
||||
// TestApplySelectsByKeyAndOrdersPrunesBeforeUpdates covers the two goals of
|
||||
// selective apply: (1) only diffs whose key is present in the request are
|
||||
// applied, and (2) when both an update and a prune are selected, the prune
|
||||
// (Delete) must land BEFORE the update in the applied Changeset — this is the
|
||||
// regression guard for the provider rejecting an Add/Update whose name still
|
||||
// conflicts with an existing record (e.g. a CNAME cannot be created while an
|
||||
// A on the same name still exists).
|
||||
func TestApplySelectsByKeyAndOrdersPrunesBeforeUpdates(t *testing.T) {
|
||||
// zone: a needs updating (9.9.9.9 -> 1.1.1.1), b is an extra record not in
|
||||
// the template (prune candidate).
|
||||
actual := []model.Record{
|
||||
{Type: model.A, Name: "a.example.com.", TTL: 300, Values: []string{"1.1.1.1"}},
|
||||
{Type: model.A, Name: "a.example.com.", TTL: 300, Values: []string{"9.9.9.9"}},
|
||||
{Type: model.A, Name: "b.example.com.", TTL: 300, Values: []string{"2.2.2.2"}},
|
||||
}
|
||||
tmpl := dto.TemplateDoc{Records: []dto.RecordDTO{
|
||||
{Type: "A", Name: "a.example.com.", TTL: 300, Values: []string{"1.1.1.1"}}, // in sync
|
||||
{Type: "A", Name: "a.example.com.", TTL: 300, Values: []string{"1.1.1.1"}}, // update
|
||||
}}
|
||||
|
||||
// applyPrunes=false → удаление b НЕ применяется
|
||||
const updKey = "A a.example.com."
|
||||
const pruneKey = "A b.example.com."
|
||||
|
||||
// Only the prune selected -> only the delete is applied.
|
||||
svc, fp := setup(t, actual, tmpl)
|
||||
if _, err := svc.Apply(context.Background(), uuid.New(), uuid.New(), ApplyRequest{ApplyUpdates: true, ApplyPrunes: false}); err != nil {
|
||||
if _, err := svc.Apply(context.Background(), uuid.New(), uuid.New(), ApplyRequest{Prunes: []string{pruneKey}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, d := range fp.applied.Diffs {
|
||||
if d.Kind == diff.Delete {
|
||||
t.Fatalf("prune must be skipped when ApplyPrunes=false, applied: %+v", fp.applied.Diffs)
|
||||
}
|
||||
if len(fp.applied.Diffs) != 1 || fp.applied.Diffs[0].Kind != diff.Delete {
|
||||
t.Fatalf("expected only the selected prune applied, got %+v", fp.applied.Diffs)
|
||||
}
|
||||
|
||||
// applyPrunes=true → удаление b применяется
|
||||
// Only the update selected -> only the update is applied.
|
||||
svc2, fp2 := setup(t, actual, tmpl)
|
||||
if _, err := svc2.Apply(context.Background(), uuid.New(), uuid.New(), ApplyRequest{ApplyUpdates: true, ApplyPrunes: true}); err != nil {
|
||||
if _, err := svc2.Apply(context.Background(), uuid.New(), uuid.New(), ApplyRequest{Updates: []string{updKey}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var sawDelete bool
|
||||
for _, d := range fp2.applied.Diffs {
|
||||
if d.Kind == diff.Delete && d.Name == "b.example.com." {
|
||||
sawDelete = true
|
||||
}
|
||||
if len(fp2.applied.Diffs) != 1 || fp2.applied.Diffs[0].Kind != diff.Update {
|
||||
t.Fatalf("expected only the selected update applied, got %+v", fp2.applied.Diffs)
|
||||
}
|
||||
if !sawDelete {
|
||||
t.Fatalf("prune must be applied when ApplyPrunes=true, applied: %+v", fp2.applied.Diffs)
|
||||
|
||||
// Nothing selected -> nothing applied.
|
||||
svc3, fp3 := setup(t, actual, tmpl)
|
||||
if _, err := svc3.Apply(context.Background(), uuid.New(), uuid.New(), ApplyRequest{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(fp3.applied.Diffs) != 0 {
|
||||
t.Fatalf("expected nothing applied when nothing is selected, got %+v", fp3.applied.Diffs)
|
||||
}
|
||||
|
||||
// CRITICAL: both selected -> the prune (Delete) must be applied FIRST,
|
||||
// the update SECOND. Regressing this order reintroduces the
|
||||
// CNAME/A-conflict bug where the provider rejects the update because the
|
||||
// stale conflicting record hasn't been deleted yet.
|
||||
svc4, fp4 := setup(t, actual, tmpl)
|
||||
if _, err := svc4.Apply(context.Background(), uuid.New(), uuid.New(), ApplyRequest{Updates: []string{updKey}, Prunes: []string{pruneKey}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(fp4.applied.Diffs) != 2 {
|
||||
t.Fatalf("expected both selected diffs applied, got %+v", fp4.applied.Diffs)
|
||||
}
|
||||
if fp4.applied.Diffs[0].Kind != diff.Delete {
|
||||
t.Fatalf("expected prune (Delete) FIRST in applied order, got %+v", fp4.applied.Diffs)
|
||||
}
|
||||
if fp4.applied.Diffs[1].Kind != diff.Update {
|
||||
t.Fatalf("expected update SECOND in applied order, got %+v", fp4.applied.Diffs)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user