summaryrefslogtreecommitdiff
path: root/lib/chef_zero/rest_base.rb
diff options
context:
space:
mode:
authorJohn Keiser <jkeiser@opscode.com>2013-05-23 13:40:35 -0700
committerJohn Keiser <jkeiser@opscode.com>2013-05-23 13:40:35 -0700
commitbc2fa1e839adc2bc49813df926b7ab841af3953d (patch)
tree163fe8a2f20dbebbac4e3d34a44e20f34d15f44d /lib/chef_zero/rest_base.rb
parent4d70bcfed51c619a2a62b257b76db5ac5a6133f2 (diff)
downloadchef-zero-bc2fa1e839adc2bc49813df926b7ab841af3953d.tar.gz
Make DataStore interface to allow data store to be replaced
Diffstat (limited to 'lib/chef_zero/rest_base.rb')
-rw-r--r--lib/chef_zero/rest_base.rb60
1 files changed, 51 insertions, 9 deletions
diff --git a/lib/chef_zero/rest_base.rb b/lib/chef_zero/rest_base.rb
index f219ff1..5371a0a 100644
--- a/lib/chef_zero/rest_base.rb
+++ b/lib/chef_zero/rest_base.rb
@@ -1,5 +1,6 @@
require 'chef_zero/rest_request'
require 'chef_zero/rest_error_response'
+require 'chef_zero/data_store/data_not_found_error'
module ChefZero
class RestBase
@@ -9,8 +10,8 @@ module ChefZero
attr_reader :server
- def data
- server.data
+ def data_store
+ server.data_store
end
def call(request)
@@ -35,17 +36,58 @@ module ChefZero
true
end
- def get_data(request, rest_path=nil)
+ def get_data(request, rest_path=nil, *options)
rest_path ||= request.rest_path
- # Grab the value we're looking for
- value = data
- rest_path.each do |path_part|
- if !value.has_key?(path_part)
+ begin
+ data_store.get(rest_path)
+ rescue DataStore::DataNotFoundError
+ if options.include?(:nil)
+ nil
+ else
raise RestErrorResponse.new(404, "Object not found: #{build_uri(request.base_uri, rest_path)}")
end
- value = value[path_part]
end
- value
+ end
+
+ def list_data(request, rest_path=nil)
+ rest_path ||= request.rest_path
+ begin
+ data_store.list(rest_path)
+ rescue DataStore::DataNotFoundError
+ raise RestErrorResponse.new(404, "Object not found: #{build_uri(request.base_uri, rest_path)}")
+ end
+ end
+
+ def delete_data(request, rest_path=nil)
+ rest_path ||= request.rest_path
+ begin
+ data_store.delete(rest_path)
+ rescue DataStore::DataNotFoundError
+ raise RestErrorResponse.new(404, "Object not found: #{build_uri(request.base_uri, request.rest_path)}")
+ end
+ end
+
+ def set_data(request, rest_path, data, *options)
+ rest_path ||= request.rest_path
+ begin
+ data_store.set(rest_path, request.body, *options)
+ rescue DataStore::DataNotFoundError
+ raise RestErrorResponse.new(404, "Object not found: #{build_uri(request.base_uri, request.rest_path)}")
+ end
+ end
+
+ def create_data(request, rest_path, name, data, *options)
+ rest_path ||= request.rest_path
+ begin
+ data_store.create(rest_path, name, data, *options)
+ rescue DataStore::DataAlreadyExistsError
+ raise RestErrorResponse.new(409, "Object already exists: #{build_uri(request.base_uri, request.rest_path + [name])}")
+ end
+ end
+
+ def exists_data?(request, rest_path=nil)
+ rest_path ||= request.rest_path
+ data_store.exists?(rest_path)
end
def error(response_code, error)