20259: Add documentation for banner and tooltip features
[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_action :find_object_by_uuid, only: [:current]
14   skip_before_action :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     if @object.runtime_token.nil?
21       @object = @object.auth
22     else
23       @object = ApiClientAuthorization.validate(token: @object.runtime_token)
24       if @object.nil?
25         raise ArvadosModel::PermissionDeniedError.new("Invalid runtime_token")
26       end
27     end
28     show
29   end
30
31   def update
32     @object.with_lock do
33       super
34     end
35   end
36
37   def find_objects_for_index
38     super
39     if action_name == 'lock' || action_name == 'unlock'
40       # Avoid loading more fields than we need
41       @objects = @objects.select(:id, :uuid, :state, :priority, :auth_uuid, :locked_by_uuid, :lock_count)
42       @select = %w(uuid state priority auth_uuid locked_by_uuid)
43     elsif action_name == 'update_priority'
44       # We're going to reload(lock: true) in the handler, which will
45       # select all attributes, but will fail if we don't select :id
46       # now.
47       @objects = @objects.select(:id, :uuid)
48     end
49   end
50
51   def lock
52     @object.lock
53     show
54   end
55
56   def unlock
57     @object.unlock
58     show
59   end
60
61   def update_priority
62     @object.reload(lock: true)
63     @object.update_priority!
64     show
65   end
66
67   def current
68     if Thread.current[:api_client_authorization].nil?
69       send_error("Not logged in", status: 401)
70     else
71       @object = Container.for_current_token
72       if @object.nil?
73         send_error("Token is not associated with a container.", status: 404)
74       else
75         show
76       end
77     end
78   end
79
80   def secret_mounts
81     c = Container.for_current_token
82     if @object && c && @object.uuid == c.uuid
83       send_json({"secret_mounts" => @object.secret_mounts})
84     else
85       send_error("Token is not associated with this container.", status: 403)
86     end
87   end
88 end