Class: ChefZero::Endpoints::DataBagEndpoint

Inherits:
RestListEndpoint show all
Defined in:
lib/chef_zero/endpoints/data_bag_endpoint.rb

Overview

/data/NAME

Constant Summary

Constants inherited from RestBase

RestBase::DEFAULT_REQUEST_VERSION, RestBase::DEFAULT_RESPONSE_VERSION

Instance Attribute Summary

Attributes inherited from RestListEndpoint

#identity_keys

Attributes inherited from RestBase

#server

Instance Method Summary collapse

Methods inherited from RestListEndpoint

#get

Methods inherited from RestBase

#accepts?, #already_json_response, build_uri, #build_uri, #call, #check_api_version, #create_data, #create_data_dir, #data_store, #delete_data, #delete_data_dir, #error, #exists_data?, #exists_data_dir?, #get_data, #get_data_or_else, #hashify_list, #head_request, #json_only, #json_response, #list_data, #list_data_or_else, #parse_json, #policy_name_invalid?, #populate_defaults, rfc2396_parser, #set_data, #text_response, #to_json

Constructor Details

#initialize(server) ⇒ DataBagEndpoint

Returns a new instance of DataBagEndpoint.



10
11
12
# File 'lib/chef_zero/endpoints/data_bag_endpoint.rb', line 10

def initialize(server)
  super(server, "id")
end

Instance Method Details

#delete(request) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/chef_zero/endpoints/data_bag_endpoint.rb', line 34

def delete(request)
  key = request.rest_path[3]
  delete_data_dir(request, request.rest_path, :recursive)
  json_response(200, {
    "chef_type" => "data_bag",
    "json_class" => "Chef::DataBag",
    "name" => key,
  })
end

#get_key(contents) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/chef_zero/endpoints/data_bag_endpoint.rb', line 25

def get_key(contents)
  data_bag_item = FFI_Yajl::Parser.parse(contents)
  if data_bag_item["json_class"] == "Chef::DataBagItem" && data_bag_item["raw_data"]
    data_bag_item["raw_data"]["id"]
  else
    data_bag_item["id"]
  end
end

#post(request) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/chef_zero/endpoints/data_bag_endpoint.rb', line 14

def post(request)
  json = FFI_Yajl::Parser.parse(request.body)
  key = identity_keys.map { |k| json[k] }.select { |v| v }.first
  response = super(request)
  if response[0] == 201
    already_json_response(201, DataBagItemEndpoint.populate_defaults(request, request.body, request.rest_path[3], key))
  else
    response
  end
end