Revision 3e5ee0a6
Added by Dominic Cleal about 10 years ago
app/models/host/managed.rb | ||
---|---|---|
end
|
||
end
|
||
|
||
self.clear_host_parameters_cache!
|
||
self.save
|
||
end
|
||
|
app/services/classification/base.rb | ||
---|---|---|
module Classification
|
||
class Base
|
||
delegate :hostgroup, :environment_id,
|
||
delegate :hostgroup, :environment_id, :puppetclass_ids, :classes,
|
||
:to => :host
|
||
|
||
def initialize args = { }
|
||
... | ... | |
raise NotImplementedError
|
||
end
|
||
|
||
def puppetclass_ids
|
||
return @puppetclass_ids if @puppetclass_ids
|
||
ids = host.host_classes.pluck(:puppetclass_id)
|
||
ids += HostgroupClass.where(:hostgroup_id => hostgroup.path_ids).pluck(:puppetclass_id) if hostgroup
|
||
@puppetclass_ids = EnvironmentClass.where(:environment_id => host.environment_id, :puppetclass_id => ids).pluck('DISTINCT puppetclass_id')
|
||
end
|
||
|
||
def classes
|
||
Puppetclass.where(:id => puppetclass_ids)
|
||
end
|
||
|
||
def possible_value_orders
|
||
class_parameters.select do |key|
|
||
# take only keys with actual values
|
test/factories/puppet_related.rb | ||
---|---|---|
trait :with_override do
|
||
override true
|
||
default_value "default value"
|
||
path "fqdn\ncomment"
|
||
path "comment"
|
||
overrides({"comment=override" => "overridden value"})
|
||
end
|
||
|
||
... | ... | |
end
|
||
|
||
trait :with_parameters do
|
||
ignore do
|
||
parameter_count 1
|
||
end
|
||
after_create do |pc,evaluator|
|
||
3.times do
|
||
evaluator.parameter_count.times do
|
||
evaluator.environments.each do |env|
|
||
lkey = FactoryGirl.create :lookup_key, :is_param => true
|
||
FactoryGirl.create :environment_class, :puppetclass_id => pc.id, :environment_id => env.id, :lookup_key_id => lkey.id
|
test/fixtures/settings.yml | ||
---|---|---|
category: Setting::Puppet
|
||
default: 'foreman_organization'
|
||
description: 'Fact to set organization from when importing hosts'
|
||
attributes48:
|
||
name: Enable_Smart_Variables_in_ENC
|
||
category: Setting::Puppet
|
||
default: "true"
|
||
description: "Foreman smart variables will be exposed via the ENC yaml output"
|
test/test_helper.rb | ||
---|---|---|
# This code will be run each time you run your specs.
|
||
class ActionController::TestCase
|
||
setup :setup_set_script_name, :set_api_user, :reset_setting_cache
|
||
teardown :reset_setting_cache
|
||
|
||
def reset_setting_cache
|
||
Setting.cache.clear
|
test/unit/classification_test.rb | ||
---|---|---|
key.reload
|
||
assert_equal value.value, enc['base']['cluster']
|
||
end
|
||
|
||
test "#classes is delegated to the host" do
|
||
pc = FactoryGirl.build(:puppetclass)
|
||
host = FactoryGirl.build(:host)
|
||
host.expects(:classes).returns([pc])
|
||
assert_equal [pc], Classification::ClassParam.new(:host => host).classes
|
||
end
|
||
|
||
test "#puppetclass_ids is delegated to the host" do
|
||
pc = FactoryGirl.build(:puppetclass)
|
||
host = FactoryGirl.build(:host)
|
||
host.expects(:puppetclass_ids).returns([pc.id])
|
||
assert_equal [pc.id], Classification::ClassParam.new(:host => host).puppetclass_ids
|
||
end
|
||
|
||
test "#enc should return hash of class to nil for classes without parameters" do
|
||
env = FactoryGirl.create(:environment)
|
||
pc = FactoryGirl.create(:puppetclass, :environments => [env])
|
||
assert_equal({pc.name => nil}, get_classparam(env, pc).enc)
|
||
end
|
||
|
||
test "#enc should not return class parameters where override is false" do
|
||
env = FactoryGirl.create(:environment)
|
||
pc = FactoryGirl.create(:puppetclass, :with_parameters, :environments => [env])
|
||
refute pc.class_params.first.override
|
||
assert_equal({pc.name => nil}, get_classparam(env, pc).enc)
|
||
end
|
||
|
||
test "#enc should return default value of class parameters without lookup_values" do
|
||
env = FactoryGirl.create(:environment)
|
||
pc = FactoryGirl.create(:puppetclass, :environments => [env])
|
||
lkey = FactoryGirl.create(:lookup_key, :as_smart_class_param, :puppetclass => pc, :override => true, :default_value => 'test')
|
||
assert_equal({pc.name => {lkey.key => lkey.default_value}}, get_classparam(env, pc).enc)
|
||
end
|
||
|
||
test "#enc should return override value of class parameters" do
|
||
env = FactoryGirl.create(:environment)
|
||
pc = FactoryGirl.create(:puppetclass, :environments => [env])
|
||
lkey = FactoryGirl.create(:lookup_key, :as_smart_class_param, :with_override, :puppetclass => pc)
|
||
classparam = get_classparam(env, pc)
|
||
classparam.expects(:attr_to_value).with('comment').returns('override')
|
||
assert_equal({pc.name => {lkey.key => 'overridden value'}}, classparam.enc)
|
||
end
|
||
|
||
private
|
||
|
||
attr_reader :classification
|
||
|
||
def get_classparam(env, classes)
|
||
classification = Classification::ClassParam.new
|
||
classification.expects(:classes).returns(Array.wrap(classes))
|
||
classification.expects(:environment_id).returns(env.id)
|
||
classification.expects(:puppetclass_ids).returns(Array.wrap(classes).map(&:id))
|
||
classification
|
||
end
|
||
|
||
end
|
test/unit/host_test.rb | ||
---|---|---|
"classes"=>["apache", "base"]}
|
||
|
||
host.importNode nodeinfo
|
||
nodeinfo["parameters"]["special_info"] = "secret" # smart variable on apache
|
||
|
||
assert_equal nodeinfo, host.info
|
||
end
|
||
... | ... | |
# four classes in config groups plus one manually added
|
||
assert_equal 5, all_classes.count
|
||
assert_equal ['base', 'chkmk', 'nagios', 'pam', 'auth'].sort, all_classes.map(&:name).sort
|
||
assert_equal all_classes, host.all_puppetclasses
|
||
end
|
||
|
||
test "search hostgroups by config group" do
|
||
... | ... | |
assert_equal (host.environment.puppetclasses - host.parent_classes).sort, host.available_puppetclasses.sort
|
||
end
|
||
|
||
test "#info ENC YAML uses all_puppetclasses for non-parameterized output" do
|
||
Setting[:Parametrized_Classes_in_ENC] = false
|
||
myclass = mock('myclass')
|
||
myclass.expects(:name).returns('myclass')
|
||
host = FactoryGirl.build(:host)
|
||
host.expects(:all_puppetclasses).returns([myclass])
|
||
enc = host.info
|
||
assert_kind_of Hash, enc
|
||
assert_equal ['myclass'], enc['classes']
|
||
end
|
||
|
||
test "#info ENC YAML uses Classification::ClassParam for parameterized output" do
|
||
Setting[:Parametrized_Classes_in_ENC] = true
|
||
Setting[:Enable_Smart_Variables_in_ENC] = true
|
||
host = FactoryGirl.build(:host)
|
||
classes = {'myclass' => {'myparam' => 'myvalue'}}
|
||
classification = mock('Classification::ClassParam')
|
||
classification.expects(:enc).returns(classes)
|
||
Classification::ClassParam.expects(:new).with(:host => host).returns(classification)
|
||
enc = host.info
|
||
assert_kind_of Hash, enc
|
||
assert_equal classes, enc['classes']
|
||
end
|
||
|
||
private
|
||
|
||
def parse_json_fixture(relative_path)
|
Also available in: Unified diff
fixes #5246 - remove second host/class lookup impl so config groups are included in ENC output
(cherry picked from commit 46b3d8f1f5dff70b961266232f0041beabea8e0d)