Project

General

Profile

Download (18.3 KB) Statistics
| Branch: | Tag: | Revision:
f2fed493 Arnoud de Jonge
module Api
module V2
dc38aad3 Joseph Magen
class HostsController < V2::BaseController
f2fed493 Arnoud de Jonge
include Api::Version2
12612809 Dominic Cleal
include Api::CompatibilityChecker
87f8f03e Shimon Shtein
include ScopesPerAction
01055e77 Greg Sutcliffe
include Foreman::Controller::SmartProxyAuth
12612809 Dominic Cleal
include Foreman::Controller::Parameters::Host
00ed8ba5 Ondřej Pražák
include ParameterAttributes
12612809 Dominic Cleal
wrap_parameters :host, :include => host_params_filter.accessible_attributes(parameter_filter_context) + ['compute_attributes']
6aaeffa0 Shimon Shtein
include HostsControllerExtension
f2fed493 Arnoud de Jonge
df6a9f34 Dominic Cleal
before_action :check_create_host_nested, :only => [:create, :update]
7ee381e9 Lukas Zapletal
df6a9f34 Dominic Cleal
before_action :find_optional_nested_object, :except => [:facts]
before_action :find_resource, :except => [:index, :create, :facts]
6aaeffa0 Shimon Shtein
check_permissions_for %w{power boot}
00ed8ba5 Ondřej Pražák
before_action :process_parameter_attributes, :only => %w{update}
c3b33536 Stephen Benjamin
1d0315e0 Marek Hulan
add_smart_proxy_filters :facts, :features => Proc.new { FactImporter.fact_features }
f2fed493 Arnoud de Jonge
9e892b43 Tomer Brisker
add_scope_for(:index) do |base_scope|
base_scope.preload([:host_statuses, :compute_resource, :hostgroup, :operatingsystem,
:interfaces, :token, :owner, :model, :environment, :location,
:organization, :image, :compute_profile, :realm, :architecture,
:ptable, :medium, :puppet_proxy, :puppet_ca_proxy])
end
87f8f03e Shimon Shtein
2656873b Martin Bačovský
api :GET, "/hosts/", N_("List all hosts")
453dc693 Joseph Magen
api :GET, "/hostgroups/:hostgroup_id/hosts", N_("List all hosts for a host group")
api :GET, "/locations/:location_id/hosts", N_("List hosts per location")
api :GET, "/organizations/:organization_id/hosts", N_("List hosts per organization")
api :GET, "/environments/:environment_id/hosts", N_("List hosts per environment")
71c08300 Tomer Brisker
param :thin, :bool, :desc => N_("Only list ID and name of hosts")
453dc693 Joseph Magen
param :hostgroup_id, String, :desc => N_("ID of host group")
param :location_id, String, :desc => N_("ID of location")
param :organization_id, String, :desc => N_("ID of organization")
param :environment_id, String, :desc => N_("ID of environment")
cb591e34 Tomas Strachota
param :include, ['parameters', 'all_parameters'], :desc => N_("Array of extra information types to include")
915b2918 Tomas Strachota
param_group :search_and_pagination, ::Api::V2::BaseController
6330baae Tomas Strachota
add_scoped_search_description_for(Host)
dc38aad3 Joseph Magen
def index
87f8f03e Shimon Shtein
@hosts = action_scope_for(:index, resource_scope_for_index)

71c08300 Tomer Brisker
if params[:thin]
d5f2f1de Amir Fefer
@subtotal = @hosts.total_entries
71c08300 Tomer Brisker
@hosts = @hosts.reorder(:name).distinct.pluck(:id, :name)
render 'thin'
return
end

e54016da Marek Hulan
# 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)
b819a37b Trey Dockendorf
if params[:include].present?
@parameters = params[:include].include?('parameters')
@all_parameters = params[:include].include?('all_parameters')
end
dc38aad3 Joseph Magen
end

2656873b Martin Bačovský
api :GET, "/hosts/:id/", N_("Show a host")
dc38aad3 Joseph Magen
param :id, :identifier_dottable, :required => true
38a9ed0c orrabin
param :show_hidden_parameters, :bool, :desc => N_("Display hidden parameter values")
dc38aad3 Joseph Magen
def show
b819a37b Trey Dockendorf
@parameters = true
@all_parameters = true
dc38aad3 Joseph Magen
end

2be84f3d Joseph Magen
def_param_group :host do
fb5ac616 Joseph Magen
param :host, Hash, :required => true, :action_aware => true do
2be84f3d Joseph Magen
param :name, String, :required => true
2656873b Martin Bačovský
param :location_id, :number, :required => true, :desc => N_("required if locations are enabled") if SETTINGS[:locations_enabled]
param :organization_id, :number, :required => true, :desc => N_("required if organizations are enabled") if SETTINGS[:organizations_enabled]
5638cc68 Joseph Magen
param :environment_id, String, :desc => N_("required if host is managed and value is not inherited from host group")
2656873b Martin Bačovský
param :ip, String, :desc => N_("not required if using a subnet with DHCP proxy")
5638cc68 Joseph Magen
param :mac, String, :desc => N_("required for managed host that is bare metal, not required if it's a virtual machine")
param :architecture_id, :number, :desc => N_("required if host is managed and value is not inherited from host group")
param :domain_id, :number, :desc => N_("required if host is managed and value is not inherited from host group")
77f70152 Stephen Benjamin
param :realm_id, :number
c6760930 Timo Goebel
Host.registered_smart_proxies.each do |name, options|
param :"#{name}_id", :number, :desc => options[:api_description]
end
8f9fdae3 Evgeni Golov
param :puppetclass_ids, Array
8f3993e3 Ondřej Pražák
param :config_group_ids, Array, :desc => N_("IDs of associated config groups")
5638cc68 Joseph Magen
param :operatingsystem_id, String, :desc => N_("required if host is managed and value is not inherited from host group")
param :medium_id, String, :desc => N_("required if not imaged based provisioning and host is managed and value is not inherited from host group")
4cbf879e Lukas Zapletal
param :pxe_loader, Operatingsystem.all_loaders, :desc => N_("DHCP filename option (Grub2/PXELinux by default)")
5638cc68 Joseph Magen
param :ptable_id, :number, :desc => N_("required if host is managed and custom partition has not been defined")
param :subnet_id, :number, :desc => N_("required if host is managed and value is not inherited from host group")
param :compute_resource_id, :number, :desc => N_("nil means host is bare metal")
param :root_pass, String, :desc => N_("required if host is managed and value is not inherited from host group or default password in settings")
2be84f3d Joseph Magen
param :model_id, :number
param :hostgroup_id, :number
param :owner_id, :number
55be35e9 Tomas Strachota
param :owner_type, Host::Base::OWNER_TYPES, :desc => N_("Host's owner type")
2be84f3d Joseph Magen
param :image_id, :number
7ee381e9 Lukas Zapletal
param :host_parameters_attributes, Array, :desc => N_("Host's parameters (array or indexed hash)") do
param :name, String, :desc => N_("Name of the parameter"), :required => true
param :value, String, :desc => N_("Parameter value"), :required => true
end
2be84f3d Joseph Magen
param :build, :bool
22d605d9 Marek Hulán
param :enabled, :bool, :desc => N_("Include this host within Foreman reporting")
fe1d56e1 Tomas Strachota
param :provision_method, Host::Managed.provision_methods.keys, :desc => N_("The method used to provision the host.")
5638cc68 Joseph Magen
param :managed, :bool, :desc => N_("True/False flag whether a host is managed or unmanaged. Note: this value also determines whether several parameters are required or not")
2656873b Martin Bačovský
param :progress_report_id, String, :desc => N_("UUID to track orchestration tasks status, GET /api/orchestration/:UUID/tasks")
626e88b9 Dominic Cleal
param :comment, String, :desc => N_("Additional information about this host")
2be84f3d Joseph Magen
param :capabilities, String
10ac97b0 Joseph Magen
param :compute_profile_id, :number
cfd1c413 Tomas Strachota
param :interfaces_attributes, Array, :desc => N_("Host's network interfaces.") do
param_group :interface_attributes, ::Api::V2::InterfacesController
2be84f3d Joseph Magen
end
cfd1c413 Tomas Strachota
param :compute_attributes, Hash, :desc => N_("Additional compute resource specific attributes.")
c804d530 Shimon Shtein
Facets.registered_facets.values.each do |facet_config|
next unless facet_config.api_param_group && facet_config.api_controller
param "#{facet_config.name}_attributes".to_sym, Hash, :desc => facet_config.api_param_group_description || (N_("Parameters for host's %s facet") % facet_config.name) do
61ccbc34 Justin Sherrill
facet_config.load_api_controller
c804d530 Shimon Shtein
param_group facet_config.api_param_group, facet_config.api_controller
end
end
dc38aad3 Joseph Magen
end
end

2656873b Martin Bačovský
api :POST, "/hosts/", N_("Create a host")
2be84f3d Joseph Magen
param_group :host, :as => :create

dc38aad3 Joseph Magen
def create
76222035 Shimon Shtein
@parameters = true
@all_parameters = true

637da2f2 Timo Goebel
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
47990639 Tomas Strachota
apply_compute_profile(@host)
497c022b Tomer Brisker
@host.suggest_default_pxe_loader if params[:host] && params[:host][:pxe_loader].nil?
cfd1c413 Tomas Strachota
dc38aad3 Joseph Magen
forward_request_url
process_response @host.save
cfd1c413 Tomas Strachota
rescue InterfaceTypeMapper::UnknownTypeExeption => e
render_error :custom_error, :status => :unprocessable_entity, :locals => { :message => e.to_s }
dc38aad3 Joseph Magen
end

2656873b Martin Bačovský
api :PUT, "/hosts/:id/", N_("Update a host")
dc38aad3 Joseph Magen
param :id, :identifier, :required => true
2be84f3d Joseph Magen
param_group :host
dc38aad3 Joseph Magen
def update
76222035 Shimon Shtein
@parameters = true
@all_parameters = true

12612809 Dominic Cleal
@host.attributes = host_attributes(host_params, @host)
e653ec5e Timo Goebel
apply_compute_profile(@host) if (params[:host] && params[:host][:compute_attributes].present?) || @host.compute_profile_id_changed?
cfd1c413 Tomas Strachota
process_response @host.save
rescue InterfaceTypeMapper::UnknownTypeExeption => e
render_error :custom_error, :status => :unprocessable_entity, :locals => { :message => e.to_s }
dc38aad3 Joseph Magen
end

2656873b Martin Bačovský
api :DELETE, "/hosts/:id/", N_("Delete a host")
dc38aad3 Joseph Magen
param :id, :identifier, :required => true

def destroy
process_response @host.destroy
end

45e7f2c7 Ondřej Pražák
api :GET, "/hosts/:id/enc", N_("Get ENC values of host")
param :id, :identifier_dottable, :required => true

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

e54016da Marek Hulan
api :GET, "/hosts/:id/status", N_("Get configuration status of host")
dc38aad3 Joseph Magen
param :id, :identifier_dottable, :required => true
cba0f866 Michael Moll
description <<-EOS
dc38aad3 Joseph Magen
Return value may either be one of the following:

710095f6 Marek Hulán
* Alerts disabled
* No reports
* Error
* Out of sync
* Active
* Pending
* No changes
cba0f866 Michael Moll
EOS
dc38aad3 Joseph Magen
def status
e54016da Marek Hulan
Foreman::Deprecation.api_deprecation_warning('The /status route is deprecated, please use the new /status/configuration instead')
render :json => { :status => @host.get_status(HostStatus::ConfigurationStatus).to_label }.to_json if @host
end

api :GET, "/hosts/:id/status/:type", N_("Get status of host")
param :id, :identifier_dottable, :required => true
cba0f866 Michael Moll
param :type, [ HostStatus::Global ] + HostStatus.status_registry.to_a.map { |s| s.humanized_name }, :required => true, :desc => N_(<<-EOS
e54016da Marek Hulan
status type, can be one of
* global
* configuration
* build
cba0f866 Michael Moll
EOS
e54016da Marek Hulan
)
description N_('Returns string representing a host status of a given type')
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
dc38aad3 Joseph Magen
end

7e8bfe82 Walter Huf
api :GET, "/hosts/:id/vm_compute_attributes", N_("Get vm attributes of host")
param :id, :identifier_dottable, :required => true
cba0f866 Michael Moll
description <<-EOS
7e8bfe82 Walter Huf
Return the host's compute attributes that can be used to create a clone of this VM
cba0f866 Michael Moll
EOS
7e8bfe82 Walter Huf
def vm_compute_attributes
render :json => {} unless @host
b95c1e1b Walter Huf
attrs = @host.vm_compute_attributes || {}
7e8bfe82 Walter Huf
safe_attrs = {}
3cd8c84b Michael Moll
attrs.each_pair do |k, v|
7e8bfe82 Walter Huf
# 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
b95c1e1b Walter Huf
render :json => safe_attrs
7e8bfe82 Walter Huf
end

2656873b Martin Bačovský
api :PUT, "/hosts/:id/disassociate", N_("Disassociate the host from a VM")
3ccd0ef6 Jason Montleon
param :id, :identifier_dottable, :required => true
def disassociate
@host.disassociate!
render 'api/v2/hosts/show'
end

2656873b Martin Bačovský
api :PUT, "/hosts/:id/power", N_("Run a power operation on host")
8588f9ac Daniel Lobato
param :id, :identifier_dottable, :required => true
2656873b Martin Bačovský
param :power_action, String, :required => true, :desc => N_("power action, valid actions are (on/start), (off/stop), (soft/reboot), (cycle/reset), (state/status)")
8588f9ac Daniel Lobato
def power
08912c32 Shimon Shtein
unless @host.supports_power?
return render_error :custom_error, :status => :unprocessable_entity, :locals => { :message => _('Power operations are not enabled on this host.') }
end

8588f9ac Daniel Lobato
valid_actions = PowerManager::SUPPORTED_ACTIONS
if valid_actions.include? params[:power_action]
c6c50b21 Tomer Brisker
render :json => { :power => @host.power.send(params[:power_action]) }, :status => :ok
8588f9ac Daniel Lobato
else
c6c50b21 Tomer Brisker
render :json => { :error => _("Unknown power action: available methods are %s") % valid_actions.join(', ') }, :status => :unprocessable_entity
8588f9ac Daniel Lobato
end
end

2656873b Martin Bačovský
api :PUT, "/hosts/:id/boot", N_("Boot host from specified device")
8588f9ac Daniel Lobato
param :id, :identifier_dottable, :required => true
2656873b Martin Bačovský
param :device, String, :required => true, :desc => N_("boot device, valid devices are disk, cdrom, pxe, bios")
8588f9ac Daniel Lobato
def boot
valid_devices = ProxyAPI::BMC::SUPPORTED_BOOT_DEVICES
if valid_devices.include? params[:device]
c6c50b21 Tomer Brisker
render :json => { :boot => @host.ipmi_boot(params[:device]) }, :status => :ok
8588f9ac Daniel Lobato
else
c6c50b21 Tomer Brisker
render :json => { :error => _("Unknown device: available devices are %s") % valid_devices.join(', ') }, :status => :unprocessable_entity
8588f9ac Daniel Lobato
end
4090ccb5 Daniel Lobato Garcia
rescue ::Foreman::Exception => e
render_message(e.to_s, :status => :unprocessable_entity)
8588f9ac Daniel Lobato
end

2656873b Martin Bačovský
api :POST, "/hosts/facts", N_("Upload facts for a host, creating the host if required")
param :name, String, :required => true, :desc => N_("hostname of the host")
param :facts, Hash, :required => true, :desc => N_("hash containing the facts for the host")
param :certname, String, :desc => N_("optional: certname of the host")
param :type, String, :desc => N_("optional: the STI type of host to create")
01055e77 Greg Sutcliffe
def facts
715d097c Shimon Shtein
@host = detect_host_type.import_host params[:name], params[:certname]
state = @host.import_facts(params[:facts].to_unsafe_h, detected_proxy)
01055e77 Greg Sutcliffe
process_response state
rescue ::Foreman::Exception => e
2950fe11 Tomas Strachota
render_message(e.to_s, :status => :unprocessable_entity)
01055e77 Greg Sutcliffe
end

2fba6ad7 Ondrej Prazak
api :PUT, "/hosts/:id/rebuild_config", N_("Rebuild orchestration config")
param :id, :identifier_dottable, :required => true
719a47e0 Dominic Cleal
param :only, Array, :desc => N_("Limit rebuild steps, valid steps are %{host_rebuild_steps}")
2fba6ad7 Ondrej Prazak
def rebuild_config
ff5900e9 Trey Dockendorf
result = @host.recreate_config(params[:only])
2fba6ad7 Ondrej Prazak
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

8a817cb2 Shlomi Zadok
api :GET, "/hosts/:id/template/:kind", N_("Preview rendered provisioning template content")
param :id, :identifier_dottable, :required => true
param :kind, String, :required => true, :desc => N_("Template kinds, available values: %{template_kinds}")
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) }, :status => :ok
end
end

01055e77 Greg Sutcliffe
private

47990639 Tomas Strachota
def apply_compute_profile(host)
b299f9c9 Dominic Cleal
host.apply_compute_profile(InterfaceMerge.new(:merge_compute_attributes => true))
47990639 Tomas Strachota
host.apply_compute_profile(ComputeAttributeMerge.new)
cfd1c413 Tomas Strachota
end

04cb74c0 Shimon Shtein
def host_attributes(params, host = nil)
cfd1c413 Tomas Strachota
return {} if params.nil?

params = params.deep_clone
if params[:interfaces_attributes]
# handle both hash and array styles of nested attributes
15c7ce27 Dominic Cleal
if params[:interfaces_attributes].is_a?(Hash) || params[:interfaces_attributes].is_a?(ActionController::Parameters)
cfd1c413 Tomas Strachota
params[:interfaces_attributes] = params[:interfaces_attributes].values
end
# map interface types
params[:interfaces_attributes] = params[:interfaces_attributes].map do |nic_attr|
9a41f58f Tomas Strachota
interface_attributes(nic_attr, allow_nil_type: host.nil?)
cfd1c413 Tomas Strachota
end
end
04cb74c0 Shimon Shtein
params = host.apply_inherited_attributes(params) if host
cfd1c413 Tomas Strachota
params
end

9a41f58f Tomas Strachota
def interface_attributes(params, allow_nil_type: false)
params[:type] = InterfaceTypeMapper.map(params[:type]) if params.has_key?(:type) || allow_nil_type
cfd1c413 Tomas Strachota
params
end

acfbc458 Marek Hulan
def action_permission
case params[:action]
when 'power'
:power
when 'boot'
:ipmi_boot
when 'console'
:console
3ccd0ef6 Jason Montleon
when 'disassociate'
:edit
45e7f2c7 Ondřej Pražák
when 'vm_compute_attributes', 'get_status', 'template', 'enc'
7e8bfe82 Walter Huf
:view
2fba6ad7 Ondrej Prazak
when 'rebuild_config'
:build
acfbc458 Marek Hulan
else
super
end
end

dc38aad3 Joseph Magen
# this is required for template generation (such as pxelinux) which is not done via a web request
def forward_request_url
@host.request_url = request.host_with_port if @host.respond_to?(:request_url)
end

01055e77 Greg Sutcliffe
def detect_host_type
return Host::Managed if params[:type].blank?
cd032085 Daniel Lobato
if params[:type].constantize.new.is_a?(Host::Base)
01055e77 Greg Sutcliffe
logger.debug "Creating host of type: #{params[:type]}"
return params[:type].constantize
else
2656873b Martin Bačovský
raise ::Foreman::Exception.new(N_("Invalid type for host creation via facts: %s"), params[:type])
01055e77 Greg Sutcliffe
end
rescue => e
2656873b Martin Bačovský
raise ::Foreman::Exception.new(N_("A problem occurred when detecting host type: %s"), e.message)
01055e77 Greg Sutcliffe
end

acfbc458 Marek Hulan
def permissions_check
4090ccb5 Daniel Lobato Garcia
permission = "#{action_permission}_hosts".to_sym
e02a2ff2 Julien Pivotto
deny_access unless Host.authorized(permission, Host).find(@host.id)
acfbc458 Marek Hulan
end
10ac97b0 Joseph Magen
453dc693 Joseph Magen
def resource_class
Host::Managed
end

def allowed_nested_id
%w(hostgroup_id location_id organization_id environment_id)
end
8e81effc Amit Karsale
def resource_class_join(association, scope)
resource_class_join = resource_class.joins(association.name)
f4d64312 kgaikwad
if action_name == 'update' && resource_class_join.merge(scope).blank?
resource_class_join
else
resource_class.joins(association.name).merge(scope)
end
8e81effc Amit Karsale
end
637da2f2 Timo Goebel
def import_host
compute_resource = ComputeResource.authorized(:edit_compute_resources).find(params[:host][:compute_resource_id])
ComputeResourceHostImporter.new(
:compute_resource => compute_resource,
:uuid => params[:host][:uuid]
).host
end
f2fed493 Arnoud de Jonge
end
end
end