Revision 8ebbbecf
Added by Shira Maximov almost 6 years ago
app/controllers/api/v2/fact_values_controller.rb | ||
---|---|---|
no_timestamp_facts.
|
||
search_for(*search_options).paginate(paginate_options).
|
||
preload(:fact_name, :host)
|
||
@fact_values = FactValue.build_facts_hash(values.all)
|
||
host_id = params[:host_id]
|
||
@fact_values = FactValue.build_facts_hash(values, host_id)
|
||
end
|
||
end
|
||
end
|
app/models/fact_value.rb | ||
---|---|---|
output
|
||
end
|
||
|
||
def self.build_facts_hash(facts)
|
||
hosts = Host.where(:id => facts.group_by(&:host_id).keys).all
|
||
|
||
hash = {}
|
||
facts.each do |fact|
|
||
hash[hosts.detect {|h| h.id == fact.host_id}.to_s] ||= {}
|
||
hash[hosts.detect {|h| h.id == fact.host_id}.to_s].update({fact.name.to_s => fact.value})
|
||
def self.build_facts_hash(values, host_id = nil)
|
||
hash = values.group_by(&:host_name).transform_values!{|val| val.map{|v| [v.fact_name_name, v.value]}.to_h}
|
||
if host_id && hash.any?
|
||
hash.merge! hash.values[0]
|
||
end
|
||
hash
|
||
end
|
test/controllers/api/v2/fact_values_controller_test.rb | ||
---|---|---|
test "should get index" do
|
||
get :index
|
||
assert_response :success
|
||
fact_values = ActiveSupport::JSON.decode(@response.body)
|
||
refute_empty fact_values
|
||
fact_values = ActiveSupport::JSON.decode(@response.body)['results']
|
||
expected_hash = {@host.name=>{"kernelversion"=>"2.6.9"}}
|
||
assert_equal expected_hash, fact_values
|
||
end
|
||
|
||
test "should get facts for given host only" do
|
||
get :index, params: { :host_id => @host.name }
|
||
get :index, params: {:host_id => @host.name}
|
||
assert_response :success
|
||
fact_values = ActiveSupport::JSON.decode(@response.body)['results']
|
||
expected_hash = FactValue.build_facts_hash(FactValue.where(:host_id => @host.id))
|
||
expected_hash = {@host.name =>{"kernelversion"=>"2.6.9"}, "kernelversion"=>"2.6.9"}
|
||
assert_equal expected_hash, fact_values
|
||
end
|
||
|
||
test "should get facts for given host id" do
|
||
get :index, params: { :host_id => @host.id }
|
||
get :index, params: {:host_id => @host.id}
|
||
assert_response :success
|
||
fact_values = ActiveSupport::JSON.decode(@response.body)['results']
|
||
expected_hash = FactValue.build_facts_hash(FactValue.where(:host_id => @host.id))
|
||
fact_values = ActiveSupport::JSON.decode(@response.body)['results']
|
||
expected_hash = {@host.name =>{"kernelversion"=>"2.6.9"}, "kernelversion"=>"2.6.9"}
|
||
assert_equal expected_hash, fact_values
|
||
end
|
||
|
||
... | ... | |
setup_user
|
||
@host.update_attribute(:hostgroup, FactoryBot.create(:hostgroup))
|
||
as_user(users(:one)) do
|
||
get :index, params: { :search => "host.hostgroup = #{@host.hostgroup.name}" }
|
||
get :index, params: {:search => "host.hostgroup = #{@host.hostgroup.name}"}
|
||
end
|
||
assert_response :success
|
||
fact_values = ActiveSupport::JSON.decode(@response.body)['results']
|
||
expected_hash = FactValue.build_facts_hash(FactValue.where(:host_id => @host.id))
|
||
fact_values = ActiveSupport::JSON.decode(@response.body)['results']
|
||
expected_hash = {@host.name=>{"kernelversion"=>"2.6.9"}}
|
||
assert_equal expected_hash, fact_values
|
||
end
|
||
|
||
test "should return empty result in case host doesn't exists" do
|
||
get :index, params: {:host_id => 9000}
|
||
assert_response :success
|
||
fact_values = ActiveSupport::JSON.decode(@response.body)['results']
|
||
expected_hash = {}
|
||
assert_equal expected_hash, fact_values
|
||
end
|
||
|
||
... | ... | |
|
||
def setup_user
|
||
@request.session[:user] = users(:one).id
|
||
users(:one).roles = [Role.default, Role.find_by_name('Viewer')]
|
||
users(:one).roles = [Role.default, Role.find_by_name('Viewer')]
|
||
end
|
||
end
|
Also available in: Unified diff
Fixes #20891 - Remove hostname from /hosts/x/facts results