lib/puppet/indirector/indirection.rb in puppet-2.7.26 vs lib/puppet/indirector/indirection.rb in puppet-3.0.0.rc4

- old
+ new

@@ -1,14 +1,16 @@ require 'puppet/util/docs' +require 'puppet/util/methodhelper' require 'puppet/indirector/envelope' require 'puppet/indirector/request' require 'puppet/util/instrumentation/instrumentable' # The class that connects functional classes with their different collection # back-ends. Each indirection has a set of associated terminus classes, # each of which is a subclass of Puppet::Indirector::Terminus. class Puppet::Indirector::Indirection + include Puppet::Util::MethodHelper include Puppet::Util::Docs extend Puppet::Util::Instrumentation::Instrumentable attr_accessor :name, :model attr_reader :termini @@ -106,17 +108,11 @@ extend(mod) options.delete(:extend) end # This is currently only used for cache_class and terminus_class. - options.each do |name, value| - begin - send(name.to_s + "=", value) - rescue NoMethodError - raise ArgumentError, "#{name} is not a valid Indirection parameter" - end - end + set_options(options) end # Set up our request object. def request(*args) Puppet::Indirector::Request.new(self.name, *args) @@ -164,29 +160,29 @@ end # Expire a cached object, if one is cached. Note that we don't actually # remove it, we expire it and write it back out to disk. This way people # can still use the expired object if they want. - def expire(key, *args) - request = request(:expire, key, *args) + def expire(key, options={}) + request = request(:expire, key, nil, options) return nil unless cache? - return nil unless instance = cache.find(request(:find, key, *args)) + return nil unless instance = cache.find(request(:find, key, nil, options)) Puppet.info "Expiring the #{self.name} cache of #{instance.name}" # Set an expiration date in the past instance.expiration = Time.now - 60 - cache.save(request(:save, instance, *args)) + cache.save(request(:save, nil, instance, options)) end # Search for an instance in the appropriate terminus, caching the # results if caching is configured.. - def find(key, *args) - request = request(:find, key, *args) + def find(key, options={}) + request = request(:find, key, nil, options) terminus = prepare(request) if result = find_in_cache(request) return result end @@ -194,23 +190,23 @@ # Otherwise, return the result from the terminus, caching if appropriate. if ! request.ignore_terminus? and result = terminus.find(request) result.expiration ||= self.expiration if result.respond_to?(:expiration) if cache? and request.use_cache? Puppet.info "Caching #{self.name} for #{request.key}" - cache.save request(:save, result, *args) + cache.save request(:save, nil, result, options) end return terminus.respond_to?(:filter) ? terminus.filter(result) : result end nil end # Search for an instance in the appropriate terminus, and return a # boolean indicating whether the instance was found. - def head(key, *args) - request = request(:head, key, *args) + def head(key, options={}) + request = request(:head, key, nil, options) terminus = prepare(request) # Look in the cache first, then in the terminus. Force the result # to be a boolean. !!(find_in_cache(request) || terminus.head(request)) @@ -225,33 +221,32 @@ end Puppet.debug "Using cached #{self.name} for #{request.key}" cached rescue => detail - puts detail.backtrace if Puppet[:trace] - Puppet.err "Cached #{self.name} for #{request.key} failed: #{detail}" + Puppet.log_exception(detail, "Cached #{self.name} for #{request.key} failed: #{detail}") nil end # Remove something via the terminus. - def destroy(key, *args) - request = request(:destroy, key, *args) + def destroy(key, options={}) + request = request(:destroy, key, nil, options) terminus = prepare(request) result = terminus.destroy(request) - if cache? and cached = cache.find(request(:find, key, *args)) + if cache? and cached = cache.find(request(:find, key, nil, options)) # Reuse the existing request, since it's equivalent. cache.destroy(request) end result end # Search for more than one instance. Should always return an array. - def search(key, *args) - request = request(:search, key, *args) + def search(key, options={}) + request = request(:search, key, nil, options) terminus = prepare(request) if result = terminus.search(request) raise Puppet::DevError, "Search results from terminus #{terminus.name} are not an array" unless result.is_a?(Array) result.each do |instance| @@ -262,12 +257,12 @@ end end # Save the instance in the appropriate terminus. This method is # normally an instance method on the indirected class. - def save(instance, key = nil) - request = request(:save, key, instance) + def save(instance, key = nil, options={}) + request = request(:save, key, instance, options) terminus = prepare(request) result = terminus.save(request) # If caching is enabled, save our document there @@ -306,10 +301,9 @@ terminus_name = terminus_class end dest_terminus = terminus(terminus_name) check_authorization(request, dest_terminus) - dest_terminus.validate(request) dest_terminus end # Create a new terminus instance.