20472: Remove special handling of update_priority
[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 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, :lock_count)
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 update_priority
51     @object.update_priority!
52     show
53   end
54
55   def current
56     if Thread.current[:api_client_authorization].nil?
57       send_error("Not logged in", status: 401)
58     else
59       @object = Container.for_current_token
60       if @object.nil?
61         send_error("Token is not associated with a container.", status: 404)
62       else
63         show
64       end
65     end
66   end
67
68   def secret_mounts
69     c = Container.for_current_token
70     if @object && c && @object.uuid == c.uuid
71       send_json({"secret_mounts" => @object.secret_mounts})
72     else
73       send_error("Token is not associated with this container.", status: 403)
74     end
75   end
76 end