[Pkg-puppet-devel] [SCM] Puppet packaging for Debian branch, upstream, updated. 2.6.5-303-gfcfa26a

Nick Lewis nick at puppetlabs.com
Thu Mar 17 10:48:42 UTC 2011


The following commit has been merged in the upstream branch:
commit 8bd80a99a259e6409a9ac0a8a60325f94b5c5e9d
Author: Nick Lewis <nick at puppetlabs.com>
Date:   Thu Oct 28 12:49:19 2010 -0700

    (#5148) Add support for PSON to facts
    
    Previously, facts could be fetched via the REST API in PSON, but came
    back as the to_s representation of a Ruby object, rather than as
    proper PSON data. This patch adds to_pson and from_pson to facts, so
    they can be properly used with PSON.

diff --git a/lib/puppet/node/facts.rb b/lib/puppet/node/facts.rb
index 5626900..0a96e55 100755
--- a/lib/puppet/node/facts.rb
+++ b/lib/puppet/node/facts.rb
@@ -1,12 +1,17 @@
+require 'time'
+
 require 'puppet/node'
 require 'puppet/indirector'
 
+require 'puppet/util/pson'
+
 # Manage a given node's facts.  This either accepts facts and stores them, or
 # returns facts for a given node.
 class Puppet::Node::Facts
   # Set up indirection, so that nodes can be looked for in
   # the node sources.
   extend Puppet::Indirector
+  extend Puppet::Util::Pson
 
   # We want to expire any cached nodes if the facts are saved.
   module NodeExpirer
@@ -62,6 +67,22 @@ class Puppet::Node::Facts
     self.values[:_timestamp]
   end
 
+  def self.from_pson(data)
+    result = new(data['name'], data['values'])
+    result.timestamp = Time.parse(data['timestamp'])
+    result.expiration = Time.parse(data['expiration'])
+    result
+  end
+
+  def to_pson(*args)
+    {
+      'expiration' => expiration,
+      'name' => name,
+      'timestamp' => timestamp,
+      'values' => strip_internal,
+    }.to_pson(*args)
+  end
+
   private
 
   # Add internal data to the facts for storage.
diff --git a/spec/unit/node/facts_spec.rb b/spec/unit/node/facts_spec.rb
index 394db79..19049e9 100755
--- a/spec/unit/node/facts_spec.rb
+++ b/spec/unit/node/facts_spec.rb
@@ -109,5 +109,29 @@ describe Puppet::Node::Facts, "when indirecting" do
       facts = Puppet::Node::Facts.new("me", "one" => "two", "three" => "four")
       facts.values[:_timestamp].should be_instance_of(Time)
     end
+
+    describe "using pson" do
+      before :each do
+        @timestamp = Time.parse("Thu Oct 28 11:16:31 -0700 2010")
+        @expiration = Time.parse("Thu Oct 28 11:21:31 -0700 2010")
+      end
+
+      it "should accept properly formatted pson" do
+        pson = %Q({"name": "foo", "expiration": "#{@expiration}", "timestamp": "#{@timestamp}", "values": {"a": "1", "b": "2", "c": "3"}})
+        format = Puppet::Network::FormatHandler.format('pson')
+        facts = format.intern(Puppet::Node::Facts,pson)
+        facts.name.should == 'foo'
+        facts.expiration.should == @expiration
+        facts.values.should == {'a' => '1', 'b' => '2', 'c' => '3', :_timestamp => @timestamp}
+      end
+
+      it "should generate properly formatted pson" do
+        Time.stubs(:now).returns(@timestamp)
+        facts = Puppet::Node::Facts.new("foo", {'a' => 1, 'b' => 2, 'c' => 3})
+        facts.expiration = @expiration
+        pson = PSON.parse(facts.to_pson)
+        pson.should == {"name"=>"foo", "timestamp"=>"Thu Oct 28 11:16:31 -0700 2010", "expiration"=>"Thu Oct 28 11:21:31 -0700 2010", "values"=>{"a"=>1, "b"=>2, "c"=>3}}
+      end
+    end
   end
 end

-- 
Puppet packaging for Debian



More information about the Pkg-puppet-devel mailing list