51a47f0186bada9235ae9c841b519eea292a4be8
[arvados.git] / services / api / app / controllers / arvados / v1 / collections_controller.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require "arvados/keep"
6 require "trashable"
7
8 class Arvados::V1::CollectionsController < ApplicationController
9   include DbCurrentTime
10   include TrashableController
11
12   def self._index_requires_parameters
13     (super rescue {}).
14       merge({
15         include_trash: {
16           type: 'boolean', required: false, description: "Include collections whose is_trashed attribute is true."
17         },
18         include_old_versions: {
19           type: 'boolean', required: false, description: "Include past collection versions."
20         },
21       })
22   end
23
24   def self._show_requires_parameters
25     (super rescue {}).
26       merge({
27         include_trash: {
28           type: 'boolean', required: false, description: "Show collection even if its is_trashed attribute is true."
29         },
30         include_old_versions: {
31           type: 'boolean', required: false, description: "Include past collection versions."
32         },
33       })
34   end
35
36   def create
37     if resource_attrs[:uuid] and (loc = Keep::Locator.parse(resource_attrs[:uuid]))
38       resource_attrs[:portable_data_hash] = loc.to_s
39       resource_attrs.delete :uuid
40     end
41     resource_attrs.delete :version
42     resource_attrs.delete :current_version_uuid
43     super
44   end
45
46   def find_objects_for_index
47     opts = {}
48     if params[:include_trash] || ['destroy', 'trash', 'untrash'].include?(action_name)
49       opts.update({include_trash: true})
50     end
51     if params[:include_old_versions] || @include_old_versions
52       opts.update({include_old_versions: true})
53     end
54     @objects = Collection.readable_by(*@read_users, opts) if !opts.empty?
55     super
56   end
57
58   def find_object_by_uuid
59     @include_old_versions = true
60
61     if loc = Keep::Locator.parse(params[:id])
62       loc.strip_hints!
63
64       # It matters which Collection object we pick because we use it to get signed_manifest_text,
65       # the value of which is affected by the value of trash_at.
66       #
67       # From postgres doc: "By default, null values sort as if larger than any non-null
68       # value; that is, NULLS FIRST is the default for DESC order, and
69       # NULLS LAST otherwise."
70       #
71       # "trash_at desc" sorts null first, then latest to earliest, so
72       # it will select the Collection object with the longest
73       # available lifetime.
74
75       if c = Collection.readable_by(*@read_users).where({ portable_data_hash: loc.to_s }).order("trash_at desc").limit(1).first
76         @object = {
77           uuid: c.portable_data_hash,
78           portable_data_hash: c.portable_data_hash,
79           manifest_text: c.signed_manifest_text,
80         }
81       end
82       true
83     else
84       super
85     end
86   end
87
88   def show
89     if @object.is_a? Collection
90       # Omit unsigned_manifest_text
91       @select ||= model_class.selectable_attributes - ["unsigned_manifest_text"]
92       super
93     else
94       send_json @object
95     end
96   end
97
98
99   def find_collections(visited, sp, &b)
100     case sp
101     when ArvadosModel
102       sp.class.columns.each do |c|
103         find_collections(visited, sp[c.name.to_sym], &b) if c.name != "log"
104       end
105     when Hash
106       sp.each do |k, v|
107         find_collections(visited, v, &b)
108       end
109     when Array
110       sp.each do |v|
111         find_collections(visited, v, &b)
112       end
113     when String
114       if m = /[a-f0-9]{32}\+\d+/.match(sp)
115         yield m[0], nil
116       elsif m = Collection.uuid_regex.match(sp)
117         yield nil, m[0]
118       end
119     end
120   end
121
122   def search_edges(visited, uuid, direction)
123     if uuid.nil? or uuid.empty? or visited[uuid]
124       return
125     end
126
127     if loc = Keep::Locator.parse(uuid)
128       loc.strip_hints!
129       return if visited[loc.to_s]
130     end
131
132     logger.debug "visiting #{uuid}"
133
134     if loc
135       # uuid is a portable_data_hash
136       collections = Collection.readable_by(*@read_users).where(portable_data_hash: loc.to_s)
137       c = collections.limit(2).all
138       if c.size == 1
139         visited[loc.to_s] = c[0]
140       elsif c.size > 1
141         name = collections.limit(1).where("name <> ''").first
142         if name
143           visited[loc.to_s] = {
144             portable_data_hash: c[0].portable_data_hash,
145             name: "#{name.name} + #{collections.count-1} more"
146           }
147         else
148           visited[loc.to_s] = {
149             portable_data_hash: c[0].portable_data_hash,
150             name: loc.to_s
151           }
152         end
153       end
154
155       if direction == :search_up
156         # Search upstream for jobs where this locator is the output of some job
157         if !Rails.configuration.API.DisabledAPIs.include?("jobs.list")
158           Job.readable_by(*@read_users).where(output: loc.to_s).each do |job|
159             search_edges(visited, job.uuid, :search_up)
160           end
161
162           Job.readable_by(*@read_users).where(log: loc.to_s).each do |job|
163             search_edges(visited, job.uuid, :search_up)
164           end
165         end
166
167         Container.readable_by(*@read_users).where(output: loc.to_s).each do |c|
168           search_edges(visited, c.uuid, :search_up)
169         end
170
171         Container.readable_by(*@read_users).where(log: loc.to_s).each do |c|
172           search_edges(visited, c.uuid, :search_up)
173         end
174       elsif direction == :search_down
175         if loc.to_s == "d41d8cd98f00b204e9800998ecf8427e+0"
176           # Special case, don't follow the empty collection.
177           return
178         end
179
180         # Search downstream for jobs where this locator is in script_parameters
181         if !Rails.configuration.API.DisabledAPIs.include?("jobs.list")
182           Job.readable_by(*@read_users).where(["jobs.script_parameters like ?", "%#{loc.to_s}%"]).each do |job|
183             search_edges(visited, job.uuid, :search_down)
184           end
185
186           Job.readable_by(*@read_users).where(["jobs.docker_image_locator = ?", "#{loc.to_s}"]).each do |job|
187             search_edges(visited, job.uuid, :search_down)
188           end
189         end
190
191         Container.readable_by(*@read_users).where(["mounts like ?", "%#{loc.to_s}%"]).each do |c|
192           search_edges(visited, c.uuid, :search_down)
193         end
194
195         Container.readable_by(*@read_users).where(["container_image = '#{loc.to_s}'"]).each do |c|
196           search_edges(visited, c.uuid, :search_down)
197         end
198
199       end
200     else
201       # uuid is a regular Arvados UUID
202       rsc = ArvadosModel::resource_class_for_uuid uuid
203       if rsc == Job
204         Job.readable_by(*@read_users).where(uuid: uuid).each do |job|
205           visited[uuid] = job.as_api_response
206           if direction == :search_up
207             # Follow upstream collections referenced in the script parameters
208             find_collections(visited, job) do |hash, col_uuid|
209               search_edges(visited, hash, :search_up) if hash
210               search_edges(visited, col_uuid, :search_up) if col_uuid
211             end
212           elsif direction == :search_down
213             # Follow downstream job output
214             search_edges(visited, job.output, direction)
215           end
216         end
217       elsif rsc == Container
218         Container.readable_by(*@read_users).where(uuid: uuid).each do |c|
219           visited[uuid] = c.as_api_response
220           if direction == :search_up
221             # Follow upstream collections referenced in the script parameters
222             find_collections(visited, c) do |hash, col_uuid|
223               search_edges(visited, hash, :search_up) if hash
224               search_edges(visited, col_uuid, :search_up) if col_uuid
225             end
226           elsif direction == :search_down
227             # Follow downstream job output
228             search_edges(visited, c.output, direction)
229           end
230         end
231       elsif rsc == Collection
232         if c = Collection.readable_by(*@read_users).where(uuid: uuid).limit(1).first
233           search_edges(visited, c.portable_data_hash, direction)
234           visited[c.portable_data_hash] = c.as_api_response
235         end
236       elsif rsc != nil
237         rsc.where(uuid: uuid).each do |r|
238           visited[uuid] = r.as_api_response
239         end
240       end
241     end
242
243     if direction == :search_up
244       # Search for provenance links pointing to the current uuid
245       Link.readable_by(*@read_users).
246         where(head_uuid: uuid, link_class: "provenance").
247         each do |link|
248         visited[link.uuid] = link.as_api_response
249         search_edges(visited, link.tail_uuid, direction)
250       end
251     elsif direction == :search_down
252       # Search for provenance links emanating from the current uuid
253       Link.readable_by(current_user).
254         where(tail_uuid: uuid, link_class: "provenance").
255         each do |link|
256         visited[link.uuid] = link.as_api_response
257         search_edges(visited, link.head_uuid, direction)
258       end
259     end
260   end
261
262   def provenance
263     visited = {}
264     search_edges(visited, @object[:portable_data_hash], :search_up)
265     search_edges(visited, @object[:uuid], :search_up)
266     send_json visited
267   end
268
269   def used_by
270     visited = {}
271     search_edges(visited, @object[:uuid], :search_down)
272     search_edges(visited, @object[:portable_data_hash], :search_down)
273     send_json visited
274   end
275
276   protected
277
278   def load_limit_offset_order_params *args
279     super
280     if action_name == 'index'
281       # Omit manifest_text and unsigned_manifest_text from index results unless expressly selected.
282       @select ||= model_class.selectable_attributes - ["manifest_text", "unsigned_manifest_text"]
283     end
284   end
285 end