From 9a466bcf5e4c370d9c3b68177a073b030f3065d4 Mon Sep 17 00:00:00 2001 From: Michael Schurter Date: Wed, 16 Aug 2017 14:41:37 -0700 Subject: [PATCH] Add diff support for ServiceCheck.Header --- nomad/structs/diff.go | 25 ++++++++++++++++++++---- nomad/structs/diff_test.go | 40 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/nomad/structs/diff.go b/nomad/structs/diff.go index 1d8dc9ea3..51365d6fa 100644 --- a/nomad/structs/diff.go +++ b/nomad/structs/diff.go @@ -592,6 +592,23 @@ func serviceCheckDiff(old, new *ServiceCheck, contextual bool) *ObjectDiff { // Diff the primitive fields. diff.Fields = fieldDiffs(oldPrimitiveFlat, newPrimitiveFlat, contextual) + + // Diff Header + headerDiff := &ObjectDiff{Type: DiffTypeNone, Name: "Header"} + if reflect.DeepEqual(old.Header, new.Header) { + return diff + } else if len(old.Header) == 0 { + headerDiff.Type = DiffTypeAdded + } else if len(new.Header) == 0 { + headerDiff.Type = DiffTypeDeleted + } else { + headerDiff.Type = DiffTypeEdited + } + diff.Objects = append(diff.Objects, headerDiff) + + oldHeaderFlat := flatmap.Flatten(old.Header, nil, false) + newHeaderFlat := flatmap.Flatten(new.Header, nil, false) + headerDiff.Fields = fieldDiffs(oldHeaderFlat, newHeaderFlat, contextual) return diff } @@ -609,17 +626,17 @@ func serviceCheckDiffs(old, new []*ServiceCheck, contextual bool) []*ObjectDiff } var diffs []*ObjectDiff - for name, oldService := range oldMap { + for name, oldCheck := range oldMap { // Diff the same, deleted and edited - if diff := serviceCheckDiff(oldService, newMap[name], contextual); diff != nil { + if diff := serviceCheckDiff(oldCheck, newMap[name], contextual); diff != nil { diffs = append(diffs, diff) } } - for name, newService := range newMap { + for name, newCheck := range newMap { // Diff the added if old, ok := oldMap[name]; !ok { - if diff := serviceCheckDiff(old, newService, contextual); diff != nil { + if diff := serviceCheckDiff(old, newCheck, contextual); diff != nil { diffs = append(diffs, diff) } } diff --git a/nomad/structs/diff_test.go b/nomad/structs/diff_test.go index 1281eaa6b..f41692ba3 100644 --- a/nomad/structs/diff_test.go +++ b/nomad/structs/diff_test.go @@ -3410,6 +3410,9 @@ func TestTaskDiff(t *testing.T) { Interval: 1 * time.Second, Timeout: 1 * time.Second, InitialStatus: "critical", + Header: map[string][]string{ + "Foo": {"bar"}, + }, }, }, }, @@ -3430,6 +3433,11 @@ func TestTaskDiff(t *testing.T) { Interval: 1 * time.Second, Timeout: 1 * time.Second, InitialStatus: "passing", + Method: "POST", + Header: map[string][]string{ + "Foo": {"bar", "baz"}, + "Eggs": {"spam"}, + }, }, }, }, @@ -3484,6 +3492,12 @@ func TestTaskDiff(t *testing.T) { Old: "1000000000", New: "1000000000", }, + { + Type: DiffTypeAdded, + Name: "Method", + Old: "", + New: "POST", + }, { Type: DiffTypeNone, Name: "Name", @@ -3527,6 +3541,32 @@ func TestTaskDiff(t *testing.T) { New: "tcp", }, }, + Objects: []*ObjectDiff{ + { + Type: DiffTypeEdited, + Name: "Header", + Fields: []*FieldDiff{ + { + Type: DiffTypeAdded, + Name: "Eggs[0]", + Old: "", + New: "spam", + }, + { + Type: DiffTypeNone, + Name: "Foo[0]", + Old: "bar", + New: "bar", + }, + { + Type: DiffTypeAdded, + Name: "Foo[1]", + Old: "", + New: "baz", + }, + }, + }, + }, }, }, },