chore(pkger): refactor diffs to use reuable identifier type

pull/17684/head
Johnny Steenbergen 2020-04-08 12:30:49 -07:00 committed by Johnny Steenbergen
parent 85e31e8194
commit afb1653bff
4 changed files with 182 additions and 144 deletions

View File

@ -161,7 +161,9 @@ func TestPkgerHTTPServer(t *testing.T) {
var diff pkger.Diff
for _, b := range sum.Buckets {
diff.Buckets = append(diff.Buckets, pkger.DiffBucket{
DiffIdentifier: pkger.DiffIdentifier{
PkgName: b.Name,
},
})
}
return sum, diff, nil
@ -215,7 +217,9 @@ func TestPkgerHTTPServer(t *testing.T) {
var diff pkger.Diff
for _, b := range sum.Buckets {
diff.Buckets = append(diff.Buckets, pkger.DiffBucket{
DiffIdentifier: pkger.DiffIdentifier{
PkgName: b.Name,
},
})
}
return sum, diff, nil
@ -317,7 +321,9 @@ func TestPkgerHTTPServer(t *testing.T) {
var diff pkger.Diff
for _, b := range sum.Buckets {
diff.Buckets = append(diff.Buckets, pkger.DiffBucket{
DiffIdentifier: pkger.DiffIdentifier{
PkgName: b.Name,
},
})
}
return sum, diff, nil
@ -409,7 +415,9 @@ func TestPkgerHTTPServer(t *testing.T) {
var diff pkger.Diff
for _, b := range sum.Buckets {
diff.Buckets = append(diff.Buckets, pkger.DiffBucket{
DiffIdentifier: pkger.DiffIdentifier{
PkgName: b.Name,
},
})
}
return sum, diff, nil

View File

@ -135,6 +135,19 @@ func (s SafeID) String() string {
return influxdb.ID(s).String()
}
// DiffIdentifier are the identifying fields for any given resource. Each resource
// dictates if the resource is new, to be removed, or will remain.
type DiffIdentifier struct {
ID SafeID `json:"id"`
Remove bool `json:"bool"`
PkgName string `json:"pkgName"`
}
// IsNew indicates the resource is new to the platform.
func (d DiffIdentifier) IsNew() bool {
return d.ID == 0
}
// Diff is the result of a service DryRun call. The diff outlines
// what is new and or updated from the current state of the platform.
type Diff struct {
@ -177,9 +190,8 @@ func (d Diff) HasConflicts() bool {
type (
// DiffBucket is a diff of an individual bucket.
DiffBucket struct {
Remove bool `json:"remove"`
ID SafeID `json:"id"`
PkgName string `json:"pkgName"`
DiffIdentifier
New DiffBucketValues `json:"new"`
Old *DiffBucketValues `json:"old,omitempty"` // using omitempty here to signal there was no prev state with a nil
}
@ -194,8 +206,11 @@ type (
func newDiffBucket(b *bucket, i *influxdb.Bucket) DiffBucket {
diff := DiffBucket{
DiffIdentifier: DiffIdentifier{
ID: SafeID(b.ID()),
Remove: b.shouldRemove,
PkgName: b.PkgName(),
},
New: DiffBucketValues{
Name: b.Name(),
Description: b.Description,
@ -215,11 +230,6 @@ func newDiffBucket(b *bucket, i *influxdb.Bucket) DiffBucket {
return diff
}
// IsNew indicates whether a pkg bucket is going to be new to the platform.
func (d DiffBucket) IsNew() bool {
return d.ID == SafeID(0)
}
func (d DiffBucket) hasConflict() bool {
return !d.IsNew() && d.Old != nil && !reflect.DeepEqual(*d.Old, d.New)
}
@ -248,17 +258,19 @@ func (d *DiffCheckValues) UnmarshalJSON(b []byte) (err error) {
// DiffCheck is a diff of an individual check.
type DiffCheck struct {
Remove bool `json:"remove"`
ID SafeID `json:"id"`
PkgName string `json:"pkgName"`
DiffIdentifier
New DiffCheckValues `json:"new"`
Old *DiffCheckValues `json:"old"`
}
func newDiffCheck(c *check, iCheck influxdb.Check) DiffCheck {
diff := DiffCheck{
DiffIdentifier: DiffIdentifier{
ID: SafeID(c.ID()),
Remove: c.shouldRemove,
PkgName: c.PkgName(),
},
New: DiffCheckValues{
Check: c.summarize().Check,
},
@ -272,17 +284,11 @@ func newDiffCheck(c *check, iCheck influxdb.Check) DiffCheck {
return diff
}
// IsNew determines if the check in the pkg is new to the platform.
func (d DiffCheck) IsNew() bool {
return d.Old == nil
}
type (
// DiffDashboard is a diff of an individual dashboard.
DiffDashboard struct {
ID SafeID `json:"id"`
Remove bool `json:"remove"`
PkgName string `json:"pkgName"`
DiffIdentifier
New DiffDashboardValues `json:"new"`
Old *DiffDashboardValues `json:"old"`
}
@ -297,8 +303,11 @@ type (
func newDiffDashboard(d *dashboard) DiffDashboard {
diff := DiffDashboard{
DiffIdentifier: DiffIdentifier{
ID: SafeID(d.ID()),
Remove: d.shouldRemove,
PkgName: d.PkgName(),
},
New: DiffDashboardValues{
Name: d.Name(),
Desc: d.Description,
@ -344,11 +353,6 @@ func newDiffDashboard(d *dashboard) DiffDashboard {
return diff
}
// IsNew indicates whether the pkg dashboard is new to the platform.
func (d DiffDashboard) IsNew() bool {
return d.ID == 0
}
// DiffChart is a diff of oa chart. Since all charts are new right now.
// the SummaryChart is reused here.
type DiffChart SummaryChart
@ -356,9 +360,8 @@ type DiffChart SummaryChart
type (
// DiffLabel is a diff of an individual label.
DiffLabel struct {
Remove bool `json:"remove"`
ID SafeID `json:"id"`
PkgName string `json:"pkgName"`
DiffIdentifier
New DiffLabelValues `json:"new"`
Old *DiffLabelValues `json:"old,omitempty"` // using omitempty here to signal there was no prev state with a nil
}
@ -371,19 +374,13 @@ type (
}
)
// IsNew indicates whether a pkg label is going to be new to the platform.
func (d DiffLabel) IsNew() bool {
return d.ID == 0
}
func (d DiffLabel) hasConflict() bool {
return !d.IsNew() && d.Old != nil && *d.Old != d.New
}
func newDiffLabel(l *label, i *influxdb.Label) DiffLabel {
diff := DiffLabel{
DiffIdentifier: DiffIdentifier{
ID: SafeID(l.ID()),
Remove: l.shouldRemove,
PkgName: l.PkgName(),
},
New: DiffLabelValues{
Name: l.Name(),
Color: l.Color,
@ -401,6 +398,10 @@ func newDiffLabel(l *label, i *influxdb.Label) DiffLabel {
return diff
}
func (d DiffLabel) hasConflict() bool {
return !d.IsNew() && d.Old != nil && *d.Old != d.New
}
// DiffLabelMapping is a diff of an individual label mapping. A
// single resource may have multiple mappings to multiple labels.
// A label can have many mappings to other resources.
@ -439,17 +440,19 @@ func (d *DiffNotificationEndpointValues) UnmarshalJSON(b []byte) (err error) {
// DiffNotificationEndpoint is a diff of an individual notification endpoint.
type DiffNotificationEndpoint struct {
ID SafeID `json:"id"`
Remove bool `json:"remove"`
PkgName string `json:"pkgName"`
DiffIdentifier
New DiffNotificationEndpointValues `json:"new"`
Old *DiffNotificationEndpointValues `json:"old"`
}
func newDiffNotificationEndpoint(ne *notificationEndpoint, i influxdb.NotificationEndpoint) DiffNotificationEndpoint {
diff := DiffNotificationEndpoint{
DiffIdentifier: DiffIdentifier{
ID: SafeID(ne.ID()),
Remove: ne.shouldRemove,
PkgName: ne.PkgName(),
},
New: DiffNotificationEndpointValues{
NotificationEndpoint: ne.summarize().NotificationEndpoint,
},
@ -463,18 +466,10 @@ func newDiffNotificationEndpoint(ne *notificationEndpoint, i influxdb.Notificati
return diff
}
// IsNew indicates if the resource will be new to the platform or if it edits
// an existing resource.
func (d DiffNotificationEndpoint) IsNew() bool {
return d.ID == 0
}
type (
// DiffNotificationRule is a diff of an individual notification rule.
DiffNotificationRule struct {
ID SafeID `json:"id"`
Remove bool `json:"bool"`
PkgName string `json:"pkgName"`
DiffIdentifier
New DiffNotificationRuleValues `json:"new"`
Old *DiffNotificationRuleValues `json:"old"`
@ -500,9 +495,11 @@ type (
func newDiffNotificationRule(r *notificationRule, iEndpoint influxdb.NotificationEndpoint) DiffNotificationRule {
sum := DiffNotificationRule{
DiffIdentifier: DiffIdentifier{
ID: SafeID(r.ID()),
Remove: r.shouldRemove,
PkgName: r.PkgName(),
},
New: DiffNotificationRuleValues{
Name: r.Name(),
Description: r.description,
@ -568,18 +565,11 @@ func newDiffNotificationRule(r *notificationRule, iEndpoint influxdb.Notificatio
return sum
}
// IsNew indicates if the resource will be new to the platform or if it edits
// an existing resource.
func (d DiffNotificationRule) IsNew() bool {
return d.ID == 0
}
type (
// DiffTask is a diff of an individual task.
DiffTask struct {
ID SafeID `json:"id"`
Remove bool `json:"remove"`
PkgName string `json:"pkgName"`
DiffIdentifier
New DiffTaskValues `json:"new"`
Old *DiffTaskValues `json:"old"`
}
@ -598,8 +588,11 @@ type (
func newDiffTask(t *task) DiffTask {
diff := DiffTask{
DiffIdentifier: DiffIdentifier{
ID: SafeID(t.ID()),
Remove: t.shouldRemove,
PkgName: t.PkgName(),
},
New: DiffTaskValues{
Name: t.Name(),
Cron: t.cron,
@ -628,11 +621,6 @@ func newDiffTask(t *task) DiffTask {
return diff
}
// IsNew indicates whether a pkg task is going to be new to the platform.
func (d DiffTask) IsNew() bool {
return d.ID == 0
}
// DiffTelegraf is a diff of an individual telegraf. This resource is always new.
type DiffTelegraf struct {
influxdb.TelegrafConfig
@ -647,9 +635,8 @@ func newDiffTelegraf(t *telegraf) DiffTelegraf {
type (
// DiffVariable is a diff of an individual variable.
DiffVariable struct {
ID SafeID `json:"id"`
Remove bool `json:"remove"`
PkgName string `json:"pkgName"`
DiffIdentifier
New DiffVariableValues `json:"new"`
Old *DiffVariableValues `json:"old,omitempty"` // using omitempty here to signal there was no prev state with a nil
}
@ -664,8 +651,11 @@ type (
func newDiffVariable(v *variable, iv *influxdb.Variable) DiffVariable {
diff := DiffVariable{
DiffIdentifier: DiffIdentifier{
ID: SafeID(v.ID()),
Remove: v.shouldRemove,
PkgName: v.PkgName(),
},
New: DiffVariableValues{
Name: v.Name(),
Description: v.Description,
@ -684,11 +674,6 @@ func newDiffVariable(v *variable, iv *influxdb.Variable) DiffVariable {
return diff
}
// IsNew indicates whether a pkg variable is going to be new to the platform.
func (d DiffVariable) IsNew() bool {
return d.ID == SafeID(0)
}
func (d DiffVariable) hasConflict() bool {
return !d.IsNew() && d.Old != nil && !reflect.DeepEqual(*d.Old, d.New)
}

View File

@ -136,7 +136,9 @@ func TestPkg(t *testing.T) {
{
name: "new bucket",
resource: DiffBucket{
DiffIdentifier: DiffIdentifier{
PkgName: "new bucket",
},
New: DiffBucketValues{
Description: "new desc",
},
@ -146,8 +148,10 @@ func TestPkg(t *testing.T) {
{
name: "existing bucket with no changes",
resource: DiffBucket{
DiffIdentifier: DiffIdentifier{
ID: 3,
PkgName: "new bucket",
},
New: DiffBucketValues{
Description: "new desc",
RetentionRules: retentionRules{{
@ -168,8 +172,10 @@ func TestPkg(t *testing.T) {
{
name: "existing bucket with desc changes",
resource: DiffBucket{
DiffIdentifier: DiffIdentifier{
ID: 3,
PkgName: "existing bucket",
},
New: DiffBucketValues{
Description: "new desc",
RetentionRules: retentionRules{{
@ -190,8 +196,10 @@ func TestPkg(t *testing.T) {
{
name: "existing bucket with retention changes",
resource: DiffBucket{
DiffIdentifier: DiffIdentifier{
ID: 3,
PkgName: "existing bucket",
},
New: DiffBucketValues{
Description: "new desc",
RetentionRules: retentionRules{{
@ -208,8 +216,10 @@ func TestPkg(t *testing.T) {
{
name: "existing bucket with retention changes",
resource: DiffBucket{
DiffIdentifier: DiffIdentifier{
ID: 3,
PkgName: "existing bucket",
},
New: DiffBucketValues{
Description: "new desc",
RetentionRules: retentionRules{{
@ -230,8 +240,10 @@ func TestPkg(t *testing.T) {
{
name: "existing bucket with retention changes",
resource: DiffBucket{
DiffIdentifier: DiffIdentifier{
ID: 3,
PkgName: "existing bucket",
},
New: DiffBucketValues{
Description: "new desc",
RetentionRules: retentionRules{{
@ -258,7 +270,9 @@ func TestPkg(t *testing.T) {
{
name: "new label",
resource: DiffLabel{
DiffIdentifier: DiffIdentifier{
PkgName: "new label",
},
New: DiffLabelValues{
Name: "new label",
Color: "new color",
@ -270,8 +284,10 @@ func TestPkg(t *testing.T) {
{
name: "existing label with no changes",
resource: DiffLabel{
DiffIdentifier: DiffIdentifier{
ID: 1,
PkgName: "existing label",
},
New: DiffLabelValues{
Name: "existing label",
Color: "color",
@ -288,8 +304,10 @@ func TestPkg(t *testing.T) {
{
name: "existing label with changes",
resource: DiffLabel{
DiffIdentifier: DiffIdentifier{
ID: 1,
PkgName: "existing label",
},
New: DiffLabelValues{
Name: "existing label",
Color: "color",
@ -306,7 +324,9 @@ func TestPkg(t *testing.T) {
{
name: "new variable",
resource: DiffVariable{
DiffIdentifier: DiffIdentifier{
PkgName: "new var",
},
New: DiffVariableValues{
Name: "new var",
Description: "new desc",
@ -321,8 +341,10 @@ func TestPkg(t *testing.T) {
{
name: "existing variable no changes",
resource: DiffVariable{
DiffIdentifier: DiffIdentifier{
ID: 2,
PkgName: "new var",
},
New: DiffVariableValues{
Name: "new var",
Description: "new desc",
@ -345,8 +367,10 @@ func TestPkg(t *testing.T) {
{
name: "existing variable with desc changes",
resource: DiffVariable{
DiffIdentifier: DiffIdentifier{
ID: 3,
PkgName: "new var",
},
New: DiffVariableValues{
Name: "new var",
Description: "new desc",
@ -368,8 +392,10 @@ func TestPkg(t *testing.T) {
{
name: "existing variable with constant arg changes",
resource: DiffVariable{
DiffIdentifier: DiffIdentifier{
ID: 3,
PkgName: "new var",
},
New: DiffVariableValues{
Name: "new var",
Description: "new desc",
@ -391,8 +417,10 @@ func TestPkg(t *testing.T) {
{
name: "existing variable with map arg changes",
resource: DiffVariable{
DiffIdentifier: DiffIdentifier{
ID: 3,
PkgName: "new var",
},
New: DiffVariableValues{
Name: "new var",
Description: "new desc",
@ -414,8 +442,10 @@ func TestPkg(t *testing.T) {
{
name: "existing variable with query arg changes",
resource: DiffVariable{
DiffIdentifier: DiffIdentifier{
ID: 3,
PkgName: "new var",
},
New: DiffVariableValues{
Name: "new var",
Description: "new desc",

View File

@ -83,8 +83,11 @@ func TestService(t *testing.T) {
require.Len(t, diff.Buckets, 2)
expected := DiffBucket{
DiffIdentifier: DiffIdentifier{
ID: SafeID(1),
PkgName: "rucket_11",
},
Old: &DiffBucketValues{
Name: "rucket_11",
Description: "old desc",
@ -114,7 +117,9 @@ func TestService(t *testing.T) {
require.Len(t, diff.Buckets, 2)
expected := DiffBucket{
DiffIdentifier: DiffIdentifier{
PkgName: "rucket_11",
},
New: DiffBucketValues{
Name: "rucket_11",
Description: "bucket 1 description",
@ -190,8 +195,10 @@ func TestService(t *testing.T) {
require.Len(t, diff.Labels, 3)
expected := DiffLabel{
DiffIdentifier: DiffIdentifier{
ID: SafeID(1),
PkgName: "label_1",
},
Old: &DiffLabelValues{
Name: "label_1",
Color: "old color",
@ -228,7 +235,9 @@ func TestService(t *testing.T) {
require.Len(t, diff.Labels, 3)
expected := DiffLabel{
DiffIdentifier: DiffIdentifier{
PkgName: "label_1",
},
New: DiffLabelValues{
Name: "label_1",
Color: "#FFFFFF",
@ -287,8 +296,10 @@ func TestService(t *testing.T) {
require.Len(t, existingEndpoints, 1)
expected := DiffNotificationEndpoint{
ID: SafeID(1),
DiffIdentifier: DiffIdentifier{
ID: 1,
PkgName: "http_none_auth_notification_endpoint",
},
Old: &DiffNotificationEndpointValues{
NotificationEndpoint: existing,
},
@ -404,8 +415,10 @@ func TestService(t *testing.T) {
require.Len(t, diff.Variables, 4)
expected := DiffVariable{
ID: SafeID(1),
DiffIdentifier: DiffIdentifier{
ID: 1,
PkgName: "var_const_3",
},
Old: &DiffVariableValues{
Name: "var_const_3",
Description: "old desc",
@ -422,8 +435,10 @@ func TestService(t *testing.T) {
assert.Equal(t, expected, diff.Variables[0])
expected = DiffVariable{
DiffIdentifier: DiffIdentifier{
// no ID here since this one would be new
PkgName: "var_map_4",
},
New: DiffVariableValues{
Name: "var_map_4",
Description: "var_map_4 desc",