diff options
-rw-r--r-- | lib/puppet/property.rb | 93 | ||||
-rw-r--r-- | lib/puppet/property/ensure.rb | 95 | ||||
-rw-r--r-- | spec/unit/property/ensure.rb | 13 |
3 files changed, 109 insertions, 92 deletions
diff --git a/lib/puppet/property.rb b/lib/puppet/property.rb index 7d5edcfc1..753c0d7c6 100644 --- a/lib/puppet/property.rb +++ b/lib/puppet/property.rb @@ -5,6 +5,7 @@ require 'puppet' require 'puppet/parameter' class Puppet::Property < Puppet::Parameter + require 'puppet/property/ensure' # Because 'should' uses an array, we have a special method for handling # it. We also want to keep copies of the original values, so that @@ -392,96 +393,4 @@ class Puppet::Property < Puppet::Parameter self.should = value end - # This property will get automatically added to any type that responds - # to the methods 'exists?', 'create', and 'destroy'. - class Ensure < Puppet::Property - @name = :ensure - - def self.defaultvalues - newvalue(:present) do - if @resource.provider and @resource.provider.respond_to?(:create) - @resource.provider.create - else - @resource.create - end - nil # return nil so the event is autogenerated - end - - newvalue(:absent) do - if @resource.provider and @resource.provider.respond_to?(:destroy) - @resource.provider.destroy - else - @resource.destroy - end - nil # return nil so the event is autogenerated - end - - defaultto do - if @resource.managed? - :present - else - nil - end - end - - # This doc will probably get overridden - @doc ||= "The basic property that the resource should be in." - end - - def self.inherited(sub) - # Add in the two properties that everyone will have. - sub.class_eval do - end - end - - def change_to_s(currentvalue, newvalue) - begin - if currentvalue == :absent or currentvalue.nil? - return "created" - elsif newvalue == :absent - return "removed" - else - return "%s changed '%s' to '%s'" % - [self.name, self.is_to_s(currentvalue), self.should_to_s(newvalue)] - end - rescue Puppet::Error, Puppet::DevError - raise - rescue => detail - raise Puppet::DevError, "Could not convert change %s to string: %s" % - [self.name, detail] - end - end - - def retrieve - # XXX This is a problem -- whether the object exists or not often - # depends on the results of other properties, yet we're the first property - # to get checked, which means that those other properties do not have - # @is values set. This seems to be the source of quite a few bugs, - # although they're mostly logging bugs, not functional ones. - if prov = @resource.provider and prov.respond_to?(:exists?) - result = prov.exists? - elsif @resource.respond_to?(:exists?) - result = @resource.exists? - else - raise Puppet::DevError, "No ability to determine if %s exists" % - @resource.class.name - end - if result - return :present - else - return :absent - end - end - - # If they're talking about the thing at all, they generally want to - # say it should exist. - #defaultto :present - defaultto do - if @resource.managed? - :present - else - nil - end - end - end end diff --git a/lib/puppet/property/ensure.rb b/lib/puppet/property/ensure.rb new file mode 100644 index 000000000..908de8b1b --- /dev/null +++ b/lib/puppet/property/ensure.rb @@ -0,0 +1,95 @@ +require 'puppet/property' + +# This property will get automatically added to any type that responds +# to the methods 'exists?', 'create', and 'destroy'. +class Puppet::Property::Ensure < Puppet::Property + @name = :ensure + + def self.defaultvalues + newvalue(:present) do + if @resource.provider and @resource.provider.respond_to?(:create) + @resource.provider.create + else + @resource.create + end + nil # return nil so the event is autogenerated + end + + newvalue(:absent) do + if @resource.provider and @resource.provider.respond_to?(:destroy) + @resource.provider.destroy + else + @resource.destroy + end + nil # return nil so the event is autogenerated + end + + defaultto do + if @resource.managed? + :present + else + nil + end + end + + # This doc will probably get overridden + @doc ||= "The basic property that the resource should be in." + end + + def self.inherited(sub) + # Add in the two properties that everyone will have. + sub.class_eval do + end + end + + def change_to_s(currentvalue, newvalue) + begin + if currentvalue == :absent or currentvalue.nil? + return "created" + elsif newvalue == :absent + return "removed" + else + return "%s changed '%s' to '%s'" % + [self.name, self.is_to_s(currentvalue), self.should_to_s(newvalue)] + end + rescue Puppet::Error, Puppet::DevError + raise + rescue => detail + raise Puppet::DevError, "Could not convert change %s to string: %s" % + [self.name, detail] + end + end + + def retrieve + # XXX This is a problem -- whether the object exists or not often + # depends on the results of other properties, yet we're the first property + # to get checked, which means that those other properties do not have + # @is values set. This seems to be the source of quite a few bugs, + # although they're mostly logging bugs, not functional ones. + if prov = @resource.provider and prov.respond_to?(:exists?) + result = prov.exists? + elsif @resource.respond_to?(:exists?) + result = @resource.exists? + else + raise Puppet::DevError, "No ability to determine if %s exists" % + @resource.class.name + end + if result + return :present + else + return :absent + end + end + + # If they're talking about the thing at all, they generally want to + # say it should exist. + #defaultto :present + defaultto do + if @resource.managed? + :present + else + nil + end + end +end + diff --git a/spec/unit/property/ensure.rb b/spec/unit/property/ensure.rb new file mode 100644 index 000000000..aa31abbdc --- /dev/null +++ b/spec/unit/property/ensure.rb @@ -0,0 +1,13 @@ +#!/usr/bin/env ruby + +Dir.chdir(File.dirname(__FILE__)) { (s = lambda { |f| File.exist?(f) ? require(f) : Dir.chdir("..") { s.call(f) } }).call("spec/spec_helper.rb") } + +require 'puppet/property/ensure' + +klass = Puppet::Property::Ensure + +describe klass do + it "should be a subclass of Property" do + klass.superclass.must == Puppet::Property + end +end |