diff options
author | Matt Robinson <matt@puppetlabs.com> | 2011-03-16 11:19:10 -0700 |
---|---|---|
committer | Matt Robinson <matt@puppetlabs.com> | 2011-03-16 11:19:10 -0700 |
commit | de8fea8ac8b25f45ffb07a2ef63a6da0cbaf0c41 (patch) | |
tree | f520b1400b4d35e63a0b1977b508d51d9422a88a /lib/puppet/resource.rb | |
parent | 86c60354da1d5a2a54baf6dbd92677a12701423d (diff) | |
parent | c60c6cacaca6b8e34c29835f9e5749fc380b8e0b (diff) | |
download | puppet-de8fea8ac8b25f45ffb07a2ef63a6da0cbaf0c41.tar.gz puppet-de8fea8ac8b25f45ffb07a2ef63a6da0cbaf0c41.tar.xz puppet-de8fea8ac8b25f45ffb07a2ef63a6da0cbaf0c41.zip |
Merge branch '2.6.next' into 2.6.x
* 2.6.next: (102 commits)
(#5073) Download plugins even if you're filtering on tags
Fix #5610: Prevent unnecessary RAL lookups
Revert "Merge branch 'ticket/2.6.x/5605' of git://github.com/stschulte/puppet into 2.6.next"
(#6723) Fix withenv environment restoration bug
(#6689) Remove extraneous include of Puppet::Util in InventoryActiveRecord
Remove extra trailing whitespace from lib/puppet/resource.rb
(#5428) More fully "stub" Puppet::Resource::Reference for use with storedconfigs
(#6707) Fix typo in rest_authconfig.rb
(#6689) Make inventory_active_record terminus search quickly
(#5392) Give a better error when realizing a non-existant resource
(#2645) Adding a less-stubby test to verify the "system" attribute's behavior
maint: Remove serialization of InventoryFact values
maint: Rename InventoryHost to InventoryNode
Fixed #2645 - Added support for creating system users
maint: Remove spec run noise
maint:Refactor of mount provider integration tests
(#6338) Support searching on metadata in InventoryActiveRecord terminus
(#6338) Implement search for InventoryActiveRecord facts terminus
(#6338) Add an InventoryActiveRecord terminus for Facts
Added integration tests for the mount provider
...
Diffstat (limited to 'lib/puppet/resource.rb')
-rw-r--r-- | lib/puppet/resource.rb | 64 |
1 files changed, 39 insertions, 25 deletions
diff --git a/lib/puppet/resource.rb b/lib/puppet/resource.rb index e832804f5..214516908 100644 --- a/lib/puppet/resource.rb +++ b/lib/puppet/resource.rb @@ -5,6 +5,11 @@ require 'puppet/util/pson' # The simplest resource class. Eventually it will function as the # base class for all resource-like behaviour. class Puppet::Resource + # This stub class is only needed for serialization compatibility with 0.25.x. + # Specifically, it exists to provide a compatibility API when using YAML + # serialized objects loaded from StoreConfigs. + Reference = Puppet::Resource + include Puppet::Util::Tagging require 'puppet/resource/type_collection_helper' @@ -87,7 +92,7 @@ class Puppet::Resource def yaml_property_munge(x) case x when Hash - x.inject({}) { |h,kv| + x.inject({}) { |h,kv| k,v = kv h[k] = self.class.value_to_pson_data(v) h @@ -104,7 +109,7 @@ class Puppet::Resource # be overridden at some point, but this works for now. %w{has_key? keys length delete empty? <<}.each do |method| define_method(method) do |*args| - @parameters.send(method, *args) + parameters.send(method, *args) end end @@ -112,13 +117,13 @@ class Puppet::Resource # to lower-case symbols. def []=(param, value) validate_parameter(param) if validate_parameters - @parameters[parameter_name(param)] = value + parameters[parameter_name(param)] = value end # Return a given parameter's value. Converts all passed names # to lower-case symbols. def [](param) - @parameters[parameter_name(param)] + parameters[parameter_name(param)] end def ==(other) @@ -140,11 +145,11 @@ class Puppet::Resource # Iterate over each param/value pair, as required for Enumerable. def each - @parameters.each { |p,v| yield p, v } + parameters.each { |p,v| yield p, v } end def include?(parameter) - super || @parameters.keys.include?( parameter_name(parameter) ) + super || parameters.keys.include?( parameter_name(parameter) ) end # These two methods are extracted into a Helper @@ -170,14 +175,6 @@ class Puppet::Resource end end - # This stub class is only needed for serialization compatibility with 0.25.x - class Reference - attr_accessor :type,:title - def initialize(type,title) - @type,@title = type,title - end - end - # Create our resource. def initialize(type, title = nil, attributes = {}) @parameters = {} @@ -204,7 +201,7 @@ class Puppet::Resource tag(self.type) tag(self.title) if valid_tag?(self.title) - @reference = Reference.new(@type,@title) # for serialization compatibility with 0.25.x + @reference = self # for serialization compatibility with 0.25.x if strict? and ! resource_type if @type == 'Class' raise ArgumentError, "Could not find declared class #{title}" @@ -234,7 +231,7 @@ class Puppet::Resource # Produce a simple hash of our parameters. def to_hash - parse_title.merge @parameters + parse_title.merge parameters end def to_s @@ -255,15 +252,26 @@ class Puppet::Resource # Convert our resource to Puppet code. def to_manifest - "%s { '%s':\n%s\n}" % [self.type.to_s.downcase, self.title, - @parameters.collect { |p, v| - if v.is_a? Array - " #{p} => [\'#{v.join("','")}\']" - else - " #{p} => \'#{v}\'" - end - }.join(",\n") - ] + # Collect list of attributes to align => and move ensure first + attr = parameters.keys + attr_max = attr.inject(0) { |max,k| k.to_s.length > max ? k.to_s.length : max } + + attr.sort! + if attr.first != :ensure && attr.include?(:ensure) + attr.delete(:ensure) + attr.unshift(:ensure) + end + + attributes = attr.collect { |k| + v = parameters[k] + if v.is_a? Array + " %-#{attr_max}s => %s,\n" % [ k, "[\'#{v.join("', '")}\']" ] + else + " %-#{attr_max}s => %s,\n" % [ k, "\'#{v}\'" ] + end + } + + "%s { '%s':\n%s}" % [self.type.to_s.downcase, self.title, attributes] end def to_ref @@ -422,4 +430,10 @@ class Puppet::Resource return { :name => title.to_s } end end + + def parameters + # @parameters could have been loaded from YAML, causing it to be nil (by + # bypassing initialize). + @parameters ||= {} + end end |