Skip to content

Commit 7eaa6fc

Browse files
committed
Remove route option validations from manifest_route
1 parent 5eff0c2 commit 7eaa6fc

File tree

4 files changed

+39
-54
lines changed

4 files changed

+39
-54
lines changed

app/messages/manifest_routes_update_message.rb

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,9 @@ class ManifestRoutesUpdateMessage < BaseMessage
88

99
class ManifestRoutesYAMLValidator < ActiveModel::Validator
1010
def validate(record)
11-
if is_not_array?(record.routes) || contains_non_route_hash_values?(record.routes)
12-
record.errors.add(:routes, message: 'must be a list of route objects')
13-
return
14-
end
11+
return unless is_not_array?(record.routes) || contains_non_route_hash_values?(record.routes)
1512

16-
contains_invalid_route_options?(record)
17-
contains_invalid_lb_algo?(record)
18-
nil
13+
record.errors.add(:routes, message: 'must be a list of route objects')
1914
end
2015

2116
def is_not_array?(routes)
@@ -25,43 +20,14 @@ def is_not_array?(routes)
2520
def contains_non_route_hash_values?(routes)
2621
routes.any? { |r| !(r.is_a?(Hash) && r[:route].present?) }
2722
end
28-
29-
def contains_invalid_route_options?(record)
30-
routes = record.routes
31-
routes.any? do |r|
32-
next unless r[:options]
33-
34-
return true unless r[:options].is_a?(Hash)
35-
36-
return false if r[:options].empty?
37-
38-
r[:options].each_key do |key|
39-
RouteOptionsMessage::VALID_MANIFEST_ROUTE_OPTIONS.exclude?(key) &&
40-
record.errors.add(:base,
41-
message: "Route '#{r[:route]}' contains invalid route option '#{key}'. \
42-
Valid keys: '#{RouteOptionsMessage::VALID_MANIFEST_ROUTE_OPTIONS.join(', ')}'")
43-
end
44-
end
45-
end
46-
47-
def contains_invalid_lb_algo?(record)
48-
routes = record.routes
49-
routes.each do |r|
50-
next unless r[:options] && r[:options][:'loadbalancing-algorithm']
51-
52-
lb_algo = r[:options][:'loadbalancing-algorithm']
53-
RouteOptionsMessage::VALID_LOADBALANCING_ALGORITHMS.exclude?(lb_algo) &&
54-
record.errors.add(:base,
55-
message: "Route '#{r[:route]}' contains invalid load-balancing algorithm '#{lb_algo}'. \
56-
Valid algorithms: '#{RouteOptionsMessage::VALID_LOADBALANCING_ALGORITHMS.join(', ')}'")
57-
end
58-
end
5923
end
6024

6125
validates_with NoAdditionalKeysValidator
6226
validates_with ManifestRoutesYAMLValidator, if: proc { |record| record.requested?(:routes) }
6327
validate :routes_are_uris, if: proc { |record| record.requested?(:routes) }
6428
validate :route_protocols_are_valid, if: proc { |record| record.requested?(:routes) }
29+
validate :route_options_are_valid, if: proc { |record| record.requested?(:routes) }
30+
validate :lb_algos_are_valid, if: proc { |record| record.requested?(:routes) }
6531
validate :no_route_is_boolean
6632
validate :default_route_is_boolean
6733
validate :random_route_is_boolean
@@ -80,6 +46,39 @@ def manifest_route_mappings
8046

8147
private
8248

49+
def route_options_are_valid
50+
return if errors[:routes].present?
51+
52+
routes.any? do |r|
53+
next unless r[:options]
54+
55+
return true unless r[:options].is_a?(Hash)
56+
57+
return false if r[:options].empty?
58+
59+
r[:options].each_key do |key|
60+
RouteOptionsMessage::VALID_MANIFEST_ROUTE_OPTIONS.exclude?(key) &&
61+
errors.add(:base,
62+
message: "Route '#{r[:route]}' contains invalid route option '#{key}'. \
63+
Valid keys: '#{RouteOptionsMessage::VALID_MANIFEST_ROUTE_OPTIONS.join(', ')}'")
64+
end
65+
end
66+
end
67+
68+
def lb_algos_are_valid
69+
return if errors[:routes].present?
70+
71+
routes.each do |r|
72+
next unless r[:options] && r[:options][:'loadbalancing-algorithm']
73+
74+
lb_algo = r[:options][:'loadbalancing-algorithm']
75+
RouteOptionsMessage::VALID_LOADBALANCING_ALGORITHMS.exclude?(lb_algo) &&
76+
errors.add(:base,
77+
message: "Route '#{r[:route]}' contains invalid load-balancing algorithm '#{lb_algo}'. \
78+
Valid algorithms: '#{RouteOptionsMessage::VALID_LOADBALANCING_ALGORITHMS.join(', ')}'")
79+
end
80+
end
81+
8382
def routes_are_uris
8483
return if errors[:routes].present?
8584

lib/cloud_controller/app_manifest/manifest_route.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@ def self.parse(route, options=nil)
2323
end
2424

2525
def valid?
26-
if @attrs[:options] && !@attrs[:options].empty?
27-
return false if @attrs[:options].keys.any? { |key| RouteOptionsMessage::VALID_ROUTE_OPTIONS.exclude?(key) }
28-
# validation for loadbalancing algorithm
29-
return false if @attrs[:options][:lb_algo] && RouteOptionsMessage::VALID_LOADBALANCING_ALGORITHMS.exclude?(@attrs[:options][:lb_algo])
30-
end
31-
3226
return false if @attrs[:host].blank?
3327

3428
return SUPPORTED_TCP_SCHEMES.include?(@attrs[:scheme]) if @attrs[:port]

spec/unit/lib/cloud_controller/app_manifest/manifest_route_spec.rb

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,6 @@ module VCAP::CloudController
116116
expect(manifest_route.valid?).to be(false)
117117
end
118118
end
119-
120-
context 'when there is an invalid loadbalancing-algorithm' do
121-
let(:route) { 'http://example.com' }
122-
let(:options) { { 'loadbalancing-algorithm': 'invalid' } }
123-
124-
it 'is invalid' do
125-
manifest_route = ManifestRoute.parse(route, options)
126-
expect(manifest_route.valid?).to be(false)
127-
end
128-
end
129119
end
130120
end
131121

spec/unit/messages/manifest_routes_update_message_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ module VCAP::CloudController
238238
msg = ManifestRoutesUpdateMessage.new(body)
239239

240240
expect(msg.valid?).to be(false)
241+
expect(msg.errors.errors.length).to eq(1)
241242
expect(msg.errors.full_messages).to include("Route 'existing.example.com' contains invalid route option 'invalid'. Valid keys: 'loadbalancing-algorithm'")
242243
end
243244
end
@@ -275,6 +276,7 @@ module VCAP::CloudController
275276
msg = ManifestRoutesUpdateMessage.new(body)
276277

277278
expect(msg.valid?).to be(false)
279+
expect(msg.errors.errors.length).to eq(1)
278280
expect(msg.errors.full_messages).to include("Route 'existing.example.com' contains invalid load-balancing algorithm 'sushi'. \
279281
Valid algorithms: 'round-robin, least-connections'")
280282
end

0 commit comments

Comments
 (0)