65d8385ad5f5b47619f1e158e674564333e09433
[arvados.git] / services / api / app / controllers / arvados / v1 / containers_controller.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 class Arvados::V1::ContainersController < ApplicationController
6   accept_attribute_as_json :environment, Hash
7   accept_attribute_as_json :mounts, Hash
8   accept_attribute_as_json :runtime_constraints, Hash
9   accept_attribute_as_json :runtime_status, Hash
10   accept_attribute_as_json :command, Array
11   accept_attribute_as_json :scheduling_parameters, Hash
12
13   skip_before_filter :find_object_by_uuid, only: [:current]
14   skip_before_filter :render_404_if_no_object, only: [:current]
15
16   def auth
17     if @object.locked_by_uuid != Thread.current[:api_client_authorization].uuid
18       raise ArvadosModel::PermissionDeniedError.new("Not locked by your token")
19     end
20     @object = @object.auth
21     show
22   end
23
24   def update
25     @object.with_lock do
26       @object.reload
27       super
28     end
29   end
30
31   def find_objects_for_index
32     super
33     if action_name == 'lock' || action_name == 'unlock'
34       # Avoid loading more fields than we need
35       @objects = @objects.select(:id, :uuid, :state, :priority, :auth_uuid, :locked_by_uuid)
36       @select = %w(uuid state priority auth_uuid locked_by_uuid)
37     end
38   end
39
40   def lock
41     @object.lock
42     show
43   end
44
45   def unlock
46     @object.unlock
47     show
48   end
49
50   def current
51     if Thread.current[:api_client_authorization].nil?
52       send_error("Not logged in", status: 401)
53     else
54       c = Container.where(auth_uuid: Thread.current[:api_client_authorization].uuid).first
55       if c.nil?
56         send_error("Token is not associated with a container.", status: 404)
57       else
58         @object = c
59         show
60       end
61     end
62   end
63
64   def secret_mounts
65     if @object &&
66        @object.auth_uuid &&
67        @object.auth_uuid == Thread.current[:api_client_authorization].uuid
68       send_json({"secret_mounts" => @object.secret_mounts})
69     else
70       send_error("Token is not associated with this container.", status: 403)
71     end
72   end
73 end