Project

General

Profile

« Previous | Next » 

Revision ea450c06

Added by Stephen Benjamin about 10 years ago

fixes #5690 - determine host taxonomy fact based on setting value

View differences:

app/models/host/base.rb
['location', 'organization'].each do |taxonomy|
next unless SETTINGS["#{taxonomy.pluralize}_enabled".to_sym]
taxonomy_class = taxonomy.classify.constantize
taxonomy_fact = Setting["#{taxonomy}_fact"]
if Setting["#{taxonomy}_fact"].present? && facts.keys.include?("#{taxonomy}_fact")
taxonomy_from_fact = taxonomy_class.find_by_title(facts["#{taxonomy}_fact"])
if taxonomy_fact.present? && facts.keys.include?(taxonomy_fact)
taxonomy_from_fact = taxonomy_class.find_by_title(facts[taxonomy_fact])
else
default_taxonomy = taxonomy_class.find_by_title(Setting["default_#{taxonomy}".to_sym])
default_taxonomy = taxonomy_class.find_by_title(Setting["default_#{taxonomy}"])
end
if self.send("#{taxonomy}").present?
test/unit/host_test.rb
test 'host taxonomies are set to setting[taxonomy_fact] if it exists' do
Setting[:create_new_host_when_facts_are_uploaded] = true
Setting[:location_fact] = "foreman_location"
Setting[:organization_fact] = "foreman_organization"
raw = parse_json_fixture('/facts.json')
raw['facts']['location_fact'] = 'Location 2'
raw['facts']['organization_fact'] = 'Organization 2'
raw['facts']['foreman_location'] = 'Location 2'
raw['facts']['foreman_organization'] = 'Organization 2'
Host.import_host_and_facts(raw['name'], raw['facts'])
assert_equal 'Location 2', Host.find_by_name('sinn1636.lan').location.title
......
test 'taxonomies from facts override already existing taxonomies in hosts' do
Setting[:create_new_host_when_facts_are_uploaded] = true
Setting[:location_fact] = "foreman_location"
Setting[:organization_fact] = "foreman_organization"
raw = parse_json_fixture('/facts.json')
raw['facts']['location_fact'] = 'Location 2'
raw['facts']['foreman_location'] = 'Location 2'
Host.import_host_and_facts(raw['name'], raw['facts'])
Host.find_by_name('sinn1636.lan').update_attribute(:location, taxonomies(:location1))
Host.find_by_name('sinn1636.lan').import_facts(raw['facts'])

Also available in: Unified diff