-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclients.rb
More file actions
91 lines (73 loc) · 2.39 KB
/
Copy pathclients.rb
File metadata and controls
91 lines (73 loc) · 2.39 KB
1
2
3
4
5
6
7
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
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
require 'chef/api_client'
class ChefServerApi::Clients < ChefServerApi::Application
provides :json
before :authenticate_every
before :is_admin, :only => :index
before :is_correct_node, :only => [ :show, :create, :update, :destroy ]
# GET /clients
def index
@list = Chef::ApiClient.list(true)
display(@list.inject({}) { |result, element| result[element.name] = absolute_slice_url(:client, :id => element.name); result })
end
# GET /clients/:id
def show
begin
@client = Chef::ApiClient.load(params[:id])
rescue Chef::Exceptions::CouchDBNotFound => e
raise NotFound, "Cannot load client #{params[:id]}"
end
display({ :name => @client.name, :admin => @client.admin })
end
# POST /clients
def create
exists = true
if params.has_key?(:inflated_object)
params[:name] ||= params[:inflated_object].name
params[:admin] ||= params[:inflated_object].admin
end
begin
Chef::ApiClient.load(params[:name])
rescue Chef::Exceptions::CouchDBNotFound
exists = false
end
raise Forbidden, "Client already exists" if exists
@client = Chef::ApiClient.new
@client.name(params[:name])
@client.admin(params[:admin]) if params[:admin]
@client.create_keys
@client.save
self.status = 201
headers['Location'] = absolute_slice_url(:client, @client.name)
display({ :uri => absolute_slice_url(:client, @client.name), :private_key => @client.private_key })
end
# PUT /clients/:id
def update
if params.has_key?(:inflated_object)
params[:private_key] ||= params[:inflated_object].private_key
params[:admin] ||= params[:inflated_object].admin
end
begin
@client = Chef::ApiClient.load(params[:id])
rescue Chef::Exceptions::CouchDBNotFound => e
raise NotFound, "Cannot load client #{params[:id]}"
end
@client.admin(params[:admin]) if params[:admin]
results = { :name => @client.name, :admin => @client.admin }
if params[:private_key] == true
@client.create_keys
results[:private_key] = @client.private_key
end
@client.save
display(results)
end
# DELETE /roles/:id
def destroy
begin
@client = Chef::ApiClient.load(params[:id])
rescue Chef::Exceptions::CouchDBNotFound => e
raise NotFound, "Cannot load client #{params[:id]}"
end
@client.destroy
display({ :name => @client.name })
end
end