-
Notifications
You must be signed in to change notification settings - Fork 364
/
Copy pathservice_instance_share.rb
100 lines (77 loc) · 3.68 KB
/
service_instance_share.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
require 'repositories/service_instance_share_event_repository'
module VCAP::CloudController
class ServiceInstanceShare
class Error < ::StandardError
end
def create(service_instance, target_spaces, user_audit_info)
validate_supported_service_type!(service_instance)
validate_service_instance_is_shareable!(service_instance)
validate_target_spaces!(service_instance, target_spaces)
validate_service_instance_state!(service_instance)
ServiceInstance.db.transaction do
target_spaces.each do |space|
service_instance.add_shared_space(space)
end
end
Repositories::ServiceInstanceShareEventRepository.record_share_event(
service_instance, target_spaces.map(&:guid), user_audit_info
)
service_instance
end
private
def error!(message)
raise Error.new(message)
end
def validate_target_spaces!(service_instance, target_spaces)
validate_not_sharing_to_self!(service_instance, target_spaces)
validate_plan_is_active!(service_instance)
target_spaces.each do |space|
validate_plan_visibility!(service_instance, space)
validate_name_uniqueness!(service_instance, space)
end
end
def validate_plan_is_active!(service_instance)
return if service_instance.service_plan.active?
error_msg = "The service instance could not be shared as the #{service_instance.service_plan.name} plan is inactive."
error!(error_msg)
end
def validate_plan_visibility!(service_instance, space)
return if service_instance.service_plan.visible_in_space?(space)
error_msg = "Access to service #{service_instance.service.label} and plan #{service_instance.service_plan.name} is not enabled in #{space.organization.name}/#{space.name}."
error!(error_msg)
end
def validate_name_uniqueness!(service_instance, space)
if space.service_instances_dataset.where(name: service_instance.name).any?
error_msg = "A service instance called #{service_instance.name} already exists in #{space.name}."
error!(error_msg)
end
return unless space.service_instances_shared_from_other_spaces_dataset.where(name: service_instance.name).any?
error_msg = "A service instance called #{service_instance.name} has already been shared with #{space.name}."
error!(error_msg)
end
def validate_not_sharing_to_self!(service_instance, spaces)
return unless spaces.include?(service_instance.space)
error!("Unable to share service instance '#{service_instance.name}' with space '#{service_instance.space.guid}'. " \
'Service instances cannot be shared into the space where they were created.')
end
def validate_supported_service_type!(service_instance)
error!('Route services cannot be shared.') if service_instance.route_service?
return if service_instance.managed_instance?
error!('User-provided services cannot be shared.')
end
def validate_service_instance_is_shareable!(service_instance)
return if service_instance.shareable?
error_msg = "The #{service_instance.service.label} service does not support service instance sharing."
error!(error_msg)
end
def validate_service_instance_state!(service_instance)
if service_instance.create_failed?
raise CloudController::Errors::ApiError.new_from_details('ServiceInstanceNotFound', service_instance.name)
elsif service_instance.create_in_progress?
error!('Service instance is currently being created. It can be shared after its creation succeeded.')
elsif service_instance.delete_in_progress?
error!('The service instance is getting deleted.')
end
end
end
end