Class: ChefZero::Endpoints::RestObjectEndpoint

Inherits:
RestBase
  • Object
show all
Defined in:
lib/chef_zero/endpoints/rest_object_endpoint.rb

Overview

Typical REST leaf endpoint (/roles/NAME or /data/BAG/NAME)

Constant Summary

Constants inherited from RestBase

RestBase::DEFAULT_REQUEST_VERSION, RestBase::DEFAULT_RESPONSE_VERSION

Instance Attribute Summary collapse

Attributes inherited from RestBase

#server

Instance Method Summary collapse

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, identity_keys = [ "name" ]) ⇒ RestObjectEndpoint

Returns a new instance of RestObjectEndpoint.



9
10
11
12
13
# File 'lib/chef_zero/endpoints/rest_object_endpoint.rb', line 9

def initialize(server, identity_keys = [ "name" ])
  super(server)
  identity_keys = [ identity_keys ] if identity_keys.is_a?(String)
  @identity_keys = identity_keys
end

Instance Attribute Details

#identity_keysObject (readonly)

Returns the value of attribute identity_keys.



15
16
17
# File 'lib/chef_zero/endpoints/rest_object_endpoint.rb', line 15

def identity_keys
  @identity_keys
end

Instance Method Details

#delete(request) ⇒ Object



42
43
44
45
46
# File 'lib/chef_zero/endpoints/rest_object_endpoint.rb', line 42

def delete(request)
  result = get_data(request)
  delete_data(request)
  already_json_response(200, populate_defaults(request, result))
end

#get(request) ⇒ Object



17
18
19
# File 'lib/chef_zero/endpoints/rest_object_endpoint.rb', line 17

def get(request)
  already_json_response(200, populate_defaults(request, get_data(request)))
end

#patch_request_body(request) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/chef_zero/endpoints/rest_object_endpoint.rb', line 48

def patch_request_body(request)
  existing_value = get_data(request, nil, :nil)
  if existing_value
    request_json = FFI_Yajl::Parser.parse(request.body)
    existing_json = FFI_Yajl::Parser.parse(existing_value)
    merged_json = existing_json.merge(request_json)
    if merged_json.size > request_json.size
      return FFI_Yajl::Encoder.encode(merged_json, pretty: true)
    end
  end

  request.body
end

#put(request) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/chef_zero/endpoints/rest_object_endpoint.rb', line 21

def put(request)
  # We grab the old body to trigger a 404 if it doesn't exist
  get_data(request)

  # If it's a rename, check for conflict and delete the old value
  if is_rename?(request)
    key = identity_key_value(request)

    begin
      create_data(request, request.rest_path[0..-2], key, request.body, :data_store_exceptions)
    rescue DataStore::DataAlreadyExistsError
      return error(409, "Cannot rename '#{request.rest_path[-1]}' to '#{key}': '#{key}' already exists")
    end
    delete_data(request)
    already_json_response(201, populate_defaults(request, request.body))
  else
    set_data(request, request.rest_path, request.body)
    already_json_response(200, populate_defaults(request, request.body))
  end
end