Class: Api::V2::HostsController

Inherits:
BaseController show all
Includes:
HostsControllerExtension, Api::Version2, Foreman::Controller::Parameters::Host, Foreman::Controller::SmartProxyAuth, ParameterAttributes, ScopesPerAction
Defined in:
app/controllers/api/v2/hosts_controller.rb

Instance Method Summary collapse

Methods included from HostsControllerExtension

#resource_name

Methods included from ParameterAttributes

#process_parameter_attributes

Methods included from ScopesPerAction

#action_scope_for

Methods included from Api::Version2

#api_version

Methods inherited from BaseController

#append_array_of_ids, #check_content_type, #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, skip_before_action

Methods inherited from BaseController

#api_request?, #controller_permission, #get_resource, parameter_filter_context, #parent_scope, #resource_scope, #resource_scope_for_index

Methods included from Foreman::Controller::BruteforceProtection

#bruteforce_attempt?

Methods included from ApplicationShared

#current_permission, #find_default_taxonomy, #find_session_taxonomy, #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

Methods included from Foreman::ThreadSession::Cleaner

#clear_thread

Methods included from Foreman::Controller::Timezone

#set_timezone

Methods included from Foreman::Controller::TopbarSweeper

#set_topbar_sweeper_controller

Methods included from Foreman::Controller::Session

#backup_session_content, #expire_session, #ignore_api_request?, #session_expiry, #set_activity_time, #update_activity_time

Methods included from Foreman::Controller::Authentication

#authenticate, #authorized, #available_sso, #check_user_enabled, #is_admin?, #path_to_authenticate, #require_login

Methods included from Foreman::Controller::MigrationChecker

needs_migration?

Instance Method Details

#bootObject


287
288
289
290
291
292
293
294
295
296
# File 'app/controllers/api/v2/hosts_controller.rb', line 287

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

#createObject


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/controllers/api/v2/hosts_controller.rb', line 129

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

#destroyObject


168
169
170
# File 'app/controllers/api/v2/hosts_controller.rb', line 168

def destroy
  process_response @host.destroy
end

#disassociateObject


245
246
247
248
# File 'app/controllers/api/v2/hosts_controller.rb', line 245

def disassociate
  @host.disassociate!
  render 'api/v2/hosts/show'
end

#encObject


175
176
177
# File 'app/controllers/api/v2/hosts_controller.rb', line 175

def enc
  render :json => { :data => @host.info }
end

#factsObject


304
305
306
307
308
309
310
# File 'app/controllers/api/v2/hosts_controller.rb', line 304

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_statusObject


209
210
211
212
213
214
215
216
217
218
# File 'app/controllers/api/v2/hosts_controller.rb', line 209

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_statusObject


191
192
193
194
195
196
197
198
# File 'app/controllers/api/v2/hosts_controller.rb', line 191

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

#indexObject


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

  # SQL optimizations queries
  @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

#powerObject


254
255
256
257
258
259
260
261
262
263
264
265
# File 'app/controllers/api/v2/hosts_controller.rb', line 254

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_statusObject


270
271
272
273
274
275
276
277
278
279
280
281
# File 'app/controllers/api/v2/hosts_controller.rb', line 270

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_configObject


315
316
317
318
319
320
321
322
323
# File 'app/controllers/api/v2/hosts_controller.rb', line 315

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

#showObject


64
65
66
67
# File 'app/controllers/api/v2/hosts_controller.rb', line 64

def show
  @parameters = true
  @all_parameters = true
end

#templateObject


328
329
330
331
332
333
334
335
# File 'app/controllers/api/v2/hosts_controller.rb', line 328

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

#updateObject


153
154
155
156
157
158
159
160
161
162
163
# File 'app/controllers/api/v2/hosts_controller.rb', line 153

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_attributesObject


226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'app/controllers/api/v2/hosts_controller.rb', line 226

def vm_compute_attributes
  render :json => {} unless @host
  attrs = @host.vm_compute_attributes || {}
  safe_attrs = {}
  attrs.each_pair do |k, v|
    # clean up the compute attributes to be suitable for output
    if v.is_a?(Proc)
      safe_attrs[k] = v.call
    elsif v.respond_to?('parent')
      # don't add folders, causes recursive json issues
    else
      safe_attrs[k] = v
    end
  end
  render :json => safe_attrs
end