summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2007-09-21 15:49:25 -0500
committerLuke Kanies <luke@madstop.com>2007-09-21 15:49:25 -0500
commit02275f0b29cee70e3f02d6d8f911eaaf3fad579d (patch)
tree83e6aa336221432bc40eea72013cff4571bacd81
parentda0555d948b837e7c16bdca164780c9e71353e4a (diff)
Adding automatic association between terminus subclasses and
the indirection they're working with. It looks like I'll be moving terminus registration to the indirection rather than the top-level Indirector.
-rw-r--r--lib/puppet/indirector/facts/yaml.rb41
-rw-r--r--lib/puppet/indirector/terminus.rb23
-rw-r--r--lib/puppet/indirector/yaml/facts.rb7
-rwxr-xr-xspec/unit/indirector/terminus.rb62
4 files changed, 88 insertions, 45 deletions
diff --git a/lib/puppet/indirector/facts/yaml.rb b/lib/puppet/indirector/facts/yaml.rb
deleted file mode 100644
index cb02f05c6..000000000
--- a/lib/puppet/indirector/facts/yaml.rb
+++ /dev/null
@@ -1,41 +0,0 @@
-Puppet::Indirector.register_terminus :facts, :yaml do
- desc "Store client facts as flat files, serialized using YAML."
-
- # Get a client's facts.
- def find(node)
- file = path(node)
-
- return nil unless FileTest.exists?(file)
-
- begin
- values = YAML::load(File.read(file))
- rescue => detail
- Puppet.err "Could not load facts for %s: %s" % [node, detail]
- end
-
- Puppet::Node::Facts.new(node, values)
- end
-
- def initialize
- Puppet.config.use(:yamlfacts)
- end
-
- # Store the facts to disk.
- def save(facts)
- File.open(path(facts.name), "w", 0600) do |f|
- begin
- f.print YAML::dump(facts.values)
- rescue => detail
- Puppet.err "Could not write facts for %s: %s" % [facts.name, detail]
- end
- end
- nil
- end
-
- private
-
- # Return the path to a given node's file.
- def path(name)
- File.join(Puppet[:yamlfactdir], name + ".yaml")
- end
-end
diff --git a/lib/puppet/indirector/terminus.rb b/lib/puppet/indirector/terminus.rb
index 4d550e9ba..a98f6dff0 100644
--- a/lib/puppet/indirector/terminus.rb
+++ b/lib/puppet/indirector/terminus.rb
@@ -20,6 +20,29 @@ class Puppet::Indirector::Terminus
raise ArgumentError, "Could not find indirection instance %s" % name
end
end
+
+ # Register our subclass with the appropriate indirection.
+ # This follows the convention that our terminus is named after the
+ # indirection.
+ def inherited(subclass)
+ longname = subclass.to_s
+ if longname =~ /#<Class/
+ raise ArgumentError, "Terminus subclasses must have associated constants"
+ end
+ names = longname.split("::")
+
+ # First figure out the indirection
+ shortname = names.pop.downcase.intern
+ subclass.indirection = shortname
+
+ if names.empty?
+ raise ArgumentError, "Terminus subclasses need to have their constants include the parent class for setting the terminus name"
+ end
+
+ # And then the name (e.g., ldap or yaml)
+ termtype = names.pop.downcase.intern
+ subclass.name = termtype
+ end
end
def initialize
diff --git a/lib/puppet/indirector/yaml/facts.rb b/lib/puppet/indirector/yaml/facts.rb
new file mode 100644
index 000000000..c09eaeab1
--- /dev/null
+++ b/lib/puppet/indirector/yaml/facts.rb
@@ -0,0 +1,7 @@
+require 'puppet/indirector/yaml'
+
+class Puppet::Indirector::Yaml::Facts < Puppet::Indirector::Yaml
+ desc "Store client facts as flat files, serialized using YAML."
+
+ register_terminus
+end
diff --git a/spec/unit/indirector/terminus.rb b/spec/unit/indirector/terminus.rb
index 1660315ca..c5f5c064a 100755
--- a/spec/unit/indirector/terminus.rb
+++ b/spec/unit/indirector/terminus.rb
@@ -4,7 +4,13 @@ require 'puppet/indirector'
describe Puppet::Indirector::Terminus do
before do
- @terminus = Class.new(Puppet::Indirector::Terminus)
+ @indirection = stub 'indirection', :name => :mystuff
+ Puppet::Indirector::Indirection.stubs(:instance).with(:mystuff).returns(@indirection)
+ @terminus = Class.new(Puppet::Indirector::Terminus) do
+ def self.to_s
+ "Terminus::Type::MyStuff"
+ end
+ end
end
it "should support the documentation methods" do
@@ -39,16 +45,64 @@ describe Puppet::Indirector::Terminus do
it "should fail when provided a name that does not resolve to an indirection" do
Puppet::Indirector::Indirection.expects(:instance).with(:myind).returns(nil)
proc { @terminus.indirection = :myind }.should raise_error(ArgumentError)
- @terminus.indirection.should be_nil
+
+ # It shouldn't overwrite our existing one (or, more normally, it shouldn't set
+ # anything).
+ @terminus.indirection.should equal(@indirection)
+ end
+end
+
+describe Puppet::Indirector::Terminus, " when being subclassed" do
+ it "should associate the subclass with an indirection based on the subclass constant" do
+ indirection = mock 'indirection'
+ Puppet::Indirector::Indirection.expects(:instance).with(:myindirection).returns(indirection)
+
+ klass = Class.new(Puppet::Indirector::Terminus) do
+ def self.to_s
+ "Puppet::Indirector::Terminus::MyIndirection"
+ end
+ end
+
+ klass.indirection.should equal(indirection)
+ end
+
+ it "should fail when the terminus subclass does not include its parent class in the constant path" do
+ indirection = mock 'indirection'
+ Puppet::Indirector::Indirection.expects(:instance).with(:myindirection).returns(indirection)
+
+ proc {
+ klass = Class.new(Puppet::Indirector::Terminus) do
+ def self.to_s
+ "MyIndirection"
+ end
+ end
+ }.should raise_error(ArgumentError)
+ end
+
+ it "should set the subclass's name to the terminus type" do
+ indirection = mock 'indirection'
+ Puppet::Indirector::Indirection.expects(:instance).with(:myindirection).returns(indirection)
+
+ klass = Class.new(Puppet::Indirector::Terminus) do
+ def self.to_s
+ "Puppet::Indirector::Terminus::Yaml::MyIndirection"
+ end
+ end
+
+ klass.name.should == :yaml
end
end
describe Puppet::Indirector::Terminus, " when a terminus instance" do
before do
@indirection = stub 'indirection', :name => :myyaml
- @terminus_class = Class.new(Puppet::Indirector::Terminus)
+ Puppet::Indirector::Indirection.stubs(:instance).with(:mystuff).returns(@indirection)
+ @terminus_class = Class.new(Puppet::Indirector::Terminus) do
+ def self.to_s
+ "Terminus::Type::MyStuff"
+ end
+ end
@terminus_class.name = :test
- @terminus_class.stubs(:indirection).returns(@indirection)
@terminus = @terminus_class.new
end