lib/puppet/resource_api.rb in puppet-resource_api-1.0.1 vs lib/puppet/resource_api.rb in puppet-resource_api-1.0.2

- old
+ new

@@ -60,11 +60,15 @@ apply_to_device end define_method(:initialize) do |attributes| # $stderr.puts "A: #{attributes.inspect}" - attributes = attributes.to_hash if attributes.is_a? Puppet::Resource + if attributes.is_a? Puppet::Resource + attributes = attributes.to_hash + else + @called_from_resource = true + end # $stderr.puts "B: #{attributes.inspect}" if feature_support?('canonicalize') attributes = my_provider.canonicalize(context, [attributes])[0] end # $stderr.puts "C: #{attributes.inspect}" @@ -74,23 +78,40 @@ validate do # enforce mandatory attributes missing_attrs = [] definition[:attributes].each do |name, options| type = Puppet::Pops::Types::TypeParser.singleton.parse(options[:type]) - unless [:read_only, :namevar].include? options[:behaviour] - missing_attrs << name if value(name).nil? && !(type.instance_of? Puppet::Pops::Types::POptionalType) + # skip read only vars and the namevar + next if [:read_only, :namevar].include? options[:behaviour] + + # skip properties if the resource is being deleted + next if definition[:attributes][:ensure] && + value(:ensure) == :absent && + options[:behaviour].nil? + + if value(name).nil? && !(type.instance_of? Puppet::Pops::Types::POptionalType) + missing_attrs << name end end + + missing_attrs -= [:ensure] if @called_from_resource + if missing_attrs.any? error_msg = "The following mandatory attributes where not provided:\n * " + missing_attrs.join(", \n * ") raise Puppet::ResourceError, error_msg end end definition[:attributes].each do |name, options| # puts "#{name}: #{options.inspect}" + if options[:behaviour] + unless [:read_only, :namevar, :parameter].include? options[:behaviour] + raise Puppet::ResourceError, "`#{options[:behaviour]}` is not a valid behaviour value" + end + end + # TODO: using newparam everywhere would suppress change reporting # that would allow more fine-grained reporting through context, # but require more invest in hooking up the infrastructure to emulate existing data param_or_property = if [:read_only, :parameter, :namevar].include? options[:behaviour] :newparam @@ -197,9 +218,12 @@ attr_def = {} my_provider.get(context).map do |resource_hash| resource_hash.each do |key| property = definition[:attributes][key.first] attr_def[key.first] = property + end + if resource_hash[namevar_name].nil? + raise Puppet::ResourceError, "`#{name}.get` did not return a value for the `#{namevar_name}` namevar attribute" end Puppet::ResourceApi::TypeShim.new(resource_hash[namevar_name], resource_hash, name, namevar_name, attr_def) end end