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

Commit 5228fe1

Browse files
FelisiaMpivotal-marcela-campo
authored andcommitted
Moved convertion of name uniqueness sql error into the action
1 parent dc57d5b commit 5228fe1

3 files changed

Lines changed: 7 additions & 2 deletions

File tree

app/actions/service_credential_binding_key_create.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def precursor(service_instance, name)
1717
ServiceKey.create(**binding_details)
1818
end
1919
rescue Sequel::ValidationFailed => e
20+
key_already_exists!(name) if e.message == 'name and service_instance_id unique'
2021
raise UnprocessableCreate.new(e.message)
2122
end
2223

@@ -36,6 +37,10 @@ def key_not_supported_for_user_provided_service!
3637
raise UnprocessableCreate.new("Service credential bindings of type 'key' are not supported for user-provided service instances.")
3738
end
3839

40+
def key_already_exists!(key_name)
41+
raise UnprocessableCreate.new("The binding name is invalid. Key binding names must be unique. The service instance already has a key binding with name '#{key_name}'.")
42+
end
43+
3944
def operation_in_progress!
4045
raise UnprocessableCreate.new('There is an operation in progress for the service instance.')
4146
end

app/models/services/service_key.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ def space
3333
def validate
3434
validates_presence :name
3535
validates_presence :service_instance
36-
validates_unique [:name, :service_instance_id], message: Sequel.lit(
37-
"The binding name is invalid. Key binding names must be unique. The service instance already has a key binding with name '#{name}'.")
36+
validates_unique [:name, :service_instance_id]
3837

3938
if service_instance
4039
MaxServiceKeysPolicy.new(

spec/unit/models/services/service_key_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module VCAP::CloudController
1919
it { is_expected.to validate_presence :name }
2020
it { is_expected.to validate_db_presence :service_instance_id }
2121
it { is_expected.to validate_db_presence :credentials }
22+
it { is_expected.to validate_uniqueness [:name, :service_instance_id] }
2223

2324
context 'MaxServiceKeysPolicy' do
2425
let(:service_key) { ServiceKey.make }

0 commit comments

Comments
 (0)