diff options
author | Nick Lewis <nick@puppetlabs.com> | 2011-03-07 15:19:20 -0800 |
---|---|---|
committer | Nick Lewis <nick@puppetlabs.com> | 2011-03-08 15:23:50 -0800 |
commit | 8ce30c83ddba87ba7e2622a46f27143159132789 (patch) | |
tree | 668e6c6ec15a84d280e6c25b4b182809587f2036 /lib/puppet/indirector | |
parent | e8145f91debc863b341a270e1d8cff6c43d93ef5 (diff) | |
download | puppet-8ce30c83ddba87ba7e2622a46f27143159132789.tar.gz puppet-8ce30c83ddba87ba7e2622a46f27143159132789.tar.xz puppet-8ce30c83ddba87ba7e2622a46f27143159132789.zip |
(#6338) Add an InventoryActiveRecord terminus for Facts
So far this terminus only supports find and save. Search is forthcoming. It
uses two new tables (inventory_host and inventory_facts) so that it won't
interact with storedconfigs.
Paired-With: Jacob Helwig
Diffstat (limited to 'lib/puppet/indirector')
-rw-r--r-- | lib/puppet/indirector/facts/inventory_active_record.rb | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/lib/puppet/indirector/facts/inventory_active_record.rb b/lib/puppet/indirector/facts/inventory_active_record.rb new file mode 100644 index 000000000..6cd63ab1a --- /dev/null +++ b/lib/puppet/indirector/facts/inventory_active_record.rb @@ -0,0 +1,33 @@ +require 'puppet/rails/inventory_host' +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 + 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 + + ActiveRecord::Base.transaction do + Puppet::Rails::InventoryFact.delete_all(:inventory_host_id => host.id) + # We don't want to save internal values as facts, because those are + # metadata that belong on the host + facts.values.each do |name,value| + next if name.to_s =~ /^_/ + host.facts.build(:name => name, :value => value) + end + host.save + end + end +end |