Merge branch '8784-dir-listings'
[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
7 class Arvados::V1::CollectionsController < ApplicationController
8   include DbCurrentTime
9
10   def self._index_requires_parameters
11     (super rescue {}).
12       merge({
13         include_trash: {
14           type: 'boolean', required: false, description: "Include collections whose is_trashed attribute is true."
15         },
16       })
17   end
18
19
20   def create
21     if resource_attrs[:uuid] and (loc = Keep::Locator.parse(resource_attrs[:uuid]))
22       resource_attrs[:portable_data_hash] = loc.to_s
23       resource_attrs.delete :uuid
24     end
25     super
26   end
27
28   def find_objects_for_index
29     if params[:include_trash] || ['destroy', 'trash', 'untrash'].include?(action_name)
30       @objects = Collection.unscoped.readable_by(*@read_users)
31     end
32     super
33   end
34
35   def find_object_by_uuid
36     if loc = Keep::Locator.parse(params[:id])
37       loc.strip_hints!
38       if c = Collection.readable_by(*@read_users).where({ portable_data_hash: loc.to_s }).limit(1).first
39         @object = {
40           uuid: c.portable_data_hash,
41           portable_data_hash: c.portable_data_hash,
42           manifest_text: c.signed_manifest_text,
43         }
44       end
45       true
46     else
47       super
48     end
49   end
50
51   def show
52     if @object.is_a? Collection
53       # Omit unsigned_manifest_text
54       @select ||= model_class.selectable_attributes - ["unsigned_manifest_text"]
55       super
56     else
57       send_json @object
58     end
59   end
60
61   def destroy
62     if !@object.is_trashed
63       @object.update_attributes!(trash_at: db_current_time)
64     end
65     earliest_delete = (@object.trash_at +
66                        Rails.configuration.blob_signature_ttl.seconds)
67     if @object.delete_at > earliest_delete
68       @object.update_attributes!(delete_at: earliest_delete)
69     end
70     show
71   end
72
73   def trash
74     if !@object.is_trashed
75       @object.update_attributes!(trash_at: db_current_time)
76     end
77     show
78   end
79
80   def untrash
81     if @object.is_trashed
82       @object.trash_at = nil
83
84       if params[:ensure_unique_name]
85         @object.save_with_unique_name!
86       else
87         @object.save!
88       end
89     else
90       raise InvalidStateTransitionError
91     end
92     show
93   end
94
95   def find_collections(visited, sp, &b)
96     case sp
97     when ArvadosModel
98       sp.class.columns.each do |c|
99         find_collections(visited, sp[c.name.to_sym], &b) if c.name != "log"
100       end
101     when Hash
102       sp.each do |k, v|
103         find_collections(visited, v, &b)
104       end
105     when Array
106       sp.each do |v|
107         find_collections(visited, v, &b)
108       end
109     when String
110       if m = /[a-f0-9]{32}\+\d+/.match(sp)
111         yield m[0], nil
112       elsif m = Collection.uuid_regex.match(sp)
113         yield nil, m[0]
114       end
115     end
116   end
117
118   def search_edges(visited, uuid, direction)
119     if uuid.nil? or uuid.empty? or visited[uuid]
120       return
121     end
122
123     if loc = Keep::Locator.parse(uuid)
124       loc.strip_hints!
125       return if visited[loc.to_s]
126     end
127
128     logger.debug "visiting #{uuid}"
129
130     if loc
131       # uuid is a portable_data_hash
132       collections = Collection.readable_by(*@read_users).where(portable_data_hash: loc.to_s)
133       c = collections.limit(2).all
134       if c.size == 1
135         visited[loc.to_s] = c[0]
136       elsif c.size > 1
137         name = collections.limit(1).where("name <> ''").first
138         if name
139           visited[loc.to_s] = {
140             portable_data_hash: c[0].portable_data_hash,
141             name: "#{name.name} + #{collections.count-1} more"
142           }
143         else
144           visited[loc.to_s] = {
145             portable_data_hash: c[0].portable_data_hash,
146             name: loc.to_s
147           }
148         end
149       end
150
151       if direction == :search_up
152         # Search upstream for jobs where this locator is the output of some job
153         Job.readable_by(*@read_users).where(output: loc.to_s).each do |job|
154           search_edges(visited, job.uuid, :search_up)
155         end
156
157         Job.readable_by(*@read_users).where(log: loc.to_s).each do |job|
158           search_edges(visited, job.uuid, :search_up)
159         end
160       elsif direction == :search_down
161         if loc.to_s == "d41d8cd98f00b204e9800998ecf8427e+0"
162           # Special case, don't follow the empty collection.
163           return
164         end
165
166         # Search downstream for jobs where this locator is in script_parameters
167         Job.readable_by(*@read_users).where(["jobs.script_parameters like ?", "%#{loc.to_s}%"]).each do |job|
168           search_edges(visited, job.uuid, :search_down)
169         end
170
171         Job.readable_by(*@read_users).where(["jobs.docker_image_locator = ?", "#{loc.to_s}"]).each do |job|
172           search_edges(visited, job.uuid, :search_down)
173         end
174       end
175     else
176       # uuid is a regular Arvados UUID
177       rsc = ArvadosModel::resource_class_for_uuid uuid
178       if rsc == Job
179         Job.readable_by(*@read_users).where(uuid: uuid).each do |job|
180           visited[uuid] = job.as_api_response
181           if direction == :search_up
182             # Follow upstream collections referenced in the script parameters
183             find_collections(visited, job) do |hash, col_uuid|
184               search_edges(visited, hash, :search_up) if hash
185               search_edges(visited, col_uuid, :search_up) if col_uuid
186             end
187           elsif direction == :search_down
188             # Follow downstream job output
189             search_edges(visited, job.output, direction)
190           end
191         end
192       elsif rsc == Collection
193         if c = Collection.readable_by(*@read_users).where(uuid: uuid).limit(1).first
194           search_edges(visited, c.portable_data_hash, direction)
195           visited[c.portable_data_hash] = c.as_api_response
196         end
197       elsif rsc != nil
198         rsc.where(uuid: uuid).each do |r|
199           visited[uuid] = r.as_api_response
200         end
201       end
202     end
203
204     if direction == :search_up
205       # Search for provenance links pointing to the current uuid
206       Link.readable_by(*@read_users).
207         where(head_uuid: uuid, link_class: "provenance").
208         each do |link|
209         visited[link.uuid] = link.as_api_response
210         search_edges(visited, link.tail_uuid, direction)
211       end
212     elsif direction == :search_down
213       # Search for provenance links emanating from the current uuid
214       Link.readable_by(current_user).
215         where(tail_uuid: uuid, link_class: "provenance").
216         each do |link|
217         visited[link.uuid] = link.as_api_response
218         search_edges(visited, link.head_uuid, direction)
219       end
220     end
221   end
222
223   def provenance
224     visited = {}
225     search_edges(visited, @object[:portable_data_hash], :search_up)
226     search_edges(visited, @object[:uuid], :search_up)
227     send_json visited
228   end
229
230   def used_by
231     visited = {}
232     search_edges(visited, @object[:uuid], :search_down)
233     search_edges(visited, @object[:portable_data_hash], :search_down)
234     send_json visited
235   end
236
237   protected
238
239   def load_limit_offset_order_params *args
240     super
241     if action_name == 'index'
242       # Omit manifest_text and unsigned_manifest_text from index results unless expressly selected.
243       @select ||= model_class.selectable_attributes - ["manifest_text", "unsigned_manifest_text"]
244     end
245   end
246 end