Class: ChefZero::Endpoints::OrganizationsEndpoint

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

Overview

/organizations

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, #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::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
# File 'lib/chef_zero/endpoints/organizations_endpoint.rb', line 17

def post(request)
  contents = FFI_Yajl::Parser.parse(request.body, :create_additions => false)
  name = contents['name']
  if name.nil?
    error(400, "Must specify '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.gsub('-', ''),
      "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)}",
      "clientname" => validator_name,
      "private_key" => private_key
    })
  end
end