summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuke Kanies <luke@puppetlabs.com>2011-03-25 16:07:25 -0700
committerLuke Kanies <luke@puppetlabs.com>2011-03-25 16:07:25 -0700
commitc0236aab1145e01e26f59cf1d823dc6966ba567b (patch)
tree130b88b1b76a8e9705361e6e90508281e7cd81a3
parentde2e84eb93416cebe6d5a92ee88ba9c98cd52661 (diff)
parent076de13a24163f485a5dea2469c4b958f73a7d1e (diff)
Merge branch 'tickets/next/6855-searching_in_resource_type' into next
-rw-r--r--lib/puppet/indirector/resource_type/parser.rb13
-rwxr-xr-xspec/unit/indirector/resource_type/parser_spec.rb31
2 files changed, 41 insertions, 3 deletions
diff --git a/lib/puppet/indirector/resource_type/parser.rb b/lib/puppet/indirector/resource_type/parser.rb
index fd5b3938a..4bcaf3f47 100644
--- a/lib/puppet/indirector/resource_type/parser.rb
+++ b/lib/puppet/indirector/resource_type/parser.rb
@@ -21,12 +21,23 @@ class Puppet::Indirector::ResourceType::Parser < Puppet::Indirector::Code
end
def search(request)
- raise ArgumentError, "Only '*' is acceptable as a search request" unless request.key == "*"
krt = request.environment.known_resource_types
# Make sure we've got all of the types loaded.
krt.loader.import_all
result = [krt.hostclasses.values, krt.definitions.values, krt.nodes.values].flatten.reject { |t| t.name == "" }
return nil if result.empty?
+ return result if request.key == "*"
+
+ # Strip the regex of any wrapping slashes that might exist
+ key = request.key.sub(/^\//, '').sub(/\/$/, '')
+ begin
+ regex = Regexp.new(key)
+ rescue => detail
+ raise ArgumentError, "Invalid regex '#{request.key}': #{detail}"
+ end
+
+ result.reject! { |t| t.name.to_s !~ regex }
+ return nil if result.empty?
result
end
end
diff --git a/spec/unit/indirector/resource_type/parser_spec.rb b/spec/unit/indirector/resource_type/parser_spec.rb
index 27e61486c..f86b319f9 100755
--- a/spec/unit/indirector/resource_type/parser_spec.rb
+++ b/spec/unit/indirector/resource_type/parser_spec.rb
@@ -67,8 +67,35 @@ describe Puppet::Indirector::ResourceType::Parser do
@terminus.search(@request)
end
- it "should fail if anyther other than '*' was provided as the search key" do
- @request.key = "foo*"
+ it "should return all results if '*' is provided as the search string" do
+ @request.key = "*"
+ type = @krt.add(Puppet::Resource::Type.new(:hostclass, "foo"))
+ node = @krt.add(Puppet::Resource::Type.new(:node, "bar"))
+ define = @krt.add(Puppet::Resource::Type.new(:definition, "baz"))
+
+ result = @terminus.search(@request)
+ result.should be_include(type)
+ result.should be_include(node)
+ result.should be_include(define)
+ end
+
+ it "should treat any search string not '*' as a regex" do
+ @request.key = "a"
+ foo = @krt.add(Puppet::Resource::Type.new(:hostclass, "foo"))
+ bar = @krt.add(Puppet::Resource::Type.new(:hostclass, "bar"))
+ baz = @krt.add(Puppet::Resource::Type.new(:hostclass, "baz"))
+
+ result = @terminus.search(@request)
+ result.should be_include(bar)
+ result.should be_include(baz)
+ result.should_not be_include(foo)
+ end
+
+ it "should fail if a provided search string is not '*' and is not a valid regex" do
+ @request.key = "*foo*"
+
+ # Add one instance so we don't just get an empty array"
+ @krt.add(Puppet::Resource::Type.new(:hostclass, "foo"))
lambda { @terminus.search(@request) }.should raise_error(ArgumentError)
end