~ubuntu-branches/ubuntu/wily/puppet/wily-proposed

« back to all changes in this revision

Viewing changes to lib/puppet/resource.rb

  • Committer: Package Import Robot
  • Author(s): Stig Sandbeck Mathisen
  • Date: 2014-10-24 13:47:15 UTC
  • mfrom: (3.1.64 sid)
  • Revision ID: package-import@ubuntu.com-20141024134715-6ig54u0c4gar36ss
Tags: 3.7.2-1
* Imported upstream release 3.7.2
* Declare compliance with Debian Policy 3.9.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
186
186
    @is_stage ||= @type.to_s.downcase == "stage"
187
187
  end
188
188
 
189
 
  # Create our resource.
 
189
  # Construct a resource from data.
 
190
  #
 
191
  # Constructs a resource instance with the given `type` and `title`. Multiple
 
192
  # type signatures are possible for these arguments and most will result in an
 
193
  # expensive call to {Puppet::Node::Environment#known_resource_types} in order
 
194
  # to resolve `String` and `Symbol` Types to actual Ruby classes.
 
195
  #
 
196
  # @param type [Symbol, String] The name of the Puppet Type, as a string or
 
197
  #   symbol. The actual Type will be looked up using
 
198
  #   {Puppet::Node::Environment#known_resource_types}. This lookup is expensive.
 
199
  # @param type [String] The full resource name in the form of
 
200
  #   `"Type[Title]"`. This method of calling should only be used when
 
201
  #   `title` is `nil`.
 
202
  # @param type [nil] If a `nil` is passed, the title argument must be a string
 
203
  #   of the form `"Type[Title]"`.
 
204
  # @param type [Class] A class that inherits from `Puppet::Type`. This method
 
205
  #   of construction is much more efficient as it skips calls to
 
206
  #   {Puppet::Node::Environment#known_resource_types}.
 
207
  #
 
208
  # @param title [String, :main, nil] The title of the resource. If type is `nil`, may also
 
209
  #   be the full resource name in the form of `"Type[Title]"`.
 
210
  #
 
211
  # @api public
190
212
  def initialize(type, title = nil, attributes = {})
191
213
    @parameters = {}
 
214
    if type.is_a?(Class) && type < Puppet::Type
 
215
      # Set the resource type to avoid an expensive `known_resource_types`
 
216
      # lookup.
 
217
      self.resource_type = type
 
218
      # From this point on, the constructor behaves the same as if `type` had
 
219
      # been passed as a symbol.
 
220
      type = type.name
 
221
    end
192
222
 
193
223
    # Set things like strictness first.
194
224
    attributes.each do |attr, value|
209
239
      extract_parameters(params)
210
240
    end
211
241
 
 
242
    if resource_type && resource_type.respond_to?(:deprecate_params)
 
243
        resource_type.deprecate_params(title, attributes[:parameters])
 
244
    end
 
245
 
212
246
    tag(self.type)
213
247
    tag(self.title) if valid_tag?(self.title)
214
248
 
254
288
    @environment ||= if catalog
255
289
                       catalog.environment_instance
256
290
                     else
257
 
                       Puppet::Node::Environment::NONE
 
291
                       Puppet.lookup(:current_environment) { Puppet::Node::Environment::NONE }
258
292
                     end
259
293
  end
260
294
 
407
441
        end
408
442
      end
409
443
 
410
 
      # If the value is an array with only one value, then
411
 
      # convert it to a single value.  This is largely so that
412
 
      # the database interaction doesn't have to worry about
413
 
      # whether it returns an array or a string.
414
 
      result[p] = if v.is_a?(Array) and v.length == 1
415
 
                    v[0]
416
 
                  else
417
 
                    v
418
 
                  end
 
444
      if Puppet[:parser] == 'current'
 
445
        # If the value is an array with only one value, then
 
446
        # convert it to a single value.  This is largely so that
 
447
        # the database interaction doesn't have to worry about
 
448
        # whether it returns an array or a string.
 
449
        #
 
450
        # This behavior is not done in the future parser, but we can't issue a
 
451
        # deprecation warning either since there isn't anything that a user can
 
452
        # do about it.
 
453
        result[p] = if v.is_a?(Array) and v.length == 1
 
454
                      v[0]
 
455
                    else
 
456
                      v
 
457
                    end
 
458
      else
 
459
        result[p] = v
 
460
      end
419
461
    end
420
462
 
421
463
    result
436
478
      param = param.to_sym
437
479
      fail Puppet::ParseError, "Must pass #{param} to #{self}" unless parameters.include?(param)
438
480
    end
 
481
 
 
482
    # Perform optional type checking
 
483
    if Puppet[:parser] == 'future'
 
484
      # Perform type checking
 
485
      arg_types = resource_type.argument_types
 
486
      # Parameters is a map from name, to parameter, and the parameter again has name and value
 
487
      parameters.each do |name, value|
 
488
        next unless t = arg_types[name.to_s]  # untyped, and parameters are symbols here (aargh, strings in the type)
 
489
        unless Puppet::Pops::Types::TypeCalculator.instance?(t, value.value)
 
490
          inferred_type = Puppet::Pops::Types::TypeCalculator.infer(value.value)
 
491
          actual = Puppet::Pops::Types::TypeCalculator.generalize!(inferred_type)
 
492
          fail Puppet::ParseError, "Expected parameter '#{name}' of '#{self}' to have type #{t.to_s}, got #{actual.to_s}"
 
493
        end
 
494
      end
 
495
    end
439
496
  end
440
497
 
441
498
  def validate_parameter(name)
487
544
  end
488
545
 
489
546
  def extract_type_and_title(argtype, argtitle)
490
 
    if    (argtitle || argtype) =~ /^([^\[\]]+)\[(.+)\]$/m then [ $1,                 $2            ]
491
 
    elsif argtitle                                         then [ argtype,            argtitle      ]
492
 
    elsif argtype.is_a?(Puppet::Type)                      then [ argtype.class.name, argtype.title ]
493
 
    elsif argtype.is_a?(Hash)                              then
 
547
    if    (argtype.nil? || argtype == :component || argtype == :whit) &&
 
548
           argtitle =~ /^([^\[\]]+)\[(.+)\]$/m                 then [ $1,                 $2            ]
 
549
    elsif argtitle.nil? && argtype =~ /^([^\[\]]+)\[(.+)\]$/m  then [ $1,                 $2            ]
 
550
    elsif argtitle                                             then [ argtype,            argtitle      ]
 
551
    elsif argtype.is_a?(Puppet::Type)                          then [ argtype.class.name, argtype.title ]
 
552
    elsif argtype.is_a?(Hash)                                  then
494
553
      raise ArgumentError, "Puppet::Resource.new does not take a hash as the first argument. "+
495
554
        "Did you mean (#{(argtype[:type] || argtype["type"]).inspect}, #{(argtype[:title] || argtype["title"]).inspect }) ?"
496
555
    else raise ArgumentError, "No title provided and #{argtype.inspect} is not a valid resource reference"