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

Commit 4dc219d

Browse files
v3(services): create service key binding
* v3(services): Add operations table for service keys [#174147679](https://www.pivotaltracker.com/story/show/174147679) * v3(services): add service key binding actor [#174147679](https://www.pivotaltracker.com/story/show/174147679) * v3(services): add last operation to service keys [#174147679](https://www.pivotaltracker.com/story/show/174147679) * v3(services): create key action binds and polls last operation [#174147679](https://www.pivotaltracker.com/story/show/174147679) * v3(services): service key controller and client implementation This commit reuses the same bind method in the client for service keys, service credential and service route bindings. Before this, service keys had their own create_service_key client method, which is still available for v2 [#174147679](https://www.pivotaltracker.com/story/show/174147679) * v3(services): configure resource urls and logging keys used for service key bindings [#174147679](https://www.pivotaltracker.com/story/show/174147679) * v3(tests): extract common tests for ServiceBindings and ServiceKey creation [#174147679](https://www.pivotaltracker.com/story/show/174147679) * v3(style): fix rubocop issues [#174147679](https://www.pivotaltracker.com/story/show/174147679) * v3(tests): fix service key bindings tests [#174147679](https://www.pivotaltracker.com/story/show/174147679) * v3(services): refactor service bindings creation [#174147679](https://www.pivotaltracker.com/story/show/174147679) * v3(services): async key bindings tests [#174163869](https://www.pivotaltracker.com/story/show/174163869) * v3(services): create key changes from PR [#174163869](https://www.pivotaltracker.com/story/show/174163869) * v3(services): Reorder signature for enqueue_bind_job Co-authored-by: Felisia Martini <fmartini@pivotal.io>
1 parent e8e660f commit 4dc219d

33 files changed

Lines changed: 1213 additions & 712 deletions

app/actions/service_credential_binding_app_create.rb

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,30 +45,6 @@ def precursor(service_instance, app: nil, name: nil, volume_mount_services_enabl
4545

4646
private
4747

48-
def complete_binding_and_save(binding, binding_details, last_operation)
49-
binding.save_with_attributes_and_new_operation(
50-
binding_details.symbolize_keys.slice(*PERMITTED_BINDING_ATTRIBUTES),
51-
{
52-
type: 'create',
53-
state: last_operation[:state],
54-
description: last_operation[:description]
55-
}
56-
)
57-
event_repository.record_create(binding, @user_audit_info, @audit_hash, manifest_triggered: false)
58-
end
59-
60-
def save_incomplete_binding(binding, broker_operation)
61-
binding.save_with_attributes_and_new_operation(
62-
{},
63-
{
64-
type: 'create',
65-
state: 'in progress',
66-
broker_provided_operation: broker_operation
67-
}
68-
)
69-
event_repository.record_start_create(binding, @user_audit_info, @audit_hash, manifest_triggered: false)
70-
end
71-
7248
def validate!(service_instance, app, volume_mount_services_enabled)
7349
app_is_required! unless app.present?
7450
space_mismatch! unless all_space_guids(service_instance).include? app.space.guid
@@ -81,6 +57,10 @@ def validate!(service_instance, app, volume_mount_services_enabled)
8157
end
8258
end
8359

60+
def permitted_binding_attributes
61+
PERMITTED_BINDING_ATTRIBUTES
62+
end
63+
8464
def all_space_guids(service_instance)
8565
(service_instance.shared_spaces + [service_instance.space]).map(&:guid)
8666
end

app/actions/service_credential_binding_key_create.rb

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,20 @@
22

33
module VCAP::CloudController
44
module V3
5-
class ServiceCredentialBindingKeyCreate
5+
class ServiceCredentialBindingKeyCreate < V3::ServiceBindingCreate
66
include ServiceCredentialBindingCreateMixin
77

88
class UnprocessableCreate < StandardError
99
end
1010

11+
def initialize(user_audit_info, audit_hash)
12+
super()
13+
@user_audit_info = user_audit_info
14+
@audit_hash = audit_hash
15+
end
16+
17+
PERMITTED_BINDING_ATTRIBUTES = [:credentials].freeze
18+
1119
def precursor(service_instance, name)
1220
validate!(service_instance)
1321

@@ -17,8 +25,13 @@ def precursor(service_instance, name)
1725
credentials: {}
1826
}
1927

20-
ServiceKey.db.transaction do
21-
ServiceKey.create(**binding_details)
28+
ServiceKey.new(**binding_details).tap do |b|
29+
b.save_with_new_operation(
30+
{
31+
type: 'create',
32+
state: 'in progress',
33+
}
34+
)
2235
end
2336
rescue Sequel::ValidationFailed => e
2437
key_validation_error!(
@@ -40,6 +53,15 @@ def validate!(service_instance)
4053
end
4154
end
4255

56+
def permitted_binding_attributes
57+
PERMITTED_BINDING_ATTRIBUTES
58+
end
59+
60+
def event_repository
61+
@event_repository ||= Repositories::ServiceGenericBindingEventRepository.new(
62+
Repositories::ServiceGenericBindingEventRepository::SERVICE_KEY_CREDENTIAL_BINDING)
63+
end
64+
4365
def key_not_supported_for_user_provided_service!
4466
raise UnprocessableCreate.new("Service credential bindings of type 'key' are not supported for user-provided service instances.")
4567
end

app/actions/service_route_binding_create.rb

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ def initialize(user_audit_info, audit_hash)
1111
@audit_hash = audit_hash
1212
end
1313

14+
PERMITTED_BINDING_ATTRIBUTES = [:route_service_url].freeze
15+
1416
def precursor(service_instance, route, message:)
1517
validate!(service_instance, route)
1618

@@ -51,44 +53,12 @@ def validate!(service_instance, route)
5153
operation_in_progress! if service_instance.operation_in_progress?
5254
end
5355

54-
def complete_binding_and_save(binding, binding_details, last_operation)
55-
binding.save_with_attributes_and_new_operation(
56-
{
57-
route_service_url: binding_details[:route_service_url]
58-
},
59-
{
60-
type: 'create',
61-
state: last_operation[:state],
62-
description: last_operation[:description],
63-
}
64-
)
65-
66-
binding.notify_diego
67-
68-
event_repository.record_create(
69-
binding,
70-
@user_audit_info,
71-
@audit_hash,
72-
manifest_triggered: false
73-
)
56+
def permitted_binding_attributes
57+
PERMITTED_BINDING_ATTRIBUTES
7458
end
7559

76-
def save_incomplete_binding(precursor, operation)
77-
precursor.save_with_attributes_and_new_operation(
78-
{},
79-
{
80-
type: 'create',
81-
state: 'in progress',
82-
broker_provided_operation: operation
83-
}
84-
)
85-
86-
event_repository.record_start_create(
87-
precursor,
88-
@user_audit_info,
89-
@audit_hash,
90-
manifest_triggered: false
91-
)
60+
def post_bind_action(binding)
61+
binding.notify_diego
9262
end
9363

9464
def operation_in_progress!

app/actions/v3/service_binding_create.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,25 @@ def save_failed_state(binding, e)
6464
)
6565
end
6666

67+
def complete_binding_and_save(binding, binding_details, last_operation)
68+
binding.save_with_attributes_and_new_operation(
69+
binding_details.symbolize_keys.slice(*permitted_binding_attributes),
70+
{
71+
type: 'create',
72+
state: last_operation[:state],
73+
description: last_operation[:description],
74+
}
75+
)
76+
77+
post_bind_action(binding)
78+
79+
event_repository.record_create(
80+
binding,
81+
@user_audit_info,
82+
@audit_hash
83+
)
84+
end
85+
6786
def save_last_operation(binding, details)
6887
binding.save_with_attributes_and_new_operation(
6988
{},
@@ -76,6 +95,20 @@ def save_last_operation(binding, details)
7695
)
7796
end
7897

98+
def save_incomplete_binding(binding, broker_operation)
99+
binding.save_with_attributes_and_new_operation(
100+
{},
101+
{
102+
type: 'create',
103+
state: 'in progress',
104+
broker_provided_operation: broker_operation
105+
}
106+
)
107+
event_repository.record_start_create(binding, @user_audit_info, @audit_hash)
108+
end
109+
110+
def post_bind_action(binding); end
111+
79112
def bindings_retrievable?(binding)
80113
binding.service_instance.service.bindings_retrievable
81114
end

app/controllers/v3/service_credential_bindings_controller.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,11 @@ def parameters
120120
private
121121

122122
def create_key_binding(message, service_instance)
123-
V3::ServiceCredentialBindingKeyCreate.new.precursor(
124-
service_instance,
125-
message.name
126-
)
123+
action = V3::ServiceCredentialBindingKeyCreate.new(user_audit_info, message.audit_hash)
124+
binding = action.precursor(service_instance, message.name)
127125

128-
head :not_implemented
129-
return
126+
pollable_job_guid = enqueue_bind_job(:key, binding.guid, message)
127+
head :accepted, 'Location' => url_builder.build_url(path: "/v3/jobs/#{pollable_job_guid}")
130128
end
131129

132130
def build_create_message(params)
@@ -161,17 +159,17 @@ def create_app_binding(message, service_instance, app)
161159

162160
case service_instance
163161
when ManagedServiceInstance
164-
pollable_job_guid = enqueue_bind_job(binding.guid, message)
162+
pollable_job_guid = enqueue_bind_job(:credential, binding.guid, message)
165163
head :accepted, 'Location' => url_builder.build_url(path: "/v3/jobs/#{pollable_job_guid}")
166164
when UserProvidedServiceInstance
167165
action.bind(binding)
168166
render status: :created, json: Presenters::V3::ServiceCredentialBindingPresenter.new(binding).to_hash
169167
end
170168
end
171169

172-
def enqueue_bind_job(binding_guid, message)
170+
def enqueue_bind_job(type, binding_guid, message)
173171
bind_job = VCAP::CloudController::V3::CreateBindingAsyncJob.new(
174-
:credential,
172+
type,
175173
binding_guid,
176174
user_audit_info: user_audit_info,
177175
audit_hash: message.audit_hash,

app/jobs/v3/create_service_binding_job_factory.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
require 'jobs/v3/create_service_route_binding_job_actor'
22
require 'jobs/v3/create_service_credential_binding_job_actor'
3+
require 'jobs/v3/create_service_key_binding_job_actor'
4+
require 'actions/service_credential_binding_app_create'
5+
require 'actions/service_credential_binding_key_create'
6+
require 'actions/service_route_binding_create'
37

48
module VCAP::CloudController
59
module V3
@@ -13,6 +17,8 @@ def self.for(type)
1317
CreateServiceRouteBindingJobActor.new
1418
when :credential
1519
CreateServiceCredentialBindingJobActor.new
20+
when :key
21+
CreateServiceKeyBindingJobActor.new
1622
else
1723
raise InvalidType
1824
end
@@ -24,6 +30,8 @@ def self.action(type, user_audit_info, audit_hash)
2430
V3::ServiceRouteBindingCreate.new(user_audit_info, audit_hash)
2531
when :credential
2632
V3::ServiceCredentialBindingAppCreate.new(user_audit_info, audit_hash)
33+
when :key
34+
V3::ServiceCredentialBindingKeyCreate.new(user_audit_info, audit_hash)
2735
else
2836
raise InvalidType
2937
end

app/jobs/v3/create_service_credential_binding_job_actor.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
require 'jobs/reoccurring_job'
2-
require 'actions/service_credential_binding_app_create'
3-
require 'cloud_controller/errors/api_error'
4-
51
module VCAP::CloudController
62
module V3
73
class CreateServiceCredentialBindingJobActor
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module VCAP::CloudController
2+
module V3
3+
class CreateServiceKeyBindingJobActor
4+
def display_name
5+
'service_keys.create'
6+
end
7+
8+
def resource_type
9+
'service_credential_binding'
10+
end
11+
12+
def get_resource(resource_id)
13+
ServiceKey.first(guid: resource_id)
14+
end
15+
end
16+
end
17+
end

app/models.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
require 'models/services/service_plan_label_model'
133133
require 'models/services/service_usage_event'
134134
require 'models/services/service_key'
135+
require 'models/services/service_key_operation'
135136
require 'models/services/service_credential_binding_view'
136137

137138
require 'models/request_count'

app/models/runtime/pollable_job_model.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def resource_exists?
2727
when 'service_route_binding'
2828
RouteBinding
2929
when 'service_credential_binding'
30-
ServiceBinding
30+
ServiceCredentialBinding::View
3131
else
3232
Sequel::Model(ActiveSupport::Inflector.pluralize(resource_type).to_sym)
3333
end

0 commit comments

Comments
 (0)