[Pkg-puppet-devel] [SCM] Puppet packaging for Debian branch, experimental, updated. debian/2.6.8-1-844-g7ec39d5

Luke Kanies luke at puppetlabs.com
Tue May 10 08:15:30 UTC 2011


The following commit has been merged in the experimental branch:
commit d3c94e62386ec03617015f6e6269b1de805954ea
Author: Luke Kanies <luke at puppetlabs.com>
Date:   Tue Apr 12 21:41:06 2011 -0700

    Adding json support to Puppet::Node
    
    Reviewed-by: Daniel Pittman <daniel at puppetlabs.com>
    Signed-off-by: Luke Kanies <luke at puppetlabs.com>

diff --git a/lib/puppet/node.rb b/lib/puppet/node.rb
index 5b0a986..4bd4d1d 100644
--- a/lib/puppet/node.rb
+++ b/lib/puppet/node.rb
@@ -20,6 +20,29 @@ class Puppet::Node
   attr_accessor :name, :classes, :source, :ipaddress, :parameters
   attr_reader :time
 
+  def self.from_pson(pson)
+    raise ArgumentError, "No name provided in pson data" unless name = pson['name']
+
+    node = new(name)
+    node.classes = pson['classes']
+    node.parameters = pson['parameters']
+    node.environment = pson['environment']
+    node
+  end
+
+  def to_pson(*args)
+    result = {
+      'document_type' => "Puppet::Node",
+      'data' => {}
+    }
+    result['data']['name'] = name
+    result['data']['classes'] = classes unless classes.empty?
+    result['data']['parameters'] = parameters unless parameters.empty?
+    result['data']['environment'] = environment.name
+
+    result.to_pson(*args)
+  end
+
   def environment
     return super if @environment
 
diff --git a/spec/unit/node_spec.rb b/spec/unit/node_spec.rb
index 7d813ba..af3110f 100755
--- a/spec/unit/node_spec.rb
+++ b/spec/unit/node_spec.rb
@@ -35,6 +35,69 @@ describe Puppet::Node do
       node.environment.name.should == :bar
     end
   end
+
+  describe "when converting to json" do
+    before do
+      @node = Puppet::Node.new("mynode")
+    end
+
+    it "should provide its name" do
+      PSON.parse(@node.to_pson)['data']['name'].should == "mynode"
+    end
+
+    it "should include the classes if set" do
+      @node.classes = %w{a b c}
+      PSON.parse(@node.to_pson)['data']['classes'].should == %w{a b c}
+    end
+
+    it "should not include the classes if there are none" do
+      PSON.parse(@node.to_pson)['data'].should_not be_include('classes')
+    end
+
+    it "should include parameters if set" do
+      @node.parameters = {"a" => "b", "c" => "d"}
+      PSON.parse(@node.to_pson)['data']['parameters'].should == {"a" => "b", "c" => "d"}
+    end
+
+    it "should not include the parameters if there are none" do
+      PSON.parse(@node.to_pson)['data'].should_not be_include('parameters')
+    end
+
+    it "should include the environment" do
+      @node.environment = "production"
+      PSON.parse(@node.to_pson)['data']['environment'].should == "production"
+    end
+  end
+
+  describe "when converting from json" do
+    before do
+      @node = Puppet::Node.new("mynode")
+      @format = Puppet::Network::FormatHandler.format('pson')
+    end
+
+    def from_json(json)
+      @format.intern(Puppet::Node, json)
+    end
+
+    it "should set its name" do
+      from_json(@node.to_pson).name.should == "mynode"
+    end
+
+    it "should include the classes if set" do
+      @node.classes = %w{a b c}
+      from_json(@node.to_pson).classes.should == %w{a b c}
+    end
+
+    it "should include parameters if set" do
+      @node.parameters = {"a" => "b", "c" => "d"}
+      from_json(@node.to_pson).parameters.should == {"a" => "b", "c" => "d"}
+    end
+
+    it "should include the environment" do
+      @node.environment = "production"
+      from_json(@node.to_pson).environment.name.should == :production
+    end
+  end
 end
 
 describe Puppet::Node, "when initializing" do

-- 
Puppet packaging for Debian



More information about the Pkg-puppet-devel mailing list