diff options
author | Nick Lewis <nick@puppetlabs.com> | 2011-03-09 13:43:57 -0800 |
---|---|---|
committer | Nick Lewis <nick@puppetlabs.com> | 2011-03-09 13:43:57 -0800 |
commit | 40e8b48eb19973ec413518be656249e134344ee0 (patch) | |
tree | 8f6a10924b74ff2b52efe992feb4be420b7ae2f7 | |
parent | eae17c3cba159d895e5ec2e87e4abe8c125a9562 (diff) | |
parent | 531e25836e1313cd508ab8394e16cf438a62ac7b (diff) | |
download | puppet-40e8b48eb19973ec413518be656249e134344ee0.tar.gz puppet-40e8b48eb19973ec413518be656249e134344ee0.tar.xz puppet-40e8b48eb19973ec413518be656249e134344ee0.zip |
Merge branch 'maint/2.6.next/6338' into 2.6.next
-rw-r--r-- | lib/puppet/indirector/facts/inventory_active_record.rb | 51 | ||||
-rw-r--r-- | lib/puppet/rails/database/004_add_inventory_service_tables.rb | 18 | ||||
-rw-r--r-- | lib/puppet/rails/database/schema.rb | 8 | ||||
-rw-r--r-- | lib/puppet/rails/inventory_fact.rb | 5 | ||||
-rw-r--r-- | lib/puppet/rails/inventory_node.rb (renamed from lib/puppet/rails/inventory_host.rb) | 2 | ||||
-rw-r--r-- | spec/unit/indirector/facts/inventory_active_record_spec.rb | 34 |
6 files changed, 54 insertions, 64 deletions
diff --git a/lib/puppet/indirector/facts/inventory_active_record.rb b/lib/puppet/indirector/facts/inventory_active_record.rb index 5b8606839..89edaf332 100644 --- a/lib/puppet/indirector/facts/inventory_active_record.rb +++ b/lib/puppet/indirector/facts/inventory_active_record.rb @@ -1,33 +1,30 @@ -require 'puppet/rails/inventory_host' +require 'puppet/rails/inventory_node' require 'puppet/rails/inventory_fact' require 'puppet/indirector/active_record' class Puppet::Node::Facts::InventoryActiveRecord < Puppet::Indirector::ActiveRecord def find(request) - host = Puppet::Rails::InventoryHost.find_by_name(request.key) - return nil unless host - facts = Puppet::Node::Facts.new(host.name, host.facts_to_hash) - facts.timestamp = host.timestamp - facts.values.each do |key,value| - facts.values[key] = value.first if value.is_a?(Array) && value.length == 1 - end + node = Puppet::Rails::InventoryNode.find_by_name(request.key) + return nil unless node + facts = Puppet::Node::Facts.new(node.name, node.facts_to_hash) + facts.timestamp = node.timestamp facts end def save(request) facts = request.instance - host = Puppet::Rails::InventoryHost.find_by_name(request.key) || Puppet::Rails::InventoryHost.create(:name => request.key, :timestamp => facts.timestamp) - host.timestamp = facts.timestamp + node = Puppet::Rails::InventoryNode.find_by_name(request.key) || Puppet::Rails::InventoryNode.create(:name => request.key, :timestamp => facts.timestamp) + node.timestamp = facts.timestamp ActiveRecord::Base.transaction do - Puppet::Rails::InventoryFact.delete_all(:inventory_host_id => host.id) + Puppet::Rails::InventoryFact.delete_all(:inventory_node_id => node.id) # We don't want to save internal values as facts, because those are - # metadata that belong on the host + # metadata that belong on the node facts.values.each do |name,value| next if name.to_s =~ /^_/ - host.facts.build(:name => name, :value => value) + node.facts.build(:name => name, :value => value) end - host.save + node.save end end @@ -46,21 +43,21 @@ class Puppet::Node::Facts::InventoryActiveRecord < Puppet::Indirector::ActiveRec end end - matching_hosts = hosts_matching_fact_filters(fact_filters) + hosts_matching_meta_filters(meta_filters) + matching_nodes = nodes_matching_fact_filters(fact_filters) + nodes_matching_meta_filters(meta_filters) # to_a because [].inject == nil - matching_hosts.inject {|hosts,this_set| hosts & this_set}.to_a.sort + matching_nodes.inject {|nodes,this_set| nodes & this_set}.to_a.sort end private - def hosts_matching_fact_filters(fact_filters) - host_sets = [] + def nodes_matching_fact_filters(fact_filters) + node_sets = [] fact_filters['eq'].each do |name,value| - host_sets << Puppet::Rails::InventoryHost.has_fact_with_value(name,value).map {|host| host.name} + node_sets << Puppet::Rails::InventoryNode.has_fact_with_value(name,value).map {|node| node.name} end fact_filters['ne'].each do |name,value| - host_sets << Puppet::Rails::InventoryHost.has_fact_without_value(name,value).map {|host| host.name} + node_sets << Puppet::Rails::InventoryNode.has_fact_without_value(name,value).map {|node| node.name} end { 'gt' => '>', @@ -69,15 +66,15 @@ class Puppet::Node::Facts::InventoryActiveRecord < Puppet::Indirector::ActiveRec 'le' => '<=' }.each do |operator_name,operator| fact_filters[operator_name].each do |name,value| - hosts_with_fact = Puppet::Rails::InventoryHost.has_fact(name) - host_sets << hosts_with_fact.select {|h| h.value_for(name).to_f.send(operator, value.to_f)}.map {|host| host.name} + nodes_with_fact = Puppet::Rails::InventoryNode.has_fact(name) + node_sets << nodes_with_fact.select {|h| h.value_for(name).to_f.send(operator, value.to_f)}.map {|node| node.name} end end - host_sets + node_sets end - def hosts_matching_meta_filters(meta_filters) - host_sets = [] + def nodes_matching_meta_filters(meta_filters) + node_sets = [] { 'eq' => '=', 'ne' => '!=', @@ -87,9 +84,9 @@ class Puppet::Node::Facts::InventoryActiveRecord < Puppet::Indirector::ActiveRec 'le' => '<=' }.each do |operator_name,operator| meta_filters[operator_name].each do |name,value| - host_sets << Puppet::Rails::InventoryHost.find(:all, :conditions => ["timestamp #{operator} ?", value]).map {|host| host.name} + node_sets << Puppet::Rails::InventoryNode.find(:all, :conditions => ["timestamp #{operator} ?", value]).map {|node| node.name} end end - host_sets + node_sets end end diff --git a/lib/puppet/rails/database/004_add_inventory_service_tables.rb b/lib/puppet/rails/database/004_add_inventory_service_tables.rb index 22298437a..a819cac1a 100644 --- a/lib/puppet/rails/database/004_add_inventory_service_tables.rb +++ b/lib/puppet/rails/database/004_add_inventory_service_tables.rb @@ -1,35 +1,35 @@ class AddInventoryServiceTables < ActiveRecord::Migration def self.up - unless ActiveRecord::Base.connection.tables.include?("inventory_hosts") - create_table :inventory_hosts do |t| + unless ActiveRecord::Base.connection.tables.include?("inventory_nodes") + create_table :inventory_nodes do |t| t.column :name, :string, :null => false t.column :timestamp, :datetime, :null => false t.column :updated_at, :datetime t.column :created_at, :datetime end - add_index :inventory_hosts, :name, :unique => true + add_index :inventory_nodes, :name, :unique => true end unless ActiveRecord::Base.connection.tables.include?("inventory_facts") create_table :inventory_facts, :id => false do |t| - t.column :inventory_host_id, :integer, :null => false + t.column :inventory_node_id, :integer, :null => false t.column :name, :string, :null => false t.column :value, :text, :null => false end - add_index :inventory_facts, [:inventory_host_id, :name], :unique => true + add_index :inventory_facts, [:inventory_node_id, :name], :unique => true end end def self.down - unless ActiveRecord::Base.connection.tables.include?("inventory_hosts") - remove_index :inventory_hosts, :name - drop_table :inventory_hosts + unless ActiveRecord::Base.connection.tables.include?("inventory_nodes") + remove_index :inventory_nodes, :name + drop_table :inventory_nodes end if ActiveRecord::Base.connection.tables.include?("inventory_facts") - remove_index :inventory_facts, [:inventory_host_id, :name] + remove_index :inventory_facts, [:inventory_node_id, :name] drop_table :inventory_facts end end diff --git a/lib/puppet/rails/database/schema.rb b/lib/puppet/rails/database/schema.rb index 5e455d6c0..9fd640fe4 100644 --- a/lib/puppet/rails/database/schema.rb +++ b/lib/puppet/rails/database/schema.rb @@ -104,22 +104,22 @@ class Puppet::Rails::Schema end add_index :param_names, :name - create_table :inventory_hosts do |t| + create_table :inventory_nodes do |t| t.column :name, :string, :null => false t.column :timestamp, :datetime, :null => false t.column :updated_at, :datetime t.column :created_at, :datetime end - add_index :inventory_hosts, :name, :unique => true + add_index :inventory_nodes, :name, :unique => true create_table :inventory_facts, :id => false do |t| - t.column :inventory_host_id, :integer, :null => false + t.column :inventory_node_id, :integer, :null => false t.column :name, :string, :null => false t.column :value, :text, :null => false end - add_index :inventory_facts, [:inventory_host_id, :name], :unique => true + add_index :inventory_facts, [:inventory_node_id, :name], :unique => true end end ensure diff --git a/lib/puppet/rails/inventory_fact.rb b/lib/puppet/rails/inventory_fact.rb index ecb6e41ee..aa6334eef 100644 --- a/lib/puppet/rails/inventory_fact.rb +++ b/lib/puppet/rails/inventory_fact.rb @@ -1,6 +1,5 @@ -require 'puppet/rails/inventory_host' +require 'puppet/rails/inventory_node' class Puppet::Rails::InventoryFact < ::ActiveRecord::Base - belongs_to :host, :class_name => "Puppet::Rails::InventoryHost" - serialize :value + belongs_to :node, :class_name => "Puppet::Rails::InventoryNode" end diff --git a/lib/puppet/rails/inventory_host.rb b/lib/puppet/rails/inventory_node.rb index 10dd62083..b3e321f94 100644 --- a/lib/puppet/rails/inventory_host.rb +++ b/lib/puppet/rails/inventory_node.rb @@ -1,6 +1,6 @@ require 'puppet/rails/inventory_fact' -class Puppet::Rails::InventoryHost < ::ActiveRecord::Base +class Puppet::Rails::InventoryNode < ::ActiveRecord::Base has_many :facts, :class_name => "Puppet::Rails::InventoryFact", :dependent => :delete_all named_scope :has_fact_with_value, lambda { |name,value| diff --git a/spec/unit/indirector/facts/inventory_active_record_spec.rb b/spec/unit/indirector/facts/inventory_active_record_spec.rb index 69d614023..c29e58400 100644 --- a/spec/unit/indirector/facts/inventory_active_record_spec.rb +++ b/spec/unit/indirector/facts/inventory_active_record_spec.rb @@ -32,23 +32,23 @@ describe "Puppet::Node::Facts::InventoryActiveRecord", :if => (Puppet.features.r end describe "#save" do - it "should use an existing host if possible" do - host = Puppet::Rails::InventoryHost.new(:name => "foo", :timestamp => Time.now) - host.save + it "should use an existing node if possible" do + node = Puppet::Rails::InventoryNode.new(:name => "foo", :timestamp => Time.now) + node.save Puppet::Node::Facts.new("foo", "uptime_days" => "60", "kernel" => "Darwin").save - Puppet::Rails::InventoryHost.count.should == 1 - Puppet::Rails::InventoryHost.first.should == host + Puppet::Rails::InventoryNode.count.should == 1 + Puppet::Rails::InventoryNode.first.should == node end - it "should create a new host if one can't be found" do - # This test isn't valid if there are hosts to begin with - Puppet::Rails::InventoryHost.count.should == 0 + it "should create a new node if one can't be found" do + # This test isn't valid if there are nodes to begin with + Puppet::Rails::InventoryNode.count.should == 0 Puppet::Node::Facts.new("foo", "uptime_days" => "60", "kernel" => "Darwin").save - Puppet::Rails::InventoryHost.count.should == 1 - Puppet::Rails::InventoryHost.first.name.should == "foo" + Puppet::Rails::InventoryNode.count.should == 1 + Puppet::Rails::InventoryNode.first.name.should == "foo" end it "should save the facts" do @@ -57,7 +57,7 @@ describe "Puppet::Node::Facts::InventoryActiveRecord", :if => (Puppet.features.r Puppet::Rails::InventoryFact.all.map{|f| [f.name,f.value]}.should =~ [["uptime_days","60"],["kernel","Darwin"]] end - it "should remove the previous facts for an existing host" do + it "should remove the previous facts for an existing node" do Puppet::Node::Facts.new("foo", "uptime_days" => "30", "kernel" => "Darwin").save bar_facts = Puppet::Node::Facts.new("bar", "uptime_days" => "35", "kernel" => "Linux") foo_facts = Puppet::Node::Facts.new("foo", "uptime_days" => "60", "is_virtual" => "false") @@ -81,18 +81,12 @@ describe "Puppet::Node::Facts::InventoryActiveRecord", :if => (Puppet.features.r @bar_facts.save end - it "should identify facts by host name" do + it "should identify facts by node name" do Puppet::Node::Facts.find("foo").should == @foo_facts end - it "should return nil if no host instance can be found" do - Puppet::Node::Facts.find("non-existent host").should == nil - end - - it "should convert all single-member arrays into non-arrays" do - Puppet::Node::Facts.new("array", "fact1" => ["value1"]).save - - Puppet::Node::Facts.find("array").values["fact1"].should == "value1" + it "should return nil if no node instance can be found" do + Puppet::Node::Facts.find("non-existent node").should == nil end end |