summaryrefslogtreecommitdiff
path: root/lib/chef_zero/endpoints/nodes_endpoint.rb
blob: 513b14786dd9060630c3e25c7a666f135bc7cccd (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
require "ffi_yajl"
require_relative "rest_object_endpoint"
require_relative "../chef_data/data_normalizer"

module ChefZero
  module Endpoints
    # /nodes
    class NodesEndpoint < RestListEndpoint

      def post(request)
        # /nodes validation
        if request.rest_path.last == "nodes"
          data = parse_json(request.body)

          if data.key?("policy_name") && policy_name_invalid?(data["policy_name"])
            return error(400, "Field 'policy_name' invalid", pretty: false)
          end

          if data.key?("policy_group") && policy_name_invalid?(data["policy_group"])
            return error(400, "Field 'policy_group' invalid", pretty: false)
          end
        end

        super(request)
      end

      def populate_defaults(request, response_json)
        node = FFI_Yajl::Parser.parse(response_json)
        node = ChefData::DataNormalizer.normalize_node(node, request.rest_path[3])
        FFI_Yajl::Encoder.encode(node, pretty: true)
      end
    end
  end
end