From ff6c5d36be4f84bf284bcdb5470323b167d150a4 Mon Sep 17 00:00:00 2001 From: danielsdeleo Date: Wed, 28 Jan 2015 17:55:36 -0800 Subject: Check before create/update to determine correct status code The memore store and ChefFS store have different behaviors when writing to an existing object, so work around that by checking for the existence of the item before creating/updating. --- lib/chef_zero/endpoints/policies_endpoint.rb | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/chef_zero/endpoints/policies_endpoint.rb b/lib/chef_zero/endpoints/policies_endpoint.rb index c115398..992d5ae 100644 --- a/lib/chef_zero/endpoints/policies_endpoint.rb +++ b/lib/chef_zero/endpoints/policies_endpoint.rb @@ -19,7 +19,15 @@ module ChefZero error = validate(request) return error if error - code = create_or_update(request) + code = + if data_store.exists?(request.rest_path) + set_data(request, request.rest_path, request.body, :data_store_exceptions) + 200 + else + name = request.rest_path[4] + data_store.create(request.rest_path[0..3], name, request.body, :create_dir) + 201 + end already_json_response(code, request.body) end @@ -29,15 +37,6 @@ module ChefZero already_json_response(200, result) end - def create_or_update(request) - set_data(request, request.rest_path, request.body, :data_store_exceptions) - 200 - rescue ChefZero::DataStore::DataNotFoundError - name = request.rest_path[4] - data_store.create(request.rest_path[0..3], name, request.body, :create_dir) - 201 - end - private def validate(request) -- cgit v1.2.1