-
Notifications
You must be signed in to change notification settings - Fork 371
Expand file tree
/
Copy pathdomain_create.rb
More file actions
61 lines (47 loc) · 1.89 KB
/
Copy pathdomain_create.rb
File metadata and controls
61 lines (47 loc) · 1.89 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
require 'repositories/deployment_event_repository'
module VCAP::CloudController
class DomainCreate
class Error < StandardError
end
INTERNAL_DEFAULT = false
def create(message:, shared_organizations: [])
domain = if message.requested?(:relationships)
PrivateDomain.new(
name: message.name,
owning_organization_guid: message.organization_guid
)
else
SharedDomain.new(
name: message.name,
internal: message.internal.nil? ? INTERNAL_DEFAULT : message.internal
)
end
domain.router_group_guid = message.router_group_guid
domain.enforce_route_policies = message.enforce_route_policies || false
domain.route_policies_scope = message.route_policies_scope
Domain.db.transaction do
domain.save
MetadataUpdate.update(domain, message)
shared_organizations.each do |shared_org|
domain.add_shared_organization(shared_org)
end
end
domain
rescue Sequel::ValidationFailed => e
validation_error!(message, e)
end
private
def validation_error!(message, error)
error!("The domain name \"#{message.name}\" is already in use") if error.errors.on(:name)&.any? { |e| [:unique].include?(e) }
error!("The \"#{message.name}\" domain is reserved and cannot be used for org-scoped domains.") if error.errors.on(:name)&.any? { |e| [:reserved].include?(e) }
if error.errors.on(:organization)&.any? { |e| [:total_private_domains_exceeded].include?(e) }
org = Organization.find(guid: message.organization_guid).name
error!("The number of private domains exceeds the quota for organization \"#{org}\"")
end
error!(error.message)
end
def error!(message)
raise Error.new(message)
end
end
end