dnscontrol/pkg/diff/diff.go

46 lines
1.8 KiB
Go
Raw Normal View History

2016-08-23 08:31:50 +08:00
package diff
import (
"fmt"
2023-05-21 01:21:45 +08:00
"github.com/StackExchange/dnscontrol/v4/models"
"github.com/fatih/color"
2016-08-23 08:31:50 +08:00
)
// Correlation stores a difference between two records.
type Correlation struct {
d *differ
Existing *models.RecordConfig
Desired *models.RecordConfig
}
// Changeset stores many Correlation.
type Changeset []Correlation
2016-08-23 08:31:50 +08:00
// Differ is an interface for computing the difference between two zones.
type Differ interface {
// IncrementalDiff performs a diff on a record-by-record basis, and returns a sets for which records need to be created, deleted, or modified.
IncrementalDiff(existing []*models.RecordConfig) (reportMsgs []string, create, toDelete, modify Changeset, err error)
// ChangedGroups performs a diff more appropriate for providers with a "RecordSet" model, where all records with the same name and type are grouped.
// Individual record changes are often not useful in such scenarios. Instead we return a map of record keys to a list of change descriptions within that group.
ChangedGroups(existing []*models.RecordConfig) (map[models.RecordKey][]string, []string, error)
}
type differ struct {
}
// get normalized content for record. target, ttl, mxprio, and specified metadata
func (d *differ) content(r *models.RecordConfig) string {
return fmt.Sprintf("%s ttl=%d", r.ToComparableNoTTL(), r.TTL)
}
2016-08-23 08:31:50 +08:00
func (c Correlation) String() string {
if c.Existing == nil {
2023-02-28 14:14:06 +08:00
return color.GreenString("+ CREATE %s %s %s", c.Desired.Type, c.Desired.GetLabelFQDN(), c.d.content(c.Desired))
2016-08-23 08:31:50 +08:00
}
if c.Desired == nil {
2023-02-28 14:14:06 +08:00
return color.RedString("- DELETE %s %s %s", c.Existing.Type, c.Existing.GetLabelFQDN(), c.d.content(c.Existing))
}
return color.YellowString("± MODIFY %s %s: (%s) -> (%s)", c.Existing.Type, c.Existing.GetLabelFQDN(), c.d.content(c.Existing), c.d.content(c.Desired))
}