Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #9135 - set hostgroup based on fact/default value #2114

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 20 additions & 14 deletions app/models/host/base.rb
Expand Up @@ -105,8 +105,14 @@ def import_facts(facts)
importer.import!

save(:validate => false)

# These are fixed based on the specific fact parser
# Operating Systems, Architectures, etc.
populate_fields_from_facts(facts, type)
set_taxonomies(facts)

# Attributes that can be set dynamically based on Settings
populate_fields_from_dynamic_facts(facts)


# we are saving here with no validations, as we want this process to be as fast
# as possible, assuming we already have all the right settings in Foreman.
Expand Down Expand Up @@ -198,24 +204,24 @@ def ==(comparison_object)
comparison_object.id == id
end

def set_taxonomies(facts)
['location', 'organization'].each do |taxonomy|
next unless SETTINGS["#{taxonomy.pluralize}_enabled".to_sym]
taxonomy_class = taxonomy.classify.constantize
taxonomy_fact = Setting["#{taxonomy}_fact"]
def populate_fields_from_dynamic_facts(facts)
%w(location organization hostgroup).each do |attr|
next if (['location', 'organization'].include?(attr) && !SETTINGS["#{attr.pluralize}_enabled".to_sym])
attr_class = attr.classify.constantize
attr_fact = Setting["#{attr}_fact"]

if taxonomy_fact.present? && facts.keys.include?(taxonomy_fact)
taxonomy_from_fact = taxonomy_class.find_by_title(facts[taxonomy_fact])
if attr_fact.present? && facts.keys.include?(attr_fact)
attr_from_fact = attr_class.find_by_title(facts[attr_fact])
else
default_taxonomy = taxonomy_class.find_by_title(Setting["default_#{taxonomy}"])
default_attr = attr_class.find_by_title(Setting["default_#{attr}"])
end

if self.send("#{taxonomy}").present?
# Change taxonomy to fact taxonomy if set, otherwise leave it as is
self.send("#{taxonomy}=", taxonomy_from_fact) unless taxonomy_from_fact.nil?
if self.send("#{attr}").present?
# Change attribute to fact value if set, otherwise leave it as is
self.send("#{attr}=", attr_from_fact) unless attr_from_fact.nil?
else
# No taxonomy was set, set to fact taxonomy or default taxonomy
self.send "#{taxonomy}=", (taxonomy_from_fact || default_taxonomy)
# No attribute was set, set to fact or default attribute
self.send "#{attr}=", (attr_from_fact || default_attr)
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion app/models/setting/puppet.rb
Expand Up @@ -25,8 +25,10 @@ def self.load_defaults
self.set('legacy_puppet_hostname', N_("Foreman will truncate hostname to 'puppet' if it starts with puppet"), false),
self.set('location_fact', N_("Hosts created after a puppet run will be placed in the location this fact dictates. The content of this fact should be the full label of the location."), 'foreman_location'),
self.set('organization_fact', N_("Hosts created after a puppet run will be placed in the organization this fact dictates. The content of this fact should be the full label of the organization."), 'foreman_organization'),
self.set('hostgroup_fact', N_("Hosts created after a puppet run will be placed in the host group this fact dictates. The content of this fact should be the full label of the host group."), 'foreman_hostgroup'),
self.set('default_location', N_("Hosts created after a puppet run that did not send a location fact will be placed in this location"), ''),
self.set('default_organization', N_("Hosts created after a puppet run that did not send a organization fact will be placed in this organization"), '')
self.set('default_organization', N_("Hosts created after a puppet run that did not send a organization fact will be placed in this organization"), ''),
self.set('default_hostgroup', N_("Hosts created after a puppet run that did not send a host group fact will be placed in this host group"), '')
].compact.each { |s| self.create s.update(:category => "Setting::Puppet")}

true
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/settings.yml
Expand Up @@ -250,3 +250,13 @@ attributes53:
category: Setting::General
default: "false"
description: "Send a welcome mail including initial username and password to new users"
attributes54:
name: default_hostgroup
category: Setting::Puppet
default: ""
description: "Hosts created after a puppet run that did not send a host group fact will be placed in this host group"
attributes55:
name: hostgroup_fact
category: Setting::Puppet
default: "foreman_hostgroup"
description: "Hosts created after a puppet run will be placed in the host group this fact dictates. The content of this fact should be the full label of the host group."
13 changes: 13 additions & 0 deletions test/unit/host_test.rb
Expand Up @@ -321,6 +321,19 @@ def teardown
assert_equal 'Organization 2', Host.find_by_name('sinn1636.lan').organization.title
end

test 'host group is set to setting[hostgroup_fact] if it exists' do
Setting[:create_new_host_when_facts_are_uploaded] = true
Setting[:hostgroup_fact] = "foreman_hostgroup"

hostgroup = FactoryGirl.create(:hostgroup)
raw = parse_json_fixture('/facts.json')
raw['facts']['foreman_hostgroup'] = hostgroup.title

Host.import_host_and_facts(raw['name'], raw['facts'])

assert_equal hostgroup, Host.find_by_name('sinn1636.lan').hostgroup
end

test 'default taxonomies are not assigned to hosts with taxonomies' do
Setting[:default_location] = taxonomies(:location1).title
raw = parse_json_fixture('/facts.json')
Expand Down