summaryrefslogtreecommitdiff
path: root/lib/chef_zero/endpoints/cookbook_artifact_identifier_endpoint.rb
blob: 851d000eed1dbac59a28b5be59bb43800b971cfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
require_relative "../chef_data/data_normalizer"

module ChefZero
  module Endpoints
    class CookbookArtifactIdentifierEndpoint < ChefZero::Endpoints::CookbookVersionEndpoint
      # 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.

      # GET /organizations/ORG/cookbook_artifacts/NAME/IDENTIFIER
      def get(request)
        cookbook_data = normalize(request, get_data(request))
        json_response(200, cookbook_data)
      end

      # PUT /organizations/ORG/cookbook_artifacts/COOKBOOK/IDENTIFIER
      def put(request)
        if exists_data?(request)
          return error(409, "Cookbooks cannot be modified, and a cookbook with this identifier already exists.")
        end

        cb_data = normalize(request, request.body)
        set_data(request, nil, to_json(cb_data), :create_dir, :create)

        already_json_response(201, request.body)
      end

      # DELETE /organizations/ORG/cookbook_artifacts/COOKBOOK/IDENTIFIER
      def delete(request)
        doomed_cookbook_json = get_data(request)
        identified_cookbook_data = normalize(request, doomed_cookbook_json)
        delete_data(request)

        # go through the recipes and delete stuff in the file store.
        hoover_unused_checksums(get_checksums(doomed_cookbook_json), request)

        # if this was the last revision, delete the directory so future requests will 404, instead of
        # returning 200 with an empty list.
        # Last one out turns out the lights: delete /organizations/ORG/cookbooks/COOKBOOK if it no longer has versions
        cookbook_path = request.rest_path[0..3]
        if exists_data_dir?(request, cookbook_path) && list_data(request, cookbook_path).size == 0
          delete_data_dir(request, cookbook_path)
        end

        json_response(200, identified_cookbook_data)
      rescue RestErrorResponse => ex
        if ex.response_code == 404
          error(404, "not_found")
        else
          raise
        end
      end

      private

      def make_file_store_path(rest_path, recipe)
        rest_path.first(2) + ["file_store", "checksums", recipe["checksum"]]
      end

      def normalize(request, cookbook_artifact_data)
        cookbook = parse_json(cookbook_artifact_data)
        ChefData::DataNormalizer.normalize_cookbook(self, request.rest_path[0..1],
          cookbook, request.rest_path[3], request.rest_path[4],
          request.base_uri, request.method, true, api_version: request.api_version)
      end
    end
  end
end