Class: ChefZero::Endpoints::ActorEndpoint

Inherits:
RestObjectEndpoint show all
Defined in:
lib/chef_zero/endpoints/actor_endpoint.rb

Overview

/organizations/ORG/clients/NAME /organizations/ORG/users/NAME /users/NAME

Instance Attribute Summary

Attributes inherited from RestObjectEndpoint

#identity_keys

Attributes inherited from RestBase

#server

Instance Method Summary collapse

Methods inherited from RestObjectEndpoint

#get, #initialize, #patch_request_body

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, #initialize, #json_only, #json_response, #list_data, #set_data, #text_response

Constructor Details

This class inherits a constructor from ChefZero::Endpoints::RestObjectEndpoint

Instance Method Details

#delete(request) ⇒ Object


11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/chef_zero/endpoints/actor_endpoint.rb', line 11

def delete(request)
  result = super
  if request.rest_path[0] == 'users'
    list_data(request, [ 'organizations' ]).each do |org|
      begin
        delete_data(request, [ 'organizations', org, 'users', request.rest_path[1] ], :data_store_exceptions)
      rescue DataStore::DataNotFoundError
      end
    end
  end
  result
end

#populate_defaults(request, response_json) ⇒ Object


77
78
79
80
81
82
83
84
85
# File 'lib/chef_zero/endpoints/actor_endpoint.rb', line 77

def populate_defaults(request, response_json)
  response = FFI_Yajl::Parser.parse(response_json, :create_additions => false)
  if request.rest_path[2] == 'clients'
    response = ChefData::DataNormalizer.normalize_client(response, request.rest_path[3])
  else
    response = ChefData::DataNormalizer.normalize_user(response, request.rest_path[3], identity_keys, server.options[:osc_compat], request.method)
  end
  FFI_Yajl::Encoder.encode(response, :pretty => true)
end

#put(request) ⇒ Object


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/chef_zero/endpoints/actor_endpoint.rb', line 24

def put(request)
  # Find out if we're updating the public key.
  request_body = FFI_Yajl::Parser.parse(request.body, :create_additions => false)
  if request_body['public_key'].nil?
    # If public_key is null, then don't overwrite it.  Weird patchiness.
    body_modified = true
    request_body.delete('public_key')
  else
    updating_public_key = true
  end

  # Generate private_key if requested.
  if request_body.has_key?('private_key')
    body_modified = true
    if request_body['private_key']
      private_key, public_key = server.gen_key_pair
      updating_public_key = true
      request_body['public_key'] = public_key
    end
    request_body.delete('private_key')
  end

  # Save request
  request.body = FFI_Yajl::Encoder.encode(request_body, :pretty => true) if body_modified

  # PUT /clients is patchy
  request.body = patch_request_body(request)

  result = super(request)

  # Inject private_key into response, delete public_key/password if applicable
  if result[0] == 200 || result[0] == 201
    if request.rest_path[0] == 'users'
      key = nil
      identity_keys.each do |identity_key|
        key ||= request_body[identity_key]
      end
      key ||= request.rest_path[-1]
      response = {
        'uri' => build_uri(request.base_uri, [ 'users', key ])
      }
    else
      response = FFI_Yajl::Parser.parse(result[2], :create_additions => false)
    end
    response['private_key'] = private_key if private_key
    response.delete('public_key') if !updating_public_key && request.rest_path[2] == 'users'
    response.delete('password')
    json_response(result[0], response)
  else
    result
  end
end