9998: Tidy up, add test.
[arvados.git] / services / api / app / controllers / arvados / v1 / containers_controller.rb
1 class Arvados::V1::ContainersController < ApplicationController
2   accept_attribute_as_json :environment, Hash
3   accept_attribute_as_json :mounts, Hash
4   accept_attribute_as_json :runtime_constraints, Hash
5   accept_attribute_as_json :command, Array
6
7   skip_before_filter :find_object_by_uuid, only: [:current]
8   skip_before_filter :render_404_if_no_object, only: [:current]
9
10   def auth
11     if @object.locked_by_uuid != Thread.current[:api_client_authorization].uuid
12       raise ArvadosModel::PermissionDeniedError.new("Not locked by your token")
13     end
14     @object = @object.auth
15     show
16   end
17
18   # Updates use row locking to resolve races between multiple
19   # dispatchers trying to lock the same container.
20   def update
21     @object.with_lock do
22       super
23     end
24   end
25
26   def lock
27     @object.lock
28     show
29   end
30
31   def unlock
32     @object.unlock
33     show
34   end
35
36   def current
37     if Thread.current[:api_client_authorization].nil?
38       send_error("Not logged in", status: 401)
39     else
40       c = Container.where(auth_uuid: Thread.current[:api_client_authorization].uuid).first
41       if c.nil?
42         send_error("Token is not associated with a container.", status: 404)
43       else
44         @object = c
45         show
46       end
47     end
48   end
49 end