Class: ChefZero::Endpoints::DataBagEndpoint

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

Overview

/data/NAME

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, #create_data, #create_data_dir, #data_store, #delete_data, #delete_data_dir, #error, #exists_data?, #exists_data_dir?, #get_data, #json_only, #json_response, #list_data, #populate_defaults, #set_data, #text_response

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, :create_additions => false)
  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, :create_additions => false)
  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