diff --git a/lib/default_hostgroup_managed_host_patch.rb b/lib/default_hostgroup_managed_host_patch.rb index bed675f..1ffde9d 100644 --- a/lib/default_hostgroup_managed_host_patch.rb +++ b/lib/default_hostgroup_managed_host_patch.rb @@ -13,6 +13,12 @@ module ClassMethods def importHostAndFacts_with_apply_hostgroup hostname, facts, certname = nil host, result = importHostAndFacts_without_apply_hostgroup(hostname, facts, certname) Rails.logger.debug "DefaultHostgroup: performing Hostgroup check" + + unless valid_hostgroup? + Rails.logger.debug "DefaultHostgroup: Could not find Hostgroup '#{Setting[:default_hostgroup]}'" + return host, result + end + # host.new_record? will only test for the early return in the core method, a real host # will have already been saved at least once. if host.present? && !host.new_record? && host.hostgroup.nil? && host.reports.empty? @@ -22,5 +28,9 @@ def importHostAndFacts_with_apply_hostgroup hostname, facts, certname = nil end return host, result end + + def valid_hostgroup? + Hostgroup.find_by_label(Setting[:default_hostgroup]) ? true : false + end end end diff --git a/test/unit/default_hostgroup_test.rb b/test/unit/default_hostgroup_test.rb index 1fdc959..8e9cc84 100644 --- a/test/unit/default_hostgroup_test.rb +++ b/test/unit/default_hostgroup_test.rb @@ -26,4 +26,12 @@ def setup_hostgroup assert_equal @hostgroup, Host.find_by_name('sinn1636.lan').hostgroup end + test "an invalid hostgroup setting does nothing" do + setup_hostgroup + Setting[:default_hostgroup] = "doesnotexist" + raw = parse_json_fixture('/facts.json') + assert Host.importHostAndFacts(raw['name'], raw['facts']) + refute Host.find_by_name('sinn1636.lan').hostgroup + end + end