Add 'sdk/java-v2/' from commit '55f103e336ca9fb8bf1720d2ef4ee8dd4e221118'
[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     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       @object.reload
34       super
35     end
36   end
37
38   def find_objects_for_index
39     super
40     if action_name == 'lock' || action_name == 'unlock'
41       # Avoid loading more fields than we need
42       @objects = @objects.select(:id, :uuid, :state, :priority, :auth_uuid, :locked_by_uuid)
43       @select = %w(uuid state priority auth_uuid locked_by_uuid)
44     end
45   end
46
47   def lock
48     @object.lock
49     show
50   end
51
52   def unlock
53     @object.unlock
54     show
55   end
56
57   def current
58     if Thread.current[:api_client_authorization].nil?
59       send_error("Not logged in", status: 401)
60     else
61       @object = Container.for_current_token
62       if @object.nil?
63         send_error("Token is not associated with a container.", status: 404)
64       else
65         show
66       end
67     end
68   end
69
70   def secret_mounts
71     c = Container.for_current_token
72     if @object && c && @object.uuid == c.uuid
73       send_json({"secret_mounts" => @object.secret_mounts})
74     else
75       send_error("Token is not associated with this container.", status: 403)
76     end
77   end
78 end