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

Constant Summary

Constants inherited from RestBase

RestBase::DEFAULT_REQUEST_VERSION, RestBase::DEFAULT_RESPONSE_VERSION

Instance Attribute Summary

Attributes inherited from RestObjectEndpoint

#identity_keys

Attributes inherited from RestBase

#server

Instance Method Summary collapse

Methods inherited from RestObjectEndpoint

#initialize, #patch_request_body

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, #initialize, #json_only, #json_response, #list_data, #list_data_or_else, #parse_json, #policy_name_invalid?, rfc2396_parser, #set_data, #text_response, #to_json

Constructor Details

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

Instance Method Details

#delete(request) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/chef_zero/endpoints/actor_endpoint.rb', line 21

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

  delete_actor_keys!(request)
  result
end

#get(request) ⇒ Object



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

def get(request)
  result = super
  user_data = parse_json(result[2])

  user_data.delete("public_key") unless request.api_v0?

  json_response(200, user_data)
end

#populate_defaults(request, response_json) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/chef_zero/endpoints/actor_endpoint.rb', line 99

def populate_defaults(request, response_json)
  response = parse_json(response_json)

  populated_response =
    if client?(request)
      ChefData::DataNormalizer.normalize_client(
        response,
        response["name"] || request.rest_path[-1],
        request.rest_path[1]
      )
    else
      ChefData::DataNormalizer.normalize_user(
        response,
        response["username"] || request.rest_path[-1],
        identity_keys,
        server.options[:osc_compat],
        request.method
      )
    end

  to_json(populated_response)
end

#put(request) ⇒ Object



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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/chef_zero/endpoints/actor_endpoint.rb', line 37

def put(request)
  # Find out if we're updating the public key.
  request_body = FFI_Yajl::Parser.parse(request.body)

  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.key?("private_key")
    body_modified = true

    if request_body.delete("private_key")
      private_key, public_key = server.gen_key_pair
      updating_public_key = true
      request_body["public_key"] = public_key
    end
  end

  # Put modified body back in `request.body`
  request.body = to_json(request_body) 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
    client_or_user_name = identity_key_value(request) || request.rest_path[-1]

    if is_rename?(request)
      rename_keys!(request, client_or_user_name)
    end

    if request.rest_path[0] == "users"
      response = {
        "uri" => build_uri(request.base_uri, [ "users", client_or_user_name ]),
      }
    else
      response = parse_json(result[2])
    end

    if client?(request)
      response["private_key"] = private_key ? private_key : false
    else
      response["private_key"] = private_key if private_key
      response.delete("public_key") unless updating_public_key
    end

    response.delete("password")

    json_response(result[0], response)
  else
    result
  end
end