diff options
-rw-r--r-- | lib/puppet/resource.rb | 57 | ||||
-rw-r--r-- | lib/puppet/resource_reference.rb | 12 | ||||
-rwxr-xr-x | spec/unit/resource.rb | 83 | ||||
-rwxr-xr-x | spec/unit/resource_reference.rb | 8 |
4 files changed, 160 insertions, 0 deletions
diff --git a/lib/puppet/resource.rb b/lib/puppet/resource.rb index 8dc09e521..9bbec2a4c 100644 --- a/lib/puppet/resource.rb +++ b/lib/puppet/resource.rb @@ -98,8 +98,65 @@ class Puppet::Resource end end + # Translate our object to a backward-compatible transportable object. + def to_trans + if @reference.builtin_type? + result = to_transobject + else + result = to_transbucket + end + + result.file = self.file + result.line = self.line + + return result + end + private + # Create an old-style TransBucket instance, for non-builtin resource types. + def to_transbucket + bucket = Puppet::TransBucket.new([]) + + bucket.type = self.type + bucket.name = self.title + + # TransBuckets don't support parameters, which is why they're being deprecated. + return bucket + end + + # Create an old-style TransObject instance, for builtin resource types. + def to_transobject + # Now convert to a transobject + result = Puppet::TransObject.new(@reference.title, @reference.type) + to_hash.each do |p, v| + if v.is_a?(Puppet::ResourceReference) + v = v.to_trans_ref + elsif v.is_a?(Array) + v = v.collect { |av| + if av.is_a?(Puppet::ResourceReference) + av = av.to_trans_ref + end + av + } + end + + # If the value is an array with only one value, then + # convert it to a single value. This is largely so that + # the database interaction doesn't have to worry about + # whether it returns an array or a string. + result[p.to_s] = if v.is_a?(Array) and v.length == 1 + v[0] + else + v + end + end + + result.tags = self.tags + + return result + end + # Produce a canonical method name. def parameter_name(param) param.to_s.downcase.to_sym diff --git a/lib/puppet/resource_reference.rb b/lib/puppet/resource_reference.rb index a3e0a7514..43479ff0c 100644 --- a/lib/puppet/resource_reference.rb +++ b/lib/puppet/resource_reference.rb @@ -51,6 +51,18 @@ class Puppet::ResourceReference end end + # Convert to the reference format that TransObject uses. Yay backward + # compatibility. + def to_trans_ref + # We have to return different cases to provide backward compatibility + # from 0.24.x to 0.23.x. + if builtin_type? + return [type.to_s.downcase, title.to_s] + else + return [type.to_s, title.to_s] + end + end + # Convert to the standard way of referring to resources. def to_s "%s[%s]" % [@type, @title] diff --git a/spec/unit/resource.rb b/spec/unit/resource.rb index 9c936f0bb..b21a6fccb 100755 --- a/spec/unit/resource.rb +++ b/spec/unit/resource.rb @@ -193,4 +193,87 @@ describe Puppet::Resource do @resource.to_manifest.should be_include(" foo => ['one','two']") end end + + it "should be able to convert itself to a TransObject instance" do + Puppet::Resource.new("one::two", "/my/file").should respond_to(:to_trans) + end + + describe "when converting to a TransObject" do + describe "and the resource is not an instance of a builtin type" do + before do + @resource = Puppet::Resource.new("foo", "bar") + end + + it "should return a simple TransBucket if it is not an instance of a builtin type" do + bucket = @resource.to_trans + bucket.should be_instance_of(Puppet::TransBucket) + bucket.type.should == @resource.type + bucket.name.should == @resource.title + end + + it "should copy over the resource's file" do + @resource.file = "/foo/bar" + @resource.to_trans.file.should == "/foo/bar" + end + + it "should copy over the resource's line" do + @resource.line = 50 + @resource.to_trans.line.should == 50 + end + end + + describe "and the resource is an instance of a builtin type" do + before do + @resource = Puppet::Resource.new("file", "bar") + end + + it "should return a TransObject if it is an instance of a builtin resource type" do + trans = @resource.to_trans + trans.should be_instance_of(Puppet::TransObject) + trans.type.should == "file" + trans.name.should == @resource.title + end + + it "should copy over the resource's file" do + @resource.file = "/foo/bar" + @resource.to_trans.file.should == "/foo/bar" + end + + it "should copy over the resource's line" do + @resource.line = 50 + @resource.to_trans.line.should == 50 + end + + # Only TransObjects support tags, annoyingly + it "should copy over the resource's tags" do + @resource.tag "foo" + @resource.to_trans.tags.should == @resource.tags + end + + it "should copy the resource's parameters into the transobject and convert the parameter name to a string" do + @resource[:foo] = "bar" + @resource.to_trans["foo"].should == "bar" + end + + it "should be able to copy arrays of values" do + @resource[:foo] = %w{yay fee} + @resource.to_trans["foo"].should == %w{yay fee} + end + + it "should reduce single-value arrays to just a value" do + @resource[:foo] = %w{yay} + @resource.to_trans["foo"].should == "yay" + end + + it "should convert resource references into the backward-compatible form" do + @resource[:foo] = Puppet::ResourceReference.new(:file, "/f") + @resource.to_trans["foo"].should == %w{file /f} + end + + it "should convert resource references into the backward-compatible form even when within arrays" do + @resource[:foo] = ["a", Puppet::ResourceReference.new(:file, "/f")] + @resource.to_trans["foo"].should == ["a", %w{file /f}] + end + end + end end diff --git a/spec/unit/resource_reference.rb b/spec/unit/resource_reference.rb index a81a8a0d1..baa3890cb 100755 --- a/spec/unit/resource_reference.rb +++ b/spec/unit/resource_reference.rb @@ -54,6 +54,14 @@ describe Puppet::ResourceReference do it "should be not considered builtin if an existing resource type does not match the type" do Puppet::ResourceReference.new("foobar", "/f").should_not be_builtin_type end + + it "should be able to produce a backward-compatible reference array" do + Puppet::ResourceReference.new("foobar", "/f").to_trans_ref.should == %w{Foobar /f} + end + + it "should downcase resource types when producing a backward-compatible reference array for builtin resource types" do + Puppet::ResourceReference.new("file", "/f").to_trans_ref.should == %w{file /f} + end end describe Puppet::ResourceReference, "when resolving resources with a catalog" do |