-
Notifications
You must be signed in to change notification settings - Fork 373
Expand file tree
/
Copy pathspace_quotas_create.rb
More file actions
70 lines (54 loc) · 2.7 KB
/
Copy pathspace_quotas_create.rb
File metadata and controls
70 lines (54 loc) · 2.7 KB
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
require 'repositories/space_quota_event_repository'
module VCAP::CloudController
class SpaceQuotasCreate
class Error < ::StandardError
end
def initialize(user_audit_info)
@user_audit_info = user_audit_info
end
# rubocop:todo Metrics/CyclomaticComplexity
def create(message, organization:)
space_quota = nil
SpaceQuotaDefinition.db.transaction do
space_quota = VCAP::CloudController::SpaceQuotaDefinition.create(
name: message.name,
organization: organization,
# Apps
memory_limit: message.total_memory_in_mb || SpaceQuotaDefinition::DEFAULT_MEMORY_LIMIT,
instance_memory_limit: message.per_process_memory_in_mb || SpaceQuotaDefinition::UNLIMITED,
app_instance_limit: message.total_instances || SpaceQuotaDefinition::UNLIMITED,
app_task_limit: message.per_app_tasks || SpaceQuotaDefinition::UNLIMITED,
log_rate_limit: message.log_rate_limit_in_bytes_per_second || QuotaDefinition::UNLIMITED,
# Services
total_services: message.total_service_instances || SpaceQuotaDefinition::DEFAULT_TOTAL_SERVICES,
total_service_keys: message.total_service_keys || SpaceQuotaDefinition::UNLIMITED,
non_basic_services_allowed: message.paid_services_allowed.nil? ? SpaceQuotaDefinition::DEFAULT_NON_BASIC_SERVICES_ALLOWED : message.paid_services_allowed,
# Routes
total_routes: message.total_routes || SpaceQuotaDefinition::DEFAULT_TOTAL_ROUTES,
total_reserved_route_ports: message.total_reserved_ports || SpaceQuotaDefinition::UNLIMITED
)
spaces = valid_spaces(message.space_guids, organization)
spaces.each { |space| space_quota.add_space(space) }
Repositories::SpaceQuotaEventRepository.new.record_space_quota_create(space_quota, @user_audit_info, message.audit_hash)
end
space_quota
rescue Sequel::ValidationFailed => e
validation_error!(e, message)
end
# rubocop:enable Metrics/CyclomaticComplexity
private
def validation_error!(error, message)
error!("Space Quota '#{message.name}' already exists.") if error.errors.on(%i[organization_id name])&.include?(:unique)
error!(error.message)
end
def valid_spaces(space_guids, organization)
spaces = Space.filter(organization_id: organization.id).where(guid: space_guids).all
return spaces if spaces.length == space_guids.length
invalid_space_guids = space_guids - spaces.map(&:guid)
error!("Spaces with guids #{invalid_space_guids} do not exist within the organization specified, or you do not have access to them.")
end
def error!(message)
raise Error.new(message)
end
end
end