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)

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, #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, 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


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

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


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

def patch_request_body(request)
  existing_value = get_data(request, nil, :nil)
  if existing_value
    request_json = FFI_Yajl::Parser.parse(request.body, :create_additions => false)
    existing_json = FFI_Yajl::Parser.parse(existing_value, :create_additions => false)
    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
41
# 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
  old_body = get_data(request)
  request_json = FFI_Yajl::Parser.parse(request.body, :create_additions => false)
  key = identity_keys.map { |k| request_json[k] }.select { |v| v }.first
  key ||= request.rest_path[-1]
  # If it's a rename, check for conflict and delete the old value
  rename = key != request.rest_path[-1]
  if rename
    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