diff --git a/app/models/host/managed.rb b/app/models/host/managed.rb index 33845c2e79d..b45addcc9c9 100644 --- a/app/models/host/managed.rb +++ b/app/models/host/managed.rb @@ -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) diff --git a/app/views/api/v2/hosts/show.json.rabl b/app/views/api/v2/hosts/show.json.rabl index 21c136c19ae..094c797b5bc 100644 --- a/app/views/api/v2/hosts/show.json.rabl +++ b/app/views/api/v2/hosts/show.json.rabl @@ -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 diff --git a/test/unit/host_test.rb b/test/unit/host_test.rb index e0220217492..78d254551e6 100644 --- a/test/unit/host_test.rb +++ b/test/unit/host_test.rb @@ -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