diff options
author | Chris Doherty <cdoherty@chef.io> | 2015-12-09 17:10:23 -0800 |
---|---|---|
committer | Chris Doherty <cdoherty@chef.io> | 2015-12-09 17:11:56 -0800 |
commit | 864c1ad03f9967f6e26226807cb9b1298183b407 (patch) | |
tree | 2ff2778c50303a5eda20279c47dd11091724642b | |
parent | 5a75e015ef1b589b1645f84cfc4e7af1214c3b5e (diff) | |
download | chef-zero-864c1ad03f9967f6e26226807cb9b1298183b407.tar.gz |
Streamline code and sync up with the similar (but not identical) /cookbooks endpoint.
-rw-r--r-- | lib/chef_zero/chef_data/data_normalizer.rb | 4 | ||||
-rw-r--r-- | lib/chef_zero/endpoints/cookbook_artifacts_cookbook_identifier.rb | 42 |
2 files changed, 20 insertions, 26 deletions
diff --git a/lib/chef_zero/chef_data/data_normalizer.rb b/lib/chef_zero/chef_data/data_normalizer.rb index 4a5ee46..c58d7d5 100644 --- a/lib/chef_zero/chef_data/data_normalizer.rb +++ b/lib/chef_zero/chef_data/data_normalizer.rb @@ -93,9 +93,8 @@ module ChefZero end end cookbook['name'] ||= "#{name}-#{version}" - # TODO this feels wrong, but the real chef server doesn't expand 'version' + # TODO this feels wrong, but the real chef server doesn't expand 'version', so we don't either. - cookbook['cookbook_name'] ||= name cookbook['frozen?'] ||= false cookbook['metadata'] ||= {} cookbook['metadata']['version'] ||= version @@ -112,6 +111,7 @@ module ChefZero if is_cookbook_artifact cookbook.delete('json_class') else + cookbook['cookbook_name'] ||= name cookbook['json_class'] ||= 'Chef::CookbookVersion' end diff --git a/lib/chef_zero/endpoints/cookbook_artifacts_cookbook_identifier.rb b/lib/chef_zero/endpoints/cookbook_artifacts_cookbook_identifier.rb index 50605ef..c89218a 100644 --- a/lib/chef_zero/endpoints/cookbook_artifacts_cookbook_identifier.rb +++ b/lib/chef_zero/endpoints/cookbook_artifacts_cookbook_identifier.rb @@ -3,13 +3,14 @@ require 'chef_zero/chef_data/data_normalizer' module ChefZero module Endpoints class CookbookArtifactsCookbookIdentifierEndpoint < RestBase - # GET /organizations/ORG/cookbook_artifacts/COOKBOOK/IDENTIFIER - def get(request) - cookbook_name, identifier = request.rest_path.last(2) - - data = get_data(request) + # these endpoints are almost, but not quite, not entirely unlike the corresponding /cookbooks endpoints. + # it could all be refactored for maximum reuse, but they're short REST methods with well-defined + # behavioral specs (pedant), so there's not a huge benefit. - return already_json_response(200, data) + # GET /organizations/ORG/cookbook_artifacts/NAME/IDENTIFIER + def get(request) + cookbook_data = normalize(request, parse_json(get_data(request))) + return json_response(200, cookbook_data) end # PUT /organizations/ORG/cookbook_artifacts/COOKBOOK/IDENTIFIER @@ -18,35 +19,28 @@ module ChefZero return error(409, "Cookbooks cannot be modified, and a cookbook with this identifier already exists.") end - # pedant dictates that we not return the "json_class" element, so let's not store it. - # XXX we have to store this and strip it on the way out with normalize_cookbook - identified_cookbook_data = parse_json(request.body).tap { |o| o.delete("json_class") } - - # we want to build these ourselves, but don't overwrite any from the client. since these would - # otherwise be Bookshelf URLs, I think we're fine. - identified_cookbook_data["recipes"].each do |r| - r["url"] ||= build_uri(request.base_uri, make_file_store_path(request.rest_path, r)) - end + set_data(request, nil, request.body, :create_dir) - identified_cookbook_json = to_json(identified_cookbook_data) - - set_data(request, nil, identified_cookbook_json, :create_dir) - - return already_json_response(201, identified_cookbook_json) + return already_json_response(201, request.body) end def make_file_store_path(rest_path, recipe) rest_path.first(2) + ["file_store", "checksums", recipe["checksum"]] end + def normalize(request, cookbook_artifact_data) + ChefData::DataNormalizer.normalize_cookbook(self, request.rest_path[0..1], + cookbook_artifact_data, request.rest_path[3], request.rest_path[4], + request.base_uri, request.method, true) + end + # DELETE /organizations/ORG/cookbook_artifacts/COOKBOOK/IDENTIFIER def delete(request) begin - identified_cookbook_json = get_data(request) + identified_cookbook_data = normalize(request, parse_json(get_data(request))) delete_data(request) # go through the recipes and delete stuff in the file store. - identified_cookbook_data = parse_json(identified_cookbook_json) identified_cookbook_data["recipes"].each do |r| file_path = make_file_store_path(request.rest_path, r) delete_data(request, file_path) @@ -58,8 +52,8 @@ module ChefZero delete_data_dir(request, artifact_path) end - already_json_response(200, identified_cookbook_json) - rescue StandardError => ex + json_response(200, identified_cookbook_data) + rescue RestErrorResponse => ex if ex.response_code == 404 error(404, "not_found") else |