Skip to content
This repository was archived by the owner on Jun 2, 2021. It is now read-only.

Commit 576213f

Browse files
FelisiaMpivotal-marcela-campo
authored andcommitted
v3(services) Created POST message for key type credential bindigns
Extracted the Create service creadential binding message into two specific one for app and key type [#174145595](https://www.pivotaltracker.com/story/show/174145595)
1 parent 8f0238c commit 576213f

8 files changed

Lines changed: 781 additions & 527 deletions

app/controllers/v3/service_credential_bindings_controller.rb

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
require 'messages/service_credential_binding_list_message'
88
require 'messages/service_credential_binding_show_message'
99
require 'messages/service_credential_binding_create_message'
10+
require 'messages/service_credential_app_binding_create_message'
11+
require 'messages/service_credential_key_binding_create_message'
1012
require 'decorators/include_binding_app_decorator'
1113
require 'decorators/include_binding_service_instance_decorator'
1214
require 'jobs/v3/create_service_credential_binding_job_actor'
@@ -40,26 +42,30 @@ def show
4042
end
4143

4244
def create
43-
message = ServiceCredentialBindingCreateMessage.new(hashed_params[:body])
44-
unprocessable!(message.errors.full_messages) unless message.valid?
45-
46-
service_instance = VCAP::CloudController::ServiceInstance.first(guid: message.service_instance_guid)
47-
resource_not_accessible!('service instance', message.service_instance_guid) unless can_read_service_instance?(service_instance)
48-
49-
app = VCAP::CloudController::AppModel.first(guid: message.app_guid)
50-
resource_not_accessible!('app', message.app_guid) unless can_access_resource?(app)
51-
unauthorized! unless can_write_to_space?(app.space)
52-
53-
action = V3::ServiceCredentialBindingCreate.new(user_audit_info, message.audit_hash)
54-
binding = action.precursor(service_instance, app: app, name: message.name, volume_mount_services_enabled: volume_services_enabled?)
55-
56-
case service_instance
57-
when ManagedServiceInstance
58-
pollable_job_guid = enqueue_bind_job(binding.guid, message)
59-
head :accepted, 'Location' => url_builder.build_url(path: "/v3/jobs/#{pollable_job_guid}")
60-
when UserProvidedServiceInstance
61-
action.bind(binding)
62-
render status: :created, json: Presenters::V3::ServiceCredentialBindingPresenter.new(binding).to_hash
45+
message = build_create_message(hashed_params[:body])
46+
47+
if message.type == 'app'
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)
50+
51+
app = VCAP::CloudController::AppModel.first(guid: message.app_guid)
52+
resource_not_accessible!('app', message.app_guid) unless can_access_resource?(app)
53+
unauthorized! unless can_write_to_space?(app.space)
54+
55+
action = V3::ServiceCredentialBindingCreate.new(user_audit_info, message.audit_hash)
56+
binding = action.precursor(service_instance, app: app, name: message.name, volume_mount_services_enabled: volume_services_enabled?)
57+
58+
case service_instance
59+
when ManagedServiceInstance
60+
pollable_job_guid = enqueue_bind_job(binding.guid, message)
61+
head :accepted, 'Location' => url_builder.build_url(path: "/v3/jobs/#{pollable_job_guid}")
62+
when UserProvidedServiceInstance
63+
action.bind(binding)
64+
render status: :created, json: Presenters::V3::ServiceCredentialBindingPresenter.new(binding).to_hash
65+
end
66+
else
67+
head :not_implemented
68+
return
6369
end
6470
rescue V3::ServiceCredentialBindingCreate::UnprocessableCreate => e
6571
unprocessable!(e.message)
@@ -122,6 +128,20 @@ def parameters
122128

123129
private
124130

131+
def build_create_message(params)
132+
generic_message = ServiceCredentialBindingCreateMessage.new(params)
133+
unprocessable!(generic_message.errors.full_messages) unless generic_message.valid?
134+
135+
specific_message = if generic_message.type == 'app'
136+
ServiceCredentialAppBindingCreateMessage.new(params)
137+
else
138+
ServiceCredentialKeyBindingCreateMessage.new(params)
139+
end
140+
141+
unprocessable!(specific_message.errors.full_messages) unless specific_message.valid?
142+
specific_message
143+
end
144+
125145
def enqueue_bind_job(binding_guid, message)
126146
bind_job = VCAP::CloudController::V3::CreateBindingAsyncJob.new(
127147
:credential,
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module VCAP::CloudController
2+
class ServiceCredentialAppBindingCreateMessage < ServiceCredentialBindingCreateMessage
3+
def relationships_message
4+
@relationships_message ||= Relationships.new(relationships&.deep_symbolize_keys)
5+
end
6+
7+
delegate :app_guid, to: :relationships_message
8+
9+
class Relationships < ServiceCredentialBindingCreateMessage::Relationships
10+
register_allowed_keys [:app]
11+
validates_with NoAdditionalKeysValidator
12+
13+
validates :app, presence: true, allow_nil: false, to_one_relationship: true
14+
15+
def app_guid
16+
HashUtils.dig(app, :data, :guid)
17+
end
18+
end
19+
end
20+
end

app/messages/service_credential_binding_create_message.rb

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,24 @@ class ServiceCredentialBindingCreateMessage < BaseMessage
44
validates_with NoAdditionalKeysValidator, RelationshipValidator
55
validates :parameters, hash: true, allow_nil: true
66
validates :type, allow_blank: false, inclusion: {
7-
in: %w(app),
8-
message: "must be 'app'"
7+
in: %w(app key),
8+
message: "must be 'app' or 'key'"
99
}
1010

1111
delegate :service_instance_guid, to: :relationships_message
12-
delegate :app_guid, to: :relationships_message
1312

1413
def relationships_message
1514
@relationships_message ||= Relationships.new(relationships&.deep_symbolize_keys)
1615
end
1716

1817
class Relationships < BaseMessage
19-
register_allowed_keys [:service_instance, :app]
20-
validates_with NoAdditionalKeysValidator
18+
register_allowed_keys [:service_instance]
2119

2220
validates :service_instance, presence: true, allow_nil: false, to_one_relationship: true
23-
validates :app, presence: true, allow_nil: false, to_one_relationship: true
2421

2522
def service_instance_guid
2623
HashUtils.dig(service_instance, :data, :guid)
2724
end
28-
29-
def app_guid
30-
HashUtils.dig(app, :data, :guid)
31-
end
3225
end
3326
end
3427
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module VCAP::CloudController
2+
class ServiceCredentialKeyBindingCreateMessage < ServiceCredentialBindingCreateMessage
3+
validates :name, string: true, presence: true
4+
5+
def relationships_message
6+
@relationships_message ||= Relationships.new(relationships&.deep_symbolize_keys)
7+
end
8+
9+
class Relationships < ServiceCredentialBindingCreateMessage::Relationships
10+
validates_with NoAdditionalKeysValidator
11+
end
12+
end
13+
end

0 commit comments

Comments
 (0)