Class: Api::V2::HostsController
Instance Method Summary
collapse
#resource_name
#process_parameter_attributes
#action_scope_for
#api_version
#append_array_of_ids, #check_media_type, #metadata_by, #metadata_order, #metadata_page, #metadata_per_page, #metadata_search, #metadata_subtotal, #metadata_total, #render_error, #root_node_name, #setup_has_many_params
#api_request?, #controller_permission, #get_resource, parameter_filter_context, #parent_scope, #resource_scope, #resource_scope_for_index
#bruteforce_attempt?
#current_permission, #find_default_taxonomy, #find_session_taxonomy, #scope_by_resource_id, #set_current_taxonomy, #set_current_user, #set_taxonomy, #store_default_taxonomy, #store_taxonomy
Methods included from FindCommon
#controller_permission, #find_resource, #resource_class_for, #resource_finder, #resource_name, #resource_scope, #scope_for
#clear_thread
#set_timezone
#set_topbar_sweeper_controller
#backup_session_content, #expire_session, #ignore_api_request?, #session_expiry, #set_activity_time, #update_activity_time
#authenticate, #authorized, #available_sso, #check_user_enabled, #is_admin?, #path_to_authenticate, #require_login
needs_migration?
Instance Method Details
288
289
290
291
292
293
294
295
296
297
|
# File 'app/controllers/api/v2/hosts_controller.rb', line 288
def boot
valid_devices = ProxyAPI::BMC::SUPPORTED_BOOT_DEVICES
if valid_devices.include? params[:device]
render :json => { :boot => @host.ipmi_boot(params[:device]) }, :status => :ok
else
render :json => { :error => _("Unknown device: available devices are %s") % valid_devices.join(', ') }, :status => :unprocessable_entity
end
rescue ::Foreman::Exception => e
render_exception(e, :status => :unprocessable_entity)
end
|
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
# File 'app/controllers/api/v2/hosts_controller.rb', line 130
def create
@parameters = true
@all_parameters = true
if params[:host][:uuid].present? && params[:host][:compute_resource_id].present?
@host = import_host
@host.assign_attributes(host_attributes(host_params))
else
@host = Host.new(host_attributes(host_params))
@host.managed = true if (params[:host] && params[:host][:managed].nil?)
end
apply_compute_profile(@host)
@host.suggest_default_pxe_loader if params[:host] && params[:host][:pxe_loader].nil?
forward_request_url
process_response @host.save
rescue InterfaceTypeMapper::UnknownTypeException => e
render_error :custom_error, :status => :unprocessable_entity, :locals => { :message => e.to_s }
end
|
169
170
171
|
# File 'app/controllers/api/v2/hosts_controller.rb', line 169
def destroy
process_response @host.destroy
end
|
#disassociate ⇒ Object
246
247
248
249
|
# File 'app/controllers/api/v2/hosts_controller.rb', line 246
def disassociate
@host.disassociate!
render 'api/v2/hosts/show'
end
|
176
177
178
|
# File 'app/controllers/api/v2/hosts_controller.rb', line 176
def enc
render :json => { :data => @host.info }
end
|
305
306
307
308
309
310
311
|
# File 'app/controllers/api/v2/hosts_controller.rb', line 305
def facts
@host = detect_host_type.import_host params[:name], params[:certname]
state = HostFactImporter.new(@host).import_facts(params[:facts].to_unsafe_h, detected_proxy)
process_response state
rescue ::Foreman::Exception => e
render_exception(e, :status => :unprocessable_entity)
end
|
#forget_status ⇒ Object
210
211
212
213
214
215
216
217
218
219
|
# File 'app/controllers/api/v2/hosts_controller.rb', line 210
def forget_status
status = @host.get_status(HostStatus.find_status_by_humanized_name(params[:type]))
if params[:type] == 'global'
render :json => { :error => _("Cannot delete global status.") }, :status => :unprocessable_entity
elsif status.type.empty? || status.id.nil?
render :json => { :error => _("Status %s does not exist.") % params[:type] }, :status => :unprocessable_entity
else
process_response status.delete
end
end
|
#get_status ⇒ Object
192
193
194
195
196
197
198
199
|
# File 'app/controllers/api/v2/hosts_controller.rb', line 192
def get_status
case params[:type]
when 'global'
@status = @host.build_global_status
else
@status = @host.get_status(HostStatus.find_status_by_humanized_name(params[:type]))
end
end
|
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'app/controllers/api/v2/hosts_controller.rb', line 41
def index
@hosts = action_scope_for(:index, resource_scope_for_index)
if params[:thin]
@subtotal = @hosts.total_entries
@hosts = @hosts.reorder(:name).distinct.pluck(:id, :name)
render 'thin'
return
end
@last_report_ids = Report.where(:host_id => @hosts.map(&:id)).group(:host_id).maximum(:id)
@last_reports = Report.where(:id => @last_report_ids.values)
if params[:include].present?
@parameters = params[:include].include?('parameters')
@all_parameters = params[:include].include?('all_parameters')
end
end
|
255
256
257
258
259
260
261
262
263
264
265
266
|
# File 'app/controllers/api/v2/hosts_controller.rb', line 255
def power
unless @host.supports_power?
return render_error :custom_error, :status => :unprocessable_entity, :locals => { :message => _('Power operations are not enabled on this host.') }
end
valid_actions = PowerManager::SUPPORTED_ACTIONS
if valid_actions.include? params[:power_action]
render :json => { :power => @host.power.send(params[:power_action]) }, :status => :ok
else
render :json => { :error => _("Unknown power action: available methods are %s") % valid_actions.join(', ') }, :status => :unprocessable_entity
end
end
|
#power_status ⇒ Object
271
272
273
274
275
276
277
278
279
280
281
282
|
# File 'app/controllers/api/v2/hosts_controller.rb', line 271
def power_status
render json: PowerManager::PowerStatus.new(host: @host).power_state
rescue => e
Foreman::Logging.exception("Failed to fetch power status", e)
resp = {
id: @host.id,
statusText: _("Failed to fetch power status: %s") % e,
}
render json: resp.merge(PowerManager::PowerStatus::HOST_POWER[:na])
end
|
#rebuild_config ⇒ Object
316
317
318
319
320
321
322
323
324
|
# File 'app/controllers/api/v2/hosts_controller.rb', line 316
def rebuild_config
result = @host.recreate_config(params[:only])
failures = result.reject { |key, value| value }.keys.map { |k| _(k) }
if failures.empty?
render_message _("Configuration successfully rebuilt."), :status => :ok
else
render_message (_("Configuration rebuild failed for: %s.") % failures.to_sentence), :status => :unprocessable_entity
end
end
|
64
65
66
67
|
# File 'app/controllers/api/v2/hosts_controller.rb', line 64
def show
@parameters = true
@all_parameters = true
end
|
329
330
331
332
333
334
335
336
|
# File 'app/controllers/api/v2/hosts_controller.rb', line 329
def template
template = @host.provisioning_template({ :kind => params[:kind] })
if template.nil?
not_found(_("No template with kind %{kind} for %{host}") % {:kind => params[:kind], :host => @host.to_label})
else
render :json => { :template => @host.render_template(template: template) }, :status => :ok
end
end
|
154
155
156
157
158
159
160
161
162
163
164
|
# File 'app/controllers/api/v2/hosts_controller.rb', line 154
def update
@parameters = true
@all_parameters = true
@host.attributes = host_attributes(host_params, @host)
apply_compute_profile(@host) if (params[:host] && params[:host][:compute_attributes].present?) || @host.compute_profile_id_changed?
process_response @host.save
rescue InterfaceTypeMapper::UnknownTypeException => e
render_error :custom_error, :status => :unprocessable_entity, :locals => { :message => e.to_s }
end
|
#vm_compute_attributes ⇒ Object
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
# File 'app/controllers/api/v2/hosts_controller.rb', line 227
def vm_compute_attributes
render :json => {} unless @host
attrs = @host.vm_compute_attributes || {}
safe_attrs = {}
attrs.each_pair do |k, v|
if v.is_a?(Proc)
safe_attrs[k] = v.call
elsif v.respond_to?('parent')
else
safe_attrs[k] = v
end
end
render :json => safe_attrs
end
|