chef-zero 0.9.4 → 0.9.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,7 +9,9 @@ module ChefZero
9
9
  container = get_data(request)
10
10
  contents = request.body
11
11
  name = JSON.parse(contents, :create_additions => false)[identity_key]
12
- if container[name]
12
+ if name.nil?
13
+ error(400, "Must specify '#{identity_key}' in JSON")
14
+ elsif container[name]
13
15
  error(409, "Object already exists")
14
16
  else
15
17
  container[name] = {}
@@ -1,8 +1,8 @@
1
1
  module ChefZero
2
2
  module Endpoints
3
3
  class NotFoundEndpoint
4
- def call(env)
5
- return [404, {"Content-Type" => "application/json"}, "Object not found: #{env['REQUEST_PATH']}"]
4
+ def call(request)
5
+ return [404, {"Content-Type" => "application/json"}, "Object not found: #{request.env['REQUEST_PATH']}"]
6
6
  end
7
7
  end
8
8
  end
@@ -25,7 +25,9 @@ module ChefZero
25
25
  container = get_data(request)
26
26
  contents = request.body
27
27
  key = get_key(contents)
28
- if container[key]
28
+ if key.nil?
29
+ error(400, "Must specify '#{identity_key}' in JSON")
30
+ elsif container[key]
29
31
  error(409, 'Object already exists')
30
32
  else
31
33
  container[key] = contents
@@ -14,20 +14,19 @@ module ChefZero
14
14
  server.data
15
15
  end
16
16
 
17
- def call(env)
18
- rest_path = env['PATH_INFO'].split('/').select { |part| part != "" }
19
- method = env['REQUEST_METHOD'].downcase.to_sym
17
+ def call(request)
18
+ method = request.method.downcase.to_sym
20
19
  if !self.respond_to?(method)
21
20
  accept_methods = [:get, :put, :post, :delete].select { |m| self.respond_to?(m) }
22
21
  accept_methods_str = accept_methods.map { |m| m.to_s.upcase }.join(', ')
23
- return [405, {"Content-Type" => "text/plain", "Allow" => accept_methods_str}, "Bad request method for '#{env['REQUEST_PATH']}': #{env['REQUEST_METHOD']}"]
22
+ return [405, {"Content-Type" => "text/plain", "Allow" => accept_methods_str}, "Bad request method for '#{request.env['REQUEST_PATH']}': #{request.env['REQUEST_METHOD']}"]
24
23
  end
25
- if json_only && !env['HTTP_ACCEPT'].split(';').include?('application/json')
24
+ if json_only && !request.env['HTTP_ACCEPT'].split(';').include?('application/json')
26
25
  return [406, {"Content-Type" => "text/plain"}, "Must accept application/json"]
27
26
  end
28
27
  # Dispatch to get()/post()/put()/delete()
29
28
  begin
30
- self.send(method, RestRequest.new(env))
29
+ self.send(method, request)
31
30
  rescue RestErrorResponse => e
32
31
  error(e.response_code, e.error)
33
32
  end
@@ -37,6 +37,20 @@ module ChefZero
37
37
  params
38
38
  end
39
39
  end
40
+
41
+ def to_s
42
+ result = "#{method} #{rest_path.join('/')}"
43
+ if query_params.size > 0
44
+ result << "?#{query_params.map { |k,v| "#{k}=#{v}" }.join('&') }"
45
+ end
46
+ if body.chomp != ''
47
+ result << "\n--- #{method} BODY ---\n"
48
+ result << body
49
+ result << "\n" if !body.end_with?("\n")
50
+ result << "--- END #{method} BODY ---"
51
+ end
52
+ result
53
+ end
40
54
  end
41
55
  end
42
56
 
@@ -1,7 +1,7 @@
1
1
  require 'chef/log'
2
2
 
3
3
  module ChefZero
4
- class Router
4
+ class RestRouter
5
5
  def initialize(routes)
6
6
  @routes = routes.map do |route, endpoint|
7
7
  pattern = Regexp.new("^#{route.gsub('*', '[^/]*')}$")
@@ -12,16 +12,16 @@ module ChefZero
12
12
  attr_reader :routes
13
13
  attr_accessor :not_found
14
14
 
15
- def call(env)
15
+ def call(request)
16
16
  begin
17
- Chef::Log.debug "#{env['REQUEST_METHOD']} #{env['PATH_INFO']}#{env['QUERY_STRING'] != '' ? "?" + env['QUERY_STRING'] : ''}"
18
- clean_path = "/" + env['PATH_INFO'].split('/').select { |part| part != "" }.join("/")
17
+ Chef::Log.debug "Request: #{request}"
18
+ clean_path = "/" + request.rest_path.join("/")
19
19
  routes.each do |route, endpoint|
20
20
  if route.match(clean_path)
21
- return endpoint.call(env)
21
+ return endpoint.call(request)
22
22
  end
23
23
  end
24
- not_found.call(env)
24
+ not_found.call(request)
25
25
  rescue
26
26
  Chef::Log.error("#{$!.inspect}\n#{$!.backtrace.join("\n")}")
27
27
  [500, {"Content-Type" => "text/plain"}, "Exception raised! #{$!.inspect}\n#{$!.backtrace.join("\n")}"]
@@ -1,6 +1,7 @@
1
1
  require 'thin'
2
2
  require 'tempfile'
3
3
  require 'chef_zero/server'
4
+ require 'chef_zero/rest_request'
4
5
  require 'chef/config'
5
6
 
6
7
  module ChefZero
@@ -17,6 +18,12 @@ module ChefZero
17
18
  def self.client_key=(value)
18
19
  @client_key = value
19
20
  end
21
+ def self.request_log
22
+ @request_log ||= []
23
+ end
24
+ def self.clear_request_log
25
+ @request_log = []
26
+ end
20
27
 
21
28
  def when_the_chef_server(description, *tags, &block)
22
29
  context "When the Chef server #{description}", *tags do
@@ -24,15 +31,19 @@ module ChefZero
24
31
  unless ChefZero::RSpec.server
25
32
  # Set up configuration so that clients will point to the server
26
33
  Thin::Logging.silent = true
27
- ChefZero::RSpec.server = ChefZero::Server.new(:port => 8889, :signals => false)
34
+ ChefZero::RSpec.server = ChefZero::Server.new(:port => 8889, :signals => false, :log_requests => true)
28
35
  ChefZero::RSpec.client_key = Tempfile.new(['chef_zero_client_key', '.pem'])
29
36
  ChefZero::RSpec.client_key.write(ChefZero::PRIVATE_KEY)
30
37
  ChefZero::RSpec.client_key.close
31
38
  # Start the server
32
39
  ChefZero::RSpec.server.start_background
40
+ ChefZero::RSpec.server.on_response do |request, response|
41
+ ChefZero::RSpec.request_log << [ request, response ]
42
+ end
33
43
  else
34
44
  ChefZero::RSpec.server.clear_data
35
45
  end
46
+ ChefZero::RSpec.clear_request_log
36
47
 
37
48
  @old_chef_server_url = Chef::Config.chef_server_url
38
49
  @old_node_name = Chef::Config.node_name
@@ -20,7 +20,7 @@ require 'rubygems'
20
20
  require 'thin'
21
21
  require 'openssl'
22
22
  require 'chef_zero'
23
- require 'chef_zero/router'
23
+ require 'chef_zero/rest_router'
24
24
  require 'timeout'
25
25
  require 'chef_zero/cookbook_data'
26
26
 
@@ -125,6 +125,14 @@ module ChefZero
125
125
  end
126
126
  end
127
127
 
128
+ def on_request(&block)
129
+ @on_request_proc = block
130
+ end
131
+
132
+ def on_response(&block)
133
+ @on_response_proc = block
134
+ end
135
+
128
136
  # Load data in a nice, friendly form:
129
137
  # {
130
138
  # 'roles' => {
@@ -205,7 +213,7 @@ module ChefZero
205
213
  private
206
214
 
207
215
  def make_app
208
- router = Router.new([
216
+ router = RestRouter.new([
209
217
  [ '/authenticate_user', AuthenticateUserEndpoint.new(self) ],
210
218
  [ '/clients', ActorsEndpoint.new(self) ],
211
219
  [ '/clients/*', ActorEndpoint.new(self) ],
@@ -240,7 +248,18 @@ module ChefZero
240
248
  [ '/file_store/*', FileStoreFileEndpoint.new(self) ],
241
249
  ])
242
250
  router.not_found = NotFoundEndpoint.new
243
- router
251
+
252
+ return proc do |env|
253
+ request = RestRequest.new(env)
254
+ if @on_request_proc
255
+ @on_request_proc.call(request)
256
+ end
257
+ response = router.call(request)
258
+ if @on_response_proc
259
+ @on_response_proc.call(request, response)
260
+ end
261
+ response
262
+ end
244
263
  end
245
264
 
246
265
  def dejsonize_children(hash)
@@ -1,3 +1,3 @@
1
1
  module ChefZero
2
- VERSION = '0.9.4'
2
+ VERSION = '0.9.5'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chef-zero
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.4
4
+ version: 0.9.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-18 00:00:00.000000000 Z
12
+ date: 2013-01-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: chef
@@ -90,7 +90,7 @@ files:
90
90
  - lib/chef_zero/rest_base.rb
91
91
  - lib/chef_zero/rest_error_response.rb
92
92
  - lib/chef_zero/rest_request.rb
93
- - lib/chef_zero/router.rb
93
+ - lib/chef_zero/rest_router.rb
94
94
  - lib/chef_zero/rspec.rb
95
95
  - lib/chef_zero/server.rb
96
96
  - lib/chef_zero/solr/query/binary_operator.rb