Skip to content

Commit

Permalink
Fixes #12132 - [API] Host all parameters should include host parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
shlomizadok authored and Dominic Cleal committed Oct 27, 2015
1 parent 9a115cf commit 4e08a71
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/models/host/managed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,11 @@ def host_inherited_params_objects
params
end

def host_params_objects
# Host parameters should always be first for the uniq order
(host_parameters + host_inherited_params_objects.reverse!).uniq {|param| param.name}
end

# JSON is auto-parsed by the API, so these should be in the right format
def self.import_host_and_facts(hostname, facts, certname = nil, proxy_id = nil)
raise(::Foreman::Exception.new("Invalid Facts, must be a Hash")) unless facts.is_a?(Hash)
Expand Down
2 changes: 1 addition & 1 deletion app/views/api/v2/hosts/show.json.rabl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ child :host_parameters => :parameters do
end

node do |host|
{ :all_parameters => partial("api/v2/parameters/base", :object => host.host_inherited_params_objects) }
{ :all_parameters => partial("api/v2/parameters/base", :object => host.host_params_objects) }
end

child :interfaces => :interfaces do
Expand Down
24 changes: 24 additions & 0 deletions test/unit/host_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2607,6 +2607,30 @@ def to_managed!
assert(host.host_inherited_params_objects.include?(location_parameter), 'Taxonomy parameters should be included')
end

test '#host_params_objects should display all parameters with overrides' do
host = FactoryGirl.create(:host,
:location => taxonomies(:location1),
:organization => taxonomies(:organization1),
:domain => domains(:mydomain))
location_parameter = LocationParameter.new(:name => 'location', :value => 'parameter')
host.location.location_parameters = [location_parameter]
host_location_override = HostParameter.new(:name => 'location', :value => 'the moon')
host.host_parameters += [host_location_override]
assert(host.host_params_objects.include?(host_location_override), 'Location parameter should be overriden')
refute(host.host_params_objects.include?(location_parameter), 'Location parameter should not be included')
end

test 'host_params_objects should display parameters in the right order' do
host = FactoryGirl.create(:host,
:location => taxonomies(:location1),
:organization => taxonomies(:organization1),
:domain => domains(:mydomain))
domain_parameter = DomainParameter.new(:name => 'domain', :value => 'here.there')
host.domain.domain_parameters = [domain_parameter]
assert_equal(domain_parameter, host.host_params_objects.first, 'with no hostgroup, DomainParameter should be first parameter')
assert(host.host_params_objects.last.is_a?(CommonParameter), 'CommonParameter should be last parameter')
end

describe '#param_true?' do
test 'returns false for unknown parameter' do
Foreman::Cast.expects(:to_bool).never
Expand Down

0 comments on commit 4e08a71

Please sign in to comment.