summaryrefslogtreecommitdiff
path: root/lib/chef_zero/rest_router.rb
diff options
context:
space:
mode:
authorJordan Running <jr@getchef.com>2016-02-11 13:06:56 -0600
committerJordan Running <jr@getchef.com>2016-02-24 13:35:19 -0600
commit2469894eab12f24893916b571a981e082dfe97df (patch)
treebc657d22f7f41dad46a68f9825189c2dd20053b9 /lib/chef_zero/rest_router.rb
parent86e99a48cc39a0b5c931c29fbfef9e196252c9c2 (diff)
downloadchef-zero-2469894eab12f24893916b571a981e082dfe97df.tar.gz
Make user and client keys endpoints pass Pedant specs
- Implement ActorKeyEndpoint, ActorKeysEndpoint. - Implement user, client keys in `ActorEndpoint#delete`, `#put`. - RestBase - Fix RestErrorResponse exceptions to report actual `rest_path` instead associated with the failed data store operation instead of `request.rest_path`. - Move `json_response`, `already_json_response` args `request_version` and `response_version` into options hash; add docs. - DataError, RestErrorResponse: Pass useful message text to `super`. - RestRouter: Clean up logging - Print request methods, paths and bodies more readably for log_level >= INFO. - Pretty-print RestRequest objects (only printed when log_level == DEBUG). - Server: Change default log_level to `:warn` (to enable logging cleanup above). - `Rakefile`, `spec/run_oc_pedant.rb` - Consume RSpec, Pedant options from `ENV['RSPEC_OPTS']`, `ENV['PEDANT_OPTS']` (see `rake -D`). - Consume `ENV['LOG_LEVEL'` (see `rake -D`). - Clean up ChefZero::Server default opts and move duplicated logic to `start_chef_server` method.
Diffstat (limited to 'lib/chef_zero/rest_router.rb')
-rw-r--r--lib/chef_zero/rest_router.rb61
1 files changed, 44 insertions, 17 deletions
diff --git a/lib/chef_zero/rest_router.rb b/lib/chef_zero/rest_router.rb
index f2770d3..889c810 100644
--- a/lib/chef_zero/rest_router.rb
+++ b/lib/chef_zero/rest_router.rb
@@ -1,3 +1,5 @@
+require 'pp'
+
module ChefZero
class RestRouter
def initialize(routes)
@@ -15,24 +17,18 @@ module ChefZero
attr_accessor :not_found
def call(request)
- begin
- ChefZero::Log.debug(request)
- ChefZero::Log.debug(request.body) if request.body
-
- clean_path = "/" + request.rest_path.join("/")
-
- response = find_endpoint(clean_path).call(request)
- ChefZero::Log.debug([
- "",
- "--- RESPONSE (#{response[0]}) ---",
- response[2],
- "--- END RESPONSE ---",
- ].join("\n"))
- return response
- rescue
- ChefZero::Log.error("#{$!.inspect}\n#{$!.backtrace.join("\n")}")
- [500, {"Content-Type" => "text/plain"}, "Exception raised! #{$!.inspect}\n#{$!.backtrace.join("\n")}"]
+ log_request(request)
+
+ clean_path = "/" + request.rest_path.join("/")
+
+ find_endpoint(clean_path).call(request).tap do |response|
+ log_response(response)
end
+ rescue => ex
+ exception = "#{ex.inspect}\n#{ex.backtrace.join("\n")}"
+
+ ChefZero::Log.error(exception)
+ [ 500, { "Content-Type" => "text/plain" }, "Exception raised! #{exception}" ]
end
private
@@ -41,5 +37,36 @@ module ChefZero
_, endpoint = routes.find { |route, endpoint| route.match(clean_path) }
endpoint || not_found
end
+
+ def log_request(request)
+ ChefZero::Log.info do
+ "#{request.method} /#{request.rest_path.join("/")}".tap do |msg|
+ next unless request.method =~ /^(POST|PUT)$/
+
+ if request.body.nil? || request.body.empty?
+ msg << " (no body)"
+ else
+ msg << [
+ "",
+ "--- #{request.method} BODY ---",
+ request.body.chomp,
+ "--- END #{request.method} BODY ---"
+ ].join("\n")
+ end
+ end
+ end
+
+ ChefZero::Log.debug { request.pretty_inspect }
+ end
+
+ def log_response(response)
+ ChefZero::Log.info {
+ [ "",
+ "--- RESPONSE (#{response[0]}) ---",
+ response[2].chomp,
+ "--- END RESPONSE ---",
+ ].join("\n")
+ }
+ end
end
end