foreman/app/controllers/concerns/api/v2/lookup_keys_common_controller.rb @ 9d43fc71
21bf889a | Joseph Mitchell Magen | module Api::V2::LookupKeysCommonController
|
|
extend ActiveSupport::Concern
|
|||
afe02d30 | Daniel Lobato | included do
|
|
df6a9f34 | Dominic Cleal | before_action :find_environment, :if => :environment_id?
|
|
before_action :find_puppetclass, :if => :puppetclass_id?
|
|||
before_action :find_host, :if => :host_id?
|
|||
before_action :find_hostgroup, :if => :hostgroup_id?
|
|||
afe02d30 | Daniel Lobato | ||
df6a9f34 | Dominic Cleal | before_action :find_smart_class_parameters, :if => :smart_class_parameter_id?
|
|
before_action :find_smart_class_parameter, :if => :smart_class_parameter_id?
|
|||
afe02d30 | Daniel Lobato | ||
df6a9f34 | Dominic Cleal | before_action :find_smart_variables, :if => :smart_variable_id?
|
|
before_action :find_smart_variable, :if => :smart_variable_id?
|
|||
afe02d30 | Daniel Lobato | ||
df6a9f34 | Dominic Cleal | before_action :find_smarts
|
|
before_action :find_smart
|
|||
afe02d30 | Daniel Lobato | ||
df6a9f34 | Dominic Cleal | before_action :return_if_smart_mismatch, :only => [:show, :update, :destroy]
|
|
cade5cc2 | Ori Rabin | ||
before_action :cast_default_value, :only => [:create, :update]
|
|||
afe02d30 | Daniel Lobato | end
|
|
def smart_variable_id?
|
|||
e0910b7e | Michael Moll | params.key?('smart_variable_id') || controller_name.match(/smart_variables/)
|
|
afe02d30 | Daniel Lobato | end
|
|
def smart_class_parameter_id?
|
|||
e0910b7e | Michael Moll | params.key?('smart_class_parameter_id') || controller_name.match(/smart_class_parameters/)
|
|
afe02d30 | Daniel Lobato | end
|
|
fb9f45e8 | Daniel Lobato | [Puppetclass, Environment, Host::Base, Hostgroup].each do |model|
|
|
model_string = model.to_s.split('::').first.downcase
|
|||
afe02d30 | Daniel Lobato | ||
fb9f45e8 | Daniel Lobato | define_method("#{model_string}_id?") do
|
|
e0910b7e | Michael Moll | params.key?("#{model_string}_id")
|
|
fb9f45e8 | Daniel Lobato | end
|
|
afe02d30 | Daniel Lobato | ||
fb9f45e8 | Daniel Lobato | define_method("find_#{model_string}") do
|
|
scope = model.authorized(:"view_#{model_string.pluralize}")
|
|||
begin
|
|||
instance_variable_set("@#{model_string}",
|
|||
resource_finder(scope, params["#{model_string}_id"]))
|
|||
rescue ActiveRecord::RecordNotFound
|
|||
model_not_found(model_string)
|
|||
end
|
|||
end
|
|||
afe02d30 | Daniel Lobato | end
|
|
def find_smart_variable
|
|||
e0910b7e | Michael Moll | id = params.key?('smart_variable_id') ? params['smart_variable_id'] : params['id']
|
|
6b4b489f | Ori Rabin | @smart_variable = VariableLookupKey.authorized(:view_external_variables).smart_variables.find_by_id(id.to_i) if id.to_i > 0
|
|
4a22ccda | Michael Moll | @smart_variable ||= begin
|
|
puppet_cond = { :puppetclass_id => @puppetclass.id } if @puppetclass
|
|||
VariableLookupKey.authorized(:view_external_variables).smart_variables.where(puppet_cond).find_by_key(id.to_s)
|
|||
end
|
|||
afe02d30 | Daniel Lobato | @smart_variable
|
|
end
|
|||
def find_smart_variables
|
|||
@smart_variables = smart_variables_resource_scope.search_for(*search_options).paginate(paginate_options)
|
|||
end
|
|||
def smart_variables_resource_scope
|
|||
6b4b489f | Ori Rabin | return VariableLookupKey.authorized(:view_external_variables).smart_variables unless (@puppetclass || @host || @hostgroup)
|
|
afe02d30 | Daniel Lobato | puppetclass_ids = @puppetclass.id if @puppetclass
|
|
puppetclass_ids ||= @hostgroup.all_puppetclasses.map(&:id) if @hostgroup
|
|||
puppetclass_ids ||= @host.all_puppetclasses.map(&:id) if @host
|
|||
6b4b489f | Ori Rabin | VariableLookupKey.authorized(:view_external_variables).global_parameters_for_class(puppetclass_ids)
|
|
afe02d30 | Daniel Lobato | end
|
|
def find_smart_class_parameter
|
|||
e0910b7e | Michael Moll | id = params.key?('smart_class_parameter_id') ? params['smart_class_parameter_id'] : params['id']
|
|
3fe2ea3d | Ori Rabin | @smart_class_parameter = PuppetclassLookupKey.authorized(:view_external_parameters).smart_class_parameters.find_by_id(id.to_i) if id.to_i > 0
|
|
4a22ccda | Michael Moll | @smart_class_parameter ||= begin
|
|
9d43fc71 | Michael Moll | puppet_cond = { 'environment_classes.puppetclass_id' => @puppetclass.id } if @puppetclass
|
|
4a22ccda | Michael Moll | env_cond = { 'environment_classes.environment_id' => @environment.id } if @environment
|
|
PuppetclassLookupKey.authorized(:view_external_parameters).smart_class_parameters.where(puppet_cond).where(env_cond).where(:key => id).first
|
|||
end
|
|||
afe02d30 | Daniel Lobato | @smart_class_parameter
|
|
end
|
|||
def find_smart_class_parameters
|
|||
@smart_class_parameters = smart_class_parameters_resource_scope.search_for(*search_options).paginate(paginate_options)
|
|||
end
|
|||
def smart_class_parameters_resource_scope
|
|||
3fe2ea3d | Ori Rabin | base = PuppetclassLookupKey.authorized(:view_external_parameters)
|
|
40b69114 | Martin Bačovský | params = if !@puppetclass && !@environment && !@host && !@hostgroup
|
|
base.smart_class_parameters
|
|||
elsif @puppetclass && @environment
|
|||
base.smart_class_parameters_for_class(@puppetclass.id, @environment.id)
|
|||
elsif @puppetclass && !@environment
|
|||
environment_ids = @puppetclass.environment_classes.pluck(:environment_id).uniq
|
|||
base.smart_class_parameters_for_class(@puppetclass.id, environment_ids)
|
|||
elsif !@puppetclass && @environment
|
|||
puppetclass_ids = @environment.environment_classes.pluck(:puppetclass_id).uniq
|
|||
base.smart_class_parameters_for_class(puppetclass_ids, @environment.id)
|
|||
elsif @host || @hostgroup
|
|||
puppetclass_ids = (@host || @hostgroup).all_puppetclasses.map(&:id)
|
|||
environment_id = (@host || @hostgroup).environment_id
|
|||
# scope :parameters_for_class uses .override
|
|||
base.parameters_for_class(puppetclass_ids, environment_id)
|
|||
end
|
|||
params.distinct
|
|||
afe02d30 | Daniel Lobato | end
|
|
def find_smarts
|
|||
@smarts = @smart_variables
|
|||
@smarts ||= @smart_class_parameters
|
|||
@smarts
|
|||
end
|
|||
def find_smart
|
|||
@smart = @smart_variable
|
|||
@smart ||= @smart_class_parameter
|
|||
@smart
|
|||
end
|
|||
def return_if_smart_mismatch
|
|||
if (@smarts && @smart && !@smarts.find_by_id(@smart.id)) || (@smarts && !@smart)
|
|||
obj = smart_variable_id? ? "Smart variable" : "Smart class parameter"
|
|||
id = if smart_variable_id?
|
|||
e0910b7e | Michael Moll | params.key?('smart_variable_id') ? params['smart_variable_id'] : params['id']
|
|
afe02d30 | Daniel Lobato | else
|
|
e0910b7e | Michael Moll | params.key?('smart_class_parameter_id') ? params['smart_variable_id'] : params['id']
|
|
afe02d30 | Daniel Lobato | end
|
|
not_found "#{obj} not found by id '#{id}'"
|
|||
end
|
|||
end
|
|||
fb9f45e8 | Daniel Lobato | ||
cade5cc2 | Ori Rabin | def cast_default_value
|
|
obj = smart_variable_id? ? "smart_variable" : "smart_class_parameter"
|
|||
cast_value(obj, :default_value)
|
|||
end
|
|||
def cast_value(obj = :override_value, value = :value)
|
|||
b03dcd1b | Michael Moll | return unless params[obj]&.key?(value)
|
|
cade5cc2 | Ori Rabin | param_value = params[obj][value]
|
|
return if param_value.is_a?(Hash)
|
|||
params[obj][value] = param_value.to_s
|
|||
end
|
|||
fb9f45e8 | Daniel Lobato | private
|
|
def model_not_found(model)
|
|||
error_message = (
|
|||
_("%{model} with id '%{id}' was not found") %
|
|||
f4459c11 | David Davis | { :id => params["#{model}_id"], :model => model.capitalize })
|
|
not_found(:error => { :message => error_message })
|
|||
fb9f45e8 | Daniel Lobato | end
|
|
21bf889a | Joseph Mitchell Magen | end
|