Module: Nucleus::Adapters::V1::CloudFoundryV2::Domains

Included in:
Nucleus::Adapters::V1::CloudFoundryV2
Defined in:
lib/nucleus/adapters/v1/cloud_foundry_v2/domains.rb

Overview

Application domain / route functionality to support the Cloud Foundry API.

Instance Method Summary collapse

Instance Method Details

#create_domain(application_name_or_id, domain) ⇒ Object

See Also:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/nucleus/adapters/v1/cloud_foundry_v2/domains.rb', line 29

def create_domain(application_name_or_id, domain)
  domains(application_name_or_id).each do |existing_domain|
    if existing_domain[:name] == domain[:name]
      raise Errors::SemanticAdapterRequestError,
            "Domain '#{domain[:name]}' is already assigned to the application"
    end
  end

  app_guid = app_guid(application_name_or_id)
  # extract the hostname and the domain name from the FQDN
  /(?<domain_host>([-\w]+\.)*)(?<domain_name>([-\w]+\.[-\w]+))/ =~ domain[:name]
  domain_host.chomp!('.') unless domain_host.nil?

  # finally build the response
  route_to_nucleus_domain(create_cf_domain(app_guid, domain_name, domain_host))
end

#delete_domain(application_name_or_id, route_id) ⇒ Object

See Also:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/nucleus/adapters/v1/cloud_foundry_v2/domains.rb', line 47

def delete_domain(application_name_or_id, route_id)
  app_guid = app_guid(application_name_or_id)
  # remove route from the app
  delete_response = delete("/v2/apps/#{app_guid}/routes/#{route_id}", expects: [201, 400])
  if delete_response.status == 400
    cf_error = delete_response.body[:code]
    if cf_error == 1002
      raise Errors::AdapterResourceNotFoundError, 'Domain not found. '\
        'CF context specific: Route does not exist or is not assigned with this application'
    else
      # delete failed with 400, but not due to invalid domain
      raise Errors::AdapterRequestError,
            "#{delete_response.body[:description]} (#{cf_error} - #{delete_response.body[:error_code]})"
    end
  end

  # check route usage
  route_in_apps = get("/v2/routes/#{route_id}/apps").body
  return unless route_in_apps[:total_results] == 0

  # route is no longer needed, delete
  delete("/v2/routes/#{route_id}")
end

#domain(application_name_or_id, domain_id) ⇒ Object

See Also:



20
21
22
23
24
25
26
# File 'lib/nucleus/adapters/v1/cloud_foundry_v2/domains.rb', line 20

def domain(application_name_or_id, domain_id)
  app_guid = app_guid(application_name_or_id)
  assigned_routes = get("/v2/apps/#{app_guid}/routes").body
  assigned_routes[:resources].each do |assigned_route|
    return route_to_nucleus_domain(assigned_route) if assigned_route[:metadata][:guid] == domain_id
  end
end

#domains(domain_id) ⇒ Object

See Also:



8
9
10
11
12
13
14
15
16
17
# File 'lib/nucleus/adapters/v1/cloud_foundry_v2/domains.rb', line 8

def domains(domain_id)
  app_guid = app_guid(domain_id)
  assigned_routes = get("/v2/apps/#{app_guid}/routes").body
  domains = []
  assigned_routes[:resources].each do |assigned_route|
    nucleus_domain = route_to_nucleus_domain(assigned_route)
    domains.push(nucleus_domain) unless nucleus_domain[:name] == app_web_url(app_guid)
  end
  domains
end