diff --git a/lib/puppet/indirector/resource_type/parser.rb b/lib/puppet/indirector/resource_type/parser.rb index 4bcaf3f47..d76349bc7 100644 --- a/lib/puppet/indirector/resource_type/parser.rb +++ b/lib/puppet/indirector/resource_type/parser.rb @@ -1,43 +1,75 @@ require 'puppet/resource/type' require 'puppet/indirector/code' require 'puppet/indirector/resource_type' class Puppet::Indirector::ResourceType::Parser < Puppet::Indirector::Code desc "Return the data-form of a resource type." def find(request) krt = request.environment.known_resource_types # This is a bit ugly. [:hostclass, :definition, :node].each do |type| # We have to us 'find_' here because it will # load any missing types from disk, whereas the plain # '' method only returns from memory. if r = krt.send("find_#{type}", [""], request.key) return r end end nil end + # This is the "search" indirection method for resource types. It searches + # through a specified environment for all custom declared classes + # (a.k.a 'hostclasses'), defined types (a.k.a. 'definitions'), and nodes. + # + # @param [Puppet::Indirector::Request] request + # Important properties of the request parameter: + # 1. request.environment : The environment in which to look for types. + # 2. request.key : A String that will be treated as a regular expression to + # be matched against the names of the available types. You may also + # pass a "*", which will match all available types. + # 3. request.options[:type] : a String that can be used to filter the output + # to only return the desired types. The current supported values are + # 'class', 'defined_type', and 'node'. def search(request) 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 == "" } + + result_candidates = [] + if request.options.has_key?(:type) + case request.options[:type] + when "class" + result_candidates = krt.hostclasses.values + when "defined_type" + result_candidates = krt.definitions.values + when "node" + result_candidates = krt.nodes.values + else + raise ArgumentError, "Unrecognized type filter: " + + "'#{request.options[:type]}', expected one " + + " of 'class', 'defined_type', or 'node'." + end + else + result_candidates = [krt.hostclasses.values, krt.definitions.values, krt.nodes.values] + end + + result = result_candidates.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/lib/puppet/network/http/api/v1.rb b/lib/puppet/network/http/api/v1.rb index ef19fe487..29146ff9b 100644 --- a/lib/puppet/network/http/api/v1.rb +++ b/lib/puppet/network/http/api/v1.rb @@ -1,83 +1,83 @@ require 'puppet/network/http/api' module Puppet::Network::HTTP::API::V1 # How we map http methods and the indirection name in the URI # to an indirection method. METHOD_MAP = { "GET" => { :plural => :search, :singular => :find }, "POST" => { :singular => :find, }, "PUT" => { :singular => :save }, "DELETE" => { :singular => :destroy }, "HEAD" => { :singular => :head } } def uri2indirection(http_method, uri, params) environment, indirection, key = uri.split("/", 4)[1..-1] # the first field is always nil because of the leading slash raise ArgumentError, "The environment must be purely alphanumeric, not '#{environment}'" unless environment =~ /^\w+$/ raise ArgumentError, "The indirection name must be purely alphanumeric, not '#{indirection}'" unless indirection =~ /^\w+$/ method = indirection_method(http_method, indirection) params[:environment] = Puppet::Node::Environment.new(environment) params.delete(:bucket_path) raise ArgumentError, "No request key specified in #{uri}" if key == "" or key.nil? key = URI.unescape(key) [indirection, method, key, params] end def indirection2uri(request) indirection = request.method == :search ? pluralize(request.indirection_name.to_s) : request.indirection_name.to_s "/#{request.environment.to_s}/#{indirection}/#{request.escaped_key}#{request.query_string}" end def request_to_uri_and_body(request) indirection = request.method == :search ? pluralize(request.indirection_name.to_s) : request.indirection_name.to_s ["/#{request.environment.to_s}/#{indirection}/#{request.escaped_key}", request.query_string.sub(/^\?/,'')] end def indirection_method(http_method, indirection) raise ArgumentError, "No support for http method #{http_method}" unless METHOD_MAP[http_method] unless method = METHOD_MAP[http_method][plurality(indirection)] - raise ArgumentError, "No support for plural #{http_method} operations" + raise ArgumentError, "No support for plurality #{plurality(indirection)} for #{http_method} operations" end method end def pluralize(indirection) return(indirection == "status" ? "statuses" : indirection + "s") end def plurality(indirection) # NOTE This specific hook for facts is ridiculous, but it's a *many*-line # fix to not need this, and our goal is to move away from the complication # that leads to the fix being too long. return :singular if indirection == "facts" return :singular if indirection == "status" return :singular if indirection == "certificate_status" return :plural if indirection == "inventory" result = (indirection =~ /s$|_search$/) ? :plural : :singular indirection.sub!(/s$|_search$/, '') indirection.sub!(/statuse$/, 'status') result end end diff --git a/spec/unit/indirector/resource_type/parser_spec.rb b/spec/unit/indirector/resource_type/parser_spec.rb index a0362496e..7315357ea 100755 --- a/spec/unit/indirector/resource_type/parser_spec.rb +++ b/spec/unit/indirector/resource_type/parser_spec.rb @@ -1,149 +1,249 @@ #!/usr/bin/env rspec require 'spec_helper' require 'puppet/indirector/resource_type/parser' require 'puppet_spec/files' describe Puppet::Indirector::ResourceType::Parser do include PuppetSpec::Files before do @terminus = Puppet::Indirector::ResourceType::Parser.new @request = Puppet::Indirector::Request.new(:resource_type, :find, "foo", nil) @krt = @request.environment.known_resource_types end it "should be registered with the resource_type indirection" do Puppet::Indirector::Terminus.terminus_class(:resource_type, :parser).should equal(Puppet::Indirector::ResourceType::Parser) end describe "when finding" do it "should return any found type from the request's environment" do type = Puppet::Resource::Type.new(:hostclass, "foo") @request.environment.known_resource_types.add(type) @terminus.find(@request).should == type end it "should attempt to load the type if none is found in memory" do dir = tmpdir("find_a_type") FileUtils.mkdir_p(dir) Puppet[:modulepath] = dir # Make a new request, since we've reset the env @request = Puppet::Indirector::Request.new(:resource_type, :find, "foo::bar", nil) manifest_path = File.join(dir, "foo", "manifests") FileUtils.mkdir_p(manifest_path) File.open(File.join(manifest_path, "bar.pp"), "w") { |f| f.puts "class foo::bar {}" } result = @terminus.find(@request) result.should be_instance_of(Puppet::Resource::Type) result.name.should == "foo::bar" end it "should return nil if no type can be found" do @terminus.find(@request).should be_nil end it "should prefer definitions to nodes" do type = @krt.add(Puppet::Resource::Type.new(:hostclass, "foo")) node = @krt.add(Puppet::Resource::Type.new(:node, "foo")) @terminus.find(@request).should == type end end describe "when searching" do - before do - @request.key = "*" + describe "when the search key is a wildcard" do + before do + @request.key = "*" + end + + it "should use the request's environment's list of known resource types" do + @request.environment.known_resource_types.expects(:hostclasses).returns({}) + + @terminus.search(@request) + end + + it "should return all results if '*' is provided as the search string" do + 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 return all known types" do + 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 not return the 'main' class" do + main = @krt.add(Puppet::Resource::Type.new(:hostclass, "")) + + # So there is a return value + foo = @krt.add(Puppet::Resource::Type.new(:hostclass, "foo")) + + @terminus.search(@request).should_not be_include(main) + end + + it "should return nil if no types can be found" do + @terminus.search(@request).should be_nil + end + + it "should load all resource types from all search paths" do + dir = tmpdir("searching_in_all") + first = File.join(dir, "first") + second = File.join(dir, "second") + FileUtils.mkdir_p(first) + FileUtils.mkdir_p(second) + Puppet[:modulepath] = "#{first}#{File::PATH_SEPARATOR}#{second}" + + # Make a new request, since we've reset the env + @request = Puppet::Indirector::Request.new(:resource_type, :search, "*", nil) + + onepath = File.join(first, "one", "manifests") + FileUtils.mkdir_p(onepath) + twopath = File.join(first, "two", "manifests") + FileUtils.mkdir_p(twopath) + + File.open(File.join(onepath, "oneklass.pp"), "w") { |f| f.puts "class one::oneklass {}" } + File.open(File.join(twopath, "twoklass.pp"), "w") { |f| f.puts "class two::twoklass {}" } + + result = @terminus.search(@request) + result.find { |t| t.name == "one::oneklass" }.should be_instance_of(Puppet::Resource::Type) + result.find { |t| t.name == "two::twoklass" }.should be_instance_of(Puppet::Resource::Type) + end + + context "when specifying a 'type' parameter" do + before :each do + @klass = @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")) + end + + it "should raise an error if you pass an invalid type filter" do + @request.options[:type] = "i bet you don't have a type called this" + expect { + @terminus.search(@request) + }.to raise_error(ArgumentError, /Unrecognized type filter/) + + end + + it "should support filtering for only hostclass results" do + @request.options[:type] = "class" + + result = @terminus.search(@request) + result.should be_include(@klass) + result.should_not be_include(@node) + result.should_not be_include(@define) + end + + it "should support filtering for only node results" do + @request.options[:type] = "node" + + result = @terminus.search(@request) + result.should_not be_include(@klass) + result.should be_include(@node) + result.should_not be_include(@define) + end + + it "should support filtering for only definition results" do + @request.options[:type] = "defined_type" + + result = @terminus.search(@request) + result.should_not be_include(@klass) + result.should_not be_include(@node) + result.should be_include(@define) + end + end end - it "should use the request's environment's list of known resource types" do - @request.environment.known_resource_types.expects(:hostclasses).returns({}) - - @terminus.search(@request) - end - - 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 - - it "should return all known types" do - 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) + context "when the search string is not a wildcard" do + + it "should treat any search string 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 support type filtering with a regex" do + @request.key = "foo" + @request.options[:type] = "class" + + foobar = @krt.add(Puppet::Resource::Type.new(:hostclass, "foobar")) + foobaz = @krt.add(Puppet::Resource::Type.new(:hostclass, "foobaz")) + foobam = @krt.add(Puppet::Resource::Type.new(:definition, "foobam")) + fooball = @krt.add(Puppet::Resource::Type.new(:node, "fooball")) + + result = @terminus.search(@request) + result.should be_include(foobar) + result.should be_include(foobaz) + result.should_not be_include(foobam) + result.should_not be_include(fooball) + end + + it "should fail if a provided search string 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 end it "should not return the 'main' class" do main = @krt.add(Puppet::Resource::Type.new(:hostclass, "")) # So there is a return value foo = @krt.add(Puppet::Resource::Type.new(:hostclass, "foo")) @terminus.search(@request).should_not be_include(main) end it "should return nil if no types can be found" do @terminus.search(@request).should be_nil end it "should load all resource types from all search paths" do dir = tmpdir("searching_in_all") first = File.join(dir, "first") second = File.join(dir, "second") FileUtils.mkdir_p(first) FileUtils.mkdir_p(second) Puppet[:modulepath] = "#{first}#{File::PATH_SEPARATOR}#{second}" # Make a new request, since we've reset the env @request = Puppet::Indirector::Request.new(:resource_type, :search, "*", nil) onepath = File.join(first, "one", "manifests") FileUtils.mkdir_p(onepath) twopath = File.join(first, "two", "manifests") FileUtils.mkdir_p(twopath) File.open(File.join(onepath, "oneklass.pp"), "w") { |f| f.puts "class one::oneklass {}" } File.open(File.join(twopath, "twoklass.pp"), "w") { |f| f.puts "class two::twoklass {}" } result = @terminus.search(@request) result.find { |t| t.name == "one::oneklass" }.should be_instance_of(Puppet::Resource::Type) result.find { |t| t.name == "two::twoklass" }.should be_instance_of(Puppet::Resource::Type) end end end