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

Commit 5948530

Browse files
author
Derik Evangelista
committed
v3(services): namespace v3 si delete action
to avoid clashing with the v2 implementation [#171726582](https://www.pivotaltracker.com/story/show/171726582)
1 parent 7b96255 commit 5948530

5 files changed

Lines changed: 163 additions & 159 deletions

File tree

app/actions/service_instance_delete.rb

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
module VCAP::CloudController
2+
module V3
3+
class ServiceInstanceDelete
4+
class AssociationNotEmptyError < StandardError; end
5+
class InstanceSharedError < StandardError; end
6+
class NotImplementedError < StandardError; end
7+
8+
def initialize(event_repo)
9+
@service_event_repository = event_repo
10+
end
11+
12+
def delete(service_instance)
13+
association_not_empty! if service_instance.has_bindings? || service_instance.has_keys? || service_instance.has_routes?
14+
15+
cannot_delete_shared_instances! if service_instance.shared?
16+
17+
case service_instance
18+
when ManagedServiceInstance
19+
raise NotImplementedError
20+
end
21+
22+
service_instance.db.transaction do
23+
service_instance.lock!
24+
service_instance.destroy
25+
service_event_repository.record_user_provided_service_instance_event(:delete, service_instance, {})
26+
end
27+
end
28+
29+
private
30+
31+
def association_not_empty!
32+
raise AssociationNotEmptyError
33+
end
34+
35+
def cannot_delete_shared_instances!
36+
raise InstanceSharedError
37+
end
38+
39+
attr_reader :service_event_repository
40+
end
41+
end
42+
end

app/controllers/v3/service_instances_controller.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
require 'actions/service_instance_update_managed'
1616
require 'actions/service_instance_update_user_provided'
1717
require 'actions/service_instance_create_user_provided'
18-
require 'actions/service_instance_delete'
18+
require 'actions/v3/service_instance_delete'
1919
require 'actions/service_instance_create_managed'
2020
require 'fetchers/service_instance_list_fetcher'
2121
require 'decorators/field_service_instance_space_decorator'
@@ -110,14 +110,14 @@ def destroy
110110
unauthorized! unless can_write_space?(service_instance.space)
111111

112112
service_event_repository = VCAP::CloudController::Repositories::ServiceEventRepository::WithUserActor.new(user_audit_info)
113-
ServiceInstanceDelete.new(service_event_repository).delete(service_instance)
113+
V3::ServiceInstanceDelete.new(service_event_repository).delete(service_instance)
114114

115115
head :no_content
116-
rescue ServiceInstanceDelete::AssociationNotEmptyError
116+
rescue V3::ServiceInstanceDelete::AssociationNotEmptyError
117117
associations_not_empty!
118-
rescue ServiceInstanceDelete::InstanceSharedError
118+
rescue V3::ServiceInstanceDelete::InstanceSharedError
119119
cannot_delete_shared_instances!(service_instance.name)
120-
rescue ServiceInstanceDelete::NotImplementedError
120+
rescue V3::ServiceInstanceDelete::NotImplementedError
121121
head :not_implemented
122122
end
123123

spec/unit/actions/service_instance_delete_spec.rb

Lines changed: 0 additions & 114 deletions
This file was deleted.
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
require 'spec_helper'
2+
require 'actions/v3/service_instance_delete'
3+
4+
module VCAP
5+
module CloudController
6+
RSpec.describe V3::ServiceInstanceDelete do
7+
describe '#delete' do
8+
subject(:action) { described_class.new(event_repository) }
9+
let(:event_repository) do
10+
dbl = double(Repositories::ServiceEventRepository::WithUserActor)
11+
allow(dbl).to receive(:record_user_provided_service_instance_event)
12+
dbl
13+
end
14+
15+
let!(:service_instance) do
16+
si = VCAP::CloudController::UserProvidedServiceInstance.make(
17+
name: 'foo',
18+
credentials: {
19+
foo: 'bar',
20+
baz: 'qux'
21+
},
22+
syslog_drain_url: 'https://foo.com',
23+
route_service_url: 'https://bar.com',
24+
tags: %w(accounting mongodb)
25+
)
26+
si.label_ids = [
27+
VCAP::CloudController::ServiceInstanceLabelModel.make(key_prefix: 'pre.fix', key_name: 'to_delete', value: 'value'),
28+
VCAP::CloudController::ServiceInstanceLabelModel.make(key_prefix: 'pre.fix', key_name: 'tail', value: 'fluffy')
29+
]
30+
si.annotation_ids = [
31+
VCAP::CloudController::ServiceInstanceAnnotationModel.make(key_prefix: 'pre.fix', key_name: 'to_delete', value: 'value').id,
32+
VCAP::CloudController::ServiceInstanceAnnotationModel.make(key_prefix: 'pre.fix', key_name: 'fox', value: 'bushy').id
33+
]
34+
si
35+
end
36+
37+
it 'deletes an user provided service instance from the database' do
38+
subject.delete(service_instance)
39+
40+
expect {
41+
service_instance.reload
42+
}.to raise_error(Sequel::Error, 'Record not found')
43+
expect(VCAP::CloudController::ServiceInstanceLabelModel.where(service_instance: service_instance)).to be_empty
44+
expect(VCAP::CloudController::ServiceInstanceAnnotationModel.where(service_instance: service_instance)).to be_empty
45+
end
46+
47+
it 'fails to delete managed service instances' do
48+
instance = VCAP::CloudController::ManagedServiceInstance.make
49+
expect { subject.delete(instance) }.to raise_error(V3::ServiceInstanceDelete::NotImplementedError)
50+
expect { instance.reload }.not_to raise_error
51+
end
52+
53+
it 'creates an audit event' do
54+
subject.delete(service_instance)
55+
56+
expect(event_repository).
57+
to have_received(:record_user_provided_service_instance_event).
58+
with(:delete, instance_of(UserProvidedServiceInstance), {})
59+
end
60+
61+
describe 'invalid pre-conditions' do
62+
context 'when there are associated service bindings' do
63+
before do
64+
VCAP::CloudController::ServiceBinding.make(service_instance: service_instance)
65+
end
66+
67+
it 'does not delete the service instance' do
68+
expect { subject.delete(service_instance) }.to raise_error(V3::ServiceInstanceDelete::AssociationNotEmptyError)
69+
expect { service_instance.reload }.not_to raise_error
70+
end
71+
end
72+
73+
context 'when there are associated service keys' do
74+
before do
75+
VCAP::CloudController::ServiceKey.make(service_instance: service_instance)
76+
end
77+
78+
it 'does not delete the service instance' do
79+
expect { subject.delete(service_instance) }.to raise_error(V3::ServiceInstanceDelete::AssociationNotEmptyError)
80+
expect { service_instance.reload }.not_to raise_error
81+
end
82+
end
83+
84+
context 'when there are associated route bindings' do
85+
before do
86+
VCAP::CloudController::RouteBinding.make(
87+
service_instance: service_instance,
88+
route: VCAP::CloudController::Route.make(space: service_instance.space)
89+
)
90+
end
91+
92+
it 'does not delete the service instance' do
93+
expect { subject.delete(service_instance) }.to raise_error(V3::ServiceInstanceDelete::AssociationNotEmptyError)
94+
expect { service_instance.reload }.not_to raise_error
95+
end
96+
end
97+
98+
context 'when the service instance is shared' do
99+
let(:space) { VCAP::CloudController::Space.make }
100+
let(:other_space) { VCAP::CloudController::Space.make }
101+
let!(:service_instance) {
102+
si = VCAP::CloudController::ServiceInstance.make(space: space)
103+
si.shared_space_ids = [other_space.id]
104+
si
105+
}
106+
107+
it 'does not delete the service instance' do
108+
expect { subject.delete(service_instance) }.to raise_error(V3::ServiceInstanceDelete::InstanceSharedError)
109+
expect { service_instance.reload }.not_to raise_error
110+
end
111+
end
112+
end
113+
end
114+
end
115+
end
116+
end

0 commit comments

Comments
 (0)