Class: Clients

Inherits:
Application show all
Defined in:
app/controllers/clients.rb

Instance Method Summary collapse

Methods inherited from Application

#access_denied, #admin_or_requesting_node, #authenticate_every, #display, #get_available_recipes, #is_admin, #is_admin_or_validator, #redirect_back_or_default, #store_location

Instance Method Details

#createObject

POST /clients

Raises:

  • (Conflict)


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
# File 'app/controllers/clients.rb', line 48

def create
  exists = true 
  if params.has_key?(:inflated_object)
    params[:name] ||= params[:inflated_object].name
    params[:admin] ||= params[:inflated_object].admin
  end

  # We can only create clients if we're the admin or the validator.
  # But only allow creating admin clients if we're already an admin.
  if params[:admin] == true && @auth_user.admin != true
    raise Forbidden, "You are not allowed to take this action."
  end

  begin
    Chef::ApiClient.cdb_load(params[:name])
  rescue Chef::Exceptions::CouchDBNotFound
    exists = false 
  end
  raise Conflict, "Client already exists" if exists

  @client = Chef::ApiClient.new
  @client.name(params[:name])
  @client.admin(params[:admin]) if params[:admin]
  @client.create_keys
  @client.cdb_save
  
  self.status = 201
  headers['Location'] = absolute_url(:client, @client.name)
  display({ :uri => absolute_url(:client, @client.name), :private_key => @client.private_key })
end

#destroyObject

DELETE /clients/:id



107
108
109
110
111
112
113
114
115
# File 'app/controllers/clients.rb', line 107

def destroy
  begin
    @client = Chef::ApiClient.cdb_load(params[:id])
  rescue Chef::Exceptions::CouchDBNotFound => e
    raise NotFound, "Cannot load client #{params[:id]}"
  end
  @client.cdb_destroy
  display({ :name => @client.name })
end

#indexObject

GET /clients



31
32
33
34
# File 'app/controllers/clients.rb', line 31

def index
  @list = Chef::ApiClient.cdb_list(true)
  display(@list.inject({}) { |result, element| result[element.name] = absolute_url(:client, :id => element.name); result })
end

#showObject

GET /clients/:id



37
38
39
40
41
42
43
44
45
# File 'app/controllers/clients.rb', line 37

def show
  begin
    @client = Chef::ApiClient.cdb_load(params[:id])
  rescue Chef::Exceptions::CouchDBNotFound => e
    raise NotFound, "Cannot load client #{params[:id]}"
  end
  #display({ :name => @client.name, :admin => @client.admin, :public_key => @client.public_key })
  display @client
end

#updateObject

PUT /clients/:id



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/controllers/clients.rb', line 80

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.cdb_load(params[:id])
  rescue Chef::Exceptions::CouchDBNotFound => e
    raise NotFound, "Cannot load client #{params[:id]}"
  end
  
  @client.admin(params[:admin]) unless params[:admin].nil?

  results = { :name => @client.name, :admin => @client.admin }

  if params[:private_key] == true
    @client.create_keys
    results[:private_key] = @client.private_key
  end

  @client.cdb_save

  display(results)
end