44require 'actions/service_instance_unshare'
55require 'cloud_controller/errors/api_error'
66require 'actions/mixins/bindings_delete'
7+ require 'errors/sub_resource_error'
78
89module VCAP ::CloudController
910 module V3
@@ -13,9 +14,6 @@ class ServiceInstanceDelete
1314 class DeleteFailed < StandardError
1415 end
1516
16- class UnbindingOperatationInProgress < StandardError
17- end
18-
1917 DeleteStatus = Struct . new ( :finished , :operation ) . freeze
2018 DeleteStarted = -> ( operation ) { DeleteStatus . new ( false , operation ) }
2119 DeleteComplete = DeleteStatus . new ( true , nil ) . freeze
@@ -24,9 +22,10 @@ class UnbindingOperatationInProgress < StandardError
2422 PollingFinished = PollingStatus . new ( true , nil ) . freeze
2523 ContinuePolling = -> ( retry_after ) { PollingStatus . new ( false , retry_after ) }
2624
27- def initialize ( service_instance , event_repo )
25+ def initialize ( service_instance , event_repo , fail_if_in_progress : true )
2826 @service_instance = service_instance
2927 @service_event_repository = event_repo
28+ @fail_if_in_progress = fail_if_in_progress
3029 end
3130
3231 def blocking_operation_in_progress?
@@ -38,7 +37,11 @@ def delete
3837 operation_in_progress! if blocking_operation_in_progress?
3938
4039 errors = remove_associations
41- raise errors . first if errors . any?
40+ if errors . any?
41+ raise errors . first if @fail_if_in_progress # Single-shot callers fail on the first error
42+
43+ SubResourceError . raise_from ( errors )
44+ end
4245
4346 result = send_deprovison_to_broker
4447 if result [ :finished ]
@@ -49,6 +52,11 @@ def delete
4952 end
5053
5154 result
55+ rescue SubResourceError => e
56+ raise if !@fail_if_in_progress && e . any_in_progress? # re-raise SubResourceError so that root job continues to run
57+
58+ update_last_operation_with_failure ( e . message ) unless service_instance . operation_in_progress?
59+ raise e
5260 rescue StandardError => e
5361 update_last_operation_with_failure ( e . message ) unless service_instance . operation_in_progress?
5462 raise e
@@ -154,7 +162,9 @@ def unshare_all_spaces
154162
155163 unshare_action = ServiceInstanceUnshare . new
156164 space_guids . each_with_object ( [ ] ) do |space_guid , errors |
157- unshare_action . unshare ( service_instance , Space . first ( guid : space_guid ) , service_event_repository . user_audit_info )
165+ unshare_action . unshare ( service_instance , Space . first ( guid : space_guid ) , service_event_repository . user_audit_info , fail_if_in_progress : @fail_if_in_progress )
166+ rescue SubResourceError => e
167+ errors . concat ( e . underlying_errors )
158168 rescue StandardError => e
159169 errors << e
160170 end
@@ -182,14 +192,12 @@ def operation_in_progress!
182192 raise CloudController ::Errors ::ApiError . new_from_details ( 'AsyncServiceInstanceOperationInProgress' , service_instance . name )
183193 end
184194
185- def unbinding_operation_in_progress! ( binding )
186- raise UnbindingOperatationInProgress . new (
187- if binding . is_a? ( VCAP ::CloudController ::ServiceBinding )
188- "An operation for the service binding between app #{ binding . app . name } and service instance #{ service_instance . name } is in progress."
189- else
190- "An operation for a service binding of service instance #{ service_instance . name } is in progress."
191- end
192- )
195+ def unbinding_in_progress_message ( binding )
196+ if binding . is_a? ( VCAP ::CloudController ::ServiceBinding )
197+ "An operation for the service binding between app #{ binding . app . name } and service instance #{ service_instance . name } is in progress."
198+ else
199+ "An operation for a service binding of service instance #{ service_instance . name } is in progress."
200+ end
193201 end
194202
195203 def delete_failed! ( message )
0 commit comments