@@ -2578,7 +2578,8 @@ def check_filtered_instances(*instances)
25782578 end
25792579
25802580 describe 'DELETE /v3/service_instances/:guid' do
2581- let ( :api_call ) { lambda { |user_headers | delete "/v3/service_instances/#{ instance . guid } " , '{}' , user_headers } }
2581+ let ( :query_params ) { '' }
2582+ let ( :api_call ) { lambda { |user_headers | delete "/v3/service_instances/#{ instance . guid } ?#{ query_params } " , '{}' , user_headers } }
25822583
25832584 context 'permissions' do
25842585 let! ( :instance ) { VCAP ::CloudController ::UserProvidedServiceInstance . make ( space : space ) }
@@ -2602,7 +2603,9 @@ def check_filtered_instances(*instances)
26022603 end
26032604
26042605 context 'user provided service instances' do
2605- let! ( :instance ) { VCAP ::CloudController ::UserProvidedServiceInstance . make ( space : space ) }
2606+ let! ( :instance ) { VCAP ::CloudController ::UserProvidedServiceInstance . make ( space : space , route_service_url : 'https://banana.example.com/' ) }
2607+ let ( :instance_labels ) { VCAP ::CloudController ::ServiceInstanceLabelModel . where ( service_instance : instance ) }
2608+ let ( :instance_annotations ) { VCAP ::CloudController ::ServiceInstanceAnnotationModel . where ( service_instance : instance ) }
26062609
26072610 before do
26082611 VCAP ::CloudController ::ServiceInstanceLabelModel . make ( key_name : 'fruit' , value : 'banana' , service_instance : instance )
@@ -2620,6 +2623,31 @@ def check_filtered_instances(*instances)
26202623 expect ( VCAP ::CloudController ::ServiceInstanceLabelModel . where ( service_instance : instance ) . all ) . to be_empty
26212624 expect ( VCAP ::CloudController ::ServiceInstanceAnnotationModel . where ( service_instance : instance ) . all ) . to be_empty
26222625 end
2626+
2627+ it 'fails to delete when there are bindings' do
2628+ VCAP ::CloudController ::ServiceBinding . make ( service_instance : instance )
2629+ api_call . call ( admin_headers )
2630+ expect ( last_response ) . to have_status_code ( 422 )
2631+ end
2632+
2633+ context 'with purge' do
2634+ let ( :query_params ) { 'purge=true' }
2635+ before ( :each ) do
2636+ @binding = VCAP ::CloudController ::ServiceBinding . make ( service_instance : instance )
2637+ @route = VCAP ::CloudController ::RouteBinding . make ( service_instance : instance )
2638+ end
2639+
2640+ it 'deletes the instance and the related resources' do
2641+ api_call . call ( admin_headers )
2642+ expect ( last_response ) . to have_status_code ( 204 )
2643+
2644+ expect { instance . reload } . to raise_error Sequel ::NoExistingObject
2645+ expect { @binding . reload } . to raise_error Sequel ::NoExistingObject
2646+ expect { @route . reload } . to raise_error Sequel ::NoExistingObject
2647+ expect ( instance_labels . count ) . to eq ( 0 )
2648+ expect ( instance_annotations . count ) . to eq ( 0 )
2649+ end
2650+ end
26232651 end
26242652
26252653 context 'managed service instance' do
@@ -3092,6 +3120,62 @@ def check_filtered_instances(*instances)
30923120 end
30933121 end
30943122
3123+ context 'when there are associations' do
3124+ RSpec . shared_examples 'associations not empty' do
3125+ it 'returns a 422 Unprocessable Entity' do
3126+ api_call . call ( admin_headers )
3127+ expect ( last_response ) . to have_status_code ( 422 )
3128+ response = parsed_response [ 'errors' ] . first
3129+ expect ( response ) . to include ( 'title' => 'CF-AssociationNotEmpty' )
3130+ expect ( response ) . to include ( 'detail' => include ( 'Please delete the service_bindings, service_keys, and routes associations for your service_instances.' ) )
3131+ end
3132+ end
3133+
3134+ let ( :service_offering ) { VCAP ::CloudController ::Service . make ( requires : %w( route_forwarding ) ) }
3135+ let ( :service_plan ) { VCAP ::CloudController ::ServicePlan . make ( service : service_offering ) }
3136+ let ( :instance ) { VCAP ::CloudController ::ManagedServiceInstance . make ( space : space , service_plan : service_plan ) }
3137+
3138+ describe 'service bindings' do
3139+ before ( :each ) { VCAP ::CloudController ::ServiceBinding . make ( service_instance : instance ) }
3140+ it_should_behave_like 'associations not empty'
3141+ end
3142+
3143+ describe 'service keys' do
3144+ before ( :each ) { VCAP ::CloudController ::ServiceKey . make ( service_instance : instance ) }
3145+ it_should_behave_like 'associations not empty'
3146+ end
3147+
3148+ describe 'route bindings' do
3149+ before ( :each ) { VCAP ::CloudController ::RouteBinding . make ( service_instance : instance ) }
3150+ it_should_behave_like 'associations not empty'
3151+ end
3152+
3153+ context 'but purge is true' do
3154+ let ( :query_params ) { 'purge=true' }
3155+ before ( :each ) do
3156+ @binding = VCAP ::CloudController ::ServiceBinding . make ( service_instance : instance )
3157+ @key = VCAP ::CloudController ::ServiceKey . make ( service_instance : instance )
3158+ @route = VCAP ::CloudController ::RouteBinding . make ( service_instance : instance )
3159+
3160+ api_call . call ( admin_headers )
3161+ end
3162+
3163+ it 'removes all associations' do
3164+ expect { @binding . reload } . to raise_error Sequel ::NoExistingObject
3165+ expect { @key . reload } . to raise_error Sequel ::NoExistingObject
3166+ expect { @route . reload } . to raise_error Sequel ::NoExistingObject
3167+ end
3168+
3169+ it 'deletes the service instance' do
3170+ expect { instance . reload } . to raise_error Sequel ::NoExistingObject
3171+ end
3172+
3173+ it 'responds with 204' do
3174+ expect ( last_response ) . to have_status_code ( 204 )
3175+ end
3176+ end
3177+ end
3178+
30953179 context 'when the creation is still in progress' do
30963180 before do
30973181 instance . save_with_new_operation ( { } , {
@@ -3173,20 +3257,6 @@ def check_filtered_instances(*instances)
31733257 end
31743258 end
31753259
3176- context 'when associations are not empty' do
3177- let ( :instance ) { VCAP ::CloudController ::ServiceInstance . make ( space : space ) }
3178-
3179- it 'returns a 422 Unprocessable Entity' do
3180- VCAP ::CloudController ::ServiceBinding . make ( service_instance : instance )
3181-
3182- api_call . call ( admin_headers )
3183- expect ( last_response ) . to have_status_code ( 422 )
3184- response = parsed_response [ 'errors' ] . first
3185- expect ( response ) . to include ( 'title' => 'CF-AssociationNotEmpty' )
3186- expect ( response ) . to include ( 'detail' => 'Please delete the service_bindings, service_keys, and routes associations for your service_instances.' )
3187- end
3188- end
3189-
31903260 context 'when the service instance does not exist' do
31913261 let ( :instance ) { Struct . new ( :guid ) . new ( 'some-fake-guid' ) }
31923262 it 'returns a 404' do
0 commit comments