186
186
@is_stage ||= @type.to_s.downcase == "stage"
189
# Create our resource.
189
# Construct a resource from data.
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.
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
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}.
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]"`.
190
212
def initialize(type, title = nil, attributes = {})
214
if type.is_a?(Class) && type < Puppet::Type
215
# Set the resource type to avoid an expensive `known_resource_types`
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.
193
223
# Set things like strictness first.
194
224
attributes.each do |attr, value|
209
239
extract_parameters(params)
242
if resource_type && resource_type.respond_to?(:deprecate_params)
243
resource_type.deprecate_params(title, attributes[:parameters])
213
247
tag(self.title) if valid_tag?(self.title)
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
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.
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
453
result[p] = if v.is_a?(Array) and v.length == 1
436
478
param = param.to_sym
437
479
fail Puppet::ParseError, "Must pass #{param} to #{self}" unless parameters.include?(param)
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}"
441
498
def validate_parameter(name)
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"