Skip to content

Commit

Permalink
fixes #11425 - adding lookup_value_matcher to host and hostgroup to m…
Browse files Browse the repository at this point in the history
…ake db schema more logical
  • Loading branch information
unorthodoxgeek authored and dLobatog committed Aug 26, 2015
1 parent 56309d5 commit 9dd0053
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 7 deletions.
11 changes: 10 additions & 1 deletion app/models/concerns/host_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ module HostCommon

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)
Expand Down Expand Up @@ -194,6 +197,12 @@ def available_puppetclasses
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.
Expand Down
4 changes: 3 additions & 1 deletion app/models/hostgroup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,16 +196,18 @@ def root_pass
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
Expand Down
1 change: 0 additions & 1 deletion app/models/lookup_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class LookupValue < ActiveRecord::Base
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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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
5 changes: 5 additions & 0 deletions test/fixtures/hostgroups.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ common:
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
Expand All @@ -25,6 +27,7 @@ db:
medium: one
puppet_proxy: puppetmaster
title: db
lookup_value_matcher: hostgroup=db

parent:
name: Parent
Expand All @@ -39,9 +42,11 @@ 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
10 changes: 6 additions & 4 deletions test/unit/host_config_group_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ class HostConfigGroupTest < ActiveSupport::TestCase

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

Expand Down

0 comments on commit 9dd0053

Please sign in to comment.