@@ -44,39 +44,18 @@ def show
4444
4545 def create
4646 message = build_create_message ( hashed_params [ :body ] )
47-
48- service_instance = VCAP ::CloudController ::ServiceInstance . first ( guid : message . service_instance_guid )
49- resource_not_accessible! ( 'service instance' , message . service_instance_guid ) unless can_read_service_instance? ( service_instance )
47+ service_instance = get_service_instance! ( message . service_instance_guid )
5048
5149 case message . type
5250 when 'app'
53- app = VCAP ::CloudController ::AppModel . first ( guid : message . app_guid )
54- # TODO: create two unprocessable_app/si methods
55- resource_not_accessible! ( 'app' , message . app_guid ) unless can_access_resource? ( app )
56-
51+ app = get_app! ( message . app_guid )
5752 unauthorized! unless can_write_to_space? ( app . space )
5853
59- action = V3 ::ServiceCredentialBindingAppCreate . new ( user_audit_info , message . audit_hash )
60- binding = action . precursor ( service_instance , app : app , name : message . name , volume_mount_services_enabled : volume_services_enabled? )
61-
62- case service_instance
63- when ManagedServiceInstance
64- pollable_job_guid = enqueue_bind_job ( binding . guid , message )
65- head :accepted , 'Location' => url_builder . build_url ( path : "/v3/jobs/#{ pollable_job_guid } " )
66- when UserProvidedServiceInstance
67- action . bind ( binding )
68- render status : :created , json : Presenters ::V3 ::ServiceCredentialBindingPresenter . new ( binding ) . to_hash
69- end
54+ create_app_binding ( message , service_instance , app )
7055 when 'key'
7156 unauthorized! unless can_write_to_space? ( service_instance . space )
7257
73- V3 ::ServiceCredentialBindingKeyCreate . new . precursor (
74- service_instance ,
75- message . name
76- )
77-
78- head :not_implemented
79- return
58+ create_key_binding ( message , service_instance )
8059 end
8160 rescue V3 ::ServiceCredentialBindingAppCreate ::UnprocessableCreate ,
8261 V3 ::ServiceCredentialBindingKeyCreate ::UnprocessableCreate => e
@@ -140,20 +119,56 @@ def parameters
140119
141120 private
142121
122+ def create_key_binding ( message , service_instance )
123+ V3 ::ServiceCredentialBindingKeyCreate . new . precursor (
124+ service_instance ,
125+ message . name
126+ )
127+
128+ head :not_implemented
129+ return
130+ end
131+
143132 def build_create_message ( params )
144133 generic_message = ServiceCredentialBindingCreateMessage . new ( params )
145134 unprocessable! ( generic_message . errors . full_messages ) unless generic_message . valid?
146135
147136 specific_message = if generic_message . type == 'app'
148- ServiceCredentialAppBindingCreateMessage . new ( params )
149- else
150- ServiceCredentialKeyBindingCreateMessage . new ( params )
151- end
137+ ServiceCredentialAppBindingCreateMessage . new ( params )
138+ else
139+ ServiceCredentialKeyBindingCreateMessage . new ( params )
140+ end
152141
153142 unprocessable! ( specific_message . errors . full_messages ) unless specific_message . valid?
154143 specific_message
155144 end
156145
146+ def get_app! ( app_guid )
147+ app = VCAP ::CloudController ::AppModel . first ( guid : app_guid )
148+ unprocessable_resource! ( 'app' , app_guid ) unless can_access_resource? ( app )
149+ app
150+ end
151+
152+ def get_service_instance! ( service_instance_guid )
153+ service_instance = VCAP ::CloudController ::ServiceInstance . first ( guid : service_instance_guid )
154+ unprocessable_resource! ( 'service instance' , service_instance_guid ) unless can_read_service_instance? ( service_instance )
155+ service_instance
156+ end
157+
158+ def create_app_binding ( message , service_instance , app )
159+ action = V3 ::ServiceCredentialBindingAppCreate . new ( user_audit_info , message . audit_hash )
160+ binding = action . precursor ( service_instance , app : app , name : message . name , volume_mount_services_enabled : volume_services_enabled? )
161+
162+ case service_instance
163+ when ManagedServiceInstance
164+ pollable_job_guid = enqueue_bind_job ( binding . guid , message )
165+ head :accepted , 'Location' => url_builder . build_url ( path : "/v3/jobs/#{ pollable_job_guid } " )
166+ when UserProvidedServiceInstance
167+ action . bind ( binding )
168+ render status : :created , json : Presenters ::V3 ::ServiceCredentialBindingPresenter . new ( binding ) . to_hash
169+ end
170+ end
171+
157172 def enqueue_bind_job ( binding_guid , message )
158173 bind_job = VCAP ::CloudController ::V3 ::CreateBindingAsyncJob . new (
159174 :credential ,
@@ -176,7 +191,7 @@ def enqueue_unbind_job(binding_guid)
176191 pollable_job . guid
177192 end
178193
179- def resource_not_accessible !( resource , guid )
194+ def unprocessable_resource !( resource , guid )
180195 unprocessable! ( "The #{ resource } could not be found: '#{ guid } '" )
181196 end
182197
0 commit comments