diff options
| author | Nick Lewis <nick@puppetlabs.com> | 2011-03-07 16:42:53 -0800 |
|---|---|---|
| committer | Nick Lewis <nick@puppetlabs.com> | 2011-03-08 15:42:27 -0800 |
| commit | f83636698229241b2ab35849437f3e515f6ac5c1 (patch) | |
| tree | 1973c4fe7178f3330151ba59c80ec63089d9f314 /lib/puppet | |
| parent | 8ce30c83ddba87ba7e2622a46f27143159132789 (diff) | |
| download | puppet-f83636698229241b2ab35849437f3e515f6ac5c1.tar.gz puppet-f83636698229241b2ab35849437f3e515f6ac5c1.tar.xz puppet-f83636698229241b2ab35849437f3e515f6ac5c1.zip | |
(#6338) Implement search for InventoryActiveRecord facts terminus
Paired-With: Max Martin
Reviewed-By: Jacob Helwig
Diffstat (limited to 'lib/puppet')
| -rw-r--r-- | lib/puppet/indirector/facts/inventory_active_record.rb | 34 | ||||
| -rw-r--r-- | lib/puppet/rails/inventory_host.rb | 26 |
2 files changed, 60 insertions, 0 deletions
diff --git a/lib/puppet/indirector/facts/inventory_active_record.rb b/lib/puppet/indirector/facts/inventory_active_record.rb index 6cd63ab1a..30cb88ea0 100644 --- a/lib/puppet/indirector/facts/inventory_active_record.rb +++ b/lib/puppet/indirector/facts/inventory_active_record.rb @@ -30,4 +30,38 @@ class Puppet::Node::Facts::InventoryActiveRecord < Puppet::Indirector::ActiveRec host.save end end + + def search(request) + return [] unless request.options + fact_names = [] + filters = Hash.new {|h,k| h[k] = []} + request.options.each do |key,value| + type, name, operator = key.to_s.split(".") + operator ||= "eq" + filters[operator] << [name,value] + end + + + host_sets = [] + filters['eq'].each do |name,value| + host_sets << Puppet::Rails::InventoryHost.has_fact_with_value(name,value).map {|host| host.name} + end + filters['ne'].each do |name,value| + host_sets << Puppet::Rails::InventoryHost.has_fact_without_value(name,value).map {|host| host.name} + end + { + 'gt' => '>', + 'lt' => '<', + 'ge' => '>=', + 'le' => '<=' + }.each do |operator_name,operator| + 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} + end + end + + # to_a because [].inject == nil + host_sets.inject {|hosts,this_set| hosts & this_set}.to_a + end end diff --git a/lib/puppet/rails/inventory_host.rb b/lib/puppet/rails/inventory_host.rb index 433e54389..10dd62083 100644 --- a/lib/puppet/rails/inventory_host.rb +++ b/lib/puppet/rails/inventory_host.rb @@ -3,6 +3,32 @@ require 'puppet/rails/inventory_fact' class Puppet::Rails::InventoryHost < ::ActiveRecord::Base has_many :facts, :class_name => "Puppet::Rails::InventoryFact", :dependent => :delete_all + named_scope :has_fact_with_value, lambda { |name,value| + { + :conditions => ["inventory_facts.name = ? AND inventory_facts.value = ?", name, value], + :joins => :facts + } + } + + named_scope :has_fact_without_value, lambda { |name,value| + { + :conditions => ["inventory_facts.name = ? AND inventory_facts.value != ?", name, value], + :joins => :facts + } + } + + named_scope :has_fact, lambda { |name| + { + :conditions => ["inventory_facts.name = ?", name], + :joins => :facts + } + } + + def value_for(fact_name) + fact = facts.find_by_name(fact_name) + fact ? fact.value : nil + end + def facts_to_hash facts.inject({}) do |fact_hash,fact| fact_hash.merge(fact.name => fact.value) |
