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

Commit 94a57c5

Browse files
committed
Consolidate communication with the routing API
* centralize calls out to routing API to instance method with memoized global variable on the domain * other minor refactors of where error messages get created [finishes #169299174] Authored-by: Mona Mohebbi <mmohebbi@pivotal.io>
1 parent e3f0e2d commit 94a57c5

12 files changed

Lines changed: 39 additions & 47 deletions

File tree

app/actions/route_create.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ def initialize(user_audit_info)
88
end
99

1010
def create(message:, space:, domain:, manifest_triggered: false)
11+
validate_tcp_route!(domain, message)
12+
1113
route = Route.new(
1214
host: message.host || '',
1315
path: message.path || '',
@@ -43,9 +45,15 @@ def create(message:, space:, domain:, manifest_triggered: false)
4345

4446
private
4547

48+
def validate_tcp_route!(domain, message)
49+
if domain.router_group_guid.present? && router_group(domain).nil?
50+
error!('Route could not be created because the specified domain does not have a valid router group.')
51+
end
52+
end
53+
4654
def port(message, domain)
4755
generated_port = if !message.requested?(:port) && domain.protocols.include?('tcp')
48-
PortGenerator.generate_port(domain.guid)
56+
PortGenerator.generate_port(domain.guid, router_group(domain).reservable_ports)
4957
else
5058
message.port || 0
5159
end
@@ -54,6 +62,10 @@ def port(message, domain)
5462
generated_port
5563
end
5664

65+
def router_group(domain)
66+
@router_group ||= domain.router_group
67+
end
68+
5769
def route_crd_client
5870
@route_crd_client ||= CloudController::DependencyLocator.instance.route_crd_client
5971
end
@@ -135,7 +147,11 @@ def validation_error_host!(error, host, domain)
135147
end
136148

137149
if error.errors.on(:host)&.include?(:host_and_path_domain_tcp)
138-
error!("Routes with protocol 'tcp' do not support paths or hosts.")
150+
error!('Hosts are not supported for TCP routes.')
151+
end
152+
153+
if error.errors.on(:path)&.include?(:host_and_path_domain_tcp)
154+
error!('Paths are not supported for TCP routes.')
139155
end
140156
end
141157

app/controllers/runtime/routes_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def self.translate_validation_exception(e, attributes)
6868
'Try a different port or request a random one be generated for you.'
6969
)
7070
end
71-
if e.errors.on(:host) == [:host_and_path_domain_tcp]
71+
if e.errors.on(:host) == [:host_and_path_domain_tcp] || e.errors.on(:path) == [:host_and_path_domain_tcp]
7272
return CloudController::Errors::ApiError.new_from_details('RouteInvalid', 'Host and path are not supported, as domain belongs to a TCP router group.')
7373
end
7474

app/controllers/v3/routes_controller.rb

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def create
6767
unprocessable_space! unless space
6868
unprocessable_domain! unless domain
6969
unauthorized! unless permission_queryer.can_write_to_space?(space.guid)
70-
validate_domain_for_route_create!(domain, message)
70+
unprocessable_wildcard! if domain.shared? && message.wildcard? && !permission_queryer.can_write_globally?
7171

7272
route = RouteCreate.new(user_audit_info).create(message: message, space: space, domain: domain)
7373

@@ -236,19 +236,6 @@ def unprocessable_protocol_path!
236236
unprocessable!('Paths are not supported for TCP routes.')
237237
end
238238

239-
def validate_domain_for_route_create!(domain, message)
240-
unprocessable_wildcard! if domain.shared? && message.wildcard? && !permission_queryer.can_write_globally?
241-
242-
if domain.protocols.include?('tcp')
243-
unprocessable_protocol_host! if message.host
244-
unprocessable_protocol_path! if message.path
245-
end
246-
247-
if domain.router_group_guid && routing_api_client.router_group(domain.router_group_guid).nil?
248-
unprocessable!('Route could not be created because the specified domain does not have a valid router group.')
249-
end
250-
end
251-
252239
def validate_app_guids!(apps_hash, desired_app_guids)
253240
existing_app_guids = apps_hash.keys
254241

app/models/runtime/shared_domain.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ def tcp?
7373

7474
if router_group_guid.present?
7575
if @router_group_type.nil?
76-
router_group = routing_api_client.router_group(router_group_guid)
7776
@router_group_type = router_group.nil? ? '' : router_group.type
7877
end
7978

@@ -84,8 +83,6 @@ def tcp?
8483
end
8584

8685
def router_group
87-
raise RoutingApi::RoutingApiDisabled unless routing_api_client.enabled?
88-
8986
@router_group ||= routing_api_client.router_group(router_group_guid)
9087
end
9188

@@ -99,12 +96,12 @@ def internal?
9996
!!internal
10097
end
10198

102-
private
103-
10499
def routing_api_client
105100
@routing_api_client ||= CloudController::DependencyLocator.instance.routing_api_client
106101
end
107102

103+
private
104+
108105
def validate_internal_domain
109106
if router_group_guid.present?
110107
errors.add(:router_group_guid, 'cannot be specified for internal domains')

app/presenters/v3/domain_presenter.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ def build_links
8282
}
8383
end
8484

85-
routing_client = CloudController::DependencyLocator.instance.routing_api_client
86-
if domain.router_group_guid && routing_client.enabled?
85+
if domain.router_group_guid && domain.routing_api_client.enabled?
8786
links[:router_group] = {
8887
href: url_builder.build_url(path: "/routing/v1/router_groups/#{domain.router_group_guid}")
8988
}

lib/cloud_controller/port_generator.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
module VCAP::CloudController
22
class PortGenerator
33
class << self
4-
def generate_port(domain_guid, possible_ports=nil)
4+
def generate_port(domain_guid, possible_ports)
55
domain = SharedDomain.where(guid: domain_guid).first
66
router_group_guid = domain.router_group_guid
77

88
unavailable_ports = Route.join(:domains, id: :domain_id).
99
where(router_group_guid: router_group_guid).
1010
select_map(:port)
1111

12-
possible_ports ||= domain.router_group.reservable_ports
1312
available_ports = possible_ports - unavailable_ports
1413

1514
size = available_ports.size

lib/cloud_controller/route_validator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def validate_host_not_included
5252

5353
def validate_path_not_included
5454
unless route.path.blank?
55-
route.errors.add(:host, :host_and_path_domain_tcp)
55+
route.errors.add(:path, :host_and_path_domain_tcp)
5656
end
5757
end
5858

spec/request/routes_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2182,6 +2182,7 @@
21822182

21832183
context 'when the routing API is unavailable' do
21842184
before do
2185+
allow(routing_api_client).to receive(:enabled?).and_return true
21852186
allow(routing_api_client).to receive(:router_group).and_raise VCAP::CloudController::RoutingApi::RoutingApiUnavailable
21862187
end
21872188

@@ -2196,7 +2197,7 @@
21962197
context 'when the routing API is disabled' do
21972198
before do
21982199
allow(routing_api_client).to receive(:enabled?).and_return false
2199-
allow(routing_api_client).to receive(:router_group).and_return router_group
2200+
allow(routing_api_client).to receive(:router_group).and_raise VCAP::CloudController::RoutingApi::RoutingApiDisabled
22002201
end
22012202

22022203
it 'returns a 503 with a helpful error message' do

spec/unit/actions/route_create_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ module VCAP::CloudController
697697
it 'errors with a helpful error message' do
698698
expect {
699699
subject.create(message: message, space: space, domain: domain)
700-
}.to raise_error(RouteCreate::Error, "Routes with protocol 'tcp' do not support paths or hosts.")
700+
}.to raise_error(RouteCreate::Error, 'Paths are not supported for TCP routes.')
701701
end
702702
end
703703
end

spec/unit/lib/cloud_controller/port_generator_spec.rb

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,6 @@ module VCAP::CloudController
3535
expect(port).to eq(-1)
3636
end
3737

38-
context 'when possible ports are not provided' do
39-
let(:router_group1) { double('router_group1', type: router_group_type, guid: router_group_guid1, reservable_ports: Array(1024..65535)) }
40-
let(:routing_api_client) { instance_double(VCAP::CloudController::RoutingApi::Client) }
41-
42-
before do
43-
allow_any_instance_of(CloudController::DependencyLocator).to receive(:routing_api_client).and_return(routing_api_client)
44-
allow(routing_api_client).to receive(:enabled?).and_return(true)
45-
allow(routing_api_client).to receive(:router_group).and_return(router_group1)
46-
end
47-
it 'generates a port' do
48-
port = PortGenerator.generate_port(domain_guid1)
49-
50-
expect((1024..65535).cover?(port)).to eq(true)
51-
end
52-
end
53-
5438
context 'when there are multi router groups' do
5539
let(:router_group_guid2) { 'router-group-guid2' }
5640
let(:router_group2) { double('router_group2', type: router_group_type, guid: router_group_guid2) }

0 commit comments

Comments
 (0)