Class: ChefZero::Endpoints::OrganizationsEndpoint

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

Overview

/organizations

Constant Summary

Constants inherited from RestBase

RestBase::DEFAULT_REQUEST_VERSION, RestBase::DEFAULT_RESPONSE_VERSION

Instance Attribute Summary

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

This class inherits a constructor from ChefZero::RestBase

Instance Method Details

#get(request) ⇒ Object



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

def get(request)
  result = {}
  data_store.list(request.rest_path).each do |name|
    result[name] = build_uri(request.base_uri, request.rest_path + [name])
  end
  json_response(200, result)
end

#post(request) ⇒ Object



17
18
19
20
21
22
23
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
# File 'lib/chef_zero/endpoints/organizations_endpoint.rb', line 17

def post(request)
  contents = FFI_Yajl::Parser.parse(request.body)
  name = contents["name"]
  full_name = contents["full_name"]
  if name.nil?
    error(400, "Must specify 'name' in JSON")
  elsif full_name.nil?
    error(400, "Must specify 'full_name' in JSON")
  elsif exists_data_dir?(request, request.rest_path + [ name ])
    error(409, "Organization already exists")
  else
    create_data_dir(request, request.rest_path, name, requestor: request.requestor)

    org = {
      "guid" => UUIDTools::UUID.random_create.to_s.delete("-"),
      "assigned_at" => Time.now.to_s,
    }.merge(contents)
    org_path = request.rest_path + [ name ]
    set_data(request, org_path + [ "org" ], FFI_Yajl::Encoder.encode(org, pretty: true))

    if server.generate_real_keys?
      # Create the validator client
      validator_name = "#{name}-validator"
      validator_path = org_path + [ "clients", validator_name ]
      private_key, public_key = server.gen_key_pair
      validator = FFI_Yajl::Encoder.encode({
        "validator" => true,
        "public_key" => public_key,
      }, pretty: true)
      set_data(request, validator_path, validator)
    end

    json_response(201, {
      "uri" => (build_uri(request.base_uri, org_path)).to_s,
      "name" => name,
      "org_type" => org["org_type"],
      "full_name" => full_name,
      "clientname" => validator_name,
      "private_key" => private_key,
    })
  end
end