diff --git a/command/plan.go b/command/plan.go index a9fab5fdef65..7526eaf4a296 100644 --- a/command/plan.go +++ b/command/plan.go @@ -353,15 +353,17 @@ func formatTaskDiff(task *api.TaskDiff, startPrefix, taskPrefix int, verbose boo // of spaces to put between the marker and object name output. func formatObjectDiff(diff *api.ObjectDiff, startPrefix, keyPrefix int) string { start := strings.Repeat(" ", startPrefix) - marker, _ := getDiffString(diff.Type) + marker, markerLen := getDiffString(diff.Type) out := fmt.Sprintf("%s%s%s%s {\n", start, marker, strings.Repeat(" ", keyPrefix), diff.Name) // Determine the length of the longest name and longest diff marker to // properly align names and values longestField, longestMarker := getLongestPrefixes(diff.Fields, diff.Objects) - subStartPrefix := startPrefix + 2 + subStartPrefix := startPrefix + keyPrefix + 2 out += alignedFieldAndObjects(diff.Fields, diff.Objects, subStartPrefix, longestField, longestMarker) - return fmt.Sprintf("%s\n%s}", out, start) + + endprefix := strings.Repeat(" ", startPrefix+markerLen+keyPrefix) + return fmt.Sprintf("%s\n%s}", out, endprefix) } // formatFieldDiff produces an annotated diff of a field. startPrefix is the diff --git a/nomad/structs/diff.go b/nomad/structs/diff.go index 2e3751cfe439..1d057b824ea8 100644 --- a/nomad/structs/diff.go +++ b/nomad/structs/diff.go @@ -98,7 +98,7 @@ func (j *Job) Diff(other *Job, contextual bool) (*JobDiff, error) { diff.Fields = fieldDiffs(oldPrimitiveFlat, newPrimitiveFlat, false) // Datacenters diff - if setDiff := stringSetDiff(j.Datacenters, other.Datacenters, "Datacenters", contextual); setDiff != nil { + if setDiff := stringSetDiff(j.Datacenters, other.Datacenters, "Datacenters", contextual); setDiff != nil && setDiff.Type != DiffTypeNone { diff.Objects = append(diff.Objects, setDiff) } diff --git a/nomad/structs/diff_test.go b/nomad/structs/diff_test.go index 3f3c350428ab..1b8f7a946994 100644 --- a/nomad/structs/diff_test.go +++ b/nomad/structs/diff_test.go @@ -397,7 +397,7 @@ func TestJobDiff(t *testing.T) { }, }, { - // Datacenter contextual + // Datacenter contextual no change Contextual: true, Old: &Job{ Datacenters: []string{"foo", "bar"}, @@ -407,11 +407,30 @@ func TestJobDiff(t *testing.T) { }, Expected: &JobDiff{ Type: DiffTypeNone, + }, + }, + { + // Datacenter contextual + Contextual: true, + Old: &Job{ + Datacenters: []string{"foo", "bar"}, + }, + New: &Job{ + Datacenters: []string{"foo", "bar", "baz"}, + }, + Expected: &JobDiff{ + Type: DiffTypeEdited, Objects: []*ObjectDiff{ { - Type: DiffTypeNone, + Type: DiffTypeAdded, Name: "Datacenters", Fields: []*FieldDiff{ + { + Type: DiffTypeAdded, + Name: "Datacenters", + Old: "", + New: "baz", + }, { Type: DiffTypeNone, Name: "Datacenters",