Class: DomainsController

Inherits:
BaseController show all
Defined in:
app/controllers/domains_controller.rb

Constant Summary

Constants inherited from BaseController

BaseController::API_VERSION, BaseController::SUPPORTED_API_VERSIONS

Instance Method Summary collapse

Methods included from UserActionLogger

#get_action_logger, #log_action

Instance Method Details

#createObject

POST /domains



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
# File 'app/controllers/domains_controller.rb', line 39

def create
  namespace = params[:id]
  Rails.logger.debug "Creating domain with namespace #{namespace}"

  domain = Domain.new(namespace, @cloud_user)
  if not domain.valid?
    Rails.logger.error "Domain is not valid"
    messages = get_error_messages(domain, "namespace", "id")
    return render_error(:unprocessable_entity, nil, nil, "ADD_DOMAIN", nil, nil, messages)
  end

  begin
    dom_available = Domain.namespace_available?(namespace)
  rescue Exception => e
    return render_exception(e, "ADD_DOMAIN") 
  end

  return render_error(:unprocessable_entity, "Namespace '#{namespace}' is already in use. Please choose another.", 103, "ADD_DOMAIN", "id") if !dom_available 

  return render_error(:conflict, "Domain already exists for user. Update the domain to modify.", 158, "ADD_DOMAIN") if !@cloud_user.domains.empty?

  @domain_name = namespace
  begin
    domain.save
  rescue Exception => e
    return render_exception(e, "ADD_DOMAIN") 
  end

  if $requested_api_version >= 1.3
    domain = RestDomain.new(domain, get_url, nolinks)
  else
    domain = RestDomain10.new(domain, get_url, nolinks)
  end
  render_success(:created, "domain", domain, "ADD_DOMAIN", "Created domain with namespace #{namespace}", true)
end

#destroyObject

DELETE /domains/<id>



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'app/controllers/domains_controller.rb', line 125

def destroy
  id = params[:id]
  force = get_bool(params[:force])

  domain = Domain.get(@cloud_user, id)
  return render_format_error(:not_found, "Domain #{id} not found", 127,
                             "DELETE_DOMAIN") if !domain || !domain.hasAccess?(@cloud_user)

  return render_format_error(:forbidden, "User does not have permission to delete domain '#{id}'",
                             132, "DELETE_DOMAIN") if domain && !domain.hasFullAccess?(@cloud_user)

  if force
    Rails.logger.debug "Force deleting domain #{id}"
    @cloud_user.applications.each do |app|
      app.cleanup_and_delete if app.domain.uuid == domain.uuid
    end if @cloud_user.applications
  else
    @cloud_user.applications.each do |app|
      return render_format_error(:bad_request, "Domain contains applications. Delete applications first or set force to true.", 
                                 128, "DELETE_DOMAIN") if app.domain.uuid == domain.uuid
    end if @cloud_user.applications
  end

  @domain_name = id
  begin
    domain.delete
    render_format_success(:no_content, nil, nil, "DELETE_DOMAIN", "Domain #{id} deleted.", true)
  rescue Exception => e
    return render_format_exception(e, "DELETE_DOMAIN") 
  end
end

#indexObject

GET /domains



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'app/controllers/domains_controller.rb', line 6

def index
  domains = Array.new
  Rails.logger.debug "Getting domains for user #{@cloud_user.}"
  Rails.logger.debug @cloud_user.domains
  @cloud_user.domains.each do |domain|
    if $requested_api_version >= 1.3
        domains.push(RestDomain.new(domain, get_url, nolinks))
    else
        domains.push(RestDomain10.new(domain, get_url, nolinks))
    end
  end
  render_success(:ok, "domains", domains, "LIST_DOMAINS")
end

#showObject

GET /domains/<id>



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/domains_controller.rb', line 21

def show
  id = params[:id]
  Rails.logger.debug "Getting domain #{id}"

  domain = Domain.get(@cloud_user, id)
  if domain and domain.hasAccess?(@cloud_user)
    @domain_name = domain.namespace
    if $requested_api_version >= 1.3
      domain = RestDomain.new(domain, get_url, nolinks)
    else
      domain = RestDomain10.new(domain, get_url, nolinks)
    end
    return render_success(:ok, "domain", domain, "SHOW_DOMAIN", "Found domain #{id}")
  end
  render_error(:not_found, "Domain #{id} not found.", 127, "SHOW_DOMAIN")
end

#updateObject

PUT /domains/<existing_id>



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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/controllers/domains_controller.rb', line 76

def update
  id = params[:existing_id]
  new_namespace = params[:id]
  domain = Domain.get(@cloud_user, id)

  new_domain = Domain.new(new_namespace, @cloud_user)
  if not new_domain.valid?
    messages = get_error_messages(new_domain, "namespace", "id")
    return render_format_error(:unprocessable_entity, nil, nil, "UPDATE_DOMAIN", nil, nil, messages)
  end
  return render_format_error(:not_found, "Domain '#{id}' not found", 127, 
                             "UPDATE_DOMAIN") if !domain || !domain.hasAccess?(@cloud_user)

  return render_format_error(:forbidden, "User does not have permission to modify domain '#{id}'",
                             132, "UPDATE_DOMAIN") if domain && !domain.hasFullAccess?(@cloud_user)

  Rails.logger.debug "Updating domain #{domain.namespace} to #{new_namespace}"
  begin
    dom_available = Domain.namespace_available?(new_namespace)
  rescue Exception => e
    return render_format_exception(e, "UPDATE_DOMAIN") 
  end

  return render_format_error(:unprocessable_entity, "Namespace '#{new_namespace}' already in use. Please choose another.",
                             103, "UPDATE_DOMAIN", "id") if !dom_available

  domain.namespace = new_namespace
  if domain.invalid?
    messages = get_error_messages(domain, "namespace", "id")
    return render_format_error(:unprocessable_entity, nil, nil, "UPDATE_DOMAIN", nil, nil, messages)
  end

  @domain_name = id
  begin
    domain.save
  rescue Exception => e
    return render_format_exception(e, "UPDATE_DOMAIN") 
  end

  @cloud_user = CloudUser.find(@login)
  if $requested_api_version >= 1.3
    domain = RestDomain.new(domain, get_url, nolinks)
  else
    domain = RestDomain10.new(domain, get_url, nolinks)
  end
  render_format_success(:ok, "domain", domain, "UPDATE_DOMAIN", "Updated domain #{id} to #{new_namespace}")
end