Revision 9dd00534
Added by Tom Caspy over 8 years ago
app/models/concerns/host_common.rb | ||
---|---|---|
|
||
alias_method :all_puppetclasses, :classes
|
||
|
||
has_many :lookup_values, :finder_sql => Proc.new { LookupValue.where('lookup_values.match' => lookup_value_match).to_sql }, :dependent => :destroy, :validate => false
|
||
has_many :lookup_values, :primary_key => :lookup_value_matcher, :foreign_key => :match, :validate => false
|
||
# See "def lookup_values_attributes=" under, for the implementation of accepts_nested_attributes_for :lookup_values
|
||
accepts_nested_attributes_for :lookup_values
|
||
|
||
before_save :set_lookup_value_matcher
|
||
|
||
# Replacement of accepts_nested_attributes_for :lookup_values,
|
||
# to work around the lack of `host_id` column in lookup_values.
|
||
def lookup_values_attributes=(lookup_values_attributes)
|
||
... | ... | |
environment.puppetclasses - parent_classes
|
||
end
|
||
|
||
protected
|
||
|
||
def set_lookup_value_matcher
|
||
self.lookup_value_matcher = lookup_value_match
|
||
end
|
||
|
||
private
|
||
|
||
# fall back to our puppet proxy in case our puppet ca is not defined/used.
|
app/models/hostgroup.rb | ||
---|---|---|
def clone(name = "")
|
||
new = self.dup
|
||
new.name = name
|
||
new.title = name
|
||
new.puppetclasses = puppetclasses
|
||
new.locations = locations
|
||
new.organizations = organizations
|
||
new.config_groups = config_groups
|
||
new.set_lookup_value_matcher
|
||
|
||
# Clone any parameters as well
|
||
self.group_parameters.each{|param| new.group_parameters << param.clone}
|
||
self.lookup_values.each do |lookup_value|
|
||
new_lookup_value = lookup_value.dup
|
||
new_lookup_value.match = "hostgroup=#{new.title}"
|
||
new_lookup_value.match = new.lookup_value_matcher
|
||
new.lookup_values << new_lookup_value
|
||
end
|
||
new
|
app/models/lookup_value.rb | ||
---|---|---|
validate :validate_value, :ensure_fqdn_exists, :ensure_hostgroup_exists
|
||
|
||
attr_accessor :host_or_hostgroup
|
||
attr_writer :managed_id, :hostgroup_id
|
||
|
||
serialize :value
|
||
attr_name :match
|
db/migrate/20150819105725_add_lookup_value_match_to_host_and_hostgroup.rb | ||
---|---|---|
class AddLookupValueMatchToHostAndHostgroup < ActiveRecord::Migration
|
||
def up
|
||
add_column :hosts, :lookup_value_matcher, :string
|
||
add_column :hostgroups, :lookup_value_matcher, :string
|
||
|
||
Host::Managed.where(:type => 'Host::Managed').find_in_batches(:batch_size => 100) do |group|
|
||
group.each do |host|
|
||
host.update_attribute(:lookup_value_matcher, host.send(:lookup_value_match))
|
||
end
|
||
end
|
||
|
||
Hostgroup.find_in_batches(:batch_size => 100) do |group|
|
||
group.each do |hostgroup|
|
||
hostgroup.update_attribute(:lookup_value_matcher, hostgroup.send(:lookup_value_match))
|
||
end
|
||
end
|
||
end
|
||
|
||
def down
|
||
remove_column :hosts, :lookup_value_matcher
|
||
remove_column :hostgroups, :lookup_value_matcher
|
||
end
|
||
end
|
test/fixtures/hostgroups.yml | ||
---|---|---|
title: Common
|
||
root_pass: $1$foreman$NW1XVtbk4/XkJqmKNrFWV0
|
||
compute_profile: one
|
||
lookup_value_matcher: hostgroup=Common
|
||
|
||
unusual:
|
||
name: Unusual
|
||
puppet_proxy: puppetmaster
|
||
title: Unusual
|
||
lookup_value_matcher: hostgroup=Unusual
|
||
|
||
db:
|
||
name: db
|
||
... | ... | |
medium: one
|
||
puppet_proxy: puppetmaster
|
||
title: db
|
||
lookup_value_matcher: hostgroup=db
|
||
|
||
parent:
|
||
name: Parent
|
||
... | ... | |
subnet: three
|
||
compute_profile: one
|
||
id: 1
|
||
lookup_value_matcher: hostgroup=Parent
|
||
|
||
inherited:
|
||
name: inherited
|
||
environment: production
|
||
title: Parent/inherited
|
||
ancestry: 1
|
||
lookup_value_matcher: hostgroup=inherited
|
test/unit/host_config_group_test.rb | ||
---|---|---|
|
||
context "host and hostgroup both have id=1" do
|
||
setup do
|
||
@host = FactoryGirl.create(:host)
|
||
@hostgroup = FactoryGirl.create(:hostgroup)
|
||
@host.update_attribute(:id, 1)
|
||
@hostgroup.update_attribute(:id, 1)
|
||
@host = Host.where(:id => 1).first
|
||
@host ||= FactoryGirl.create(:host, :id => 1)
|
||
|
||
@hostgroup = Hostgroup.where(:id => 1).first
|
||
@hostgroup ||= FactoryGirl.create(:hostgroup, :id => 1)
|
||
|
||
@config_group = FactoryGirl.create(:config_group)
|
||
end
|
||
|
Also available in: Unified diff
fixes #11425 - adding lookup_value_matcher to host and hostgroup to make db schema more logical