diff --git a/app/controllers/v3/service_route_bindings_controller.rb b/app/controllers/v3/service_route_bindings_controller.rb index 6255462cf74..3e94a0154ea 100644 --- a/app/controllers/v3/service_route_bindings_controller.rb +++ b/app/controllers/v3/service_route_bindings_controller.rb @@ -108,6 +108,8 @@ def destroy def parameters route_binding_not_found! unless @route_binding && can_read_from_space?(@route_binding.route.space) + not_found_with_message!(@route_binding) unless @route_binding.create_succeeded? + unauthorized! unless can_write_to_active_space?(@route_binding.route.space) suspended! unless is_space_active?(@route_binding.route.space) @@ -244,6 +246,12 @@ def route_binding_not_found! resource_not_found!(:service_route_binding) end + def not_found_with_message!(service_route_binding) + operation = service_route_binding.last_operation.type == 'create' ? 'Creation' : 'Deletion' + state = service_route_binding.last_operation.state + resource_not_found_with_message!("#{operation} of route binding #{state}") + end + def service_instance_not_found!(guid) unprocessable!("The service instance could not be found: #{guid}") end diff --git a/spec/request/service_route_bindings_spec.rb b/spec/request/service_route_bindings_spec.rb index d0946585f8a..3023220bd57 100644 --- a/spec/request/service_route_bindings_spec.rb +++ b/spec/request/service_route_bindings_spec.rb @@ -1628,7 +1628,24 @@ end end - context 'when there is an operation in progress' do + context "when last binding operation is in 'create succeeded' state" do + before do + binding.save_with_new_operation({}, { + type: 'create', + state: 'succeeded' + }) + end + + it 'returns the parameters' do + api_call.call(admin_headers) + expect(last_response).to have_status_code(200) + expect(parsed_response).to include( + { 'abra' => 'kadabra', 'kadabra' => 'alakazan' } + ) + end + end + + context "when last binding operation is in 'create in progress' state" do before do binding.save_with_new_operation({}, { type: 'create', @@ -1638,16 +1655,73 @@ it 'returns the appropriate error' do api_call.call(admin_headers) - expect(last_response).to have_status_code(422) + expect(last_response).to have_status_code(404) expect(parsed_response['errors']).to include( include({ - 'detail' => 'There is an operation in progress for the service route binding.', - 'title' => 'CF-UnprocessableEntity', - 'code' => 10_008 + 'detail' => 'Creation of route binding in progress', + 'title' => 'CF-ResourceNotFound', + 'code' => 10_010 }) ) end end + + context "when last binding operation is in 'create failed' state" do + before do + binding.save_with_new_operation({}, { + type: 'create', + state: 'failed' + }) + end + + it 'returns an error' do + api_call.call(admin_headers) + expect(last_response).to have_status_code(404) + expect(parsed_response['errors']).to include(include({ + 'detail' => 'Creation of route binding failed', + 'title' => 'CF-ResourceNotFound', + 'code' => 10_010 + })) + end + end + + context "when last binding operation is in 'delete failed' state" do + before do + binding.save_with_new_operation({}, { + type: 'delete', + state: 'failed' + }) + end + + it 'returns an error' do + api_call.call(admin_headers) + expect(last_response).to have_status_code(404) + expect(parsed_response['errors']).to include(include({ + 'detail' => 'Deletion of route binding failed', + 'title' => 'CF-ResourceNotFound', + 'code' => 10_010 + })) + end + end + + context "when last binding operation is in 'delete in progress' state" do + before do + binding.save_with_new_operation({}, { + type: 'delete', + state: 'in progress' + }) + end + + it 'returns an error' do + api_call.call(admin_headers) + expect(last_response).to have_status_code(404) + expect(parsed_response['errors']).to include(include({ + 'detail' => 'Deletion of route binding in progress', + 'title' => 'CF-ResourceNotFound', + 'code' => 10_010 + })) + end + end end context 'user provided service instances' do