Class: ChefZero::Endpoints::ActorsEndpoint

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

Overview

/users, /organizations/ORG/clients or /organizations/ORG/users

Instance Attribute Summary

Attributes inherited from RestListEndpoint

#identity_keys

Attributes inherited from RestBase

#server

Instance Method Summary collapse

Methods inherited from RestListEndpoint

#get_key, #initialize

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, #populate_defaults, #set_data, #text_response

Constructor Details

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

Instance Method Details

#get(request) ⇒ Object


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/chef_zero/endpoints/actors_endpoint.rb', line 8

def get(request)
  response = super(request)

  if request.query_params['email']
    results = FFI_Yajl::Parser.parse(response[2], :create_additions => false)
    new_results = {}
    results.each do |name, url|
      record = get_data(request, request.rest_path + [ name ], :nil)
      if record
        record = FFI_Yajl::Parser.parse(record, :create_additions => false)
        new_results[name] = url if record['email'] == request.query_params['email']
      end
    end
    response[2] = FFI_Yajl::Encoder.encode(new_results, :pretty => true)
  end

  if request.query_params['verbose']
    results = FFI_Yajl::Parser.parse(response[2], :create_additions => false)
    results.each do |name, url|
      record = get_data(request, request.rest_path + [ name ], :nil)
      if record
        record = FFI_Yajl::Parser.parse(record, :create_additions => false)
        record = ChefData::DataNormalizer.normalize_user(record, name, identity_keys, server.options[:osc_compat])
        results[name] = record
      end
    end
    response[2] = FFI_Yajl::Encoder.encode(results, :pretty => true)
  end
  response
end

#post(request) ⇒ Object


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/chef_zero/endpoints/actors_endpoint.rb', line 39

def post(request)
  # First, find out if the user actually posted a public key.  If not, make
  # one.
  request_body = FFI_Yajl::Parser.parse(request.body, :create_additions => false)
  public_key = request_body['public_key']
  if !public_key
    private_key, public_key = server.gen_key_pair
    request_body['public_key'] = public_key
    request.body = FFI_Yajl::Encoder.encode(request_body, :pretty => true)
  end

  result = super(request)

  if result[0] == 201
    # If we generated a key, stuff it in the response.
    response = FFI_Yajl::Parser.parse(result[2], :create_additions => false)
    response['private_key'] = private_key if private_key
    response['public_key'] = public_key unless request.rest_path[0] == 'users'
    json_response(201, response)
  else
    result
  end
end