Skip to content

Commit ac114ba

Browse files
committed
fix: add service_id and plan_id to OSBAPI (service) fetch endpoints
Add optional service_id and plan_id query parameters to fetch_service_instance and fetch_service_binding broker requests, matching the pattern used by last_operation endpoints. Enables delegate/router brokers to route fetch requests without falling back to trying all sub-brokers. While optional per OSBAPI spec, these parameters are helpful hints that CC already has available. An aim of a platform (like CC) is to make it easier to write brokers - especially stateless brokers, so low cost hints like this bring the code in line with the product aims. - [x] I have reviewed the contributing guide - [x] I have viewed, signed, and submitted the Contributor License Agreement - [x] I have made this pull request to the `main` branch - [x] I have run all the unit tests using `bundle exec rake` - [ ] I have run CF Acceptance Tests
1 parent 6d5b46b commit ac114ba

File tree

4 files changed

+53
-8
lines changed

4 files changed

+53
-8
lines changed

lib/services/service_brokers/v2/client.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,13 @@ def fetch_and_handle_service_binding_last_operation(service_binding, user_guid:
312312
end
313313

314314
def fetch_service_instance(instance, user_guid: nil)
315-
path = service_instance_resource_path(instance)
315+
path = fetch_service_instance_path(instance)
316316
response = @http_client.get(path, user_guid:)
317317
@response_parser.parse_fetch_service_instance(path, response).deep_symbolize_keys
318318
end
319319

320320
def fetch_service_binding(service_binding, user_guid: nil)
321-
path = service_binding_resource_path(service_binding.guid, service_binding.service_instance_guid)
321+
path = fetch_service_binding_path(service_binding)
322322
response = @http_client.get(path, user_guid:)
323323
@response_parser.parse_fetch_service_binding(path, response).deep_symbolize_keys
324324
end
@@ -407,12 +407,28 @@ def service_binding_last_operation_path(service_binding)
407407
"#{service_binding_resource_path(service_binding.guid, service_binding.service_instance_guid)}/last_operation?#{query_params.to_query}"
408408
end
409409

410+
def fetch_service_binding_path(service_binding)
411+
query_params = {
412+
'service_id' => service_binding.service_instance.service.broker_provided_id,
413+
'plan_id' => service_binding.service_instance.service_plan.broker_provided_id
414+
}
415+
"#{service_binding_resource_path(service_binding.guid, service_binding.service_instance_guid)}?#{query_params.to_query}"
416+
end
417+
410418
def service_instance_resource_path(instance, opts={})
411419
path = "/v2/service_instances/#{instance.guid}"
412420
path += '?accepts_incomplete=true' if opts[:accepts_incomplete]
413421
path
414422
end
415423

424+
def fetch_service_instance_path(instance)
425+
query_params = {
426+
'service_id' => instance.service.broker_provided_id,
427+
'plan_id' => instance.service_plan.broker_provided_id
428+
}
429+
"#{service_instance_resource_path(instance)}?#{query_params.to_query}"
430+
end
431+
416432
def hashified_public_annotations(annotations)
417433
public_annotations = []
418434
annotations.each do |annotation|

spec/request/service_instances_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,6 +1515,7 @@ def check_filtered_instances(*instances)
15151515
to_return(status: 200, body: { state: 'succeeded' }.to_json, headers: {})
15161516

15171517
stub_request(:get, "#{instance.service.service_broker.broker_url}/v2/service_instances/#{instance.guid}").
1518+
with(query: { service_id: service_plan.service.unique_id, plan_id: service_plan.unique_id }).
15181519
to_return(status: 200, body: { dashboard_url: }.to_json)
15191520

15201521
execute_all_jobs(expected_successes: 1, expected_failures: 0)
@@ -2121,6 +2122,7 @@ def check_filtered_instances(*instances)
21212122
to_return(status: last_operation_status_code, body: last_operation_response.to_json, headers: {})
21222123

21232124
stub_request(:get, "#{service_instance.service_broker.broker_url}/v2/service_instances/#{service_instance.guid}").
2125+
with(query: { service_id: service_instance.service_plan.service.unique_id, plan_id: service_instance.service_plan.unique_id }).
21242126
to_return(status: 200, body: { dashboard_url: }.to_json)
21252127
end
21262128

spec/request/service_route_bindings_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,9 +844,11 @@
844844
let(:fetch_binding_body) do
845845
{ route_service_url: }
846846
end
847+
let(:fetch_binding_query) { { service_id: service_instance.service.broker_provided_id, plan_id: service_instance.service_plan.broker_provided_id } }
847848

848849
before do
849850
stub_request(:get, broker_bind_url).
851+
with(query: fetch_binding_query).
850852
to_return(status: fetch_binding_status_code, body: fetch_binding_body.to_json, headers: {})
851853
end
852854

@@ -856,6 +858,7 @@
856858
encoded_user_guid = Base64.strict_encode64("{\"user_id\":\"#{user.guid}\"}")
857859
expect(
858860
a_request(:get, broker_bind_url).with(
861+
query: fetch_binding_query,
859862
headers: { 'X-Broker-Api-Originating-Identity' => "cloudfoundry #{encoded_user_guid}" }
860863
)
861864
).to have_been_made.once
@@ -1634,12 +1637,14 @@
16341637
context 'managed service instances' do
16351638
let(:broker_base_url) { service_instance.service_broker.broker_url }
16361639
let(:broker_fetch_binding_url) { "#{broker_base_url}/v2/service_instances/#{service_instance.guid}/service_bindings/#{binding.guid}" }
1640+
let(:broker_fetch_binding_query) { { service_id: service_instance.service.broker_provided_id, plan_id: service_instance.service_plan.broker_provided_id } }
16371641
let(:broker_status_code) { 200 }
16381642
let(:broker_response) { { parameters: { abra: 'kadabra', kadabra: 'alakazan' } } }
16391643
let(:parameters_response) { { code: 200, response_object: broker_response[:parameters] } }
16401644

16411645
before do
16421646
stub_request(:get, broker_fetch_binding_url).
1647+
with(query: broker_fetch_binding_query).
16431648
to_return(status: broker_status_code, body: broker_response.to_json, headers: {})
16441649
end
16451650

@@ -1679,6 +1684,7 @@
16791684
expect(
16801685
a_request(:get, broker_fetch_binding_url).
16811686
with(
1687+
query: broker_fetch_binding_query,
16821688
headers: { 'X-Broker-Api-Originating-Identity' => "cloudfoundry #{encoded_user_guid}" }
16831689
)
16841690
).to have_been_made.once
@@ -1719,6 +1725,7 @@
17191725
context 'when the broker returns params with mixed data types' do
17201726
before do
17211727
stub_request(:get, broker_fetch_binding_url).
1728+
with(query: broker_fetch_binding_query).
17221729
to_return(status: broker_status_code, body: "{\"parameters\":#{parameters_mixed_data_types_as_json_string}}")
17231730
end
17241731

spec/unit/lib/services/service_brokers/v2/client_spec.rb

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2275,10 +2275,15 @@ module VCAP::Services::ServiceBrokers::V2
22752275
allow(http_client).to receive(:get).and_return(broker_response)
22762276
end
22772277

2278-
it 'makes a get request with the correct path' do
2278+
it 'makes a get request with the correct path including service_id and plan_id' do
22792279
client.fetch_service_binding(binding)
2280+
2281+
service_id = binding.service_instance.service.broker_provided_id
2282+
plan_id = binding.service_instance.service_plan.broker_provided_id
2283+
query_params = "?plan_id=#{plan_id}&service_id=#{service_id}"
2284+
22802285
expect(http_client).to have_received(:get).with(
2281-
"/v2/service_instances/#{binding.service_instance.guid}/service_bindings/#{binding.guid}",
2286+
"/v2/service_instances/#{binding.service_instance.guid}/service_bindings/#{binding.guid}#{query_params}",
22822287
{ user_guid: nil }
22832288
)
22842289
end
@@ -2293,8 +2298,13 @@ module VCAP::Services::ServiceBrokers::V2
22932298

22942299
it 'makes a request with the correct user_guid' do
22952300
client.fetch_service_binding(binding, user_guid:)
2301+
2302+
service_id = binding.service_instance.service.broker_provided_id
2303+
plan_id = binding.service_instance.service_plan.broker_provided_id
2304+
query_params = "?plan_id=#{plan_id}&service_id=#{service_id}"
2305+
22962306
expect(http_client).to have_received(:get).with(
2297-
"/v2/service_instances/#{binding.service_instance.guid}/service_bindings/#{binding.guid}",
2307+
"/v2/service_instances/#{binding.service_instance.guid}/service_bindings/#{binding.guid}#{query_params}",
22982308
{ user_guid: }
22992309
)
23002310
end
@@ -2309,10 +2319,15 @@ module VCAP::Services::ServiceBrokers::V2
23092319
allow(http_client).to receive(:get).and_return(broker_response)
23102320
end
23112321

2312-
it 'makes a get request with the correct path' do
2322+
it 'makes a get request with the correct path including service_id and plan_id' do
23132323
client.fetch_service_instance(instance)
2324+
2325+
service_id = instance.service.broker_provided_id
2326+
plan_id = instance.service_plan.broker_provided_id
2327+
query_params = "?plan_id=#{plan_id}&service_id=#{service_id}"
2328+
23142329
expect(http_client).to have_received(:get).with(
2315-
"/v2/service_instances/#{instance.guid}",
2330+
"/v2/service_instances/#{instance.guid}#{query_params}",
23162331
{ user_guid: nil }
23172332
)
23182333
end
@@ -2327,8 +2342,13 @@ module VCAP::Services::ServiceBrokers::V2
23272342

23282343
it 'makes a request with the correct user_guid' do
23292344
client.fetch_service_instance(instance, user_guid:)
2345+
2346+
service_id = instance.service.broker_provided_id
2347+
plan_id = instance.service_plan.broker_provided_id
2348+
query_params = "?plan_id=#{plan_id}&service_id=#{service_id}"
2349+
23302350
expect(http_client).to have_received(:get).with(
2331-
"/v2/service_instances/#{instance.guid}",
2351+
"/v2/service_instances/#{instance.guid}#{query_params}",
23322352
{ user_guid: }
23332353
)
23342354
end

0 commit comments

Comments
 (0)