Skip to content

Commit 6949db7

Browse files
committed
Introduce strategy parameter for binding creation
1 parent 2f4b3bd commit 6949db7

File tree

8 files changed

+44
-6
lines changed

8 files changed

+44
-6
lines changed

app/actions/service_credential_binding_app_create.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def precursor(service_instance, message:, app: nil, volume_mount_services_enable
4848
num_valid_bindings += 1
4949
end
5050

51-
validate_number_of_bindings!(num_valid_bindings)
51+
validate_number_of_bindings!(num_valid_bindings, strategy: message.strategy)
5252
validate_app_guid_name_uniqueness!(app.guid, message.name, service_instance.guid)
5353

5454
new_binding.save_with_attributes_and_new_operation(
@@ -82,8 +82,12 @@ def validate_binding!(binding, desired_binding_name:)
8282
name_cannot_be_changed! if binding.name != desired_binding_name
8383
end
8484

85-
def validate_number_of_bindings!(number_of_bindings)
86-
too_many_bindings! if number_of_bindings >= max_bindings_per_app_service_instance
85+
def validate_number_of_bindings!(number_of_bindings, strategy:)
86+
if strategy == 'multiple'
87+
too_many_bindings! if number_of_bindings >= max_bindings_per_app_service_instance
88+
elsif number_of_bindings >= 1
89+
already_bound!
90+
end
8791
end
8892

8993
def validate_app_guid_name_uniqueness!(target_app_guid, desired_binding_name, target_service_instance_guid)

app/messages/service_credential_app_binding_create_message.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
module VCAP::CloudController
22
class ServiceCredentialAppBindingCreateMessage < ServiceCredentialBindingCreateMessage
3+
validates :strategy, allow_blank: false, allow_nil: true, inclusion: {
4+
in: %w[single multiple],
5+
message: "must be 'single' or 'multiple'"
6+
}
7+
38
def relationships_message
49
@relationships_message ||= Relationships.new(relationships&.deep_symbolize_keys)
510
end

app/messages/service_credential_binding_create_message.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'messages/metadata_base_message'
22
module VCAP::CloudController
33
class ServiceCredentialBindingCreateMessage < MetadataBaseMessage
4-
register_allowed_keys %i[type name relationships parameters]
4+
register_allowed_keys %i[type strategy name relationships parameters]
55
validates_with NoAdditionalKeysValidator, RelationshipValidator
66
validates :parameters, hash: true, allow_nil: true
77
validates :type, allow_blank: false, inclusion: {

docs/v3/source/includes/resources/service_credential_bindings/_create.md.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ Name | Type | Description
116116
Name | Type | Description |
117117
---- | ---- | ----------- |
118118
**relationships.app** | [_to-one relationship_](#to-one-relationships) | The app to be bound. Required when type is `app`
119+
**strategy** | string | Strategy for creating the service credential binding. Valid values are `single` (default) and `multiple` (experimental). Only valid when type is `app`.
119120
**parameters** | _object_ | A JSON object that is passed to the service broker
120121
**metadata.labels** | [_label object_](#labels) | Labels applied to the service credential binding
121122
**metadata.annotations** | [_annotation object_](#annotations) | Annotations applied to the service credential binding

docs/v3/source/includes/resources/service_credential_bindings/_header.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ Service credential bindings are used to make the details of the connection to a
44

55
Service credential bindings can be of type `app` or `key`.
66

7-
A service credential binding is of type `app` when it is a binding between a [service instance](#service-instances) and an [application](#apps)
7+
A service credential binding is of type `app` when it is a binding between a [service instance](#service-instances) and an [application](#apps).
88
Not all services support this binding, as some services deliver value to users directly without integration with an application.
99
Field `broker_catalog.features.bindable` from [service plan](#the-service-plan-object) of the service instance can be used to determine if it is bindable.
1010

1111
A service credential binding is of type `key` when it only retrieves the details of the service instance and makes them available to the developer.
12-

spec/unit/actions/service_credential_binding_app_create_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ module V3
190190
end
191191

192192
context 'when multiple bindings are allowed' do
193+
let(:message) { VCAP::CloudController::ServiceCredentialAppBindingCreateMessage.new({ name: name, strategy: 'multiple' }) }
193194
let(:binding_1) { ServiceBinding.make(service_instance:, app:, name:) }
194195
let(:binding_2) { ServiceBinding.make(service_instance:, app:, name:) }
195196

@@ -276,6 +277,16 @@ module V3
276277
'The app has too many bindings to this service instance (limit: 2). Consider deleting existing/orphaned bindings.')
277278
end
278279
end
280+
281+
context "when the strategy = 'multiple' parameter is omitted" do
282+
let(:message) { VCAP::CloudController::ServiceCredentialAppBindingCreateMessage.new({ name: }) }
283+
284+
it 'raises an error' do
285+
expect do
286+
action.precursor(service_instance, app:, message:)
287+
end.to raise_error(ServiceCredentialBindingAppCreate::UnprocessableCreate, 'The app is already bound to the service instance')
288+
end
289+
end
279290
end
280291
end
281292

spec/unit/messages/service_credential_app_binding_create_message_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module VCAP::CloudController
88
let(:params) do
99
{
1010
type: 'app',
11+
strategy: 'single',
1112
name: 'some-name',
1213
parameters: {
1314
some_param: 'very important',
@@ -26,6 +27,7 @@ module VCAP::CloudController
2627
it 'builds a valid ServiceCredentialBindingCreateMessage' do
2728
expect(message).to be_valid
2829
expect(message.type).to eq('app')
30+
expect(message.strategy).to eq('single')
2931
expect(message.name).to eq('some-name')
3032
expect(message.service_instance_guid).to eq('some-instance-guid')
3133
expect(message.app_guid).to eq('some-app-guid')
@@ -58,6 +60,20 @@ module VCAP::CloudController
5860
end
5961
end
6062

63+
context 'strategy' do
64+
it 'accepts single and multiple' do
65+
%w[single multiple].each do |strategy|
66+
params[:strategy] = strategy
67+
expect(subject.new(params)).to be_valid
68+
end
69+
end
70+
71+
it 'is invalid with any other value' do
72+
params[:strategy] = 'test'
73+
expect(subject.new(params)).not_to be_valid
74+
end
75+
end
76+
6177
context 'name' do
6278
it 'accepts empty' do
6379
params[:name] = ''

spec/unit/messages/service_credential_binding_create_message_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module VCAP::CloudController
88
let(:params) do
99
{
1010
type: 'app',
11+
strategy: 'single',
1112
name: 'some-name',
1213
parameters: {
1314
some_param: 'very important',
@@ -29,6 +30,7 @@ module VCAP::CloudController
2930
it 'builds a valid ServiceCredentialBindingCreateMessage' do
3031
expect(message).to be_valid
3132
expect(message.type).to eq('app')
33+
expect(message.strategy).to eq('single')
3234
expect(message.name).to eq('some-name')
3335
expect(message.service_instance_guid).to eq('some-instance-guid')
3436
expect(message.parameters).to eq({ some_param: 'very important', another_param: 'epa' })

0 commit comments

Comments
 (0)