summaryrefslogtreecommitdiffstats
path: root/lib/puppet/indirector/indirection.rb
diff options
context:
space:
mode:
authorMarkus Roberts <Markus@reality.com>2010-07-09 18:12:17 -0700
committerMarkus Roberts <Markus@reality.com>2010-07-09 18:12:17 -0700
commit3180b9d9b2c844dade1d361326600f7001ec66dd (patch)
tree98fe7c5ac7eb942aac9c39f019a17b0b3f5a57f4 /lib/puppet/indirector/indirection.rb
parent543225970225de5697734bfaf0a6eee996802c04 (diff)
downloadpuppet-3180b9d9b2c844dade1d361326600f7001ec66dd.tar.gz
puppet-3180b9d9b2c844dade1d361326600f7001ec66dd.tar.xz
puppet-3180b9d9b2c844dade1d361326600f7001ec66dd.zip
Code smell: Two space indentation
Replaced 106806 occurances of ^( +)(.*$) with The ruby community almost universally (i.e. everyone but Luke, Markus, and the other eleven people who learned ruby in the 1900s) uses two-space indentation. 3 Examples: The code: end # Tell getopt which arguments are valid def test_get_getopt_args element = Setting.new :name => "foo", :desc => "anything", :settings => Puppet::Util::Settings.new assert_equal([["--foo", GetoptLong::REQUIRED_ARGUMENT]], element.getopt_args, "Did not produce appropriate getopt args") becomes: end # Tell getopt which arguments are valid def test_get_getopt_args element = Setting.new :name => "foo", :desc => "anything", :settings => Puppet::Util::Settings.new assert_equal([["--foo", GetoptLong::REQUIRED_ARGUMENT]], element.getopt_args, "Did not produce appropriate getopt args") The code: assert_equal(str, val) assert_instance_of(Float, result) end # Now test it with a passed object becomes: assert_equal(str, val) assert_instance_of(Float, result) end # Now test it with a passed object The code: end assert_nothing_raised do klass[:Yay] = "boo" klass["Cool"] = :yayness end becomes: end assert_nothing_raised do klass[:Yay] = "boo" klass["Cool"] = :yayness end
Diffstat (limited to 'lib/puppet/indirector/indirection.rb')
-rw-r--r--lib/puppet/indirector/indirection.rb498
1 files changed, 249 insertions, 249 deletions
diff --git a/lib/puppet/indirector/indirection.rb b/lib/puppet/indirector/indirection.rb
index 2b7da22ec..309eed7b6 100644
--- a/lib/puppet/indirector/indirection.rb
+++ b/lib/puppet/indirector/indirection.rb
@@ -7,302 +7,302 @@ require 'puppet/util/cacher'
# 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::Cacher
- include Puppet::Util::Docs
-
- @@indirections = []
-
- # Find an indirection by name. This is provided so that Terminus classes
- # can specifically hook up with the indirections they are associated with.
- def self.instance(name)
- @@indirections.find { |i| i.name == name }
+ include Puppet::Util::Cacher
+ include Puppet::Util::Docs
+
+ @@indirections = []
+
+ # Find an indirection by name. This is provided so that Terminus classes
+ # can specifically hook up with the indirections they are associated with.
+ def self.instance(name)
+ @@indirections.find { |i| i.name == name }
+ end
+
+ # Return a list of all known indirections. Used to generate the
+ # reference.
+ def self.instances
+ @@indirections.collect { |i| i.name }
+ end
+
+ # Find an indirected model by name. This is provided so that Terminus classes
+ # can specifically hook up with the indirections they are associated with.
+ def self.model(name)
+ return nil unless match = @@indirections.find { |i| i.name == name }
+ match.model
+ end
+
+ attr_accessor :name, :model
+
+ # Create and return our cache terminus.
+ def cache
+ raise(Puppet::DevError, "Tried to cache when no cache class was set") unless cache_class
+ terminus(cache_class)
+ end
+
+ # Should we use a cache?
+ def cache?
+ cache_class ? true : false
+ end
+
+ attr_reader :cache_class
+ # Define a terminus class to be used for caching.
+ def cache_class=(class_name)
+ validate_terminus_class(class_name) if class_name
+ @cache_class = class_name
+ end
+
+ # This is only used for testing.
+ def delete
+ @@indirections.delete(self) if @@indirections.include?(self)
+ end
+
+ # Set the time-to-live for instances created through this indirection.
+ def ttl=(value)
+ raise ArgumentError, "Indirection TTL must be an integer" unless value.is_a?(Fixnum)
+ @ttl = value
+ end
+
+ # Default to the runinterval for the ttl.
+ def ttl
+ @ttl ||= Puppet[:runinterval].to_i
+ end
+
+ # Calculate the expiration date for a returned instance.
+ def expiration
+ Time.now + ttl
+ end
+
+ # Generate the full doc string.
+ def doc
+ text = ""
+
+ text += scrub(@doc) + "\n\n" if @doc
+
+ if s = terminus_setting
+ text += "* **Terminus Setting**: #{terminus_setting}"
end
- # Return a list of all known indirections. Used to generate the
- # reference.
- def self.instances
- @@indirections.collect { |i| i.name }
- end
+ text
+ end
- # Find an indirected model by name. This is provided so that Terminus classes
- # can specifically hook up with the indirections they are associated with.
- def self.model(name)
- return nil unless match = @@indirections.find { |i| i.name == name }
- match.model
- end
-
- attr_accessor :name, :model
-
- # Create and return our cache terminus.
- def cache
- raise(Puppet::DevError, "Tried to cache when no cache class was set") unless cache_class
- terminus(cache_class)
- end
+ def initialize(model, name, options = {})
+ @model = model
+ @name = name
- # Should we use a cache?
- def cache?
- cache_class ? true : false
- end
+ @cache_class = nil
+ @terminus_class = nil
- attr_reader :cache_class
- # Define a terminus class to be used for caching.
- def cache_class=(class_name)
- validate_terminus_class(class_name) if class_name
- @cache_class = class_name
- end
+ raise(ArgumentError, "Indirection #{@name} is already defined") if @@indirections.find { |i| i.name == @name }
+ @@indirections << self
- # This is only used for testing.
- def delete
- @@indirections.delete(self) if @@indirections.include?(self)
+ if mod = options[:extend]
+ extend(mod)
+ options.delete(:extend)
end
- # Set the time-to-live for instances created through this indirection.
- def ttl=(value)
- raise ArgumentError, "Indirection TTL must be an integer" unless value.is_a?(Fixnum)
- @ttl = value
+ # 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
-
- # Default to the runinterval for the ttl.
- def ttl
- @ttl ||= Puppet[:runinterval].to_i
+ end
+
+ # Set up our request object.
+ def request(*args)
+ Puppet::Indirector::Request.new(self.name, *args)
+ end
+
+ # Return the singleton terminus for this indirection.
+ def terminus(terminus_name = nil)
+ # Get the name of the terminus.
+ raise Puppet::DevError, "No terminus specified for #{self.name}; cannot redirect" unless terminus_name ||= terminus_class
+
+ termini[terminus_name] ||= make_terminus(terminus_name)
+ end
+
+ # This can be used to select the terminus class.
+ attr_accessor :terminus_setting
+
+ # Determine the terminus class.
+ def terminus_class
+ unless @terminus_class
+ if setting = self.terminus_setting
+ self.terminus_class = Puppet.settings[setting].to_sym
+ else
+ raise Puppet::DevError, "No terminus class nor terminus setting was provided for indirection #{self.name}"
+ end
end
-
- # Calculate the expiration date for a returned instance.
- def expiration
- Time.now + ttl
+ @terminus_class
+ end
+
+ def reset_terminus_class
+ @terminus_class = nil
+ end
+
+ # Specify the terminus class to use.
+ def terminus_class=(klass)
+ validate_terminus_class(klass)
+ @terminus_class = klass
+ end
+
+ # This is used by terminus_class= and cache=.
+ def validate_terminus_class(terminus_class)
+ raise ArgumentError, "Invalid terminus name #{terminus_class.inspect}" unless terminus_class and terminus_class.to_s != ""
+ unless Puppet::Indirector::Terminus.terminus_class(self.name, terminus_class)
+ raise ArgumentError, "Could not find terminus #{terminus_class} for indirection #{self.name}"
end
+ end
- # Generate the full doc string.
- def doc
- text = ""
+ # 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)
- text += scrub(@doc) + "\n\n" if @doc
+ return nil unless cache?
- if s = terminus_setting
- text += "* **Terminus Setting**: #{terminus_setting}"
- end
+ return nil unless instance = cache.find(request(:find, key, *args))
- text
- end
+ Puppet.info "Expiring the #{self.name} cache of #{instance.name}"
- def initialize(model, name, options = {})
- @model = model
- @name = name
-
- @cache_class = nil
- @terminus_class = nil
-
- raise(ArgumentError, "Indirection #{@name} is already defined") if @@indirections.find { |i| i.name == @name }
- @@indirections << self
-
- if mod = options[:extend]
- 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
- end
+ # Set an expiration date in the past
+ instance.expiration = Time.now - 60
- # Set up our request object.
- def request(*args)
- Puppet::Indirector::Request.new(self.name, *args)
- end
+ cache.save(request(:save, instance, *args))
+ end
- # Return the singleton terminus for this indirection.
- def terminus(terminus_name = nil)
- # Get the name of the terminus.
- raise Puppet::DevError, "No terminus specified for #{self.name}; cannot redirect" unless terminus_name ||= terminus_class
+ # Search for an instance in the appropriate terminus, caching the
+ # results if caching is configured..
+ def find(key, *args)
+ request = request(:find, key, *args)
+ terminus = prepare(request)
- termini[terminus_name] ||= make_terminus(terminus_name)
+ begin
+ if result = find_in_cache(request)
+ return result
+ end
+ rescue => detail
+ puts detail.backtrace if Puppet[:trace]
+ Puppet.err "Cached #{self.name} for #{request.key} failed: #{detail}"
end
- # This can be used to select the terminus class.
- attr_accessor :terminus_setting
-
- # Determine the terminus class.
- def terminus_class
- unless @terminus_class
- if setting = self.terminus_setting
- self.terminus_class = Puppet.settings[setting].to_sym
- else
- raise Puppet::DevError, "No terminus class nor terminus setting was provided for indirection #{self.name}"
- end
- end
- @terminus_class
- end
+ # Otherwise, return the result from the terminus, caching if appropriate.
+ if ! request.ignore_terminus? and result = terminus.find(request)
+ result.expiration ||= self.expiration
+ if cache? and request.use_cache?
+ Puppet.info "Caching #{self.name} for #{request.key}"
+ cache.save request(:save, result, *args)
+ end
- def reset_terminus_class
- @terminus_class = nil
+ return terminus.respond_to?(:filter) ? terminus.filter(result) : result
end
- # Specify the terminus class to use.
- def terminus_class=(klass)
- validate_terminus_class(klass)
- @terminus_class = klass
- end
+ nil
+ end
- # This is used by terminus_class= and cache=.
- def validate_terminus_class(terminus_class)
- raise ArgumentError, "Invalid terminus name #{terminus_class.inspect}" unless terminus_class and terminus_class.to_s != ""
- unless Puppet::Indirector::Terminus.terminus_class(self.name, terminus_class)
- raise ArgumentError, "Could not find terminus #{terminus_class} for indirection #{self.name}"
- end
+ def find_in_cache(request)
+ # See if our instance is in the cache and up to date.
+ return nil unless cache? and ! request.ignore_cache? and cached = cache.find(request)
+ if cached.expired?
+ Puppet.info "Not using expired #{self.name} for #{request.key} from cache; expired at #{cached.expiration}"
+ return nil
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)
-
- return nil unless cache?
-
- return nil unless instance = cache.find(request(:find, key, *args))
+ Puppet.debug "Using cached #{self.name} for #{request.key}"
+ cached
+ end
- Puppet.info "Expiring the #{self.name} cache of #{instance.name}"
+ # Remove something via the terminus.
+ def destroy(key, *args)
+ request = request(:destroy, key, *args)
+ terminus = prepare(request)
- # Set an expiration date in the past
- instance.expiration = Time.now - 60
+ result = terminus.destroy(request)
- cache.save(request(:save, instance, *args))
+ if cache? and cached = cache.find(request(:find, key, *args))
+ # Reuse the existing request, since it's equivalent.
+ cache.destroy(request)
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)
- terminus = prepare(request)
-
- begin
- if result = find_in_cache(request)
- return result
- end
- rescue => detail
- puts detail.backtrace if Puppet[:trace]
- Puppet.err "Cached #{self.name} for #{request.key} failed: #{detail}"
- end
-
- # Otherwise, return the result from the terminus, caching if appropriate.
- if ! request.ignore_terminus? and result = terminus.find(request)
- result.expiration ||= self.expiration
- if cache? and request.use_cache?
- Puppet.info "Caching #{self.name} for #{request.key}"
- cache.save request(:save, result, *args)
- end
-
- return terminus.respond_to?(:filter) ? terminus.filter(result) : result
- end
-
- nil
- end
+ result
+ end
- def find_in_cache(request)
- # See if our instance is in the cache and up to date.
- return nil unless cache? and ! request.ignore_cache? and cached = cache.find(request)
- if cached.expired?
- Puppet.info "Not using expired #{self.name} for #{request.key} from cache; expired at #{cached.expiration}"
- return nil
- end
+ # Search for more than one instance. Should always return an array.
+ def search(key, *args)
+ request = request(:search, key, *args)
+ terminus = prepare(request)
- Puppet.debug "Using cached #{self.name} for #{request.key}"
- cached
+ 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|
+ instance.expiration ||= self.expiration
+ end
+ return result
end
+ end
- # Remove something via the terminus.
- def destroy(key, *args)
- request = request(:destroy, key, *args)
- terminus = prepare(request)
-
- result = terminus.destroy(request)
+ # Save the instance in the appropriate terminus. This method is
+ # normally an instance method on the indirected class.
+ def save(key, instance = nil)
+ request = request(:save, key, instance)
+ terminus = prepare(request)
- if cache? and cached = cache.find(request(:find, key, *args))
- # Reuse the existing request, since it's equivalent.
- cache.destroy(request)
- end
+ result = terminus.save(request)
- result
- end
+ # If caching is enabled, save our document there
+ cache.save(request) if cache?
- # Search for more than one instance. Should always return an array.
- def search(key, *args)
- request = request(:search, key, *args)
- 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|
- instance.expiration ||= self.expiration
- end
- return result
- end
- end
+ result
+ end
- # Save the instance in the appropriate terminus. This method is
- # normally an instance method on the indirected class.
- def save(key, instance = nil)
- request = request(:save, key, instance)
- terminus = prepare(request)
+ private
- result = terminus.save(request)
+ # Check authorization if there's a hook available; fail if there is one
+ # and it returns false.
+ def check_authorization(request, terminus)
+ # At this point, we're assuming authorization makes no sense without
+ # client information.
+ return unless request.node
- # If caching is enabled, save our document there
- cache.save(request) if cache?
+ # This is only to authorize via a terminus-specific authorization hook.
+ return unless terminus.respond_to?(:authorized?)
- result
+ unless terminus.authorized?(request)
+ msg = "Not authorized to call #{request.method} on #{request}"
+ msg += " with #{request.options.inspect}" unless request.options.empty?
+ raise ArgumentError, msg
end
-
- private
-
- # Check authorization if there's a hook available; fail if there is one
- # and it returns false.
- def check_authorization(request, terminus)
- # At this point, we're assuming authorization makes no sense without
- # client information.
- return unless request.node
-
- # This is only to authorize via a terminus-specific authorization hook.
- return unless terminus.respond_to?(:authorized?)
-
- unless terminus.authorized?(request)
- msg = "Not authorized to call #{request.method} on #{request}"
- msg += " with #{request.options.inspect}" unless request.options.empty?
- raise ArgumentError, msg
- end
+ end
+
+ # Setup a request, pick the appropriate terminus, check the request's authorization, and return it.
+ def prepare(request)
+ # Pick our terminus.
+ if respond_to?(:select_terminus)
+ unless terminus_name = select_terminus(request)
+ raise ArgumentError, "Could not determine appropriate terminus for #{request}"
+ end
+ else
+ terminus_name = terminus_class
end
- # Setup a request, pick the appropriate terminus, check the request's authorization, and return it.
- def prepare(request)
- # Pick our terminus.
- if respond_to?(:select_terminus)
- unless terminus_name = select_terminus(request)
- raise ArgumentError, "Could not determine appropriate terminus for #{request}"
- end
- else
- terminus_name = terminus_class
- end
-
- dest_terminus = terminus(terminus_name)
- check_authorization(request, dest_terminus)
-
- dest_terminus
- end
+ dest_terminus = terminus(terminus_name)
+ check_authorization(request, dest_terminus)
+
+ dest_terminus
+ end
- # Create a new terminus instance.
- def make_terminus(terminus_class)
- # Load our terminus class.
- unless klass = Puppet::Indirector::Terminus.terminus_class(self.name, terminus_class)
- raise ArgumentError, "Could not find terminus #{terminus_class} for indirection #{self.name}"
- end
- klass.new
+ # Create a new terminus instance.
+ def make_terminus(terminus_class)
+ # Load our terminus class.
+ unless klass = Puppet::Indirector::Terminus.terminus_class(self.name, terminus_class)
+ raise ArgumentError, "Could not find terminus #{terminus_class} for indirection #{self.name}"
end
+ klass.new
+ end
- # Cache our terminus instances indefinitely, but make it easy to clean them up.
- cached_attr(:termini) { Hash.new }
+ # Cache our terminus instances indefinitely, but make it easy to clean them up.
+ cached_attr(:termini) { Hash.new }
end