Merge branch 'master' into 3642-search-for-active-only
[arvados.git] / services / api / app / controllers / arvados / v1 / collections_controller.rb
1 class Arvados::V1::CollectionsController < ApplicationController
2   def create
3     if resource_attrs[:uuid] and (loc = Locator.parse(resource_attrs[:uuid]))
4       resource_attrs[:portable_data_hash] = loc.to_s
5       resource_attrs.delete :uuid
6     end
7     super
8   end
9
10   def find_object_by_uuid
11     if loc = Locator.parse(params[:id])
12       loc.strip_hints!
13       if c = Collection.readable_by(*@read_users).where({ portable_data_hash: loc.to_s }).limit(1).first
14         @object = {
15           uuid: c.portable_data_hash,
16           portable_data_hash: c.portable_data_hash,
17           manifest_text: c.manifest_text,
18           files: c.files,
19           data_size: c.data_size
20         }
21       end
22     else
23       super
24     end
25     true
26   end
27
28   def show
29     sign_manifests(@object[:manifest_text])
30     if @object.is_a? Collection
31       render json: @object.as_api_response
32     else
33       render json: @object
34     end
35   end
36
37   def index
38     sign_manifests(*@objects.map { |c| c[:manifest_text] })
39     super
40   end
41
42   def script_param_edges(visited, sp)
43     case sp
44     when Hash
45       sp.each do |k, v|
46         script_param_edges(visited, v)
47       end
48     when Array
49       sp.each do |v|
50         script_param_edges(visited, v)
51       end
52     when String
53       return if sp.empty?
54       if loc = Locator.parse(sp)
55         search_edges(visited, loc.to_s, :search_up)
56       end
57     end
58   end
59
60   def search_edges(visited, uuid, direction)
61     if uuid.nil? or uuid.empty? or visited[uuid]
62       return
63     end
64
65     if loc = Locator.parse(uuid)
66       loc.strip_hints!
67       return if visited[loc.to_s]
68     end
69
70     logger.debug "visiting #{uuid}"
71
72     if loc
73       # uuid is a portable_data_hash
74       if c = Collection.readable_by(*@read_users).where(portable_data_hash: loc.to_s).limit(1).first
75         visited[loc.to_s] = {
76           portable_data_hash: c.portable_data_hash,
77           files: c.files,
78           data_size: c.data_size
79         }
80       end
81
82       if direction == :search_up
83         # Search upstream for jobs where this locator is the output of some job
84         Job.readable_by(*@read_users).where(output: loc.to_s).each do |job|
85           search_edges(visited, job.uuid, :search_up)
86         end
87
88         Job.readable_by(*@read_users).where(log: loc.to_s).each do |job|
89           search_edges(visited, job.uuid, :search_up)
90         end
91       elsif direction == :search_down
92         if loc.to_s == "d41d8cd98f00b204e9800998ecf8427e+0"
93           # Special case, don't follow the empty collection.
94           return
95         end
96
97         # Search downstream for jobs where this locator is in script_parameters
98         Job.readable_by(*@read_users).where(["jobs.script_parameters like ?", "%#{loc.to_s}%"]).each do |job|
99           search_edges(visited, job.uuid, :search_down)
100         end
101       end
102     else
103       # uuid is a regular Arvados UUID
104       rsc = ArvadosModel::resource_class_for_uuid uuid
105       if rsc == Job
106         Job.readable_by(*@read_users).where(uuid: uuid).each do |job|
107           visited[uuid] = job.as_api_response
108           if direction == :search_up
109             # Follow upstream collections referenced in the script parameters
110             script_param_edges(visited, job.script_parameters)
111           elsif direction == :search_down
112             # Follow downstream job output
113             search_edges(visited, job.output, direction)
114           end
115         end
116       elsif rsc == Collection
117         if c = Collection.readable_by(*@read_users).where(uuid: uuid).limit(1).first
118           search_edges(visited, c.portable_data_hash, direction)
119           visited[c.portable_data_hash] = c.as_api_response
120         end
121       elsif rsc != nil
122         rsc.where(uuid: uuid).each do |r|
123           visited[uuid] = r.as_api_response
124         end
125       end
126     end
127
128     if direction == :search_up
129       # Search for provenance links pointing to the current uuid
130       Link.readable_by(*@read_users).
131         where(head_uuid: uuid, link_class: "provenance").
132         each do |link|
133         visited[link.uuid] = link.as_api_response
134         search_edges(visited, link.tail_uuid, direction)
135       end
136     elsif direction == :search_down
137       # Search for provenance links emanating from the current uuid
138       Link.readable_by(current_user).
139         where(tail_uuid: uuid, link_class: "provenance").
140         each do |link|
141         visited[link.uuid] = link.as_api_response
142         search_edges(visited, link.head_uuid, direction)
143       end
144     end
145   end
146
147   def provenance
148     visited = {}
149     search_edges(visited, @object[:uuid] || @object[:portable_data_hash], :search_up)
150     render json: visited
151   end
152
153   def used_by
154     visited = {}
155     search_edges(visited, @object[:uuid] || @object[:portable_data_hash], :search_down)
156     render json: visited
157   end
158
159   protected
160
161   def apply_filters
162     if action_name == 'index'
163       # Omit manifest_text from index results unless expressly selected.
164       @select ||= model_class.api_accessible_attributes(:user).
165         map { |attr_spec| attr_spec.first.to_s } - ["manifest_text"]
166     end
167     super
168   end
169
170   def sign_manifests(*manifests)
171     if current_api_client_authorization
172       signing_opts = {
173         key: Rails.configuration.blob_signing_key,
174         api_token: current_api_client_authorization.api_token,
175         ttl: Rails.configuration.blob_signing_ttl,
176       }
177       manifests.each do |text|
178         Collection.munge_manifest_locators(text) do |loc|
179           Blob.sign_locator(loc.to_s, signing_opts)
180         end
181       end
182     end
183   end
184 end