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

Commit 7a9ed4e

Browse files
v3(services): refactor bindings models save methods
[#175938303](https://www.pivotaltracker.com/story/show/175938303)
1 parent 814e373 commit 7a9ed4e

6 files changed

Lines changed: 41 additions & 45 deletions

File tree

app/actions/service_credential_binding_app_create.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,10 @@ def precursor(service_instance, app: nil, name: nil, volume_mount_services_enabl
3030
credentials: {}
3131
}
3232

33-
ServiceBinding.new(**binding_details).tap do |b|
34-
b.save_with_new_operation(
35-
{
36-
type: 'create',
37-
state: 'in progress',
38-
}
33+
ServiceBinding.new.tap do |b|
34+
b.save_with_attributes_and_new_operation(
35+
binding_details,
36+
CREATE_IN_PROGRESS_OPERATION
3937
)
4038
end
4139
rescue Sequel::ValidationFailed, Sequel::UniqueConstraintViolation => e

app/actions/service_credential_binding_key_create.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,10 @@ def precursor(service_instance, name)
2525
credentials: {}
2626
}
2727

28-
ServiceKey.new(**binding_details).tap do |b|
29-
b.save_with_new_operation(
30-
{
31-
type: 'create',
32-
state: 'in progress',
33-
}
28+
ServiceKey.new.tap do |b|
29+
b.save_with_attributes_and_new_operation(
30+
binding_details,
31+
CREATE_IN_PROGRESS_OPERATION
3432
)
3533
end
3634
rescue Sequel::ValidationFailed => e

app/actions/service_route_binding_create.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ def initialize(user_audit_info, audit_hash)
1616
def precursor(service_instance, route, message:)
1717
validate!(service_instance, route)
1818

19-
RouteBinding.db.transaction do
20-
b = RouteBinding.new.save_with_new_operation(
21-
{
22-
service_instance: service_instance,
23-
route: route,
24-
},
25-
{
26-
type: 'create',
27-
state: 'in progress',
28-
}
29-
)
30-
MetadataUpdate.update(b, message)
31-
b
19+
binding_details = {
20+
service_instance: service_instance,
21+
route: route,
22+
}
23+
24+
RouteBinding.new.tap do |b|
25+
RouteBinding.db.transaction do
26+
b.save_with_attributes_and_new_operation(
27+
binding_details,
28+
CREATE_IN_PROGRESS_OPERATION
29+
)
30+
MetadataUpdate.update(b, message)
31+
end
3232
end
3333
end
3434

app/actions/v3/service_binding_create.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class ServiceBindingCreate
1010
PollingFinished = PollingStatus.new(true, nil).freeze
1111
ContinuePolling = ->(retry_after) { PollingStatus.new(false, retry_after) }
1212

13+
CREATE_IN_PROGRESS_OPERATION = { type: 'create', state: 'in progress' }.freeze
14+
1315
def bind(binding, parameters: {}, accepts_incomplete: false)
1416
client = VCAP::Services::ServiceClientProvider.provide(instance: binding.service_instance)
1517
details = client.bind(binding, arbitrary_parameters: parameters, accepts_incomplete: accepts_incomplete)

app/models/services/service_key.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,6 @@ def required_parameters
9999
end
100100

101101
def save_with_attributes_and_new_operation(attributes, operation)
102-
save_with_new_operation(operation, attributes: attributes)
103-
self
104-
end
105-
106-
def save_with_new_operation(last_operation, attributes: {})
107102
ServiceKey.db.transaction do
108103
self.lock!
109104
set(attributes.except(:parameters, :route_services_url, :endpoints))
@@ -116,9 +111,10 @@ def save_with_new_operation(last_operation, attributes: {})
116111
# it is important to create the service key operation with the service key
117112
# instead of doing self.service_key_operation = x
118113
# because mysql will deadlock when requests happen concurrently otherwise.
119-
ServiceKeyOperation.create(last_operation.merge(service_key_id: self.id))
114+
ServiceKeyOperation.create(operation.merge(service_key_id: self.id))
120115
self.service_key_operation(reload: true)
121116
end
117+
self
122118
end
123119
end
124120
end

spec/unit/models/services/service_key_spec.rb

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def new_model
156156
end
157157
end
158158

159-
describe '#save_with_new_operation' do
159+
describe '#save_with_attributes_and_new_operation' do
160160
let(:service_instance) { ServiceInstance.make }
161161
let(:binding) {
162162
ServiceKey.new(
@@ -172,7 +172,7 @@ def new_model
172172
type: 'create',
173173
description: '10%'
174174
}
175-
binding.save_with_new_operation(last_operation)
175+
binding.save_with_attributes_and_new_operation({}, last_operation)
176176

177177
expect(binding.last_operation.state).to eq 'in progress'
178178
expect(binding.last_operation.description).to eq '10%'
@@ -186,15 +186,15 @@ def new_model
186186
end
187187

188188
it 'should rollback the binding' do
189-
expect { binding.save_with_new_operation({ state: 'will fail' }) }.to raise_error(Sequel::DatabaseError)
189+
expect { binding.save_with_attributes_and_new_operation({}, { state: 'will fail' }) }.to raise_error(Sequel::DatabaseError)
190190
expect(ServiceKey.where(guid: binding.guid).count).to eq(0)
191191
end
192192
end
193193

194194
context 'when called twice' do
195-
it 'does saves the second operation' do
196-
binding.save_with_new_operation({ state: 'in progress', type: 'create', description: 'description' })
197-
binding.save_with_new_operation({ state: 'in progress', type: 'delete' })
195+
it 'does save the second operation' do
196+
binding.save_with_attributes_and_new_operation({}, { state: 'in progress', type: 'create', description: 'description' })
197+
binding.save_with_attributes_and_new_operation({}, { state: 'in progress', type: 'delete' })
198198

199199
expect(binding.last_operation.state).to eq 'in progress'
200200
expect(binding.last_operation.type).to eq 'delete'
@@ -220,7 +220,7 @@ def new_model
220220
}
221221

222222
it 'updates the attributes' do
223-
binding.save_with_new_operation(last_operation, attributes: attributes)
223+
binding.save_with_attributes_and_new_operation(attributes, last_operation)
224224
binding.reload
225225
expect(binding.last_operation.state).to eq 'in progress'
226226
expect(binding.last_operation.description).to eq '10%'
@@ -232,13 +232,15 @@ def new_model
232232

233233
it 'only saves permitted attributes' do
234234
expect {
235-
binding.save_with_new_operation(last_operation, attributes: attributes.merge(
236-
parameters: {
237-
foo: 'bar',
238-
ding: 'dong'
239-
},
240-
endpoints: [{ host: 'mysqlhost', ports: ['3306'] }],
241-
))
235+
binding.save_with_attributes_and_new_operation(attributes.merge(
236+
parameters: {
237+
foo: 'bar',
238+
ding: 'dong'
239+
},
240+
endpoints: [{ host: 'mysqlhost', ports: ['3306'] }],
241+
),
242+
last_operation
243+
)
242244
}.not_to raise_error
243245
end
244246
end

0 commit comments

Comments
 (0)