13996: Explicitly get string representation of URLs
[arvados.git] / services / api / app / controllers / arvados / v1 / schema_controller.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 class Arvados::V1::SchemaController < ApplicationController
6   skip_before_action :catch_redirect_hint
7   skip_before_action :find_objects_for_index
8   skip_before_action :find_object_by_uuid
9   skip_before_action :load_filters_param
10   skip_before_action :load_limit_offset_order_params
11   skip_before_action :load_read_auths
12   skip_before_action :load_where_param
13   skip_before_action :render_404_if_no_object
14   skip_before_action :require_auth_scope
15
16   include DbCurrentTime
17
18   def index
19     expires_in 24.hours, public: true
20     send_json discovery_doc
21   end
22
23   protected
24
25   def discovery_doc
26     Rails.cache.fetch 'arvados_v1_rest_discovery' do
27       Rails.application.eager_load!
28       discovery = {
29         kind: "discovery#restDescription",
30         discoveryVersion: "v1",
31         id: "arvados:v1",
32         name: "arvados",
33         version: "v1",
34         revision: "20131114",
35         source_version: AppVersion.hash,
36         sourceVersion: AppVersion.hash, # source_version should be deprecated in the future
37         packageVersion: AppVersion.package_version,
38         generatedAt: db_current_time.iso8601,
39         title: "Arvados API",
40         description: "The API to interact with Arvados.",
41         documentationLink: "http://doc.arvados.org/api/index.html",
42         defaultCollectionReplication: Rails.configuration.Collections["DefaultReplication"],
43         protocol: "rest",
44         baseUrl: root_url + "arvados/v1/",
45         basePath: "/arvados/v1/",
46         rootUrl: root_url,
47         servicePath: "arvados/v1/",
48         batchPath: "batch",
49         uuidPrefix: Rails.configuration.ClusterID,
50         defaultTrashLifetime: Rails.configuration.Collections["DefaultTrashLifetime"],
51         blobSignatureTtl: Rails.configuration.Collections["BlobSigningTTL"],
52         maxRequestSize: Rails.configuration.API["MaxRequestSize"],
53         maxItemsPerResponse: Rails.configuration.API["MaxItemsPerResponse"],
54         dockerImageFormats: Rails.configuration.Containers["SupportedDockerImageFormats"],
55         crunchLogBytesPerEvent: Rails.configuration.Containers["Logging"]["LogBytesPerEvent"],
56         crunchLogSecondsBetweenEvents: Rails.configuration.Containers["Logging"]["LogSecondsBetweenEvents"],
57         crunchLogThrottlePeriod: Rails.configuration.Containers["Logging"]["LogThrottlePeriod"],
58         crunchLogThrottleBytes: Rails.configuration.Containers["Logging"]["LogThrottleBytes"],
59         crunchLogThrottleLines: Rails.configuration.Containers["Logging"]["LogThrottleLines"],
60         crunchLimitLogBytesPerJob: Rails.configuration.Containers["Logging"]["LimitLogBytesPerJob"],
61         crunchLogPartialLineThrottlePeriod: Rails.configuration.Containers["Logging"]["LogPartialLineThrottlePeriod"],
62         crunchLogUpdatePeriod: Rails.configuration.Containers["Logging"]["LogUpdatePeriod"],
63         crunchLogUpdateSize: Rails.configuration.Containers["Logging"]["LogUpdateSize"],
64         remoteHosts: Rails.configuration.RemoteClusters.map {|k,v| v["Host"]},
65         remoteHostsViaDNS: Rails.configuration.RemoteClusters["*"]["Proxy"],
66         websocketUrl: Rails.configuration.Services["Websocket"]["ExternalURL"].to_s,
67         workbenchUrl: Rails.configuration.Services["Workbench1"]["ExternalURL"].to_s,
68         keepWebServiceUrl: Rails.configuration.Services["WebDAV"]["ExternalURL"].to_s,
69         gitUrl: Rails.configuration.Services["GitHTTP"]["ExternalURL"].to_s,
70         parameters: {
71           alt: {
72             type: "string",
73             description: "Data format for the response.",
74             default: "json",
75             enum: [
76                    "json"
77                   ],
78             enumDescriptions: [
79                                "Responses with Content-Type of application/json"
80                               ],
81             location: "query"
82           },
83           fields: {
84             type: "string",
85             description: "Selector specifying which fields to include in a partial response.",
86             location: "query"
87           },
88           key: {
89             type: "string",
90             description: "API key. Your API key identifies your project and provides you with API access, quota, and reports. Required unless you provide an OAuth 2.0 token.",
91             location: "query"
92           },
93           oauth_token: {
94             type: "string",
95             description: "OAuth 2.0 token for the current user.",
96             location: "query"
97           }
98         },
99         auth: {
100           oauth2: {
101             scopes: {
102               "https://api.curoverse.com/auth/arvados" => {
103                 description: "View and manage objects"
104               },
105               "https://api.curoverse.com/auth/arvados.readonly" => {
106                 description: "View objects"
107               }
108             }
109           }
110         },
111         schemas: {},
112         resources: {}
113       }
114
115       ActiveRecord::Base.descendants.reject(&:abstract_class?).each do |k|
116         begin
117           ctl_class = "Arvados::V1::#{k.to_s.pluralize}Controller".constantize
118         rescue
119           # No controller -> no discovery.
120           next
121         end
122         object_properties = {}
123         k.columns.
124           select { |col| col.name != 'id' && !col.name.start_with?('secret_') }.
125           collect do |col|
126           if k.serialized_attributes.has_key? col.name
127             object_properties[col.name] = {
128               type: k.serialized_attributes[col.name].object_class.to_s
129             }
130           elsif k.attribute_types[col.name].is_a? JsonbType::Hash
131             object_properties[col.name] = {
132               type: Hash.to_s
133             }
134           elsif k.attribute_types[col.name].is_a? JsonbType::Array
135             object_properties[col.name] = {
136               type: Array.to_s
137             }
138           else
139             object_properties[col.name] = {
140               type: col.type
141             }
142           end
143         end
144         discovery[:schemas][k.to_s + 'List'] = {
145           id: k.to_s + 'List',
146           description: k.to_s + ' list',
147           type: "object",
148           properties: {
149             kind: {
150               type: "string",
151               description: "Object type. Always arvados##{k.to_s.camelcase(:lower)}List.",
152               default: "arvados##{k.to_s.camelcase(:lower)}List"
153             },
154             etag: {
155               type: "string",
156               description: "List version."
157             },
158             items: {
159               type: "array",
160               description: "The list of #{k.to_s.pluralize}.",
161               items: {
162                 "$ref" => k.to_s
163               }
164             },
165             next_link: {
166               type: "string",
167               description: "A link to the next page of #{k.to_s.pluralize}."
168             },
169             next_page_token: {
170               type: "string",
171               description: "The page token for the next page of #{k.to_s.pluralize}."
172             },
173             selfLink: {
174               type: "string",
175               description: "A link back to this list."
176             }
177           }
178         }
179         discovery[:schemas][k.to_s] = {
180           id: k.to_s,
181           description: k.to_s,
182           type: "object",
183           uuidPrefix: (k.respond_to?(:uuid_prefix) ? k.uuid_prefix : nil),
184           properties: {
185             uuid: {
186               type: "string",
187               description: "Object ID."
188             },
189             etag: {
190               type: "string",
191               description: "Object version."
192             }
193           }.merge(object_properties)
194         }
195         discovery[:resources][k.to_s.underscore.pluralize] = {
196           methods: {
197             get: {
198               id: "arvados.#{k.to_s.underscore.pluralize}.get",
199               path: "#{k.to_s.underscore.pluralize}/{uuid}",
200               httpMethod: "GET",
201               description: "Gets a #{k.to_s}'s metadata by UUID.",
202               parameters: {
203                 uuid: {
204                   type: "string",
205                   description: "The UUID of the #{k.to_s} in question.",
206                   required: true,
207                   location: "path"
208                 }
209               },
210               parameterOrder: [
211                                "uuid"
212                               ],
213               response: {
214                 "$ref" => k.to_s
215               },
216               scopes: [
217                        "https://api.curoverse.com/auth/arvados",
218                        "https://api.curoverse.com/auth/arvados.readonly"
219                       ]
220             },
221             index: {
222               id: "arvados.#{k.to_s.underscore.pluralize}.index",
223               path: k.to_s.underscore.pluralize,
224               httpMethod: "GET",
225               description:
226                  %|Index #{k.to_s.pluralize}.
227
228                    The <code>index</code> method returns a
229                    <a href="/api/resources.html">resource list</a> of
230                    matching #{k.to_s.pluralize}. For example:
231
232                    <pre>
233                    {
234                     "kind":"arvados##{k.to_s.camelcase(:lower)}List",
235                     "etag":"",
236                     "self_link":"",
237                     "next_page_token":"",
238                     "next_link":"",
239                     "items":[
240                        ...
241                     ],
242                     "items_available":745,
243                     "_profile":{
244                      "request_time":0.157236317
245                     }
246                     </pre>|,
247               parameters: {
248               },
249               response: {
250                 "$ref" => "#{k.to_s}List"
251               },
252               scopes: [
253                        "https://api.curoverse.com/auth/arvados",
254                        "https://api.curoverse.com/auth/arvados.readonly"
255                       ]
256             },
257             create: {
258               id: "arvados.#{k.to_s.underscore.pluralize}.create",
259               path: "#{k.to_s.underscore.pluralize}",
260               httpMethod: "POST",
261               description: "Create a new #{k.to_s}.",
262               parameters: {},
263               request: {
264                 required: true,
265                 properties: {
266                   k.to_s.underscore => {
267                     "$ref" => k.to_s
268                   }
269                 }
270               },
271               response: {
272                 "$ref" => k.to_s
273               },
274               scopes: [
275                        "https://api.curoverse.com/auth/arvados"
276                       ]
277             },
278             update: {
279               id: "arvados.#{k.to_s.underscore.pluralize}.update",
280               path: "#{k.to_s.underscore.pluralize}/{uuid}",
281               httpMethod: "PUT",
282               description: "Update attributes of an existing #{k.to_s}.",
283               parameters: {
284                 uuid: {
285                   type: "string",
286                   description: "The UUID of the #{k.to_s} in question.",
287                   required: true,
288                   location: "path"
289                 }
290               },
291               request: {
292                 required: true,
293                 properties: {
294                   k.to_s.underscore => {
295                     "$ref" => k.to_s
296                   }
297                 }
298               },
299               response: {
300                 "$ref" => k.to_s
301               },
302               scopes: [
303                        "https://api.curoverse.com/auth/arvados"
304                       ]
305             },
306             delete: {
307               id: "arvados.#{k.to_s.underscore.pluralize}.delete",
308               path: "#{k.to_s.underscore.pluralize}/{uuid}",
309               httpMethod: "DELETE",
310               description: "Delete an existing #{k.to_s}.",
311               parameters: {
312                 uuid: {
313                   type: "string",
314                   description: "The UUID of the #{k.to_s} in question.",
315                   required: true,
316                   location: "path"
317                 }
318               },
319               response: {
320                 "$ref" => k.to_s
321               },
322               scopes: [
323                        "https://api.curoverse.com/auth/arvados"
324                       ]
325             }
326           }
327         }
328         # Check for Rails routes that don't match the usual actions
329         # listed above
330         d_methods = discovery[:resources][k.to_s.underscore.pluralize][:methods]
331         Rails.application.routes.routes.each do |route|
332           action = route.defaults[:action]
333           httpMethod = ['GET', 'POST', 'PUT', 'DELETE'].map { |method|
334             method if route.verb.match(method)
335           }.compact.first
336           if httpMethod and
337               route.defaults[:controller] == 'arvados/v1/' + k.to_s.underscore.pluralize and
338               ctl_class.action_methods.include? action
339             if !d_methods[action.to_sym]
340               method = {
341                 id: "arvados.#{k.to_s.underscore.pluralize}.#{action}",
342                 path: route.path.spec.to_s.sub('/arvados/v1/','').sub('(.:format)','').sub(/:(uu)?id/,'{uuid}'),
343                 httpMethod: httpMethod,
344                 description: "#{action} #{k.to_s.underscore.pluralize}",
345                 parameters: {},
346                 response: {
347                   "$ref" => (action == 'index' ? "#{k.to_s}List" : k.to_s)
348                 },
349                 scopes: [
350                          "https://api.curoverse.com/auth/arvados"
351                         ]
352               }
353               route.segment_keys.each do |key|
354                 if key != :format
355                   key = :uuid if key == :id
356                   method[:parameters][key] = {
357                     type: "string",
358                     description: "",
359                     required: true,
360                     location: "path"
361                   }
362                 end
363               end
364             else
365               # We already built a generic method description, but we
366               # might find some more required parameters through
367               # introspection.
368               method = d_methods[action.to_sym]
369             end
370             if ctl_class.respond_to? "_#{action}_requires_parameters".to_sym
371               ctl_class.send("_#{action}_requires_parameters".to_sym).each do |l, v|
372                 if v.is_a? Hash
373                   method[:parameters][l] = v
374                 else
375                   method[:parameters][l] = {}
376                 end
377                 if !method[:parameters][l][:default].nil?
378                   # The JAVA SDK is sensitive to all values being strings
379                   method[:parameters][l][:default] = method[:parameters][l][:default].to_s
380                 end
381                 method[:parameters][l][:type] ||= 'string'
382                 method[:parameters][l][:description] ||= ''
383                 method[:parameters][l][:location] = (route.segment_keys.include?(l) ? 'path' : 'query')
384                 if method[:parameters][l][:required].nil?
385                   method[:parameters][l][:required] = v != false
386                 end
387               end
388             end
389             d_methods[action.to_sym] = method
390
391             if action == 'index'
392               list_method = method.dup
393               list_method[:id].sub!('index', 'list')
394               list_method[:description].sub!('Index', 'List')
395               list_method[:description].sub!('index', 'list')
396               d_methods[:list] = list_method
397             end
398           end
399         end
400       end
401       Rails.configuration.API["DisabledAPIs"].each do |method|
402         ctrl, action = method.split('.', 2)
403         discovery[:resources][ctrl][:methods].delete(action.to_sym)
404       end
405       discovery
406     end
407   end
408 end