Skip to content
This repository was archived by the owner on Jun 2, 2021. It is now read-only.

Commit e0c19e1

Browse files
monamohebbiJenGoldstrichTeal Stannardnickjameswebb
committed
Update manifest diff to only display meaningful changes
[#173802711] Co-authored-by: Jenna Goldstrich <jgoldstrich@pivotal.io> Co-authored-by: Mona Mohebbi <mmohebbi@pivotal.io> Co-authored-by: Teal Stannard <tstannard@pivotal.io> Co-authored-by: Nick Webb <nwebb@pivotal.io>
1 parent 89d665b commit e0c19e1

3 files changed

Lines changed: 95 additions & 5 deletions

File tree

app/actions/space_diff_manifest.rb

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,62 @@
33

44
module VCAP::CloudController
55
class SpaceDiffManifest
6+
def self.filter_manifest_app_hash(manifest_app_hash)
7+
if manifest_app_hash.key? 'sidecars'
8+
manifest_app_hash['sidecars'] = manifest_app_hash['sidecars'].map do |hash|
9+
hash.slice(
10+
'name',
11+
'command',
12+
'process_types',
13+
'memory'
14+
)
15+
end
16+
manifest_app_hash = manifest_app_hash.except('sidecars') if manifest_app_hash['sidecars'] == [{}]
17+
end
18+
if manifest_app_hash.key? 'processes'
19+
manifest_app_hash['processes'] = manifest_app_hash['processes'].map do |hash|
20+
hash.slice(
21+
'type',
22+
'command',
23+
'disk_quota',
24+
'health-check-http-endpoint',
25+
'health-check-invocation-timeout',
26+
'health-check-type',
27+
'instances',
28+
'memory',
29+
'timeout'
30+
)
31+
end
32+
manifest_app_hash = manifest_app_hash.except('processes') if manifest_app_hash['processes'] == [{}]
33+
end
34+
35+
if manifest_app_hash.key? 'services'
36+
manifest_app_hash['services'] = manifest_app_hash['services'].map do |hash|
37+
hash.slice(
38+
'name',
39+
'parameters'
40+
)
41+
end
42+
manifest_app_hash = manifest_app_hash.except('services') if manifest_app_hash['services'] == [{}]
43+
end
44+
45+
if manifest_app_hash.key? 'metadata'
46+
manifest_app_hash['metadata'] = manifest_app_hash['metadata'].map do |hash|
47+
hash.slice(
48+
'labels',
49+
'annotations'
50+
)
51+
end
52+
manifest_app_hash = manifest_app_hash.except('metadata') if manifest_app_hash['metadata'] == [{}]
53+
end
54+
manifest_app_hash
55+
end
56+
657
def self.generate_diff(app_manifests, space)
758
json_diff = []
8-
59+
recognized_top_level_keys = NamedAppManifestMessage.allowed_keys.map(&:to_s)
960
app_manifests.each_with_index do |manifest_app_hash, index|
61+
manifest_app_hash = SpaceDiffManifest.filter_manifest_app_hash(manifest_app_hash)
1062
existing_app = space.app_models.find { |app| app.name == manifest_app_hash['name'] }
1163

1264
if existing_app.nil?
@@ -23,13 +75,16 @@ def self.generate_diff(app_manifests, space)
2375
end
2476

2577
manifest_app_hash.each do |key, value|
78+
next unless recognized_top_level_keys.include?(key)
79+
2680
existing_value = existing_app_hash[key]
2781

2882
key_diffs = JsonDiff.diff(
2983
existing_value,
3084
value,
3185
include_was: true,
3286
)
87+
3388
key_diffs.each do |diff|
3489
diff['path'] = "/applications/#{index}/#{key}" + diff['path']
3590

spec/request/space_manifests_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,6 @@
538538
let(:diff_json) do
539539
{
540540
diff: a_collection_containing_exactly(
541-
{ op: 'add', path: '/applications/0/new-key', value: 'hoh' },
542541
{ op: 'replace', path: '/applications/0/stack', was: process1.stack.name, value: 'big brother' },
543542
)
544543
}

spec/unit/actions/space_diff_manifest_spec.rb

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,25 @@ module VCAP::CloudController
4949
end
5050
end
5151

52+
context 'when there is an unrecognized top-level field' do
53+
before do
54+
default_manifest['applications'][0]['foo'] = 'bar'
55+
end
56+
57+
it 'returns an empty diff' do
58+
expect(subject).to eq([])
59+
end
60+
end
61+
5262
context 'when there are changes in the manifest' do
5363
before do
54-
default_manifest['applications'][0]['new-key'] = 'hoh'
64+
default_manifest['applications'][0]['random_route'] = true
5565
default_manifest['applications'][0]['stack'] = 'big brother'
5666
end
5767

5868
it 'returns the correct diff' do
5969
expect(subject).to match_array([
60-
{ 'op' => 'add', 'path' => '/applications/0/new-key', 'value' => 'hoh' },
70+
{ 'op' => 'add', 'path' => '/applications/0/random_route', 'value' => true },
6171
{ 'op' => 'replace', 'path' => '/applications/0/stack', 'was' => process1.stack.name, 'value' => 'big brother' },
6272
])
6373
end
@@ -75,7 +85,7 @@ module VCAP::CloudController
7585
end
7686
end
7787

78-
context 'when there is a difference in a nested array' do
88+
context 'when there is a change inside of a nested array' do
7989
before do
8090
default_manifest['applications'][0]['sidecars'] = [
8191
{ 'name' => 'sidecar1', 'command' => 'bundle exec rake lol', 'process_types' => ['web', 'worker'] }
@@ -95,6 +105,32 @@ module VCAP::CloudController
95105
end
96106
end
97107

108+
context 'when there is an unrecognized field in a nested hash' do
109+
before do
110+
default_manifest['applications'][0]['processes'][0]['foo'] = 'bar'
111+
default_manifest['applications'][0]['services'] = [
112+
{
113+
'foo' => 'bar'
114+
}
115+
]
116+
117+
default_manifest['applications'][0]['metadata'] = [
118+
{
119+
'foo' => 'bar'
120+
}
121+
]
122+
default_manifest['applications'][0]['sidecars'] = [
123+
{
124+
'foo' => 'bar'
125+
}
126+
]
127+
end
128+
129+
it 'returns an empty diff' do
130+
expect(subject).to eq([])
131+
end
132+
end
133+
98134
context 'when there is a new app' do
99135
let(:app_manifests) do
100136
[

0 commit comments

Comments
 (0)